checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
---
2
2
SHA256:
3
- metadata.gz: 16e2d50fd1711c97719e64495e342530a9452b93adae1c5839b5dc63b5d0d10a
4
- data.tar.gz: 9f7d5cdd2dc0947458d9cb1aea5fc13192d971cc73e148d18256d1a278c91c8d
3
+ metadata.gz: 1413d4e46506230bb025b09141ed382ff241a020711771023fdde4c3a5ecf426
4
+ data.tar.gz: 230997aef7c02db7fde6940fe047d59ba63063a4b71a07369a95ac28d935d925
5
5
SHA512:
6
- metadata.gz: 763fa51ab38bb4980762ddaf2acb82fb09c6671888e160b877f2a7e01f8dde7055c705af0160d7759991bcad48e5ac94dd570b850daa66bf5eb4d07beed0806c
7
- data.tar.gz: 5faf3811d054334db971bbd8c4620eb04939b83e7ea00b678c339589ae2e951397aa7402e993cced792bebf6dcbfafc44181f4a3d7a25f763b1d94cd7f2efda0
6
+ metadata.gz: ebf9ed99dc0a9d83de31b3a093f02afef815f4fc699662b320338a9e3d3ffff3dfd41aa5a6cc69460d5deeaad6d4cde88a45a323f91994ea7c7a5f0390d4b0be
7
+ data.tar.gz: 9e3a7a7773a0fca1986bdac76b1a55a38fe63ff12e60825c755cb416ecb98117b6d1025341f14f48b7b22c46f408442d29f7c628ceb2801e64f0fc3e0732e6b7
data/.github/CODEOWNERS ADDED
@@ -0,0 +1 @@
1
+ * @RadekMolenda @cfabianski
data/.rubocop.yml CHANGED
@@ -18,7 +18,10 @@ Metrics/LineLength:
18
18
Exclude:
19
19
- 'config/**/*'
20
20
- 'spec/**/*'
21
- Max: 120
21
+ Max: 160
22
+
23
+ Metrics/ClassLength:
24
+ Max: 200
22
25
23
26
Metrics/BlockLength:
24
27
Exclude:
@@ -38,3 +41,6 @@ Layout/AlignParameters:
38
41
39
42
Bundler/OrderedGems:
40
43
Enabled: false
44
+
45
+ Metrics/ParameterLists:
46
+ Enabled: false
data/Gemfile CHANGED
@@ -4,6 +4,5 @@ source "https://rubygems.org"
4
4
5
5
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
7
- gem "webmock"
8
7
# Specify your gem's dependencies in bearer.gemspec
9
8
gemspec
data/Gemfile.lock CHANGED
@@ -1,52 +1,61 @@
1
1
PATH
2
2
remote: .
3
3
specs:
4
- bearer (0.1.0)
4
+ bearer (2.0.0)
5
5
6
6
GEM
7
7
remote: https://rubygems.org/
8
8
specs:
9
- addressable (2.6.0)
10
- public_suffix (>= 2.0.2, < 4.0)
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
11
ast (2.4.0)
12
- childprocess (0.9.0)
13
- ffi (~> 1.0, >= 1.0.11)
12
+ backport (1.1.2)
13
+ byebug (11.0.1)
14
+ childprocess (3.0.0)
14
15
coderay (1.1.2)
15
16
crack (0.4.3)
16
17
safe_yaml (~> 1.0.0)
17
18
diff-lcs (1.3)
18
- ffi (1.10.0)
19
- hashdiff (0.3.8)
19
+ hashdiff (1.0.0)
20
+ htmlentities (4.3.4)
20
21
iniparse (1.4.4)
21
- jaro_winkler (1.5.2)
22
+ jaro_winkler (1.5.3)
22
23
method_source (0.9.2)
23
- overcommit (0.46.0)
24
- childprocess (~> 0.6, >= 0.6.3)
24
+ mini_portile2 (2.4.0)
25
+ nokogiri (1.10.4)
26
+ mini_portile2 (~> 2.4.0)
27
+ overcommit (0.50.0)
28
+ childprocess (>= 0.6.3, < 4)
25
29
iniparse (~> 1.4)
26
- parallel (1.14.0)
27
- parser (2.6.0.0)
30
+ parallel (1.17.0)
31
+ parser (2.6.4.1)
28
32
ast (~> 2.4.0)
29
33
powerpack (0.1.2)
30
34
pry (0.12.2)
31
35
coderay (~> 1.1.0)
32
36
method_source (~> 0.9.0)
37
+ pry-byebug (3.7.0)
38
+ byebug (~> 11.0)
39
+ pry (~> 0.10)
33
40
psych (3.1.0)
34
- public_suffix (3.0.3)
41
+ public_suffix (4.0.1)
35
42
rainbow (3.0.0)
36
43
rake (10.5.0)
44
+ reverse_markdown (1.3.0)
45
+ nokogiri
37
46
rspec (3.8.0)
38
47
rspec-core (~> 3.8.0)
39
48
rspec-expectations (~> 3.8.0)
40
49
rspec-mocks (~> 3.8.0)
41
- rspec-core (3.8.0)
50
+ rspec-core (3.8.2)
42
51
rspec-support (~> 3.8.0)
43
- rspec-expectations (3.8.2)
52
+ rspec-expectations (3.8.4)
44
53
diff-lcs (>= 1.2.0, < 2.0)
45
54
rspec-support (~> 3.8.0)
46
- rspec-mocks (3.8.0)
55
+ rspec-mocks (3.8.1)
47
56
diff-lcs (>= 1.2.0, < 2.0)
48
57
rspec-support (~> 3.8.0)
49
- rspec-support (3.8.0)
58
+ rspec-support (3.8.2)
50
59
rubocop (0.65.0)
51
60
jaro_winkler (~> 1.5.1)
52
61
parallel (~> 1.10)
@@ -56,26 +65,43 @@ GEM
56
65
rainbow (>= 2.2.2, < 4.0)
57
66
ruby-progressbar (~> 1.7)
58
67
unicode-display_width (~> 1.4.0)
59
- ruby-progressbar (1.10.0)
68
+ ruby-progressbar (1.10.1)
60
69
safe_yaml (1.0.5)
70
+ solargraph (0.37.2)
71
+ backport (~> 1.1)
72
+ bundler (>= 1.17.2)
73
+ htmlentities (~> 4.3, >= 4.3.4)
74
+ jaro_winkler (~> 1.5)
75
+ nokogiri (~> 1.9, >= 1.9.1)
76
+ parser (~> 2.3)
77
+ reverse_markdown (~> 1.0, >= 1.0.5)
78
+ rubocop (~> 0.52)
79
+ thor (~> 0.19, >= 0.19.4)
80
+ tilt (~> 2.0)
81
+ yard (~> 0.9)
82
+ thor (0.20.3)
83
+ tilt (2.0.10)
61
84
unicode-display_width (1.4.1)
62
- webmock (3.5.1)
85
+ webmock (3.7.6)
63
86
addressable (>= 2.3.6)
64
87
crack (>= 0.3.2)
65
- hashdiff
88
+ hashdiff (>= 0.4.0, < 2.0.0)
89
+ yard (0.9.20)
66
90
67
91
PLATFORMS
68
92
ruby
69
93
70
94
DEPENDENCIES
71
95
bearer!
72
- bundler (~> 1.16)
96
+ bundler (~> 2.0)
73
97
overcommit
74
98
pry
99
+ pry-byebug
75
100
rake (~> 10.0)
76
101
rspec (~> 3.0)
77
102
rubocop (~> 0.65.0)
103
+ solargraph
78
104
webmock
79
105
80
106
BUNDLED WITH
81
- 1.17.2
107
+ 2.0.2
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
# Bearer
2
2
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bearer`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This gem is a Ruby client to universally call any API using [Bearer.sh](https://www.bearer.sh).
4
4
5
- TODO: Delete this and the text above, and describe your gem
5
+ _NB: If you are using Rails, also have a look at the [Rails](https://github.com/bearer/bearer-rails) gem_
6
6
7
7
## Installation
8
8
@@ -14,15 +14,82 @@ gem 'bearer'
14
14
15
15
And then execute:
16
16
17
- $ bundle
18
-
17
+ ```shell
18
+ $ bundle
19
+ ```
19
20
Or install it yourself as:
20
21
21
- $ gem install bearer
22
+ ```shell
23
+ $ gem install bearer
24
+ ```
22
25
23
26
## Usage
24
27
25
- TODO: Write usage instructions here
28
+ Grab your Bearer [Secret Key](https://app.bearer.sh/keys) and integration id from
29
+ the [Dashboard](https://app.bearer.sh) and then you can use the client as follows:
30
+
31
+ ### Calling any APIs
32
+
33
+ ```ruby
34
+ require "bearer"
35
+
36
+ bearer = Bearer.new("BEARER_SECRET_KEY") # find it on https://app.bearer.sh/keys
37
+ github = (
38
+ bearer
39
+ .integration("your integration id") # you'll find it on the Bearer dashboard https://app.bearer.sh
40
+ .auth("your auth id") # Create an auth id for your integration via the dashboard
41
+ )
42
+
43
+ puts JSON.parse(github.get("/repositories").body)
44
+ ```
45
+
46
+ We use `Net::HTTP` internally and we
47
+ return it's response from the request methods (`request`,
48
+ `get`, `head`, `post`, `put`, `patch`, `delete`).
49
+
50
+ More advanced examples:
51
+
52
+ ```ruby
53
+ # With query parameters
54
+ puts JSON.parse(github.get("/repositories", query: { since: 364 }).body)
55
+
56
+ # With body data
57
+ puts JSON.parse(github.post("/user/repos", body: { name: "Just setting up my Bearer.sh" }).body)
58
+ ```
59
+
60
+ ### Global configuration
61
+
62
+ You can configure the client globally with your [Secret Key](https://app.bearer.sh/keys):
63
+
64
+ ```ruby
65
+ Bearer::Configuration.setup do |config|
66
+ config.secret_key = "BEARER_SECRET_KEY" # copy and paste your Bearer `Secret Key`
67
+ end
68
+ ```
69
+
70
+ You can now use the client without having to pass the Secret Key each time:
71
+
72
+ ```ruby
73
+ github = Bearer.integration("your integration id").auth("your auth id")
74
+
75
+ puts JSON.parse(github.get("/repositories").body)
76
+ ```
77
+ ### Setting the request timeout
78
+
79
+ By default in bearer client read and open timeouts are set to 5 seconds. Bearer allows to increase the read timeout to up to 30 seconds
80
+
81
+ ```ruby
82
+ Bearer::Configuration.setup do |config|
83
+ # increase the request timeout to 10 seconds, and reduce the open connection timeout to 1 second
84
+ config.http_client_settings = { read_timeout: 10, open_timeout: 1 }
85
+ end
86
+
87
+ # it is also possible to set the read_timeout and other Net::HTTP client settings per integration
88
+
89
+ github = Bearer.integration("your integration id", { read_timeout: 10 })
90
+
91
+ puts JSON.parse(github.get("/repositories").body) # This request will timeout after 10 seconds
92
+ ```
26
93
27
94
## Development
28
95
@@ -32,8 +99,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
99
33
100
## Contributing
34
101
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bearer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bearer/bearer-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
103
37
104
## Code of Conduct
38
105
39
- Everyone interacting in the Bearer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/bearer/blob/master/CODE_OF_CONDUCT.md).
106
+ Everyone interacting in the Bearer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bearer/bearer-ruby/blob/master/CODE_OF_CONDUCT.md).
data/bearer.gemspec CHANGED
@@ -6,10 +6,10 @@ Gem::Specification.new do |spec|
6
6
spec.name = "bearer"
7
7
spec.version = Bearer::VERSION
8
8
spec.authors = ["Bearer Team<engineering@bearer.sh>"]
9
- spec.email = ["antoine@bearer.sh"]
9
+ spec.email = ["engineering@bearer.sh"]
10
10
11
11
spec.summary = %q{Bearer Ruby}
12
- spec.description = %q{Bearer Ruby description}
12
+ spec.description = %q{Ruby client to universally call any API using Bearer.sh}
13
13
spec.homepage = "https://www.bearer.sh"
14
14
spec.licenses = ['MIT']
15
15
if spec.respond_to?(:metadata)
@@ -26,10 +26,13 @@ Gem::Specification.new do |spec|
26
26
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
spec.require_paths = ["lib"]
28
28
29
- spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "bundler", "~> 2.0"
30
30
spec.add_development_dependency "rake", "~> 10.0"
31
31
spec.add_development_dependency "rspec", "~> 3.0"
32
32
spec.add_development_dependency "rubocop", "~> 0.65.0"
33
33
spec.add_development_dependency "pry"
34
+ spec.add_development_dependency "pry-byebug"
34
35
spec.add_development_dependency "overcommit"
36
+ spec.add_development_dependency "webmock"
37
+ spec.add_development_dependency "solargraph"
35
38
end
data/lib/bearer.rb CHANGED
@@ -1,27 +1,36 @@
1
1
# frozen_string_literal: true
2
2
3
- require_relative "./bearer/version"
4
3
require_relative "./bearer/configuration"
5
- require "net/http"
4
+ require_relative "./bearer/integration"
6
- require "json"
7
5
8
- module Bearer
9
- INT_URL = "https://int.bearer.sh/api/v3/functions/backend/"
10
-
11
- def self.call(integration_uuid, integration_name, params: {}, body: {})
12
- uri = URI("#{INT_URL}#{integration_uuid}/#{integration_name}")
13
- uri.query = params.map { |k, v| "#{k}=#{v}" }.join("&")
6
+ # Ruby bindings for Bearer
7
+ class Bearer
8
+ # Create an instance of the Bearer client
9
+ # @param secret_key [String] developer secret Key from https://app.bearer.sh/settings.
10
+ # @param host [String] used internally
11
+ def initialize(secret_key = Bearer::Configuration.secret_key, host: Bearer::Configuration.host)
12
+ @secret_key = secret_key
13
+ @host = host
14
+ end
14
15
15
- req = Net::HTTP::Post.new(
16
- uri,
17
- "Content-Type" => "application/json",
18
- "Authorization" => Bearer::Configuration.api_key,
19
- "User-Agent" => "Bearer (#{Bearer::VERSION})"
16
+ # Return an integration client
17
+ #
18
+ # @param http_client_settings [Hash<String,String>] sent as keyword arguments to Net::HTTP.start method
19
+ # @param integration_id [String] bearer api id
20
+ # @return [Bearer::Integration]
21
+ def integration(integration_id, http_client_settings: {})
22
+ Integration.new(
23
+ integration_id: integration_id,
24
+ host: @host,
25
+ secret_key: @secret_key,
26
+ http_client_settings: http_client_settings
20
27
)
21
- req.body = body.to_json
22
- res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
23
- http.request(req)
24
- end
25
- JSON.parse(res.body)
28
+ end
29
+
30
+ # @see {Bearer#integration}
31
+ # @param (see #integration)
32
+ # @return [Bearer::Integration]
33
+ def self.integration(integration_id, http_client_settings: {})
34
+ new.integration(integration_id, http_client_settings: http_client_settings)
26
35
end
27
36
end
data/lib/bearer/configuration.rb CHANGED
@@ -4,26 +4,137 @@ require "singleton"
4
4
5
5
require_relative "./errors"
6
6
7
- module Bearer
7
+ class Bearer
8
+ # stores global Bearer configuration options
9
+ # @see https://app.bearer.sh/settings
10
+ # @attr_writer [String] secret_key secret key from https://app.bearer.sh/settings
11
+ # @attr_writer [String] publishable_key publishable key from https://app.bearer.sh/settings
12
+ # @attr_writer [String] encryption_key encryption key from https://app.bearer.sh/settings
13
+ # @attr_writer [Hash] http_client_settings options passed as a parameters to Net::HTTP#start
14
+ # @attr_writer [String] host mainly used internally
8
15
class Configuration
9
16
include Singleton
10
17
11
- FIELDS = %i[api_key client_id secret].freeze
18
+ PRODUCTION_INTEGRATION_HOST = "https://proxy.bearer.sh"
19
+
20
+ FIELDS = %i[
21
+ secret_key
22
+ publishable_key
23
+ encryption_key
24
+ host
25
+ http_client_settings
26
+ ].freeze
27
+
28
+ DEPRECATED_FIELDS = %i[
29
+ api_key
30
+ client_id
31
+ secret
32
+ integration_host
33
+ http_client_params
34
+ ].freeze
35
+
36
+ DEFAULT_READ_TIMEOUT = 5
37
+ DEFAULT_OPEN_TIMEOUT = 5
38
+
39
+ # @return [String]
40
+ def integration_host
41
+ deprecate("integration_host", "host")
42
+ host
43
+ end
44
+
45
+ # @return [String]
46
+ def host
47
+ @host ||= PRODUCTION_INTEGRATION_HOST
48
+ end
49
+
50
+ # @return [Hash]
51
+ def http_client_settings
52
+ default_http_client_settings.merge(@http_client_settings || {})
53
+ end
54
+
55
+ # @deprecated use {#http_client_settings} instead
56
+ # @return [Hash<String,String>]
57
+ def http_client_params
58
+ deprecate("http_client_params", "http_client_settings")
59
+ http_client_settings
60
+ end
61
+
62
+ # @return [String]
63
+ def secret_key
64
+ raise_if_missing(:secret_key)
65
+ end
66
+
67
+ # @return [String]
68
+ def publishable_key
69
+ raise_if_missing(:publishable_key)
70
+ end
71
+
72
+ # @return [String]
73
+ def encryption_key
74
+ raise_if_missing(:encryption_key)
75
+ end
76
+
77
+ # @deprecated Use {#secret_key} instead.
78
+ # @return [String]
79
+ def api_key
80
+ deprecate("api_key", "secret_key")
81
+ secret_key
82
+ end
83
+
84
+ # @deprecated Use {#publishable_key} instead.
85
+ # @return [String]
86
+ def client_id
87
+ deprecate("client_id", "publishable_key")
88
+ publishable_key
89
+ end
90
+
91
+ # @deprecated Use {#encryption_key} instead.
92
+ # @return [String]
93
+ def secret
94
+ deprecate("secret", "encryption_key")
95
+ encryption_key
96
+ end
12
97
13
98
attr_writer(*FIELDS)
14
99
15
- FIELDS.each do |field|
16
- define_method field do
17
- value = instance_variable_get(:"@#{field}")
100
+ # @deprecated Use {#secret_key=} instead.
101
+ # @return [void]
102
+ def api_key=(value)
103
+ deprecate("api_key=", "secret_key=")
104
+ @secret_key = value
105
+ end
106
+
107
+ # @deprecated Use {#publishable_key=} instead.
108
+ # @return [void]
109
+ def client_id=(value)
110
+ deprecate("client_id=", "publishable_key=")
111
+ @publishable_key = value
112
+ end
18
113
19
- raise ::Bearer::Errors::Configuration, "Bearer #{field} is missing!" unless value
114
+ # @deprecated Use {#encryption_key=} instead.
115
+ # @return [void]
116
+ def secret=(value)
117
+ deprecate("secret=", "encryption_key=")
118
+ @encryption_key = value
119
+ end
20
120
21
- value
22
- end
121
+ # @deprecated Use {#host=} instead.
122
+ # @return [void]
123
+ def integration_host=(value)
124
+ deprecate("integration_host=", "host=")
125
+ @host = value
126
+ end
127
+
128
+ # @deprecated Use {#http_client_settings=} instead.
129
+ # @return [void]
130
+ def http_client_params=(value)
131
+ deprecate("http_client_params=", "http_client_settings=")
132
+ @http_client_settings = value
23
133
end
24
134
25
135
class << self
26
- EXISTING_METHODS = FIELDS.flat_map { |field| [field, "#{field}=".to_sym] }
136
+ ALL_METHODS = [*FIELDS, *DEPRECATED_FIELDS].freeze
137
+ EXISTING_METHODS = ALL_METHODS.flat_map { |field| [field, "#{field}=".to_sym] }
27
138
28
139
def method_missing(name, *args, &block)
29
140
super unless EXISTING_METHODS.include? name
@@ -33,16 +144,50 @@ module Bearer
33
144
def respond_to_missing?(name, include_private = false)
34
145
EXISTING_METHODS.include?(name) || super
35
146
end
36
- end
37
147
38
- def self.reset
39
- FIELDS.each do |field|
40
- instance.public_send("#{field}=", nil)
148
+ def reset
149
+ FIELDS.each do |field|
150
+ instance.public_send("#{field}=", nil)
151
+ end
152
+ end
153
+
154
+ def setup
155
+ yield(instance)
41
156
end
42
157
end
43
158
44
- def self.setup
45
- yield(instance)
159
+ private
160
+
161
+ def raise_if_missing(field)
162
+ value = instance_variable_get(:"@#{field}")
163
+
164
+ raise ::Bearer::Errors::Configuration, "Bearer #{field} is missing!" unless value
165
+
166
+ value
167
+ end
168
+
169
+ def deprecate(old_field, new_field)
170
+ puts "Bearer Deprecation Warning: #{old_field} is deprecated, use #{new_field} instead"
171
+ end
172
+
173
+ # defaults to 5 seconds
174
+ # @return [Integer]
175
+ def open_timeout
176
+ @open_timeout || DEFAULT_READ_TIMEOUT
177
+ end
178
+
179
+ # defaults to 5 seconds
180
+ # @return [Integer]
181
+ def read_timeout
182
+ @read_timeout || DEFAULT_READ_TIMEOUT
183
+ end
184
+
185
+ # @return [Hash]
186
+ def default_http_client_settings
187
+ {
188
+ read_timeout: read_timeout,
189
+ open_timeout: open_timeout
190
+ }
46
191
end
47
192
end
48
193
end
data/lib/bearer/errors.rb CHANGED
@@ -1,6 +1,6 @@
1
1
# frozen_string_literal: true
2
2
3
- module Bearer
3
+ class Bearer
4
4
module Errors
5
5
class Configuration < StandardError; end
6
6
end
data/lib/bearer/integration.rb ADDED
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+
6
+ require_relative "./errors"
7
+ require_relative "./version"
8
+
9
+ class Bearer
10
+ class Integration
11
+ # @param integration_id [String] integration id
12
+ # @param host [String] "https://proxy.bearer.sh" | "https://proxy.staging.bearer.sh"
13
+ # @param http_client_settings [Hash<String,String>] http client settings see Net::HTTP#start
14
+ # @param auth_id [String] the auth id used to connect
15
+ # @param setup_id [String] the setup id used to store the credentials
16
+ # @param @deprecated read_timeout [String] use http_client_settings instead
17
+ def initialize(
18
+ integration_id:,
19
+ host:,
20
+ secret_key:,
21
+ http_client_settings: {},
22
+ read_timeout: nil,
23
+ auth_id: nil,
24
+ setup_id: nil
25
+ )
26
+ @integration_id = integration_id
27
+ @host = host
28
+ @secret_key = secret_key
29
+ @auth_id = auth_id
30
+ @read_timeout = read_timeout
31
+ @http_client_settings = http_client_settings
32
+ @setup_id = setup_id
33
+ end
34
+
35
+ # Returns a new integration client instance that will use the given auth id for requests
36
+ # @param auth_id [String] the auth id used to connect
37
+ # @return [Bearer::Integration]
38
+ def auth(auth_id)
39
+ self.class.new(
40
+ integration_id: @integration_id,
41
+ host: @host,
42
+ secret_key: @secret_key,
43
+ auth_id: auth_id
44
+ )
45
+ end
46
+
47
+ # Returns a new integration client instance that will use the given setup id for requests
48
+ # @param setup_id [String] uuid setup id used to store credentials
49
+ # @return [Bearer::Integration]
50
+ def setup(setup_id)
51
+ self.class.new(
52
+ integration_id: @integration_id,
53
+ host: @host,
54
+ secret_key: @secret_key,
55
+ setup_id: setup_id,
56
+ auth_id: @auth_id
57
+ )
58
+ end
59
+
60
+ # An alias for `#auth`
61
+ # @see {#auth}
62
+ def authenticate(auth_id)
63
+ auth(auth_id)
64
+ end
65
+
66
+ # Makes a HEAD request to the API configured for this integration and returns the response
67
+ # @param (see #request)
68
+ def get(endpoint, headers: nil, body: nil, query: nil)
69
+ request("GET", endpoint, headers: headers, body: body, query: query)
70
+ end
71
+
72
+ # Makes a HEAD request to the API configured for this integration and returns the response
73
+ # @param (see #request)
74
+ def head(endpoint, headers: nil, body: nil, query: nil)
75
+ request("HEAD", endpoint, headers: headers, body: body, query: query)
76
+ end
77
+
78
+ # Makes a POST request to the API configured for this integration and returns the response
79
+ # @param (see #request)
80
+ def post(endpoint, headers: nil, body: nil, query: nil)
81
+ request("POST", endpoint, headers: headers, body: body, query: query)
82
+ end
83
+
84
+ # Makes a PUT request to the API configured for this integration and returns the response
85
+ # @param (see #request)
86
+ def put(endpoint, headers: nil, body: nil, query: nil)
87
+ request("PUT", endpoint, headers: headers, body: body, query: query)
88
+ end
89
+
90
+ # Makes a GET request to the API configured for this integration and returns the response
91
+ # @param (see #request)
92
+ def patch(endpoint, headers: nil, body: nil, query: nil)
93
+ request("PATCH", endpoint, headers: headers, body: body, query: query)
94
+ end
95
+
96
+ # Makes a DELETE request to the API configured for this integration and returns the response
97
+ # @param (see #request)
98
+ def delete(endpoint, headers: nil, body: nil, query: nil)
99
+ request("DELETE", endpoint, headers: headers, body: body, query: query)
100
+ end
101
+
102
+ # Makes a request to the API configured for this integration and returns the response
103
+ # @param method [String] GET/HEAD/POST/PUT/PATCH/DELETE
104
+ # @param endpoint [String] the URL relative to the configured API's base URL
105
+ # @param headers [Hash<String, String>] any headers to send to the API
106
+ # @param body [Hash] any request body data to send
107
+ # @param query [Hash<String, String>] parameters to add to the URL's query string
108
+ def request(method, endpoint, headers: nil, body: nil, query: nil)
109
+ pre_headers = {
110
+ "Authorization": @secret_key,
111
+ "User-Agent": "Bearer-Ruby (#{Bearer::VERSION})",
112
+ "Bearer-Auth-Id": @auth_id,
113
+ "Bearer-Setup-Id": @setup_id,
114
+ "Content-Type": "application/json"
115
+ }
116
+
117
+ request_headers = pre_headers.merge(headers || {}).reject { |_k, v| v.nil? }
118
+
119
+ endpoint = endpoint.sub(%r{\A/}, "")
120
+ url = "#{@host}/#{@integration_id}/#{endpoint}"
121
+
122
+ make_request(method: method, url: url, query: query, body: body, headers: request_headers)
123
+ end
124
+
125
+ private
126
+
127
+ # @return [Hash]
128
+ def http_client_settings
129
+ Bearer::Configuration.http_client_settings.merge(@http_client_settings)
130
+ end
131
+
132
+ def make_request(method:, url:, query:, body:, headers:)
133
+ parsed_url = URI(url)
134
+ parsed_url.query = URI.encode_www_form(query) if query
135
+
136
+ Net::HTTP.start(
137
+ parsed_url.hostname,
138
+ parsed_url.port,
139
+ use_ssl: parsed_url.scheme == "https",
140
+ **http_client_settings
141
+ ) do |http|
142
+ http.send_request(method, parsed_url, body ? body.to_json : nil, headers)
143
+ end
144
+ end
145
+ end
146
+ end
data/lib/bearer/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
# frozen_string_literal: true
2
2
3
- module Bearer
4
- VERSION = "0.1.1"
3
+ class Bearer
4
+ VERSION = "2.0.0"
5
5
end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
--- !ruby/object:Gem::Specification
2
2
name: bearer
3
3
version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 2.0.0
5
5
platform: ruby
6
6
authors:
7
7
- Bearer Team<engineering@bearer.sh>
8
8
autorequire:
9
9
bindir: exe
10
10
cert_chain: []
11
- date: 2019-03-20 00:00:00.000000000 Z
11
+ date: 2019-10-09 00:00:00.000000000 Z
12
12
dependencies:
13
13
- !ruby/object:Gem::Dependency
14
14
name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
requirements:
17
17
- - "~>"
18
18
- !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: '2.0'
20
20
type: :development
21
21
prerelease: false
22
22
version_requirements: !ruby/object:Gem::Requirement
23
23
requirements:
24
24
- - "~>"
25
25
- !ruby/object:Gem::Version
26
- version: '1.16'
26
+ version: '2.0'
27
27
- !ruby/object:Gem::Dependency
28
28
name: rake
29
29
requirement: !ruby/object:Gem::Requirement
@@ -81,6 +81,20 @@ dependencies:
81
81
- !ruby/object:Gem::Version
82
82
version: '0'
83
83
- !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
84
98
name: overcommit
85
99
requirement: !ruby/object:Gem::Requirement
86
100
requirements:
@@ -94,13 +108,42 @@ dependencies:
94
108
- - ">="
95
109
- !ruby/object:Gem::Version
96
110
version: '0'
97
- description: Bearer Ruby description
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: solargraph
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Ruby client to universally call any API using Bearer.sh
98
140
email:
99
- - antoine@bearer.sh
141
+ - engineering@bearer.sh
100
142
executables: []
101
143
extensions: []
102
144
extra_rdoc_files: []
103
145
files:
146
+ - ".github/CODEOWNERS"
104
147
- ".github/PULL_REQUEST_TEMPLATE.md"
105
148
- ".github/main.workflow"
106
149
- ".gitignore"
@@ -123,6 +166,7 @@ files:
123
166
- lib/bearer.rb
124
167
- lib/bearer/configuration.rb
125
168
- lib/bearer/errors.rb
169
+ - lib/bearer/integration.rb
126
170
- lib/bearer/version.rb
127
171
homepage: https://www.bearer.sh
128
172
licenses:
@@ -144,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
188
- !ruby/object:Gem::Version
145
189
version: '0'
146
190
requirements: []
147
- rubygems_version: 3.0.3
191
+ rubygems_version: 3.0.1
148
192
signing_key:
149
193
specification_version: 4
150
194
summary: Bearer Ruby