Class: Locomotive::Steam::Httparty::Webservice

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

Class Method Summary collapse

Class Method Details

.consume(url, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/locomotive/steam/monkey_patches/httparty.rb', line 10

def self.consume(url, options = {})
  url = ::HTTParty.normalize_base_uri(url)

  uri = URI.parse(url)
  options[:base_uri] = "#{uri.scheme}://#{uri.host}"
  options[:base_uri] += ":#{uri.port}" if uri.port != 80
  path = uri.request_uri

  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 ||= '/'

  # Locomotive::Steam::Logger.debug "[WebService] consuming #{path}, #{options.inspect}"

  response = self.get(path, options)

  if response.code == 200
    _response = response.parsed_response
    if _response.respond_to?(:underscore_keys)
      _response.underscore_keys
    else
      _response.collect(&:underscore_keys)
    end
  else
    Locomotive::Steam::Logger.error "[WebService] consumed #{path}, #{options.inspect}, response = #{response.inspect}"
    nil
  end

end