Class: DashFu::Mario::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/dash-fu/mario.rb

Overview

HTTP Session.

Instance Method Summary collapse

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.

Yields:

  • (response.code, response.body, {})


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.message, {}
  end
end