Class: Locomotive::Httparty::Webservice

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/locomotive/httparty/webservice.rb

Class Method Summary collapse

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, options = {})
  options[:base_uri], path = self.extract_base_uri_and_path(url)

  options.delete(:format) if options[:format] == 'default'

  # auth ?
  username, password = options.delete(:username), options.delete(:password)
  options[:basic_auth] = { username: username, password: password } if username

  self.perform_request_to(path, options)
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, options)
  # [DEBUG] puts "[WebService] consuming #{path}, #{options.inspect}"

  response        = self.get(path, options)
  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