Class: Abt::Providers::Harvest::Api
- Inherits:
-
Object
- Object
- Abt::Providers::Harvest::Api
- 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
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#account_id ⇒ Object
readonly
Returns the value of attribute account_id.
Instance Method Summary collapse
- #connection ⇒ Object
- #get_paged(path, query = {}) ⇒ Object
-
#initialize(access_token:, account_id:) ⇒ Api
constructor
A new instance of Api.
- #request(*args) ⇒ Object
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 = account_id end |
Instance Attribute Details
#access_token ⇒ Object (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_id ⇒ Object (readonly)
Returns the value of attribute account_id.
10 11 12 |
# File 'lib/abt/providers/harvest/api.rb', line 10 def account_id @account_id end |
Instance Method Details
#connection ⇒ Object
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"] = 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 |