Class: Harvest::HTTP::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/harvest/httpclient.rb

Overview

Make

Instance Method Summary collapse

Constructor Details

#initialize(domain:, account_id:, personal_token:) ⇒ Api

Returns a new instance of Api.



58
59
60
61
62
# File 'lib/harvest/httpclient.rb', line 58

def initialize(domain:, account_id:, personal_token:)
  @domain = domain
  @account_id = 
  @personal_token = personal_token
end

Instance Method Details

#api_call(struct) ⇒ Object

Make a api call to an endpoint.

Parameters:



75
76
77
78
79
80
81
# File 'lib/harvest/httpclient.rb', line 75

def api_call(struct)
  JSON.parse(
    send(struct.http_method.to_sym, struct).tap do
      # require 'pry'; binding.pry
    end
  )
end

#api_caller(path, http_method: 'get', param: {}, payload: nil, headers: {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/harvest/httpclient.rb', line 112

def api_caller(path, http_method: 'get', param: {}, payload: nil, headers: {})
  ApiCall.new(
    {
      path: path,
      http_method: http_method.to_sym,
      params: param,
      payload: payload,
      headers: headers
    }
  )
end

#clientObject



64
65
66
67
68
69
70
# File 'lib/harvest/httpclient.rb', line 64

def client
  Harvest::HTTP::Client
    .new
    .domain(@domain)
    .headers(@personal_token, @account_id)
    .client
end

#pagination(struct) ⇒ Object

Pagination through request

Parameters:

  • struct (Struct::Pagination)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/harvest/httpclient.rb', line 86

def pagination(struct)
  struct.param[:page] = struct.page_count
  page = api_call(struct.to_api_call).tap do
    # require 'pry'; binding.pry
  end
  struct.rows.concat(page[struct.data_key])

  return struct.rows if struct.page_count >= page['total_pages']

  struct.page_count += 1
  pagination(struct)
end

#paginator(http_method: 'get', page_count: 1, param: {}, entries: [], headers: {}) ⇒ Object

Create Paginaation struct message to pass to pagination call



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/harvest/httpclient.rb', line 100

def paginator(http_method: 'get', page_count: 1, param: {}, entries: [], headers: {})
  Struct::Pagination.new(
    {
      http_method: http_method,
      param: param,
      rows: entries,
      page_count: page_count,
      headers: headers
    }
  )
end