Class: Abt::Providers::Harvest::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/abt/providers/harvest/api.rb

Constant Summary collapse

API_ENDPOINT =
"https://api.harvestapp.com/v2"
VERBS =
[:get, :post, :patch].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, account_id:) ⇒ Api

Returns a new instance of Api.



12
13
14
15
# File 'lib/abt/providers/harvest/api.rb', line 12

def initialize(access_token:, account_id:)
  @access_token = access_token
  @account_id = 
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



10
11
12
# File 'lib/abt/providers/harvest/api.rb', line 10

def access_token
  @access_token
end

#account_idObject (readonly)

Returns the value of attribute account_id.



10
11
12
# File 'lib/abt/providers/harvest/api.rb', line 10

def 
  @account_id
end

Instance Method Details

#connectionObject



52
53
54
55
56
57
58
# File 'lib/abt/providers/harvest/api.rb', line 52

def connection
  @connection ||= Faraday.new(API_ENDPOINT) do |connection|
    connection.headers["Authorization"] = "Bearer #{access_token}"
    connection.headers["Harvest-Account-Id"] = 
    connection.headers["Content-Type"] = "application/json"
  end
end

#get_paged(path, query = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/abt/providers/harvest/api.rb', line 23

def get_paged(path, query = {})
  result_key = path.split("?").first.split("/").last

  page = 1
  records = []

  loop do
    result = get(path, query.merge(page: page))
    records += result[result_key]
    break if result["total_pages"] == page

    page += 1
  end

  records
end

#request(*args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/abt/providers/harvest/api.rb', line 40

def request(*args)
  response = connection.public_send(*args)

  if response.success?
    Oj.load(response.body)
  else
    error_class = Abt::HttpError.error_class_for_status(response.status)
    encoded_response_body = response.body.force_encoding("utf-8")
    raise error_class, "Code: #{response.status}, body: #{encoded_response_body}"
  end
end