2011-06-22 22:57:24 +00:00
|
|
|
require 'uri'
|
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
module Locomotive
|
|
|
|
module Httparty
|
|
|
|
class Webservice
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 21:44:24 +00:00
|
|
|
include ::HTTParty
|
2010-07-06 00:05:47 +00:00
|
|
|
|
|
|
|
def self.consume(url, options = {})
|
|
|
|
url = HTTParty.normalize_base_uri(url)
|
|
|
|
|
2011-06-22 22:57:24 +00:00
|
|
|
uri = URI.parse(url)
|
|
|
|
options[:base_uri] = "#{uri.scheme}://#{uri.host}"
|
|
|
|
options[:base_uri] += ":#{uri.port}" if uri.port != 80
|
|
|
|
path = uri.request_uri
|
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
options.delete(:format) if options[:format] == 'default'
|
|
|
|
|
|
|
|
username, password = options.delete(:username), options.delete(:password)
|
|
|
|
options[:basic_auth] = { :username => username, :password => password } if username
|
|
|
|
|
|
|
|
path ||= '/'
|
|
|
|
|
2010-07-20 10:21:18 +00:00
|
|
|
# puts "[WebService] consuming #{path}, #{options.inspect}"
|
2010-07-06 00:05:47 +00:00
|
|
|
|
2011-01-03 21:09:19 +00:00
|
|
|
response = self.get(path, options)
|
|
|
|
|
|
|
|
if response.code == 200
|
2011-01-04 11:49:14 +00:00
|
|
|
if response.respond_to?(:underscore_keys)
|
|
|
|
response.underscore_keys
|
2011-01-03 21:09:19 +00:00
|
|
|
else
|
2011-01-04 11:49:14 +00:00
|
|
|
response.collect(&:underscore_keys)
|
2011-01-03 21:09:19 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|