1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Merge pull request #444 from JonMidhir/implement_is_a_in_httparty_response

Implement is_a? and kind_of? in httparty response
This commit is contained in:
John Nunemaker 2015-11-16 08:54:55 -05:00
commit 99e398705b
3 changed files with 28 additions and 4 deletions

View file

@ -11,8 +11,8 @@ def constantize(camel_cased_word)
constant
end
Then /it should return an? (\w+)$/ do |class_string|
expect(@response_from_httparty).to be_a(class_string.class)
Then /it should return an? ([\w\:]+)$/ do |class_string|
expect(@response_from_httparty.parsed_response).to be_a(Object.const_get(class_string))
end
Then /the return value should match '(.*)'/ do |expected_text|
@ -20,7 +20,7 @@ Then /the return value should match '(.*)'/ do |expected_text|
end
Then /it should return a Hash equaling:/ do |hash_table|
expect(@response_from_httparty).to be_a(Hash)
expect(@response_from_httparty.parsed_response).to be_a(Hash)
expect(@response_from_httparty.keys.length).to eq(hash_table.rows.length)
hash_table.hashes.each do |pair|
key, value = pair["key"], pair["value"]
@ -30,7 +30,7 @@ Then /it should return a Hash equaling:/ do |hash_table|
end
Then /it should return an Array equaling:/ do |array|
expect(@response_from_httparty).to be_a(Array)
expect(@response_from_httparty.parsed_response).to be_a(Array)
expect(@response_from_httparty.parsed_response).to eq(array.raw)
end

View file

@ -27,6 +27,12 @@ module HTTParty
Response
end
def is_a?(klass)
self.class == klass || self.class < klass
end
alias_method :kind_of?, :is_a?
def code
response.code.to_i
end

View file

@ -114,6 +114,24 @@ RSpec.describe HTTParty::Response do
end
end
describe "#is_a?" do
subject { HTTParty::Response.new(@request_object, @response_object, @parsed_response) }
it { is_expected.to respond_to(:is_a?).with(1).arguments }
it { expect(subject.is_a?(HTTParty::Response)).to be_truthy }
it { expect(subject.is_a?(BasicObject)).to be_truthy }
it { expect(subject.is_a?(Object)).to be_falsey }
end
describe "#kind_of?" do
subject { HTTParty::Response.new(@request_object, @response_object, @parsed_response) }
it { is_expected.to respond_to(:kind_of?).with(1).arguments }
it { expect(subject.kind_of?(HTTParty::Response)).to be_truthy }
it { expect(subject.kind_of?(BasicObject)).to be_truthy }
it { expect(subject.kind_of?(Object)).to be_falsey }
end
describe "semantic methods for response codes" do
def response_mock(klass)
response = klass.new('', '', '')