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.



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

def initialize(url, options = {})
  @url = url
  @options = options
  @cookie = options[:cookie]
end

Instance Method Details

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/daddy/http_client.rb', line 13

def get(path, 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])
      request.headers['Authorization'] = basic
    end

    if @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
    response.body.force_encoding('UTF-8')
  end
end

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



38
39
40
# File 'lib/daddy/http_client.rb', line 38

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