Class: Segurofacil::REST::Request

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

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Content-Type' => 'application/x-www-form-urlencoded',
  'Accept'       => 'application/json',
  'User-Agent'   => "segurofacil-ruby/#{Segurofacil::VERSION}"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, method, options = {}) ⇒ Request

Returns a new instance of Request.



16
17
18
19
20
# File 'lib/segurofacil/rest/request.rb', line 16

def initialize(path, method, options={})
  @path = path
  @method = method
  @parameters = options.fetch(:params, Hash.new)
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



8
9
10
# File 'lib/segurofacil/rest/request.rb', line 8

def method
  @method
end

#parametersObject (readonly)

Returns the value of attribute parameters.



8
9
10
# File 'lib/segurofacil/rest/request.rb', line 8

def parameters
  @parameters
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/segurofacil/rest/request.rb', line 8

def path
  @path
end

Class Method Details

.post(path, options = {}) ⇒ Object



42
43
44
# File 'lib/segurofacil/rest/request.rb', line 42

def post(path, options={})
  self.new path, 'POST', options
end

Instance Method Details

#performObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/segurofacil/rest/request.rb', line 22

def perform
  json_res = {}

  begin
    res = RestClient::Request.execute request_params
    json_res[:body] = MultiJson.load res.body
    json_res[:code] = res.code
  rescue RestClient::ExceptionWithResponse => e
    parsed_error = MultiJson.load e.http_body

    json_res[:error] = true
    json_res[:body] = parsed_error
    json_res[:code] = e.response.code
  end

  json_res
end