Class: Camp3::Request

Inherits:
Object
  • Object
show all
Includes:
Logging, HTTParty
Defined in:
lib/camp3/request.rb

Defined Under Namespace

Modules: Result

Instance Attribute Summary

Attributes included from Logging

#logger

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token, user_agent, client) ⇒ Request

Returns a new instance of Request.



20
21
22
23
24
25
# File 'lib/camp3/request.rb', line 20

def initialize(access_token, user_agent, client)
  @access_token = access_token
  @client = client

  self.class.headers 'User-Agent' => user_agent
end

Class Method Details

.decode(response) ⇒ Object

Decodes a JSON response into Ruby object.



47
48
49
50
51
# File 'lib/camp3/request.rb', line 47

def self.decode(response)
  response ? JSON.load(response) : {}
rescue JSON::ParserError
  raise Error::Parsing, 'The response is not a valid JSON'
end

.parse(body) ⇒ Object

Converts the response body to a Resource.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/camp3/request.rb', line 28

def self.parse(body)
  body = decode(body)

  if body.is_a? Hash
    Resource.create(body)
  elsif body.is_a? Array
    PaginatedResponse.new(body.collect! { |e| Resource.create(e) })
  elsif body
    true
  elsif !body
    false
  elsif body.nil?
    false
  else
    raise Error::Parsing, "Couldn't parse a response body"
  end
end