the callAPI action also returns now the status

did committed Mar 08, 2017
commit 2768cf1a79b8d6f352ef77e8df8731dce9d5c893
Showing 4 changed files with 28 additions and 4 deletions
locomotive/steam/services/action_service.rb b/lib/locomotive/steam/services/action_service.rb +1 -1
@@ @@ -98,7 +98,7 @@ module Locomotive
end
def call_api_lambda(liquid_context)
- -> (method, url, options) { api_service.consume(url, (options || {}).with_indifferent_access.merge(method: method)) }
+ -> (method, url, options) { api_service.consume(url, (options || {}).with_indifferent_access.merge(method: method), true) }
end
def redirect_to_lambda(liquid_context)
locomotive/steam/services/external_api_service.rb b/lib/locomotive/steam/services/external_api_service.rb +17 -2
@@ @@ -16,7 +16,7 @@ module Locomotive
# - header_auth
# - with_user_agent
#
- def consume(url, options = {})
+ def consume(url, options = {}, with_status = false)
base_uri, path = extract_base_uri_and_path(url)
method = (options[:method] || 'GET').to_s.downcase
@@ @@ -24,7 +24,11 @@ module Locomotive
_options = build_httpparty_options(options, method)
_options[:base_uri] = base_uri
- perform_request_to(method, path, _options)
+ if with_status
+ verbose_perform_request_to(method, path, _options)
+ else
+ perform_request_to(method, path, _options)
+ end
end
private
@@ @@ -76,6 +80,17 @@ module Locomotive
end
end
+ def verbose_perform_request_to(method, path, options)
+ response = self.class.send(method.to_sym, path, options)
+ parsed_response = response.parsed_response
+ {
+ status: response.code,
+ data: HashConverter.to_underscore(parsed_response)
+ }
+ rescue Exception => e
+ { status: nil, error: e.message }
+ end
+
end
end
end
spec/integration/services/external_api_service_spec.rb +9 -0
@@ @@ -15,6 +15,15 @@ describe Locomotive::Steam::ExternalAPIService do
it { expect(subject.size).to_not eq 0 }
+ context 'returns the status too' do
+
+ subject { service.consume(url, options, true) }
+
+ it { expect(subject[:status]).to eq 200 }
+ it { expect(subject[:data].size).to_not eq 0 }
+
+ end
+
end
end
spec/unit/services/action_service_spec.rb +1 -1
@@ @@ -204,7 +204,7 @@ describe Locomotive::Steam::ActionService do
data: {
token: '123456789'
}
- }
+ }, true
)
subject
end