Skip to content

Commit

Permalink
Switch between string and symbol keys for accessing parsed_response
Browse files Browse the repository at this point in the history
values
  • Loading branch information
Robert Paul committed Apr 27, 2017
1 parent a34b05c commit 4ec134b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/gibbon/api_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ def handle_error(error)

if parsed_response
error_params[:body] = parsed_response
error_params[:title] = parsed_response["title"] if parsed_response["title"]
error_params[:detail] = parsed_response["detail"] if parsed_response["detail"]

title_key = symbolize_keys ? :title : "title"
detail_key = symbolize_keys ? :detail : "detail"

error_params[:title] = parsed_response[title_key] if parsed_response[title_key]
error_params[:detail] = parsed_response[detail_key] if parsed_response[detail_key]
end

end
Expand Down
14 changes: 14 additions & 0 deletions spec/gibbon/api_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,19 @@
expect(boom.raw_body).to eq "A non JSON response"
end
end

context "when symbolize_keys is true" do
it "sets title and detail on the error params" do
response_values = {:status => 422, :headers => {}, :body => '{"title": "foo", "detail": "bar"}'}
exception = Faraday::Error::ClientError.new("the server responded with status 422", response_values)
api_request = Gibbon::APIRequest.new(builder: Gibbon::Request.new(symbolize_keys: true))
begin
api_request.send :handle_error, exception
rescue => boom
expect(boom.title).to eq "foo"
expect(boom.detail).to eq "bar"
end
end
end
end
end

0 comments on commit 4ec134b

Please sign in to comment.