Class: Fortnox::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, url, params = {}) ⇒ Request

Returns a new instance of Request.



5
6
7
8
9
# File 'lib/fortnox/request.rb', line 5

def initialize(type, url, params = {})
  @url = url
  @type = type
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/fortnox/request.rb', line 3

def params
  @params
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/fortnox/request.rb', line 3

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/fortnox/request.rb', line 3

def url
  @url
end

Instance Method Details

#auth_headersObject



34
35
36
# File 'lib/fortnox/request.rb', line 34

def auth_headers
  client.auth_headers
end

#clientObject



38
39
40
# File 'lib/fortnox/request.rb', line 38

def client
  Fortnox.client
end

#contentObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/fortnox/request.rb', line 11

def content
  case type
  when :post, :put
    { body: params.to_json }
  when :get
    { query: params }
  when :delete
    {}
  end
end

#executeObject



22
23
24
# File 'lib/fortnox/request.rb', line 22

def execute
  response.success? ? response.parsed_response : raise_from(response)
end

#raise_from(response) ⇒ Object



26
27
28
# File 'lib/fortnox/request.rb', line 26

def raise_from(response)
  raise Fortnox::Error.from_response(response)
end

#responseObject



30
31
32
# File 'lib/fortnox/request.rb', line 30

def response
  @response ||= client.send(type, url, {headers: auth_headers}.merge(Fortnox::Client.headers))
end