Class: Raven::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/rb_raven_api/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_config) ⇒ Http

Returns a new instance of Http.



7
8
9
# File 'lib/rb_raven_api/http.rb', line 7

def initialize(_config)
  @config, @errors, @success = _config, [], false
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/rb_raven_api/http.rb', line 5

def config
  @config
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/rb_raven_api/http.rb', line 5

def errors
  @errors
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/rb_raven_api/http.rb', line 5

def response
  @response
end

#successObject

Returns the value of attribute success.



5
6
7
# File 'lib/rb_raven_api/http.rb', line 5

def success
  @success
end

Instance Method Details

#build_params(params = {}) ⇒ Object



19
20
21
# File 'lib/rb_raven_api/http.rb', line 19

def build_params(params = {})
  params.merge key: @config.api_key, format: 'json'
end

#get(path = '', params = {}) ⇒ Object



11
12
13
# File 'lib/rb_raven_api/http.rb', line 11

def get(path = '', params = {})
  request 'get', "#{path}?#{RestClient::Payload.generate(build_params(params))}"
end

#post(path = '', params = {}) ⇒ Object



15
16
17
# File 'lib/rb_raven_api/http.rb', line 15

def post(path = '', params = {})
  request 'post', path, build_params(params)
end

#request(http_verb, path, params = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rb_raven_api/http.rb', line 23

def request(http_verb, path, params = nil)
  url  = "#{@config.host}#{path}"
  args = http_verb == 'post' ? [http_verb, url, params] : [http_verb, url]

  response = RestClient.send *args do |res, req, raw_res|
    body = raw_res.body
    code = raw_res.code.to_i

    self.response = body
    self.errors   = []

    case code
    when 200
      begin 
        parsed = JSON.parse body
      rescue JSON::ParserError => e
        self.response = body
      end
      self.success = true
    when 204
      self.errors << RequestError.new('No Content', code, path, params)
    else
      self.errors << RequestError.new(body, code, path, params)
    end

    Response.new(self, code, path, params)
  end
end