Skip to content

Commit 9ab845d

Browse files
authored
chore(kno-1566): add tenants functions (#6)
1 parent bcf0ea8 commit 9ab845d

19 files changed

+356
-109
lines changed

.github/workflows/ruby.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7+
8+
name: Ruby
9+
10+
on:
11+
push:
12+
branches: [ "main" ]
13+
pull_request:
14+
branches: [ "main" ]
15+
16+
permissions:
17+
contents: read
18+
19+
jobs:
20+
test:
21+
22+
runs-on: ubuntu-latest
23+
strategy:
24+
matrix:
25+
ruby-version: ['2.6', '2.7', '3.0']
26+
27+
steps:
28+
- uses: actions/checkout@v3
29+
- name: Set up Ruby
30+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
32+
# uses: ruby/setup-ruby@v1
33+
uses: ruby/setup-ruby@2b019609e2b0f1ea1a2bc8ca11cb82ab46ada124
34+
with:
35+
ruby-version: ${{ matrix.ruby-version }}
36+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
37+
- name: Run tests
38+
run: bundle exec rspec spec
39+
40+
lint:
41+
42+
runs-on: ubuntu-latest
43+
steps:
44+
- name: Checkout code
45+
uses: actions/checkout@v3
46+
- name: Install Ruby and gems
47+
uses: ruby/setup-ruby@8f312efe1262fb463d906e9bf040319394c18d3e # v1.92
48+
with:
49+
bundler-cache: true
50+
# Add or replace any other lints here
51+
- name: Lint Ruby files
52+
run: bundle exec rubocop

.rubocop.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
AllCops:
2+
TargetRubyVersion: 2.5
3+
Metrics/LineLength:
4+
Max: 120
5+
Metrics/MethodLength:
6+
Max: 35
7+
Metrics/ParameterLists:
8+
Max: 6

Gemfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# frozen_string_literal: true
22

3-
source "https://rubygems.org"
3+
source 'https://rubygems.org'
44

55
gemspec
6+
7+
group :test do
8+
gem 'rspec', '~> 3.11'
9+
end

Gemfile.lock

+15
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@ GEM
77
remote: https://rubygems.org/
88
specs:
99
ast (2.4.2)
10+
diff-lcs (1.5.0)
1011
parallel (1.20.1)
1112
parser (3.0.2.0)
1213
ast (~> 2.4.1)
1314
rainbow (3.0.0)
1415
rake (13.0.6)
1516
regexp_parser (2.1.1)
1617
rexml (3.2.5)
18+
rspec (3.11.0)
19+
rspec-core (~> 3.11.0)
20+
rspec-expectations (~> 3.11.0)
21+
rspec-mocks (~> 3.11.0)
22+
rspec-core (3.11.0)
23+
rspec-support (~> 3.11.0)
24+
rspec-expectations (3.11.0)
25+
diff-lcs (>= 1.2.0, < 2.0)
26+
rspec-support (~> 3.11.0)
27+
rspec-mocks (3.11.1)
28+
diff-lcs (>= 1.2.0, < 2.0)
29+
rspec-support (~> 3.11.0)
30+
rspec-support (3.11.0)
1731
rubocop (1.18.4)
1832
parallel (~> 1.10)
1933
parser (>= 3.0.0.0)
@@ -42,6 +56,7 @@ DEPENDENCIES
4256
bundler (>= 2.0.1)
4357
knockapi!
4458
rake
59+
rspec (~> 3.11)
4560
standard
4661

4762
BUNDLED WITH

bin/console

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

3-
require "bundler/setup"
4-
require "knock"
4+
require 'bundler/setup'
5+
require 'knock'
56

6-
require "irb"
7-
IRB.start(__FILE__)
7+
require 'irb'
8+
IRB.start(__FILE__)

knockapi.gemspec

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
# frozen_string_literal: true
22

3-
lib = File.expand_path("lib", __dir__)
3+
lib = File.expand_path('lib', __dir__)
44
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5-
require "knock/version"
5+
require 'knock/version'
66

77
Gem::Specification.new do |spec|
8-
spec.name = "knockapi"
8+
spec.name = 'knockapi'
99
spec.version = Knock::VERSION
10-
spec.authors = ["Knock Labs, Inc."]
11-
spec.email = ["[email protected]"]
12-
spec.description = "API client for Knock"
13-
spec.summary = "API client for Knock"
14-
spec.homepage = "https://github.com/knocklabs/knock-ruby"
15-
spec.license = "MIT"
10+
spec.authors = ['Knock Labs, Inc.']
11+
spec.email = ['[email protected]']
12+
spec.description = 'API client for Knock'
13+
spec.summary = 'API client for Knock'
14+
spec.homepage = 'https://github.com/knocklabs/knock-ruby'
15+
spec.license = 'MIT'
1616
spec.metadata = {
17-
"documentation_uri" => "https://docs.knock.app"
17+
'documentation_uri' => 'https://docs.knock.app'
1818
}
1919

2020
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
2121
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
2222
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23-
spec.require_paths = ["lib"]
23+
spec.require_paths = ['lib']
2424

25-
spec.add_development_dependency "bundler", ">= 2.0.1"
26-
spec.add_development_dependency "rake"
27-
spec.add_development_dependency "standard"
25+
spec.add_development_dependency 'bundler', '>= 2.0.1'
26+
spec.add_development_dependency 'rake'
27+
spec.add_development_dependency 'standard'
2828

29-
spec.required_ruby_version = ">= 2.5"
29+
spec.required_ruby_version = '>= 2.5'
3030
end

lib/knock.rb

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# frozen_string_literal: true
22

3-
require "knock/version"
4-
require "json"
3+
require 'knock/version'
4+
require 'json'
55

6+
# Setup for Knock client
67
module Knock
7-
API_HOSTNAME = ENV["KNOCK_API_HOSTNAME"] || "api.knock.app"
8+
API_HOSTNAME = ENV['KNOCK_API_HOSTNAME'] || 'api.knock.app'
89

910
def self.key=(value)
1011
Base.key = value
@@ -15,26 +16,27 @@ def self.key
1516
end
1617

1718
def self.key!
18-
key || raise("Knock.key not set")
19+
key || raise('Knock.key not set')
1920
end
2021

21-
autoload :Base, "knock/base"
22-
autoload :Client, "knock/client"
22+
autoload :Base, 'knock/base'
23+
autoload :Client, 'knock/client'
2324

2425
# Resources
25-
autoload :Preferences, "knock/preferences"
26-
autoload :Users, "knock/users"
27-
autoload :Workflows, "knock/workflows"
28-
autoload :BulkOperations, "knock/bulk_operations"
29-
autoload :Objects, "knock/objects"
30-
autoload :Messages, "knock/messages"
26+
autoload :Preferences, 'knock/preferences'
27+
autoload :Users, 'knock/users'
28+
autoload :Workflows, 'knock/workflows'
29+
autoload :BulkOperations, 'knock/bulk_operations'
30+
autoload :Objects, 'knock/objects'
31+
autoload :Tenants, 'knock/tenants'
32+
autoload :Messages, 'knock/messages'
3133

3234
# Errors
33-
autoload :APIError, "knock/errors"
34-
autoload :AuthenticationError, "knock/errors"
35-
autoload :InvalidRequestError, "knock/errors"
35+
autoload :APIError, 'knock/errors'
36+
autoload :AuthenticationError, 'knock/errors'
37+
autoload :InvalidRequestError, 'knock/errors'
3638

37-
key = ENV["KNOCK_API_KEY"]
39+
key = ENV['KNOCK_API_KEY']
3840
Knock.key = key unless key.nil?
3941

4042
# Triggers the workflow with the given key

lib/knock/base.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
# frozen_string_literal: true
2+
13
module Knock
24
## The Base class handles setting and reading the Knock API Key for authentication
35
module Base
46
attr_accessor :key
57

68
class << self
7-
attr_writer :key
8-
attr_reader :key
9+
attr_accessor :key
910
end
1011
end
1112
end

lib/knock/bulk_operations.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
require "net/http"
2-
require "uri"
1+
# frozen_string_literal: true
2+
3+
require 'net/http'
4+
require 'uri'
35

46
module Knock
57
# Provides convienience methods for working with bulk operations

lib/knock/client.rb

+32-28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Knock
24
# A Net::HTTP based API client for interacting with the Knock API
35
module Client
@@ -18,9 +20,7 @@ def execute_request(request:)
1820
http_status = response.code.to_i
1921
handle_error_response(response: response) if http_status >= 400
2022

21-
if response.body and response.body != "" do
22-
JSON.parse(response.body)
23-
end
23+
JSON.parse(response.body) if response.body && (response.body != '')
2424
end
2525

2626
def get_request(path:, auth: false, params: {}, access_token: nil)
@@ -29,19 +29,19 @@ def get_request(path:, auth: false, params: {}, access_token: nil)
2929

3030
request = Net::HTTP::Get.new(
3131
uri.to_s,
32-
"Content-Type" => "application/json"
32+
'Content-Type' => 'application/json'
3333
)
3434

35-
request["Authorization"] = "Bearer #{access_token || Knock.key!}" if auth
36-
request["User-Agent"] = user_agent
35+
request['Authorization'] = "Bearer #{access_token || Knock.key!}" if auth
36+
request['User-Agent'] = user_agent
3737
request
3838
end
3939

40-
def post_request(path:, auth: false, idempotency_key: nil, body: nil)
41-
request = Net::HTTP::Post.new(path, "Content-Type" => "application/json")
40+
def post_request(path:, auth: false, _idempotency_key: nil, body: nil)
41+
request = Net::HTTP::Post.new(path, 'Content-Type' => 'application/json')
4242
request.body = body.to_json if body
43-
request["Authorization"] = "Bearer #{Knock.key!}" if auth
44-
request["User-Agent"] = user_agent
43+
request['Authorization'] = "Bearer #{Knock.key!}" if auth
44+
request['User-Agent'] = user_agent
4545
request
4646
end
4747

@@ -51,68 +51,72 @@ def delete_request(path:, auth: false, params: {})
5151

5252
request = Net::HTTP::Delete.new(
5353
uri.to_s,
54-
"Content-Type" => "application/json"
54+
'Content-Type' => 'application/json'
5555
)
5656

57-
request["Authorization"] = "Bearer #{Knock.key!}" if auth
58-
request["User-Agent"] = user_agent
57+
request['Authorization'] = "Bearer #{Knock.key!}" if auth
58+
request['User-Agent'] = user_agent
5959
request
6060
end
6161

62-
def put_request(path:, auth: false, idempotency_key: nil, body: nil)
63-
request = Net::HTTP::Put.new(path, "Content-Type" => "application/json")
62+
def put_request(path:, auth: false, _idempotency_key: nil, body: nil)
63+
request = Net::HTTP::Put.new(path, 'Content-Type' => 'application/json')
6464
request.body = body.to_json if body
65-
request["Authorization"] = "Bearer #{Knock.key!}" if auth
66-
request["User-Agent"] = user_agent
65+
request['Authorization'] = "Bearer #{Knock.key!}" if auth
66+
request['User-Agent'] = user_agent
6767
request
6868
end
6969

7070
def user_agent
7171
"Knock Ruby - v#{Knock::VERSION}"
7272
end
7373

74+
# rubocop:disable Metrics/AbcSize
75+
7476
def handle_error_response(response:)
7577
http_status = response.code.to_i
7678
json = JSON.parse(response.body)
7779

7880
case http_status
7981
when 400
8082
raise InvalidRequestError.new(
81-
message: json["message"],
83+
message: json['message'],
8284
http_status: http_status,
83-
request_id: response["x-request-id"]
85+
request_id: response['x-request-id']
8486
)
8587
when 401
8688
raise AuthenticationError.new(
87-
message: json["message"],
89+
message: json['message'],
8890
http_status: http_status,
89-
request_id: response["x-request-id"]
91+
request_id: response['x-request-id']
9092
)
9193
when 404
9294
raise APIError.new(
93-
message: json["message"],
95+
message: json['message'],
9496
http_status: http_status,
95-
request_id: response["x-request-id"]
97+
request_id: response['x-request-id']
9698
)
9799
when 422
98-
message = json["message"]
99-
errors = extract_error(json["errors"]) if json["errors"]
100+
message = json['message']
101+
errors = extract_error(json['errors']) if json['errors']
100102
message += " (#{errors})" if errors
101103

102104
raise InvalidRequestError.new(
103105
message: message,
104106
http_status: http_status,
105-
request_id: response["x-request-id"]
107+
request_id: response['x-request-id']
106108
)
107109
end
108110
end
109111

112+
# rubocop:enable Metrics/AbcSize
113+
110114
private
111115

112116
def extract_error(errors)
113117
errors.map do |error|
114-
"#{error["field"]}: #{error["message"]} (#{error["type"]})"
115-
end.join("; ")
118+
"#{error['field']}: #{error['message']} (#{error['type']})"
119+
end.join('; ')
116120
end
117121
end
118122
end

0 commit comments

Comments
 (0)