checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
---
2
- SHA1:
3
- metadata.gz: 59430de0df644577fe21a4c7f4d89c7cca728f4d
4
- data.tar.gz: 88520cb954578a548c762ca0282d7e85ebaa8ea0
2
+ SHA256:
3
+ metadata.gz: de9001a9e249f065997001450d453c225bf127ddd03faa337a9ec01054349299
4
+ data.tar.gz: 841fc4c60204d05fa101d904dacd5ade476d862258ce5d6c16206b67d8999456
5
5
SHA512:
6
- metadata.gz: a4b319355ba0c0026f993abc046e05515fb1938fcc15b7f836e2eb0e8788b4fabfc59c51e209cb9ea2e652f939560dfe7c89a58bb623f3431fe60c3699d2a30c
7
- data.tar.gz: 77c75045db0d26fe21fb4028d327244e0a4043c15d0c193a8892674ad2369de10485efdca2b115fd99951b1cc9344226d96e98c9cbd65a892dc356f5d511b562
6
+ metadata.gz: b9dff72f5b2a6436cefc6ca8706e7bf6e2956fb8a2d62f3f59444950f800549022f0a2ad7243a52570d1abe516c28d9500ff87ac90f3a2cb5274a9348bc2578e
7
+ data.tar.gz: 15da4523a462a3b16a67921a0f29ae78fb1812f7802260980c03a25d84f052dab45ab788cf19654e4cd6fd225fb12f7ed3bfcac9418a066d90cc3ab51e0ffaf3
data/.github/CODEOWNERS ADDED
@@ -0,0 +1 @@
1
+ * @RadekMolenda @cfabianski
data/.github/PULL_REQUEST_TEMPLATE.md ADDED
@@ -0,0 +1,8 @@
1
+ # What
2
+ Technically describe what you did, add a screenshot if needed.
3
+ # Why
4
+ What is the business or user need behind what you did. User can be yourself!
5
+
6
+ ### Checklist
7
+ - [ ] Rebased on master
8
+ - [ ] Squashed commits to have tidy commit history
data/.github/main.workflow ADDED
@@ -0,0 +1,23 @@
1
+ workflow "Build, Test, and Publish" {
2
+ on = "push"
3
+ resolves = ["Publish"]
4
+ }
5
+
6
+ action "Build" {
7
+ uses = "scarhand/actions-ruby@master"
8
+ args = "build *.gemspec"
9
+ }
10
+
11
+ # Filter for a new tag
12
+ action "Tag" {
13
+ needs = "Build"
14
+ uses = "actions/bin/filter@master"
15
+ args = "tag v*"
16
+ }
17
+
18
+ action "Publish" {
19
+ needs = "Tag"
20
+ uses = "scarhand/actions-ruby@master"
21
+ args = "push *.gem"
22
+ secrets = ["RUBYGEMS_AUTH_TOKEN"]
23
+ }
data/.jenkins/stack.yml ADDED
@@ -0,0 +1,12 @@
1
+ apiVersion: v1
2
+ kind: Pod
3
+ metadata:
4
+ labels:
5
+ name: bearer-ruby
6
+ spec:
7
+ containers:
8
+ - name: ruby261
9
+ image: ruby:2.6.1-alpine
10
+ command:
11
+ - cat
12
+ tty: true
data/.overcommit.yml ADDED
@@ -0,0 +1,38 @@
1
+ # Use this file to configure the Overcommit hooks you wish to use. This will
2
+ # extend the default configuration defined in:
3
+ # https://github.com/brigade/overcommit/blob/master/config/default.yml
4
+ #
5
+ # At the topmost level of this YAML file is a key representing type of hook
6
+ # being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
7
+ # customize each hook, such as whether to only run it on certain files (via
8
+ # `include`), whether to only display output if it fails (via `quiet`), etc.
9
+ #
10
+ # For a complete list of hooks, see:
11
+ # https://github.com/brigade/overcommit/tree/master/lib/overcommit/hook
12
+ #
13
+ # For a complete list of options that you can use to customize hooks, see:
14
+ # https://github.com/brigade/overcommit#configuration
15
+ #
16
+ # Uncomment the following lines to make the configuration take effect.
17
+
18
+ PreCommit:
19
+ RuboCop:
20
+ enabled: true
21
+ command: ['bundle', 'exec', 'rubocop']
22
+ on_warn: fail # Treat all warnings as failures
23
+
24
+ include:
25
+ - '**/*.gemspec'
26
+ - '**/*.rb'
27
+ - '**/Gemfile'
28
+ - template-dir/hooks/overcommit-hook
29
+
30
+ TrailingWhitespace:
31
+ enabled: true
32
+
33
+ YamlSyntax:
34
+ enabled: true
35
+
36
+ CommitMsg:
37
+ CapitalizedSubject:
38
+ enabled: false
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - '**/vendor/**/*'
5
+ - '**/vendor/**/.*'
6
+ - 'spec/**/*'
7
+ - 'bearer.gemspec'
8
+ - '.jenkins/**/*'
9
+ - 'bin/test'
10
+ - 'bin/console'
11
+ - 'bin/setup'
12
+
13
+ Style/StringLiterals:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Metrics/LineLength:
18
+ Exclude:
19
+ - 'config/**/*'
20
+ - 'spec/**/*'
21
+ Max: 160
22
+
23
+ Metrics/ClassLength:
24
+ Max: 200
25
+
26
+ Metrics/BlockLength:
27
+ Exclude:
28
+ - 'app/admin/**/*'
29
+
30
+ Metrics/MethodLength:
31
+ Max: 20
32
+
33
+ Style/Documentation:
34
+ Enabled: false
35
+
36
+ Layout/AlignHash:
37
+ Enabled: false
38
+
39
+ Layout/AlignParameters:
40
+ Enabled: false
41
+
42
+ Bundler/OrderedGems:
43
+ Enabled: false
44
+
45
+ Metrics/ParameterLists:
46
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.1
data/Gemfile CHANGED
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
source "https://rubygems.org"
2
4
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
6
7
+ gem "webmock"
5
8
# Specify your gem's dependencies in bearer.gemspec
6
9
gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,84 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bearer (1.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ ast (2.4.0)
12
+ byebug (11.0.1)
13
+ childprocess (3.0.0)
14
+ coderay (1.1.2)
15
+ crack (0.4.3)
16
+ safe_yaml (~> 1.0.0)
17
+ diff-lcs (1.3)
18
+ hashdiff (1.0.0)
19
+ iniparse (1.4.4)
20
+ jaro_winkler (1.5.3)
21
+ method_source (0.9.2)
22
+ overcommit (0.50.0)
23
+ childprocess (>= 0.6.3, < 4)
24
+ iniparse (~> 1.4)
25
+ parallel (1.17.0)
26
+ parser (2.6.4.1)
27
+ ast (~> 2.4.0)
28
+ powerpack (0.1.2)
29
+ pry (0.12.2)
30
+ coderay (~> 1.1.0)
31
+ method_source (~> 0.9.0)
32
+ pry-byebug (3.7.0)
33
+ byebug (~> 11.0)
34
+ pry (~> 0.10)
35
+ psych (3.1.0)
36
+ public_suffix (4.0.1)
37
+ rainbow (3.0.0)
38
+ rake (10.5.0)
39
+ rspec (3.8.0)
40
+ rspec-core (~> 3.8.0)
41
+ rspec-expectations (~> 3.8.0)
42
+ rspec-mocks (~> 3.8.0)
43
+ rspec-core (3.8.2)
44
+ rspec-support (~> 3.8.0)
45
+ rspec-expectations (3.8.4)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.8.0)
48
+ rspec-mocks (3.8.1)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.8.0)
51
+ rspec-support (3.8.2)
52
+ rubocop (0.65.0)
53
+ jaro_winkler (~> 1.5.1)
54
+ parallel (~> 1.10)
55
+ parser (>= 2.5, != 2.5.1.1)
56
+ powerpack (~> 0.1)
57
+ psych (>= 3.1.0)
58
+ rainbow (>= 2.2.2, < 4.0)
59
+ ruby-progressbar (~> 1.7)
60
+ unicode-display_width (~> 1.4.0)
61
+ ruby-progressbar (1.10.1)
62
+ safe_yaml (1.0.5)
63
+ unicode-display_width (1.4.1)
64
+ webmock (3.7.6)
65
+ addressable (>= 2.3.6)
66
+ crack (>= 0.3.2)
67
+ hashdiff (>= 0.4.0, < 2.0.0)
68
+
69
+ PLATFORMS
70
+ ruby
71
+
72
+ DEPENDENCIES
73
+ bearer!
74
+ bundler (~> 2.0)
75
+ overcommit
76
+ pry
77
+ pry-byebug
78
+ rake (~> 10.0)
79
+ rspec (~> 3.0)
80
+ rubocop (~> 0.65.0)
81
+ webmock
82
+
83
+ BUNDLED WITH
84
+ 2.0.2
data/Jenkinsfile ADDED
@@ -0,0 +1,39 @@
1
+ def label() {
2
+ def value = "bearer-ruby-${UUID.randomUUID().toString()}"
3
+ return value
4
+ }
5
+
6
+
7
+ pipeline {
8
+ agent {
9
+ kubernetes {
10
+ label label()
11
+ defaultContainer 'jnlp'
12
+ yamlFile '.jenkins/stack.yml'
13
+ }
14
+ } // and agent
15
+
16
+ stages {
17
+ stage("Rubocop") {
18
+ steps {
19
+ container("ruby261") {
20
+ ansiColor('xterm') {
21
+ sh "apk add --update git make build-base"
22
+ sh "bundle --jobs 20 --retry 5 --path=vendor"
23
+ sh "bundle exec rubocop -c .rubocop.yml"
24
+ }
25
+ }
26
+ }
27
+ }//end stage
28
+
29
+ stage("Tests") {
30
+ steps {
31
+ container("ruby261") {
32
+ ansiColor('xterm') {
33
+ sh "bin/test"
34
+ }
35
+ }
36
+ }
37
+ }//end stage
38
+ }// end stages
39
+ } // end pipeline
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,79 @@ 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
+ ### Calling custom functions
61
+
62
+ ```ruby
63
+ require "bearer"
64
+
65
+ bearer = Bearer.new("BEARER_SECRET_KEY")
66
+ github = bearer.integration("your integration id")
67
+
68
+ puts github.invoke("your function name")
69
+ ```
70
+
71
+ [Learn more](https://docs.bearer.sh/working-with-bearer/manipulating-apis) on how to use custom functions with Bearer.sh.
72
+
73
+ ### Global configuration
74
+
75
+ You can configure the client globally with your [Secret Key](https://app.bearer.sh/keys):
76
+
77
+ ```ruby
78
+ Bearer::Configuration.setup do |config|
79
+ config.secret_key = "BEARER_SECRET_KEY" # copy and paste your Bearer `Secret Key`
80
+ end
81
+ ```
82
+
83
+ You can now use the client without having to pass the Secret Key each time:
84
+
85
+ ```ruby
86
+ github = Bearer.integration("your integration id").auth("your auth id")
87
+
88
+ puts JSON.parse(github.get("/repositories").body)
89
+ ```
26
90
27
91
## Development
28
92
@@ -32,8 +96,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
96
33
97
## Contributing
34
98
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.
99
+ 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
100
37
101
## Code of Conduct
38
102
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).
103
+ 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/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
require "bundler/gem_tasks"
2
4
require "rspec/core/rake_task"
3
5
4
6
RSpec::Core::RakeTask.new(:spec)
5
7
6
- task :default => :spec
8
+ task default: :spec
data/bearer.gemspec CHANGED
@@ -1,20 +1,17 @@
1
-
2
1
lib = File.expand_path("../lib", __FILE__)
3
2
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
require "bearer/version"
5
4
6
5
Gem::Specification.new do |spec|
7
- spec.name = "bearer"
8
- spec.version = Bearer::VERSION
9
- spec.authors = ["Bearer Team<engineering@bearer.sh>"]
10
- spec.email = ["antoine@bearer.sh"]
6
+ spec.name = "bearer"
7
+ spec.version = Bearer::VERSION
8
+ spec.authors = ["Bearer Team<engineering@bearer.sh>"]
9
+ spec.email = ["engineering@bearer.sh"]
11
10
12
- spec.summary = %q{Bearer Ruby}
13
- spec.description = %q{Bearer Ruby description}
14
- spec.homepage = "https://www.bearer.sh"
15
- spec.licenses = ['MIT']
11
+ spec.summary = %q{Bearer Ruby}
12
+ spec.description = %q{Ruby client to universally call any API using Bearer.sh}
13
+ spec.homepage = "https://www.bearer.sh"
14
+ spec.licenses = ['MIT']
16
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
- # to allow pushing to a single host or delete this section to allow pushing to any host.
18
15
if spec.respond_to?(:metadata)
19
16
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
20
17
else
@@ -22,14 +19,18 @@ Gem::Specification.new do |spec|
22
19
"public gem pushes."
23
20
end
24
21
25
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
22
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
23
f.match(%r{^(test|spec|features)/})
27
24
end
28
- spec.bindir = "exe"
29
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
27
spec.require_paths = ["lib"]
31
28
32
- spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "bundler", "~> 2.0"
33
30
spec.add_development_dependency "rake", "~> 10.0"
34
31
spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "rubocop", "~> 0.65.0"
33
+ spec.add_development_dependency "pry"
34
+ spec.add_development_dependency "pry-byebug"
35
+ spec.add_development_dependency "overcommit"
35
36
end
data/bin/console CHANGED
@@ -1,14 +1,11 @@
1
1
#!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
3
4
require "bundler/setup"
4
- require "bearer"
5
+ require_relative "../lib/bearer"
5
6
6
7
# You can add fixtures and/or initialization code here to make experimenting
7
8
# with your gem easier. You can also use a different console, if you like.
8
9
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
10
+ require "pry"
11
+ Pry.start
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,8 +1,11 @@
1
1
#!/usr/bin/env bash
2
+
2
3
set -euo pipefail
4
+
3
5
IFS=#x27;\n\t'
4
6
set -vx
5
7
6
8
bundle install
7
9
8
10
# Do any other automated setup that you need to do here
11
+ overcommit --install
data/bin/test ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env sh
2
+
3
+ set -euo pipefail
4
+
5
+ IFS=#x27;\n\t'
6
+ set -vx
7
+
8
+ bundle exec rake spec
data/lib/bearer.rb CHANGED
@@ -1,5 +1,33 @@
1
- require "bearer/version"
1
+ # frozen_string_literal: true
2
2
3
- module Bearer
4
- # Your code goes here...
3
+ require_relative "./bearer/configuration"
4
+ require_relative "./bearer/integration"
5
+
6
+ class Bearer
7
+ # Public: Create an instance of the Bearer client
8
+ #
9
+ # api_key - developer API Key from the Dashboard. Defaults to the value from the `Bearer::Configuration` object
10
+ def initialize(secret_key = Bearer::Configuration.secret_key, integration_host: nil)
11
+ @secret_key = secret_key
12
+ @integration_host = integration_host || Bearer::Configuration.integration_host
13
+ end
14
+
15
+ # Public: Return an integration client
16
+ #
17
+ # integration_id - the integration's unique identifier from the Dashboard
18
+ def integration(integration_id)
19
+ Integration.new(integration_id: integration_id, integration_host: @integration_host, secret_key: @secret_key)
20
+ end
21
+
22
+ class << self
23
+ def call(integration_buid, integration_name, params: {}, body: {})
24
+ integration(integration_buid).invoke(integration_name, body: body, query: params)
25
+ end
26
+
27
+ alias invoke call
28
+
29
+ def integration(integration_id)
30
+ new.integration(integration_id)
31
+ end
32
+ end
5
33
end
data/lib/bearer/configuration.rb ADDED
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ require_relative "./errors"
6
+
7
+ class Bearer
8
+ class Configuration
9
+ include Singleton
10
+
11
+ PRODUCTION_INTEGRATION_HOST = "https://int.bearer.sh"
12
+
13
+ FIELDS = %i[
14
+ secret_key
15
+ publishable_key
16
+ encryption_key
17
+ integration_host
18
+ http_client_params
19
+ ].freeze
20
+
21
+ DEPRECATED_FIELDS = %i[
22
+ api_key
23
+ client_id
24
+ secret
25
+ ].freeze
26
+
27
+ DEFAULT_READ_TIMEOUT = 5
28
+ DEFAULT_OPEN_TIMEOUT = 5
29
+
30
+ # @return [Hash]
31
+ def integration_host
32
+ @integration_host ||= PRODUCTION_INTEGRATION_HOST
33
+ end
34
+
35
+ # @return [Hash]
36
+ def http_client_params
37
+ default_http_client_params.merge(@http_client_params || {})
38
+ end
39
+
40
+ # @return [String]
41
+ def secret_key
42
+ raise_if_missing(:secret_key)
43
+ end
44
+
45
+ # @return [String]
46
+ def publishable_key
47
+ raise_if_missing(:publishable_key)
48
+ end
49
+
50
+ # @return [String]
51
+ def encryption_key
52
+ raise_if_missing(:encryption_key)
53
+ end
54
+
55
+ # @return [String]
56
+ def api_key
57
+ deprecate("api_key", "secret_key")
58
+ secret_key
59
+ end
60
+
61
+ # @return [String]
62
+ def client_id
63
+ deprecate("client_id", "publishable_key")
64
+ publishable_key
65
+ end
66
+
67
+ # @return [String]
68
+ def secret
69
+ deprecate("secret", "encryption_key")
70
+ encryption_key
71
+ end
72
+
73
+ attr_writer(*FIELDS)
74
+
75
+ def api_key=(value)
76
+ deprecate("api_key=", "secret_key=")
77
+ @secret_key = value
78
+ end
79
+
80
+ def client_id=(value)
81
+ deprecate("client_id=", "publishable_key=")
82
+ @publishable_key = value
83
+ end
84
+
85
+ def secret=(value)
86
+ deprecate("secret=", "encryption_key=")
87
+ @encryption_key = value
88
+ end
89
+
90
+ class << self
91
+ ALL_METHODS = [*FIELDS, *DEPRECATED_FIELDS].freeze
92
+ EXISTING_METHODS = ALL_METHODS.flat_map { |field| [field, "#{field}=".to_sym] }
93
+
94
+ def method_missing(name, *args, &block)
95
+ super unless EXISTING_METHODS.include? name
96
+ instance.public_send(name, *args, &block)
97
+ end
98
+
99
+ def respond_to_missing?(name, include_private = false)
100
+ EXISTING_METHODS.include?(name) || super
101
+ end
102
+
103
+ def reset
104
+ FIELDS.each do |field|
105
+ instance.public_send("#{field}=", nil)
106
+ end
107
+ end
108
+
109
+ def setup
110
+ yield(instance)
111
+ end
112
+ end
113
+
114
+ private
115
+
116
+ def raise_if_missing(field)
117
+ value = instance_variable_get(:"@#{field}")
118
+
119
+ raise ::Bearer::Errors::Configuration, "Bearer #{field} is missing!" unless value
120
+
121
+ value
122
+ end
123
+
124
+ def deprecate(old_field, new_field)
125
+ puts "Bearer Deprecation Warning: #{old_field} is deprecated, use #{new_field} instead"
126
+ end
127
+
128
+ # defaults to 5 seconds
129
+ # @return [Integer]
130
+ def open_timeout
131
+ @open_timeout || DEFAULT_READ_TIMEOUT
132
+ end
133
+
134
+ # defaults to 5 seconds
135
+ # @return [Integer]
136
+ def read_timeout
137
+ @read_timeout || DEFAULT_READ_TIMEOUT
138
+ end
139
+
140
+ # @return [Hash]
141
+ def default_http_client_params
142
+ {
143
+ read_timeout: read_timeout,
144
+ open_timeout: open_timeout
145
+ }
146
+ end
147
+ end
148
+ end
data/lib/bearer/errors.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Bearer
4
+ module Errors
5
+ class Configuration < StandardError; end
6
+
7
+ class FunctionError < StandardError
8
+ attr_reader :data
9
+
10
+ def initialize(data)
11
+ super(data&.to_json)
12
+ @data = data
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/bearer/integration.rb ADDED
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+ require "forwardable"
6
+
7
+ require_relative "./errors"
8
+ require_relative "./version"
9
+
10
+ class Bearer
11
+ class Integration
12
+ FUNCTIONS_PATH = "api/v4/functions/backend"
13
+ PROXY_FUNCTION_NAME = "bearer-proxy"
14
+ extend Forwardable
15
+
16
+ def initialize(
17
+ integration_id:,
18
+ integration_host:,
19
+ secret_key:,
20
+ read_timeout: nil,
21
+ setup_id: nil,
22
+ auth_id: nil
23
+ )
24
+ @integration_id = integration_id
25
+ @integration_host = integration_host
26
+ @secret_key = secret_key
27
+ @setup_id = setup_id
28
+ @auth_id = auth_id
29
+ @read_timeout = read_timeout
30
+ end
31
+
32
+ # Public: Invoke an integration function
33
+ #
34
+ # function_name - function to invoke
35
+ # body - data to pass in the body of the request
36
+ # query - parameters to pass in the query string of the request
37
+ #
38
+ # Returns the response from the function
39
+ def invoke(function_name, body: nil, query: nil)
40
+ url = "#{@integration_host}/#{FUNCTIONS_PATH}/#{@integration_id}/#{function_name}"
41
+ headers = {
42
+ "Content-Type" => "application/json",
43
+ "Authorization": @secret_key,
44
+ "User-Agent" => "Bearer-Ruby (#{Bearer::VERSION})"
45
+ }
46
+
47
+ response = make_request(method: "POST", url: url, query: query, body: body, headers: headers)
48
+
49
+ JSON.parse(response.body).tap do |response_data|
50
+ raise Errors::FunctionError, response_data["error"] if response_data["error"]
51
+ end
52
+ end
53
+
54
+ # Public: Returns a new integration client instance that will use the given setup id for requests
55
+ #
56
+ # setup_id - the setup id from the dashboard
57
+ def setup(setup_id)
58
+ self.class.new(
59
+ integration_id: @integration_id,
60
+ integration_host: @integration_host,
61
+ secret_key: @secret_key,
62
+ setup_id: setup_id,
63
+ auth_id: @auth_id
64
+ )
65
+ end
66
+
67
+ # Public: Returns a new integration client instance that will use the given auth id for requests
68
+ #
69
+ # auth_id - the auth id used to connect
70
+ def auth(auth_id)
71
+ self.class.new(
72
+ integration_id: @integration_id,
73
+ integration_host: @integration_host,
74
+ secret_key: @secret_key,
75
+ setup_id: @setup_id,
76
+ auth_id: auth_id
77
+ )
78
+ end
79
+
80
+ # Public: An alias for `#auth`
81
+ def authenticate(auth_id)
82
+ auth(auth_id)
83
+ end
84
+
85
+ # Public: Makes a GET request to the API configured for this integration and returns the response
86
+ #
87
+ # See `self.request` for a description of the parameters
88
+ def get(endpoint, headers: nil, body: nil, query: nil)
89
+ request("GET", endpoint, headers: headers, body: body, query: query)
90
+ end
91
+
92
+ # Public: Makes a GET request to the API configured for this integration and returns the response
93
+ #
94
+ # See `self.request` for a description of the parameters
95
+ def head(endpoint, headers: nil, body: nil, query: nil)
96
+ request("HEAD", endpoint, headers: headers, body: body, query: query)
97
+ end
98
+
99
+ # Public: Makes a GET request to the API configured for this integration and returns the response
100
+ #
101
+ # See `self.request` for a description of the parameters
102
+ def post(endpoint, headers: nil, body: nil, query: nil)
103
+ request("POST", endpoint, headers: headers, body: body, query: query)
104
+ end
105
+
106
+ # Public: Makes a GET request to the API configured for this integration and returns the response
107
+ #
108
+ # See `self.request` for a description of the parameters
109
+ def put(endpoint, headers: nil, body: nil, query: nil)
110
+ request("PUT", endpoint, headers: headers, body: body, query: query)
111
+ end
112
+
113
+ # Public: Makes a GET request to the API configured for this integration and returns the response
114
+ #
115
+ # See `self.request` for a description of the parameters
116
+ def patch(endpoint, headers: nil, body: nil, query: nil)
117
+ request("PATCH", endpoint, headers: headers, body: body, query: query)
118
+ end
119
+
120
+ # Public: Makes a GET request to the API configured for this integration and returns the response
121
+ #
122
+ # See `self.request` for a description of the parameters
123
+ def delete(endpoint, headers: nil, body: nil, query: nil)
124
+ request("DELETE", endpoint, headers: headers, body: body, query: query)
125
+ end
126
+
127
+ # Public: Makes a request to the API configured for this integration and returns the response
128
+
129
+ # method - GET/HEAD/POST/PUT/PATCH/DELETE
130
+ # endpoint - the URL relative to the configured API's base URL
131
+ # headers - any headers to send to the API
132
+ # body - any request body data to send
133
+ # query - parameters to add to the URL's query string
134
+ def request(method, endpoint, headers: nil, body: nil, query: nil)
135
+ pre_headers = {
136
+ "Authorization": @secret_key,
137
+ "User-Agent": "Bearer-Ruby (#{Bearer::VERSION})",
138
+ "Bearer-Auth-Id": @auth_id,
139
+ "Bearer-Setup-Id": @setup_id,
140
+ # TODO: Remove this when integration service content type support is fixed
141
+ "Content-Type": "application/json"
142
+ }
143
+
144
+ headers&.each do |key, value|
145
+ pre_headers["Bearer-Proxy-#{key}"] = value
146
+ end
147
+
148
+ request_headers = pre_headers.reject { |_k, v| v.nil? }
149
+ endpoint = endpoint.sub(%r{\A/}, "")
150
+ url = "#{@integration_host}/#{FUNCTIONS_PATH}/#{@integration_id}/#{PROXY_FUNCTION_NAME}/#{endpoint}"
151
+
152
+ make_request(method: method, url: url, query: query, body: body, headers: request_headers)
153
+ end
154
+
155
+ private
156
+
157
+ # @return [Hash]
158
+ def http_client_params
159
+ Bearer::Configuration.http_client_params
160
+ end
161
+
162
+ def make_request(method:, url:, query:, body:, headers:)
163
+ parsed_url = URI(url)
164
+ parsed_url.query = URI.encode_www_form(query) if query
165
+
166
+ Net::HTTP.start(
167
+ parsed_url.hostname,
168
+ parsed_url.port,
169
+ use_ssl: parsed_url.scheme == "https",
170
+ **http_client_params
171
+ ) do |http|
172
+ http.send_request(method, parsed_url, body ? body.to_json : nil, headers)
173
+ end
174
+ end
175
+ end
176
+ end
data/lib/bearer/version.rb CHANGED
@@ -1,3 +1,5 @@
1
- module Bearer
2
- VERSION = "0.1.0"
1
+ # frozen_string_literal: true
2
+
3
+ class Bearer
4
+ VERSION = "1.1.0"
3
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.0
4
+ version: 1.1.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: 2018-11-27 00:00:00.000000000 Z
11
+ date: 2019-10-02 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
@@ -52,24 +52,93 @@ dependencies:
52
52
- - "~>"
53
53
- !ruby/object:Gem::Version
54
54
version: '3.0'
55
- description: Bearer Ruby description
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.65.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.65.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
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
98
+ name: overcommit
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Ruby client to universally call any API using Bearer.sh
56
112
email:
57
- - antoine@bearer.sh
113
+ - engineering@bearer.sh
58
114
executables: []
59
115
extensions: []
60
116
extra_rdoc_files: []
61
117
files:
118
+ - ".github/CODEOWNERS"
119
+ - ".github/PULL_REQUEST_TEMPLATE.md"
120
+ - ".github/main.workflow"
62
121
- ".gitignore"
122
+ - ".jenkins/stack.yml"
123
+ - ".overcommit.yml"
63
124
- ".rspec"
125
+ - ".rubocop.yml"
126
+ - ".ruby-version"
64
127
- ".travis.yml"
65
128
- CODE_OF_CONDUCT.md
66
129
- Gemfile
130
+ - Gemfile.lock
131
+ - Jenkinsfile
67
132
- README.md
68
133
- Rakefile
69
134
- bearer.gemspec
70
135
- bin/console
71
136
- bin/setup
137
+ - bin/test
72
138
- lib/bearer.rb
139
+ - lib/bearer/configuration.rb
140
+ - lib/bearer/errors.rb
141
+ - lib/bearer/integration.rb
73
142
- lib/bearer/version.rb
74
143
homepage: https://www.bearer.sh
75
144
licenses:
@@ -91,8 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
160
- !ruby/object:Gem::Version
92
161
version: '0'
93
162
requirements: []
94
- rubyforge_project:
163
+ rubygems_version: 3.0.3
95
- rubygems_version: 2.6.14
96
164
signing_key:
97
165
specification_version: 4
98
166
summary: Bearer Ruby