Class: SillyPutty::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/silly_putty/base.rb

Direct Known Subclasses

KirkClient, NetHttpClient, PatronClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Base



7
8
9
10
11
# File 'lib/silly_putty/base.rb', line 7

def initialize(host, port)
  @host = host
  @port = port
  @base_url = "http://" + host + ":" + port.to_s
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



4
5
6
# File 'lib/silly_putty/base.rb', line 4

def base_url
  @base_url
end

#hostObject (readonly)

Returns the value of attribute host.



4
5
6
# File 'lib/silly_putty/base.rb', line 4

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



4
5
6
# File 'lib/silly_putty/base.rb', line 4

def port
  @port
end

#request_handlerObject

Returns the value of attribute request_handler.



5
6
7
# File 'lib/silly_putty/base.rb', line 5

def request_handler
  @request_handler
end

#response_handlerObject

Returns the value of attribute response_handler.



5
6
7
# File 'lib/silly_putty/base.rb', line 5

def response_handler
  @response_handler
end

Instance Method Details

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/silly_putty/base.rb', line 13

def request(method, uri, body, headers)
  if (method == :GET || method == :DELETE || method == :HEAD) && body
    raise ArgumentError, "#{method} must not contain a body!"
  end

  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