greenhouse_api 0.1.12 → 0.3.2

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: 830ea391f1b8d83fb74fa520488aa0e8992b6be8766db0279ab2ed12ecb8d186
4
- data.tar.gz: 7d858fc9cbcf1ee073f1e7dbe1c57f9fe9599b539f3cac259054c550ddd57a35
3
+ metadata.gz: dcac2674a597136d59e32eea777b9b7ea622bcbccde40f051ad202eb51bc1171
4
+ data.tar.gz: 579ba5e1a02c968a23835547235281fa1c45e5ccc1ee2ae072fa8d8653d60138
5
5
  SHA512:
6
- metadata.gz: 490a9d4d9bdb550a1151d4b4e704f698868c93d3f2c2629eaa0ba22eca8f65e873d35cb3ac7ac83b7cf8ce3b83bef3f78c60fc9380982ebf421b48f40642acf3
7
- data.tar.gz: b47bd1b90c371069f5512ca9e60573b4bf322bda04b5c7c8d1228e9a792b69b97db99ee54324946f3d3be8de27281e9454f5867abd94b6f522b014a3a0602ce6
6
+ metadata.gz: 2b689c40434ff819aed78a8a79938f0cd264cd0de2fbefdc6be105325add4f313cf11195b594ad6ce45e50bd24658de502db29971f37795859bc1ea569695776
7
+ data.tar.gz: 1a7158a04d7826095ff9d5258b41b78ee49908b40ce65653a095c7e7e0999f6419b80adbc6450d82b47ddcdb90efac8be60c84ce7196701149d9c9a1a68a2aa8
@@ -1,3 +1,4 @@
1
+ # typed: strict
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require "greenhouse_api/version"
@@ -0,0 +1,125 @@
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 get_one(resource, id)
66
+ response = request(
67
+ http_method: :get,
68
+ headers: headers,
69
+ endpoint: "#{resource}/#{id}"
70
+ )
71
+ compose_response(response)
72
+ end
73
+
74
+ def list_many(resource, params = {})
75
+ limit = params.delete(:limit)
76
+ page = params[:page] || 1
77
+ data = []
78
+ response = nil
79
+
80
+ loop do
81
+ per_page = if params[:per_page]
82
+ params[:per_page]
83
+ else
84
+ limit ? [limit - data.length, MAX_PER_PAGE].min : MAX_PER_PAGE
85
+ end
86
+
87
+ response = request(
88
+ http_method: :get,
89
+ headers: headers,
90
+ endpoint: resource,
91
+ params: params.merge(page: page, per_page: per_page)
92
+ )
93
+ break if response.status != 200
94
+
95
+ data.concat(response.body)
96
+
97
+ if last_page?(response) || data_limit_reached?(data, limit) || params[:page]
98
+ break
99
+ else
100
+ page += 1
101
+ end
102
+ end
103
+
104
+ if response.status == 200
105
+ Response.new(
106
+ body: data,
107
+ headers: response.headers,
108
+ status: response.status
109
+ )
110
+ else
111
+ response
112
+ end
113
+ end
114
+
115
+ private
116
+
117
+ def last_page?(response)
118
+ !response.headers['link'].to_s.include?('rel="next"')
119
+ end
120
+
121
+ def data_limit_reached?(data, limit)
122
+ limit && data.length >= limit
123
+ end
124
+ end
125
+ end
@@ -1,120 +1,63 @@
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 get_one(resource, id)
16
+ base_client.get_one(resource, id)
17
+ end
18
+
19
+ def list_many(resource, params = {})
20
+ base_client.list_many(resource, params)
21
+ end
22
+
26
23
  def list_candidates(params = {})
27
- list_many('candidates', params)
24
+ candidates_client.list_all(params)
28
25
  end
29
26
 
30
27
  def get_current_offer_for_application(application_id)
31
- response = request(
32
- http_method: :get,
33
- headers: headers,
34
- endpoint: "applications/#{application_id}/offers/current_offer",
35
- params: {}
36
- )
37
-
38
- if response.status == 200
39
- Response.new(
40
- body: response.body,
41
- headers: response.headers,
42
- status: response.status
43
- )
44
- else
45
- response
46
- end
28
+ offers_client.get_current_offer_for_application(application_id)
47
29
  end
48
30
 
49
- def list_many(resource, params = {})
50
- limit = params.delete(:limit)
51
- page = 1
52
- data = []
53
- response = nil
54
-
55
- loop do
56
- per_page = limit ? [limit - data.length, MAX_PER_PAGE].min : MAX_PER_PAGE
57
-
58
- response = request(
59
- http_method: :get,
60
- headers: headers,
61
- endpoint: resource,
62
- params: params.merge(page: page, per_page: per_page)
63
- )
64
- break if response.status != 200
65
-
66
- data.concat(response.body)
31
+ def create_user(first_name:, last_name:, email:, on_behalf_of_id:, **additional_args)
32
+ users_client.create_user(first_name: first_name, last_name: last_name, email: email, on_behalf_of_id: on_behalf_of_id, **additional_args)
33
+ end
67
34
 
68
- if last_page?(response) || data_limit_reached?(data, limit)
69
- break
70
- else
71
- page += 1
72
- end
73
- end
35
+ def disable_user(user, on_behalf_of_id)
36
+ users_client.disable_user(user, on_behalf_of_id)
37
+ end
74
38
 
75
- if response.status == 200
76
- Response.new(
77
- body: data,
78
- headers: response.headers,
79
- status: response.status
80
- )
81
- else
82
- response
83
- end
39
+ def enable_user(user, on_behalf_of_id)
40
+ users_client.enable_user(user, on_behalf_of_id)
84
41
  end
85
42
 
86
43
  private
87
44
 
88
- def last_page?(response)
89
- !response.headers['link'].to_s.include?('rel="next"')
90
- end
45
+ attr_accessor :api_key
91
46
 
92
- def data_limit_reached?(data, limit)
93
- limit && data.length >= limit
47
+ def candidates_client
48
+ @candidates_client ||= Resources::Candidates.new(api_key)
94
49
  end
95
50
 
96
- def headers
97
- credential = Base64.strict_encode64(@api_key + ':')
98
-
99
- {
100
- 'Authorization' => 'Basic ' + credential,
101
- }
51
+ def offers_client
52
+ @offers_client ||= Resources::Offers.new(api_key)
102
53
  end
103
54
 
104
- def request(http_method:, headers:, endpoint:, params:, body: {})
105
- response = Faraday.public_send(http_method) do |request|
106
- request.headers = headers
107
- request.path = "#{API_URL}/#{endpoint}"
108
- request.params = params
109
- request.body = body
110
- end
55
+ def users_client
56
+ @users_client ||= Resources::Users.new(api_key)
57
+ end
111
58
 
112
- Response.new(
113
- body: response.body && !response.body.empty? ? JSON.parse(response.body) : '',
114
- headers: response.headers,
115
- status: response.status
116
- )
59
+ def base_client
60
+ @base_client ||= BaseClient.new(api_key)
117
61
  end
118
62
  end
119
63
  end
120
-
@@ -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.12"
3
+ VERSION = "0.3.2"
3
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: greenhouse_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florence Lau
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2021-02-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,7 +104,11 @@ 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: []