Class: FavroApi::Request

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

Constant Summary collapse

API_URL =
'https://favro.com/api/v1'.freeze
ENDPOINTS =
{
  cards:          '/cards',
  collections:    '/collections',
  comments:       '/comments',
  custom_fields:  '/customFields',
  organizations:  '/organizations',
  tasks:          '/tasks',
  tasklists:      '/tasklists',
  users:          '/users',
  widgets:        '/widgets'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

Returns a new instance of Request.



23
24
25
26
27
28
29
30
# File 'lib/favro_api/request.rb', line 23

def initialize(options = {})
  self.url           = options[:url]
  self.endpoint      = options[:endpoint]
  self.method        = options[:method] || :get
  self.params        = options[:params] || {}
  self.last_response = options[:last_response]
  self.page          = options[:page]
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



20
21
22
# File 'lib/favro_api/request.rb', line 20

def connection
  @connection
end

#endpointObject

Returns the value of attribute endpoint.



20
21
22
# File 'lib/favro_api/request.rb', line 20

def endpoint
  @endpoint
end

#last_responseObject

Returns the value of attribute last_response.



20
21
22
# File 'lib/favro_api/request.rb', line 20

def last_response
  @last_response
end

#methodObject

Returns the value of attribute method.



20
21
22
# File 'lib/favro_api/request.rb', line 20

def method
  @method
end

#pageObject

Returns the value of attribute page.



20
21
22
# File 'lib/favro_api/request.rb', line 20

def page
  @page
end

#paramsObject

Returns the value of attribute params.



20
21
22
# File 'lib/favro_api/request.rb', line 20

def params
  @params
end

#urlObject

Returns the value of attribute url.



20
21
22
# File 'lib/favro_api/request.rb', line 20

def url
  @url
end

Class Method Details

.fetch(endpoint, page: nil, last_response: nil, params: {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/favro_api/request.rb', line 51

def fetch(endpoint, page: nil, last_response: nil, params: {})
  new(
    endpoint:      endpoint,
    last_response: last_response,
    page:          page,
    method:        :get,
    params:        params
  ).fetch
end

Instance Method Details

#fetchObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/favro_api/request.rb', line 32

def fetch
  self.connection = Faraday.new("#{uri.scheme}://#{uri.hostname}") do |faraday|
    faraday.adapter :net_http
    faraday.basic_auth(FavroApi.auth.email, FavroApi.auth.token)
    faraday.headers['organizationId'] = params.delete(:organization_id)
    faraday.params['page']      = page || (last_response ? last_response.page + 1 : 0)
    faraday.params['requestId'] = last_response&.request_id
  end

  response = Response.new(response: connection.send(method, uri.request_uri, params))

  if response.error?
    raise ApiError, "Got API error. Code: #{response.status}, message: #{response.body}"
  end

  response
end