Class: DashFu::Mario::Session
- Inherits:
-
Object
- Object
- DashFu::Mario::Session
- Defined in:
- lib/dash-fu/mario.rb
Overview
HTTP Session.
Instance Method Summary collapse
-
#get(path, headers = {}) {|response.code, response.body, {}| ... } ⇒ Object
Make a GET request and yield response to the block.
-
#get_json(path, headers = {}, &block) ⇒ Object
Make a GET request and yield response to the block.
-
#initialize(http) ⇒ Session
constructor
:nodoc:.
Constructor Details
#initialize(http) ⇒ Session
:nodoc:
134 135 136 |
# File 'lib/dash-fu/mario.rb', line 134 def initialize(http) #:nodoc: @http = http end |
Instance Method Details
#get(path, headers = {}) {|response.code, response.body, {}| ... } ⇒ Object
Make a GET request and yield response to the block. Response consists of three arguments: status code, response body and response headers. The block may be called asynchronoulsy.
141 142 143 144 |
# File 'lib/dash-fu/mario.rb', line 141 def get(path, headers = {}, &block) response = @http.request(get_request(path, headers)) yield response.code, response.body, {} end |
#get_json(path, headers = {}, &block) ⇒ Object
Make a GET request and yield response to the block. If the response status is 200 the second argument is the response JSON object. The block may be called asynchronously.
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/dash-fu/mario.rb', line 149 def get_json(path, headers = {}, &block) response = @http.request(get_request(path, headers)) if Net::HTTPOK === response json = JSON.parse(response.body) rescue nil if json yield response.code.to_i, json, {} else yield 500, "Not a JSON document", {} end else yield response.code.to_i, response., {} end end |