Class: Locomotive::Httparty::Webservice
- Inherits:
-
Object
- Object
- Locomotive::Httparty::Webservice
- Includes:
- HTTParty
- Defined in:
- lib/locomotive/httparty/webservice.rb
Class Method Summary collapse
- .consume(url, options = {}) ⇒ Object
- .extract_base_uri_and_path(url) ⇒ Object
- .perform_request_to(path, options) ⇒ Object
Class Method Details
.consume(url, options = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/locomotive/httparty/webservice.rb', line 9 def self.consume(url, = {}) [:base_uri], path = self.extract_base_uri_and_path(url) .delete(:format) if [:format] == 'default' # auth ? username, password = .delete(:username), .delete(:password) [:basic_auth] = { username: username, password: password } if username self.perform_request_to(path, ) end |
.extract_base_uri_and_path(url) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/locomotive/httparty/webservice.rb', line 21 def self.extract_base_uri_and_path(url) url = HTTParty.normalize_base_uri(url) uri = URI.parse(url) path = uri.request_uri || '/' base_uri = "#{uri.scheme}://#{uri.host}" base_uri += ":#{uri.port}" if uri.port != 80 [base_uri, path] end |
.perform_request_to(path, options) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/locomotive/httparty/webservice.rb', line 32 def self.perform_request_to(path, ) # [DEBUG] puts "[WebService] consuming #{path}, #{options.inspect}" response = self.get(path, ) parsed_response = response.parsed_response if response.code == 200 if parsed_response.respond_to?(:underscore_keys) parsed_response.underscore_keys else parsed_response.collect(&:underscore_keys) end else nil end end |