Skip to content

Commit

Permalink
v3.0.0: introduces parsed json for response.data
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNe0x1 committed Oct 21, 2024
1 parent cb08efe commit 2684bb0
Show file tree
Hide file tree
Showing 18 changed files with 27 additions and 148 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use it like:

```ruby
response = DHC.get('http://datastore/v2/feedbacks')
response.data.items[0]
response.data.items[0].recommended
response.data.dig('items', 0)
response.data.dig('items', 0, 'recommended')
response.body
response.headers
```
Expand Down Expand Up @@ -236,8 +236,8 @@ User-Agent DHC (9.4.2; MyRailsApplicationName) [https://github.com/DePayFi/dhc]
```ruby
response.request #<DHC::Request> the associated request.

response.data #<OpenStruct> in case response body contains parsable JSON.
response.data.something.nested
response.data # JSON
response.data.dig('something', 'nested')

response.body #<String>

Expand All @@ -256,9 +256,6 @@ The response data can be access with dot-notation and square-bracket notation. Y

```ruby
response = DHC.request(url: 'http://datastore/entry/1')
response.data.as_open_struct #<OpenStruct name='depay.fi'>
response.data.as_json # { name: 'depay.fi' }
response.data.name # 'depay.fi'
response.data[:name] # 'depay.fi'
```

Expand Down Expand Up @@ -347,7 +344,7 @@ If your error handler returns anything else but `nil` it replaces the response b
```ruby
handler = ->(response){ do_something_with_response; return {name: 'unknown'} }
response = DHC.get('http://something', rescue: handler)
response.data.name # 'unknown'
response.data[:name] # 'unknown'
```

### Ignore certain errors
Expand Down Expand Up @@ -481,7 +478,7 @@ If you configure `expires_at` and `refresh` proc in addition to `bearer`, DHC wi
```ruby
refresh = ->(response = nil){
if response
if response.code == 401 && response.data && response.data.error_code == 'ACCESS_TOKEN_EXPIRED'
if response.code == 401 && response.data && response.data[:error_code] == 'ACCESS_TOKEN_EXPIRED'
session[:access_token] = new_access_token
end
else
Expand Down
4 changes: 0 additions & 4 deletions lib/dhc/errors/parser_error.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/dhc/formats/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ def to_sym

def parse(input, object_class)
::JSON.parse(input, object_class: object_class)
rescue ::JSON::ParserError => e
raise DHC::ParserError.new(e.message, input)
end
end
end
3 changes: 1 addition & 2 deletions lib/dhc/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# The response contains the raw response (typhoeus)
# and provides functionality to access response data.
class DHC::Response
autoload :Data, 'dhc/response/data'

attr_accessor :request, :body_replacement
attr_reader :from_cache
Expand All @@ -23,7 +22,7 @@ def initialize(raw, request, from_cache: false)
end

def data
@data ||= body.present? ? DHC::Response::Data.new(self) : nil
@data ||= body.present? ? JSON.parse(body) : nil
end

def [](key)
Expand Down
28 changes: 0 additions & 28 deletions lib/dhc/response/data.rb

This file was deleted.

18 changes: 0 additions & 18 deletions lib/dhc/response/data/base.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/dhc/response/data/collection.rb

This file was deleted.

29 changes: 0 additions & 29 deletions lib/dhc/response/data/item.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/dhc/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module DHC
VERSION ||= '2.4.0'
VERSION ||= '3.0.0'
end
4 changes: 2 additions & 2 deletions spec/basic_methods/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
DHC.delete('http://datastore/v2/feedbacks/12121')
end

it 'makes response data available in a rails way' do
it 'makes response data available' do
response = DHC.delete('http://datastore/v2/feedbacks/12121')
expect(response.data.recommended).to eq true
expect(response.data['recommended']).to eq true
end

it 'provides response headers' do
Expand Down
6 changes: 3 additions & 3 deletions spec/basic_methods/get_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
DHC.get(:feedbacks, params: parameters)
end

it 'makes response data available in a rails way' do
it 'makes response data available' do
response = DHC.get('http://datastore/v2/feedbacks', params: parameters)
expect(response.data.total).to eq 99
expect(response.data['total']).to eq 99
end

it 'provides response headers' do
Expand All @@ -43,7 +43,7 @@

it 'requests json and parses response body' do
data = DHC.json.get('http://datastore/v2/feedbacks').data
expect(data.some).to eq 'json'
expect(data['some']).to eq 'json'
end
end
end
4 changes: 2 additions & 2 deletions spec/basic_methods/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
DHC.post(:feedbacks, body: feedback)
end

it 'makes response data available in a rails way' do
it 'makes response data available' do
response = DHC.post('http://datastore/v2/feedbacks', body: feedback)
expect(response.data.source_id).to eq 'aaa'
expect(response.data['source_id']).to eq 'aaa'
end

it 'provides response headers' do
Expand Down
4 changes: 2 additions & 2 deletions spec/basic_methods/put_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
DHC.put(:feedbacks, body: change)
end

it 'makes response data available in a rails way' do
it 'makes response data available' do
response = DHC.put('http://datastore/v2/feedbacks', body: change)
expect(response.data.recommended).to eq false
expect(response.data['recommended']).to eq false
end

it 'provides response headers' do
Expand Down
2 changes: 1 addition & 1 deletion spec/basic_methods/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

it 'does a request returning a response' do
response = DHC.request(url: 'http://datastore/v2/feedbacks', params: { has_reviews: true }, method: :get)
expect(response.data.total).to eq total
expect(response.data['total']).to eq total
end
end
end
2 changes: 1 addition & 1 deletion spec/interceptors/auth/bearer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def bearer_token
before do
refresh = ->(response = nil) {
if response
if response.code == 401 && response.data && response.data.error_code == 'ACCESS_TOKEN_EXPIRED'
if response.code == 401 && response.data && response.data['error_code'] == 'ACCESS_TOKEN_EXPIRED'
session[:access_token] = third_access_token
end
else
Expand Down
6 changes: 3 additions & 3 deletions spec/request/error_handling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def expect_status_code(status_code)
it 'requests json and parses response body' do
expect(-> {
DHC.json.get('http://datastore/v2/feedbacks').data
}).to raise_error(DHC::ParserError)
}).to raise_error(JSON::ParserError)
end
end

Expand All @@ -75,14 +75,14 @@ def expect_status_code(status_code)
stub_request(:get, 'http://something').to_return(status: 400)
handler = ->(_response) { { name: 'unknown' }.to_json }
request = DHC::Request.new(url: 'http://something', rescue: handler)
expect(request.response.data.name).to eq 'unknown'
expect(request.response.data['name']).to eq 'unknown'
end

it 'does not exchange body with handlers return if the handler returns nil' do
stub_request(:get, 'http://something').to_return(status: 400, body: { message: 'an error occurred' }.to_json)
handler = ->(_response) { nil }
request = DHC::Request.new(url: 'http://something', rescue: handler)
expect(request.response.data.message).to eq 'an error occurred'
expect(request.response.data['message']).to eq 'an error occurred'
end
end
end
2 changes: 1 addition & 1 deletion spec/response/data_accessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

it 'makes data accessible with square bracket accessor (symbol)' do
expect(
DHC.json.get('http://depay.fi')[:MyProp]
DHC.json.get('http://depay.fi')['MyProp']
).to eq 'MyValue'
end
end
Expand Down
28 changes: 4 additions & 24 deletions spec/response/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,15 @@
let(:body) { { some_key: { nested: value } } }

it 'makes data from response body available' do
expect(response.data.some_key.nested).to eq value
end

it 'makes data from response body available with hash bracket notation' do
expect(response.data[:some_key][:nested]).to eq value
expect(response.data.dig("some_key", "nested")).to eq value
end

it 'can be converted to json with the as_json method' do
expect(response.data.as_json).to eq body.as_json
end

it 'can be converted to an open struct with the as_open_struct method' do
expect(response.data.as_open_struct).to eq JSON.parse(response.body, object_class: OpenStruct)
end

it 'returns nil when data is not available' do
expect(response.data.something).to be_nil
expect(response.data["something"]).to be_nil
end
end

Expand All @@ -41,20 +33,8 @@
expect(response.data.as_json).to eq body.as_json
end

it 'can be converted to an open struct with the as_open_struct method' do
expect(response.data.as_open_struct).to eq JSON.parse(response.body, object_class: OpenStruct)
end

it 'is a collection of items' do
expect(response.data.size).to eq(1)
end

it 'makes item data from response body available' do
expect(response.data.first.some_key.nested).to eq value
end

it 'makes item data from response body available with hash bracket notation' do
expect(response.data.first[:some_key][:nested]).to eq value
expect(response.data.dig(0, "some_key", "nested")).to eq value
end
end
end
Expand All @@ -77,7 +57,7 @@
DHC.get('http://listings')
rescue DHC::Error => error
expect(
error.response.request.response.data.meta.errors.detect { |item| item.code == 2000 }.msg
error.response.request.response.data.dig("meta", "errors").detect { |item| item["code"] == 2000 }['msg']
).to eq 'I like to hide error messages (this is meta).'
end
end
Expand Down

0 comments on commit 2684bb0

Please sign in to comment.