Class: Commentatr::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/commentatr/request.rb

Constant Summary collapse

HOST =
"api.commentatr.net"
PORT =
80

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, get_params = {}, post_params = {}) ⇒ Request

Returns a new instance of Request.



9
10
11
12
# File 'lib/commentatr/request.rb', line 9

def initialize(method, uri, get_params = {}, post_params = {})
  @method, @uri, @get_params, @post_params = method, uri, get_params, post_params
  @response = nil
end

Instance Attribute Details

#get_paramsObject

Returns the value of attribute get_params.



6
7
8
# File 'lib/commentatr/request.rb', line 6

def get_params
  @get_params
end

#methodObject

Returns the value of attribute method.



6
7
8
# File 'lib/commentatr/request.rb', line 6

def method
  @method
end

#post_paramsObject

Returns the value of attribute post_params.



6
7
8
# File 'lib/commentatr/request.rb', line 6

def post_params
  @post_params
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/commentatr/request.rb', line 7

def response
  @response
end

#uriObject

Returns the value of attribute uri.



6
7
8
# File 'lib/commentatr/request.rb', line 6

def uri
  @uri
end

Instance Method Details

#makeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/commentatr/request.rb', line 14

def make
  uri = @uri + ("?" + uri_params(@get_params) unless @get_params.empty?).to_s

  http = ::Net::HTTP.start(HOST, PORT)
  response = http.send_request(@method.to_s.upcase, uri, uri_params(@post_params))

  begin
    body = JSON.parse(response.body)
  rescue
    body = nil
  end

  @response = Commentatr::Response.new(response.code.to_i, body)
end