make the webservice helper more robust (because of the numerous api errors in tumblr)
This commit is contained in:
parent
c828301f37
commit
d8a8ad4de0
@ -17,7 +17,18 @@ module Locomotive
|
||||
|
||||
# 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
|
||||
|
@ -4,18 +4,22 @@ describe Locomotive::Httparty::Webservice 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
|
||||
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')
|
||||
end
|
||||
|
||||
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')
|
||||
end
|
||||
|
||||
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' })
|
||||
end
|
||||
|
||||
|
@ -30,7 +30,8 @@ describe Locomotive::Liquid::Tags::Consume do
|
||||
context '#rendering' 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 %}"
|
||||
Liquid::Template.parse(template).render.should == 'Locomotive rocks !'
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user