Class: SillyPutty::Base
- Inherits:
-
Object
- Object
- SillyPutty::Base
- Defined in:
- lib/silly_putty/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#request_handler ⇒ Object
Returns the value of attribute request_handler.
-
#response_handler ⇒ Object
Returns the value of attribute response_handler.
Instance Method Summary collapse
-
#initialize(host, port) ⇒ Base
constructor
A new instance of Base.
- #request(method, uri, body, headers) ⇒ Object
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_url ⇒ Object (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 |
#host ⇒ Object (readonly)
Returns the value of attribute host.
4 5 6 |
# File 'lib/silly_putty/base.rb', line 4 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
4 5 6 |
# File 'lib/silly_putty/base.rb', line 4 def port @port end |
#request_handler ⇒ Object
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_handler ⇒ Object
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 |