Class: Chute::Connection

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/chute/connection.rb

Class Method Summary collapse

Class Method Details

.base_uriObject



10
11
12
# File 'lib/chute/connection.rb', line 10

def self.base_uri
  Chute.api_endpoint
end

.headersObject



14
15
16
17
18
19
20
21
# File 'lib/chute/connection.rb', line 14

def self.headers
  {
      'Authorization' => "Bearer #{Chute.access_token}",
      'Content-Type' => 'application/json',
      'Accepts' => 'application/json',
      'x-client_id' => Chute.app_id
  }
end

.parse(object) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/chute/connection.rb', line 44

def self.parse(object)
  if object.respond_to?(:each_pair)
    Chute::Response.new(object)
  else
    [200, 201].include?(object.code)
  end
end

.request(request_type, url, body = "") ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chute/connection.rb', line 23

def self.request(request_type, url, body="")
  body = body.to_json unless String === body
  options = body.empty? ? {headers: headers} : {headers: headers, body: body}

  begin
    response = send(request_type, "#{base_uri}#{url}", options)
    parse(response)
  rescue Errno::ECONNREFUSED
    p 'Service Unavailable'
    Chute::Response.with_code_and_error(503, 'Service Unavailable')
    raise ChuteApiUnavailableException.new('Could not connect to the Server')
  rescue MultiJson::DecodeError => ex
    p 'Internal Server Error'
    Chute::Response.with_code_and_error(500, 'Internal Server Error')
    raise ChuteApiInternalException, "Chute API Exception. #{ex.message}"
  rescue Exception => ex
    p 'Unknown Error'
    raise
  end
end