make the webservice helper more robust (because of the numerous api errors in tumblr)

This commit is contained in:
dinedine 2011-01-03 22:09:19 +01:00
parent c828301f37
commit d8a8ad4de0
3 changed files with 21 additions and 5 deletions

View File

@ -17,7 +17,18 @@ module Locomotive
# puts "[WebService] consuming #{path}, #{options.inspect}" # puts "[WebService] consuming #{path}, #{options.inspect}"
self.get(path, options).try(:underscore_keys) response = self.get(path, options)
if response.code == 200
if response.respond_to?(:each)
response.collect(&:underscore_keys)
else
response.try(:underscore_keys)
end
else
nil
end
end end
end end

View File

@ -4,18 +4,22 @@ describe Locomotive::Httparty::Webservice do
context '#consuming' do context '#consuming' do
before(:each) do
@response = mock('response', :code => 200, :underscore_keys => [])
end
it 'sets the base uri from a simple url' do it 'sets the base uri from a simple url' do
Locomotive::Httparty::Webservice.expects(:get).with('/', { :base_uri => 'http://blog.locomotiveapp.org' }) Locomotive::Httparty::Webservice.expects(:get).with('/', { :base_uri => 'http://blog.locomotiveapp.org' }).returns(@response)
Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org') Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org')
end end
it 'sets both the base uri and the path from an url with parameters' do it 'sets both the base uri and the path from an url with parameters' do
Locomotive::Httparty::Webservice.expects(:get).with('/api/read/json?num=3', { :base_uri => 'http://blog.locomotiveapp.org' }) Locomotive::Httparty::Webservice.expects(:get).with('/api/read/json?num=3', { :base_uri => 'http://blog.locomotiveapp.org' }).returns(@response)
Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org/api/read/json?num=3') Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org/api/read/json?num=3')
end end
it 'sets auth credentials' do it 'sets auth credentials' do
Locomotive::Httparty::Webservice.expects(:get).with('/', { :base_uri => 'http://blog.locomotiveapp.org', :basic_auth => { :username => 'john', :password => 'foo' } }) Locomotive::Httparty::Webservice.expects(:get).with('/', { :base_uri => 'http://blog.locomotiveapp.org', :basic_auth => { :username => 'john', :password => 'foo' } }).returns(@response)
Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org', { :username => 'john', :password => 'foo' }) Locomotive::Httparty::Webservice.consume('http://blog.locomotiveapp.org', { :username => 'john', :password => 'foo' })
end end

View File

@ -30,7 +30,8 @@ describe Locomotive::Liquid::Tags::Consume do
context '#rendering' do context '#rendering' do
it 'puts the response into the liquid variable' do it 'puts the response into the liquid variable' do
Locomotive::Httparty::Webservice.stubs(:get).returns({ 'title' => 'Locomotive rocks !' }) response = mock('response', :code => 200, :underscore_keys => { 'title' => 'Locomotive rocks !' })
Locomotive::Httparty::Webservice.stubs(:get).returns(response)
template = "{% consume blog from \"http://blog.locomotiveapp.org/api/read\" %}{{ blog.title }}{% endconsume %}" template = "{% consume blog from \"http://blog.locomotiveapp.org/api/read\" %}{{ blog.title }}{% endconsume %}"
Liquid::Template.parse(template).render.should == 'Locomotive rocks !' Liquid::Template.parse(template).render.should == 'Locomotive rocks !'
end end