greenhouse_api 0.1.11 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 251acd5051c995e9fb934939882496cdd3746f01fbf2a53c925cf05a4b74c7fd
4
- data.tar.gz: f723a91437727ad6bac4bc807c9057700315a21b92d0a7207ea515d9e142a7a4
3
+ metadata.gz: 9ff456467fd8aef10884057caf6e3b2d7dc2ea9976b64ffa2b2062b255e9b560
4
+ data.tar.gz: 78db6ff1cf53e4dc3d836ad7b40f466ed96c34d5fd5b2f7bc10ee811e119c3eb
5
5
  SHA512:
6
- metadata.gz: d99604f62c19be4dba917c8493f55dc148a40aab4fba697b4464bafb08e7fbc02f3ddc682e5809bc1e270e3785b8adb635c3067b192f3c78b2ee48d3a902fc9d
7
- data.tar.gz: 780cce37fb60f9a9f7f23bdd10dde4f6ffebe24c2cce0fea98cb8ceb04822d943ca9b1f026e6dae797d8be742b509285750dc1139e8bed92a51fcebe591277e5
6
+ metadata.gz: '0834e93de4a38950e2d2a958690fce1429e86a96d3ca3d74150c1822c0efa23e143b281e242d56ea26cd4d7250e77a0ad2bcd226f944d860746b9db8e5b322ca'
7
+ data.tar.gz: 65808f8f66a3aadbee453d51bc57a713159e9611772f20f28d9074dcad8fc6b34e436604942dc0aff46016461c4f7415458cfcf347723c7f6fa85048405e1988
@@ -1,3 +1,4 @@
1
+ # typed: strict
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require "greenhouse_api/version"
@@ -0,0 +1,116 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require 'faraday'
5
+ require 'base64'
6
+ require 'json'
7
+ require 'sorbet-runtime'
8
+
9
+ module GreenhouseApi
10
+ class Response
11
+ attr_reader :headers, :body, :status
12
+
13
+ def initialize(headers:, body:, status:)
14
+ @headers = headers
15
+ @body = body
16
+ @status = status
17
+ end
18
+ end
19
+
20
+ class BaseClient
21
+ extend T::Sig
22
+
23
+ MAX_PER_PAGE = 500
24
+ API_URL = 'https://harvest.greenhouse.io'
25
+
26
+ def initialize(api_key)
27
+ @api_key = api_key
28
+ end
29
+
30
+ def headers
31
+ credential = Base64.strict_encode64(@api_key + ':')
32
+
33
+ {
34
+ 'Authorization' => 'Basic ' + credential,
35
+ }
36
+ end
37
+
38
+ def request(http_method:, headers:, endpoint:, params: {}, body: {}, api_version: 'v1')
39
+ response = Faraday.public_send(http_method) do |request|
40
+ request.headers = headers
41
+ request.path = "#{API_URL}/#{api_version}/#{endpoint}"
42
+ request.params = params
43
+ request.body = body
44
+ end
45
+
46
+ Response.new(
47
+ body: response.body && !response.body.empty? ? JSON.parse(response.body) : '',
48
+ headers: response.headers,
49
+ status: response.status
50
+ )
51
+ end
52
+
53
+ def compose_response(response)
54
+ if [200, 201].include?(response&.status)
55
+ Response.new(
56
+ body: response.body,
57
+ headers: response.headers,
58
+ status: response.status
59
+ )
60
+ else
61
+ response
62
+ end
63
+ end
64
+
65
+ def list_many(resource, params = {})
66
+ limit = params.delete(:limit)
67
+ page = params[:page] || 1
68
+ data = []
69
+ response = nil
70
+
71
+ loop do
72
+ per_page = if params[:per_page]
73
+ params[:per_page]
74
+ else
75
+ limit ? [limit - data.length, MAX_PER_PAGE].min : MAX_PER_PAGE
76
+ end
77
+
78
+ response = request(
79
+ http_method: :get,
80
+ headers: headers,
81
+ endpoint: resource,
82
+ params: params.merge(page: page, per_page: per_page)
83
+ )
84
+ break if response.status != 200
85
+
86
+ data.concat(response.body)
87
+
88
+ if last_page?(response) || data_limit_reached?(data, limit) || params[:page]
89
+ break
90
+ else
91
+ page += 1
92
+ end
93
+ end
94
+
95
+ if response.status == 200
96
+ Response.new(
97
+ body: data,
98
+ headers: response.headers,
99
+ status: response.status
100
+ )
101
+ else
102
+ response
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def last_page?(response)
109
+ !response.headers['link'].to_s.include?('rel="next"')
110
+ end
111
+
112
+ def data_limit_reached?(data, limit)
113
+ limit && data.length >= limit
114
+ end
115
+ end
116
+ end
@@ -1,101 +1,59 @@
1
+ # typed: false
1
2
  # frozen_string_literal: true
2
3
 
3
- require 'faraday'
4
- require 'base64'
5
- require 'json'
4
+ require_relative 'base_client'
5
+ require_relative 'resources/candidates'
6
+ require_relative 'resources/offers'
7
+ require_relative 'resources/users'
6
8
 
7
9
  module GreenhouseApi
8
- class Response
9
- attr_reader :headers, :body, :status
10
-
11
- def initialize(headers:, body:, status:)
12
- @headers = headers
13
- @body = body
14
- @status = status
15
- end
16
- end
17
-
18
10
  class Client
19
- MAX_PER_PAGE = 500
20
- API_URL = 'https://harvest.greenhouse.io/v1'
21
-
22
11
  def initialize(api_key)
23
12
  @api_key = api_key
24
13
  end
25
14
 
15
+ def list_many(resource, params = {})
16
+ base_client.list_many(resource, params)
17
+ end
18
+
26
19
  def list_candidates(params = {})
27
- list_many('candidates', params)
20
+ candidates_client.list_all(params)
28
21
  end
29
22
 
30
- def list_many(resource, params = {})
31
- limit = params.delete(:limit)
32
- page = 1
33
- data = []
34
- response = nil
35
-
36
- loop do
37
- per_page = limit ? [limit - data.length, MAX_PER_PAGE].min : MAX_PER_PAGE
38
-
39
- response = request(
40
- http_method: :get,
41
- headers: headers,
42
- endpoint: resource,
43
- params: params.merge(page: page, per_page: per_page)
44
- )
45
- break if response.status != 200
23
+ def get_current_offer_for_application(application_id)
24
+ offers_client.get_current_offer_for_application(application_id)
25
+ end
46
26
 
47
- data.concat(response.body)
27
+ def create_user(first_name:, last_name:, email:, on_behalf_of_id:, **additional_args)
28
+ users_client.create_user(first_name: first_name, last_name: last_name, email: email, on_behalf_of_id: on_behalf_of_id, **additional_args)
29
+ end
48
30
 
49
- if last_page?(response) || data_limit_reached?(data, limit)
50
- break
51
- else
52
- page += 1
53
- end
54
- end
31
+ def disable_user(user, on_behalf_of_id)
32
+ users_client.disable_user(user, on_behalf_of_id)
33
+ end
55
34
 
56
- if response.status == 200
57
- Response.new(
58
- body: data,
59
- headers: response.headers,
60
- status: response.status
61
- )
62
- else
63
- response
64
- end
35
+ def enable_user(user, on_behalf_of_id)
36
+ users_client.enable_user(user, on_behalf_of_id)
65
37
  end
66
38
 
67
39
  private
68
40
 
69
- def last_page?(response)
70
- !response.headers['link'].to_s.include?('rel="next"')
71
- end
41
+ attr_accessor :api_key
72
42
 
73
- def data_limit_reached?(data, limit)
74
- limit && data.length >= limit
43
+ def candidates_client
44
+ @candidates_client ||= Resources::Candidates.new(api_key)
75
45
  end
76
46
 
77
- def headers
78
- credential = Base64.strict_encode64(@api_key + ':')
79
-
80
- {
81
- 'Authorization' => 'Basic ' + credential,
82
- }
47
+ def offers_client
48
+ @offers_client ||= Resources::Offers.new(api_key)
83
49
  end
84
50
 
85
- def request(http_method:, headers:, endpoint:, params:, body: {})
86
- response = Faraday.public_send(http_method) do |request|
87
- request.headers = headers
88
- request.path = "#{API_URL}/#{endpoint}"
89
- request.params = params
90
- request.body = body
91
- end
51
+ def users_client
52
+ @users_client ||= Resources::Users.new(api_key)
53
+ end
92
54
 
93
- Response.new(
94
- body: response.body && !response.body.empty? ? JSON.parse(response.body) : '',
95
- headers: response.headers,
96
- status: response.status
97
- )
55
+ def base_client
56
+ @base_client ||= BaseClient.new(api_key)
98
57
  end
99
58
  end
100
59
  end
101
-
@@ -0,0 +1,17 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require 'sorbet-runtime'
5
+
6
+ module GreenhouseApi
7
+ module Resources
8
+ class Candidates < BaseClient
9
+ extend T::Sig
10
+
11
+ sig { params(params: T.nilable(T::Hash[String, T.any(Integer, String)])).returns(Response) }
12
+ def list_all(params = {})
13
+ list_many('candidates', params)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require 'sorbet-runtime'
5
+
6
+ module GreenhouseApi
7
+ module Resources
8
+ class Offers < BaseClient
9
+ extend T::Sig
10
+
11
+ sig { params(application_id: String).returns(Response) }
12
+ def get_current_offer_for_application(application_id)
13
+ response = request(
14
+ http_method: :get,
15
+ headers: headers,
16
+ endpoint: "applications/#{application_id}/offers/current_offer",
17
+ )
18
+ compose_response(response)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require 'sorbet-runtime'
5
+
6
+ module GreenhouseApi
7
+ module Resources
8
+ class Users < BaseClient
9
+ extend T::Sig
10
+
11
+ ON_BEHALF_OF = 'On-Behalf-Of'
12
+
13
+ sig { params(first_name: String, last_name: String, email: String, on_behalf_of_id: String, additional_args: T.any(T::Boolean, String)).returns(Response) }
14
+ def create_user(first_name:, last_name:, email:, on_behalf_of_id:, **additional_args)
15
+ body = { first_name: first_name, last_name: last_name, email: email }.merge(additional_args).to_json
16
+ response = request(
17
+ http_method: :post,
18
+ headers: headers.merge(ON_BEHALF_OF => on_behalf_of_id),
19
+ endpoint: "users",
20
+ body: body
21
+ )
22
+ compose_response(response)
23
+ end
24
+
25
+ sig { params(user: T::Hash[String, T.any(Integer, String)], on_behalf_of_id: String).returns(Response) }
26
+ def disable_user(user, on_behalf_of_id)
27
+ body = { user: user }.to_json
28
+ response = request(
29
+ http_method: :patch,
30
+ headers: headers.merge(ON_BEHALF_OF => on_behalf_of_id),
31
+ endpoint: "users/disable",
32
+ body: body,
33
+ api_version: 'v2'
34
+ )
35
+ compose_response(response)
36
+ end
37
+
38
+ sig { params(user: T::Hash[String, T.any(Integer, String)], on_behalf_of_id: String).returns(Response) }
39
+ def enable_user(user, on_behalf_of_id)
40
+ body = { user: user }.to_json
41
+ response = request(
42
+ http_method: :patch,
43
+ headers: headers.merge(ON_BEHALF_OF => on_behalf_of_id),
44
+ endpoint: "users/enable",
45
+ body: body,
46
+ api_version: 'v2'
47
+ )
48
+ compose_response(response)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,3 +1,4 @@
1
+ # typed: strict
1
2
  module GreenhouseApi
2
- VERSION = "0.1.11"
3
+ VERSION = "0.3.1"
3
4
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: greenhouse_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florence Lau
8
8
  - Tanner Johnson
9
9
  - Quan Nguyen
10
- autorequire:
10
+ autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2020-12-02 00:00:00.000000000 Z
13
+ date: 2021-03-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -26,6 +26,20 @@ dependencies:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: 0.15.4
29
+ - !ruby/object:Gem::Dependency
30
+ name: sorbet-runtime
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0.5'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0.5'
29
43
  - !ruby/object:Gem::Dependency
30
44
  name: rspec
31
45
  requirement: !ruby/object:Gem::Requirement
@@ -40,6 +54,20 @@ dependencies:
40
54
  - - "~>"
41
55
  - !ruby/object:Gem::Version
42
56
  version: '3.10'
57
+ - !ruby/object:Gem::Dependency
58
+ name: sorbet
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0.5'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0.5'
43
71
  - !ruby/object:Gem::Dependency
44
72
  name: vcr
45
73
  requirement: !ruby/object:Gem::Requirement
@@ -76,13 +104,17 @@ extensions: []
76
104
  extra_rdoc_files: []
77
105
  files:
78
106
  - lib/greenhouse_api.rb
107
+ - lib/greenhouse_api/base_client.rb
79
108
  - lib/greenhouse_api/client.rb
109
+ - lib/greenhouse_api/resources/candidates.rb
110
+ - lib/greenhouse_api/resources/offers.rb
111
+ - lib/greenhouse_api/resources/users.rb
80
112
  - lib/greenhouse_api/version.rb
81
113
  homepage: https://github.com/gusto/greenhouse_api
82
114
  licenses: []
83
115
  metadata:
84
116
  homepage_uri: https://github.com/gusto/greenhouse_api
85
- post_install_message:
117
+ post_install_message:
86
118
  rdoc_options: []
87
119
  require_paths:
88
120
  - lib
@@ -97,8 +129,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
129
  - !ruby/object:Gem::Version
98
130
  version: '0'
99
131
  requirements: []
100
- rubygems_version: 3.1.4
101
- signing_key:
132
+ rubygems_version: 3.0.3
133
+ signing_key:
102
134
  specification_version: 4
103
135
  summary: API client for working with Greenhouse Harvest API
104
136
  test_files: []