Class: Daddy::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/daddy/http_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ HttpClient

Returns a new instance of HttpClient.



8
9
10
11
12
13
# File 'lib/daddy/http_client.rb', line 8

def initialize(url, options = {})
  @url = url
  @options = options
  @cookie = options[:cookie]
  Rails.logger.debug "[HttpClient] url: #{@url}"
end

Instance Method Details

#get(path, params = {}) ⇒ Object



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
42
43
44
45
# File 'lib/daddy/http_client.rb', line 15

def get(path, params = {})
  Rails.logger.debug "[HttpClient] path: #{path}"
  Rails.logger.debug "[HttpClient] params: #{params}"

  response = connection.get(path, params) do |request|
    if @options[:auth_user] and @options[:auth_password]
      basic = 'Basic ' + Base64.encode64(@options[:auth_user] + ':' + @options[:auth_password])
      Rails.logger.debug "[HttpClient] authorization: #{basic}"

      request.headers['Authorization'] = basic
    end
    
    if @cookie
      Rails.logger.debug "[HttpClient] cookie: #{@cookie}"
      request.headers['Cookie'] = @cookie
    end
    
    params.each do |key, value|
      request.params[key] = value
    end
  end
  
  @cookie = response.headers['set-cookie']
  
  if block_given?
    yield response
  else
    Rails.logger.debug "[HttpClient] stutus: #{response.status}"
    response.body.force_encoding('UTF-8')
  end
end

#post(path, params = {}) ⇒ Object



47
48
49
# File 'lib/daddy/http_client.rb', line 47

def post(path, params = {})
  response = connection.post(path, params)
end