Class: Requester

Inherits:
Object
  • Object
show all
Defined in:
lib/esp-commons/libraries/requester.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, headers_accept = nil) ⇒ Requester

Returns a new instance of Requester.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/esp-commons/libraries/requester.rb', line 7

def initialize(url, headers_accept = nil)
  @response ||= RestClient::Request.execute(
    :method => :get,
    :url => url,
    :timeout => nil,
    :headers => {
      :Accept => headers_accept
    }
  ) do |response, request, result, &block|
    response
  end
end

Instance Method Details

#responseObject



20
21
22
# File 'lib/esp-commons/libraries/requester.rb', line 20

def response
  @response
end

#response_bodyObject



42
43
44
# File 'lib/esp-commons/libraries/requester.rb', line 42

def response_body
  @response_body ||= response.body.force_encoding('UTF-8')
end

#response_hashObject



46
47
48
# File 'lib/esp-commons/libraries/requester.rb', line 46

def response_hash
  @response_hash ||= ActiveSupport::JSON.decode(response_body) rescue {}
end

#response_headersObject



34
35
36
# File 'lib/esp-commons/libraries/requester.rb', line 34

def response_headers
  @response_headers ||= stringify_headers response.headers
end

#response_statusObject



38
39
40
# File 'lib/esp-commons/libraries/requester.rb', line 38

def response_status
  @response_status ||= response.code
end

#stringify_headers(headers) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/esp-commons/libraries/requester.rb', line 24

def stringify_headers(headers)
  headers.map do |k, v|
    {
      k.to_s.split('_').map do |s|
        s.capitalize.gsub('Etag', 'ETag').gsub('Ua', 'UA')
      end.join('-') => v
    }
  end.reduce Hash.new, :merge
end