Class: Dyn::HttpClient::Base

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

Direct Known Subclasses

CurbClient, NetHttpClient, PatronClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, protocol = 'http') ⇒ Base

Returns a new instance of Base.



25
26
27
28
29
30
# File 'lib/dyn/http/http_client.rb', line 25

def initialize(host, port, protocol = 'http')
  @host = host
  @port = port
  @base_url = "#{protocol}://#{host}:#{port.to_s}"
  @default_headers = {"User-Agent" => "dyn-rb 0.0.1"}
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



22
23
24
# File 'lib/dyn/http/http_client.rb', line 22

def base_url
  @base_url
end

#default_headersObject

Returns the value of attribute default_headers.



23
24
25
# File 'lib/dyn/http/http_client.rb', line 23

def default_headers
  @default_headers
end

#hostObject (readonly)

Returns the value of attribute host.



22
23
24
# File 'lib/dyn/http/http_client.rb', line 22

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



22
23
24
# File 'lib/dyn/http/http_client.rb', line 22

def port
  @port
end

#request_handlerObject

Returns the value of attribute request_handler.



23
24
25
# File 'lib/dyn/http/http_client.rb', line 23

def request_handler
  @request_handler
end

#response_handlerObject

Returns the value of attribute response_handler.



23
24
25
# File 'lib/dyn/http/http_client.rb', line 23

def response_handler
  @response_handler
end

Instance Method Details

#delete(uri, body, headers) ⇒ Object



65
66
67
# File 'lib/dyn/http/http_client.rb', line 65

def delete(uri, body, headers)
  request(:DELETE, uri, body, headers)
end

#get(uri, body, headers) ⇒ Object



53
54
55
# File 'lib/dyn/http/http_client.rb', line 53

def get(uri, body, headers)
  request(:GET, uri, body, headers)
end

#head(uri, body, headers) ⇒ Object



49
50
51
# File 'lib/dyn/http/http_client.rb', line 49

def head(uri, body, headers)
  request(:HEAD, uri, body, headers)
end

#post(uri, body, headers) ⇒ Object



57
58
59
# File 'lib/dyn/http/http_client.rb', line 57

def post(uri, body, headers)
  request(:POST, uri, body, headers)
end

#put(uri, body, headers) ⇒ Object



61
62
63
# File 'lib/dyn/http/http_client.rb', line 61

def put(uri, body, headers)
  request(:PUT, uri, body, headers)
end

#request(method, uri, body, headers) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dyn/http/http_client.rb', line 32

def request(method, uri, body, headers)
  if (method == :HEAD) && body
    raise ArgumentError, "#{method} must not contain a body!"
  end
  headers = @default_headers.merge(headers || {})
  original = {:method => method, :base_url => @base_url, :path => uri, :body => body, :headers => headers}
  fixedup  = fixup_request(original)

  @request_handler.call(fixedup) if @request_handler

  response = perform_request(fixedup[:method], fixedup[:path], fixedup[:body], fixedup[:headers])

  @response_handler.call({:status => response.status, :body => response.body, :headers => response.headers}) if @response_handler

  response
end