Class: GreenhouseApi::BaseClient

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/greenhouse_api/base_client.rb

Constant Summary collapse

MAX_PER_PAGE =
500
API_URL =
'https://harvest.greenhouse.io'

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ BaseClient

Returns a new instance of BaseClient.



26
27
28
# File 'lib/greenhouse_api/base_client.rb', line 26

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#compose_response(response) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/greenhouse_api/base_client.rb', line 53

def compose_response(response)
  if [200, 201].include?(response&.status)
    Response.new(
      body: response.body,
      headers: response.headers,
      status: response.status
    )
  else
    response
  end
end

#get_one(resource, id) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/greenhouse_api/base_client.rb', line 65

def get_one(resource, id)
  response = request(
    http_method: :get,
    headers: headers,
    endpoint: "#{resource}/#{id}"
  )
  compose_response(response)
end

#headersObject



30
31
32
33
34
35
36
# File 'lib/greenhouse_api/base_client.rb', line 30

def headers
  credential = Base64.strict_encode64(@api_key + ':')

  {
    'Authorization' => 'Basic ' + credential,
  }
end

#list_many(resource, params = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/greenhouse_api/base_client.rb', line 74

def list_many(resource, params = {})
  limit = params.delete(:limit)
  page = params[:page] || 1
  data = []
  response = nil

  loop do
    per_page = if params[:per_page]
      params[:per_page]
    else
      limit ? [limit - data.length, MAX_PER_PAGE].min : MAX_PER_PAGE
    end

    response = request(
      http_method: :get,
      headers: headers,
      endpoint: resource,
      params: params.merge(page: page, per_page: per_page)
    )
    break if response.status != 200

    data.concat(response.body)

    if last_page?(response) || data_limit_reached?(data, limit) || params[:page]
      break
    else
      page += 1
    end
  end

  if response.status == 200
    Response.new(
      body: data,
      headers: response.headers,
      status: response.status
    )
  else
    response
  end
end

#request(http_method:, headers:, endpoint:, params: {}, body: {}, api_version: 'v1') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/greenhouse_api/base_client.rb', line 38

def request(http_method:, headers:, endpoint:, params: {}, body: {}, api_version: 'v1')
  response = Faraday.public_send(http_method) do |request|
    request.headers = headers
    request.path = "#{API_URL}/#{api_version}/#{endpoint}"
    request.params = params
    request.body = body
  end

  Response.new(
    body: response.body && !response.body.empty? ? JSON.parse(response.body) : '',
    headers: response.headers,
    status: response.status
  )
end