Skip to content

Commit

Permalink
Merge pull request #482 from studiolift/response_respond_to_predicates
Browse files Browse the repository at this point in the history
Add status predicate methods to Response#respond_to?
  • Loading branch information
greatuserongithub authored Jun 24, 2016
2 parents 01b23ca + 0562843 commit 0d808c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/httparty/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ def inspect
%(#<#{self.class}:0x#{inspect_id} parsed_response=#{parsed_response.inspect}, @response=#{response.inspect}, @headers=#{headers.inspect}>)
end

RESPOND_TO_METHODS = [:request, :response, :parsed_response, :body, :headers]

CODES_TO_OBJ = ::Net::HTTPResponse::CODE_CLASS_TO_OBJ.merge ::Net::HTTPResponse::CODE_TO_OBJ

CODES_TO_OBJ.each do |response_code, klass|
name = klass.name.sub("Net::HTTP", '')
define_method("#{underscore(name)}?") do
name = "#{underscore(name)}?".to_sym

RESPOND_TO_METHODS << name

define_method(name) do
klass === response
end
end
Expand All @@ -64,7 +70,7 @@ def inspect
end

def respond_to?(name, include_all = false)
return true if [:request, :response, :parsed_response, :body, :headers].include?(name)
return true if RESPOND_TO_METHODS.include?(name)
parsed_response.respond_to?(name, include_all) || response.respond_to?(name, include_all)
end

Expand Down
5 changes: 5 additions & 0 deletions spec/httparty/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
expect(response.respond_to?(:parsed_response)).to be_truthy
end

it "responds to predicates" do
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
expect(response.respond_to?(:success?)).to be_truthy
end

it "responds to anything parsed_response responds to" do
response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
expect(response.respond_to?(:[])).to be_truthy
Expand Down

0 comments on commit 0d808c7

Please sign in to comment.