clerk-sdk-ruby 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6f285e5ca6280db100eed30796279e412901f157a3fd43d16a41a64d9c0cd85f
4
+ data.tar.gz: 32d0fab6c78b57c0f06fa9d3ef077f077cb6533ada6a400f98bec6a087d939e8
5
+ SHA512:
6
+ metadata.gz: 7721083974266f6b18c6ceb1e1a14abc623caad474a7fcf7a065aafa4ac51c11f732f12db257fa46e3f903a4c9985eb0e6c21e451d3ffc877b7f434ba482fe91
7
+ data.tar.gz: b6a5689badfc699d7d03accb5a301a3504adf8eae009ef450b462fee9a20f995c2ec255ec9b771175fd76209ba991544a51a9e8cd6f0a14f82d6342a922942fb
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.15
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in clerk-sdk-ruby.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ clerk-sdk-ruby (1.0.0)
5
+ faraday (~> 1.4.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ faraday (1.4.1)
11
+ faraday-excon (~> 1.1)
12
+ faraday-net_http (~> 1.0)
13
+ faraday-net_http_persistent (~> 1.1)
14
+ multipart-post (>= 1.2, < 3)
15
+ ruby2_keywords (>= 0.0.4)
16
+ faraday-excon (1.1.0)
17
+ faraday-net_http (1.0.1)
18
+ faraday-net_http_persistent (1.1.0)
19
+ minitest (5.14.2)
20
+ multipart-post (2.1.1)
21
+ rake (13.0.3)
22
+ ruby2_keywords (0.0.4)
23
+
24
+ PLATFORMS
25
+ x86_64-linux
26
+
27
+ DEPENDENCIES
28
+ clerk-sdk-ruby!
29
+ minitest (~> 5.0)
30
+ rake (~> 13.0)
31
+
32
+ BUNDLED WITH
33
+ 2.2.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Clerk Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,157 @@
1
+ # Clerk Ruby SDK
2
+
3
+ Thank you for choosing [Clerk](https://clerk.dev/) for your authentication,
4
+ session & user management needs!
5
+
6
+ This SDK allows you to call the Clerk Backend API from Ruby code without having
7
+ to implement the calls yourself.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'clerk-sdk-ruby', require: "clerk"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install clerk-sdk-ruby
24
+
25
+ ## Quick Start
26
+
27
+ First, you need to get an API key for a Clerk instance. This is done via the
28
+ [Clerk dashboard](https://dashboard.clerk.dev/applications).
29
+
30
+ Then you can instantiate a `Clerk::SDK` instance and access all
31
+ [Backend API](https://docs.clerk.dev/backend/backend-api-reference) endpoints.
32
+ Here's a quick example:
33
+
34
+ ```ruby
35
+ clerk = Clerk::SDK.new(api_key: "your_api_key")
36
+ # List all users
37
+ clerk.users.all
38
+ # Get your first user
39
+ user = clerk.users.all(limit: 1).first
40
+ # Extract their primary email address ID
41
+ email_id = user["primary_email_address_id"]
42
+ # Send them a welcome email
43
+ clerk.emails.create(
44
+ email_address_id: email_id,
45
+ from_email_name: "welcome",
46
+ subject: "Welcome to MyApp",
47
+ body: "Welcome to MyApp, #{user["first_name"]}",
48
+ )
49
+ ```
50
+
51
+ ## Configuration
52
+
53
+ The SDK can be configured in three ways: environment variables, configuration
54
+ singleton and constructor arguments. The priority goes like this:
55
+
56
+ 1. Constructor arguments
57
+ 2. Configuration object
58
+ 3. Environment variables
59
+
60
+ If an argument is not provided, the configuration object is looked up, which
61
+ falls back to the associated environment variable. Here's an example with all
62
+ supported configuration settings their environment variable equivalents:
63
+
64
+ ```ruby
65
+ Clerk.configure do |c|
66
+ c.api_key = "your_api_key" # if omitted: ENV.fetch("CLERK_API_KEY") - will fail if unset
67
+ c.base_url = "https://..." # if omitted: "https://api.clerk.dev/v1/"
68
+ c.logger = Logger.new(STDOUT) # if omitted, no logging
69
+ c.middleware_cache_store = ActiveSupport::Cache::FileStore.new("/tmp/clerk_middleware_cache") # if omitted: Rails.cache or no caching (if not in a Rails app)
70
+ end
71
+ ```
72
+
73
+ You can customize each instance of the `Clerk::SDK` object by passing keyword
74
+ arguments to the constructor:
75
+
76
+ ```ruby
77
+ clerk = Clerk::SDK.new(
78
+ api_key: "X",
79
+ base_url: "Y",
80
+ logger: Logger.new()
81
+ )
82
+ ```
83
+
84
+ For full customization, you can instead pass a `Faraday` object directly, which
85
+ will ignore all the other arguments, if passed:
86
+
87
+ ```ruby
88
+ faraday = Faraday.new()
89
+ clerk = Clerk::SDK.new(connection: faraday)
90
+ ```
91
+
92
+ Refer to the [Faraday documentation](https://lostisland.github.io/faraday/usage/#customizing-faradayconnection)
93
+ for details.
94
+
95
+ ## Rack middleware
96
+
97
+ The SDK comes with a Rack middleware which sets the Clerk session and user in
98
+ the Rack environment. The keys are: `clerk_session` and `clerk_user` for the
99
+ session and user respectively. If the API responds with an error `clerk_error`
100
+ will be set.
101
+
102
+ ## Rails integration
103
+
104
+ The SDK will automatically add the [Rack middleware](#rack-middleware) to the
105
+ middleware stack, using `Rails.cache` for its cache. For easier access to the
106
+ Clerk session and user, include the `Clerk::Authenticatable` concern in your
107
+ controller:
108
+
109
+ ```ruby
110
+ require "clerk/authenticatable"
111
+
112
+ class ApplicationController < ActionController::Base
113
+ include Clerk::Authenticatable
114
+ end
115
+ ```
116
+
117
+ This gives your controller and views access to the following methods:
118
+
119
+ - `clerk_session`
120
+ - `clerk_user`
121
+ - `clerk_user_signed_in?`
122
+
123
+ ## Internals
124
+
125
+ The API client depends on the excellent [Faraday](https://rubygems.org/gems/faraday)
126
+ gem for HTTP requests. You can swap out the original implementation with your
127
+ own customized instance.
128
+
129
+ The API client sends all requests as `application/x-www-form-urlencoded`. The
130
+ API then responds with JSON which is then converted and returned as a Ruby
131
+ `Hash`, or `Array` of hashes. Errors are also returned as a JSON object, with a
132
+ single key (`errors`) containing an array of error objects.
133
+
134
+ Read the [API documentation](https://docs.clerk.dev/backend/backend-api-reference)
135
+ for details on expected parameters and response formats.
136
+
137
+ ## Development
138
+
139
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
140
+ `bundle exec rake` to run the tests. You can also run `bin/console` for an
141
+ interactive prompt that will allow you to experiment.
142
+
143
+ To install this gem onto your local machine, run `bundle exec rake install`. To
144
+ release a new version, update the version number in `version.rb`, and then run
145
+ `bundle exec rake release`, which will create a git tag for the version, push
146
+ git commits and the created tag, and push the `.gem` file to
147
+ [rubygems.org](https://rubygems.org).
148
+
149
+ ## Contributing
150
+
151
+ Bug reports and pull requests are welcome on GitHub at
152
+ https://github.com/clerkinc/clerk-sdk-ruby.
153
+
154
+ ## License
155
+
156
+ The gem is available as open source under the terms of the
157
+ [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "clerk"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/clerk/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "clerk-sdk-ruby"
7
+ spec.version = Clerk::VERSION
8
+ spec.authors = ["Clerk"]
9
+ spec.email = ["ruby-sdk@clerk.dev"]
10
+
11
+ spec.summary = "Clerk SDK for Ruby."
12
+ spec.description = "Client SDK for the Clerk backend API."
13
+ spec.homepage = "https://github.com/clerkinc/clerk-sdk-ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/clerkinc/clerk-sdk-ruby"
19
+ spec.metadata["changelog_uri"] = "https://github.com/clerkinc/clerk-sdk-ruby/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "faraday", "~> 1.4.1"
31
+ end
data/lib/clerk.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "clerk/version"
4
+ require_relative "clerk/sdk"
5
+
6
+ module Clerk
7
+ class << self
8
+ attr_accessor :configuration
9
+
10
+ def configure
11
+ self.configuration ||= Config.new
12
+ yield(configuration)
13
+ end
14
+ end
15
+
16
+ class Config
17
+ PRODUCTION_BASE_URL = "https://api.clerk.dev/v1/".freeze
18
+ attr_accessor :api_key, :base_url, :logger, :middleware_cache_store
19
+
20
+ def initialize
21
+ @base_url = ENV.fetch("CLERK_API_BASE", PRODUCTION_BASE_URL)
22
+ @api_key = ENV.fetch("CLERK_API_KEY")
23
+ end
24
+ end
25
+ end
26
+
27
+ if defined?(::Rails)
28
+ require_relative "clerk/railtie"
29
+ end
@@ -0,0 +1,37 @@
1
+ require "active_support/concern"
2
+
3
+ module Clerk
4
+ module Authenticatable
5
+ extend ActiveSupport::Concern
6
+
7
+ protected
8
+ def clerk_session
9
+ request.env["clerk_session"]
10
+ end
11
+
12
+ def clerk_user
13
+ request.env["clerk_user"]
14
+ end
15
+
16
+ def clerk_user_signed_in?
17
+ !!clerk_user
18
+ end
19
+
20
+ def clerk_sign_in_url
21
+ ENV.fetch("CLERK_SIGN_IN_URL")
22
+ end
23
+
24
+ def clerk_sign_up_url
25
+ ENV.fetch("CLERK_SIGN_UP_URL")
26
+ end
27
+
28
+ def clerk_user_profile_url
29
+ ENV.fetch("CLERK_USER_PROFILE_URL")
30
+ end
31
+
32
+ included do
33
+ helper_method :clerk_session, :clerk_user, :clerk_user_signed_in?,
34
+ :clerk_sign_in_url, :clerk_sign_up_url, :clerk_user_profile_url
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ module Clerk
2
+ module Errors
3
+ class Base < StandardError
4
+ attr_reader :status
5
+
6
+ def initialize(msg, status:)
7
+ @errors = msg["errors"]
8
+ @status = status
9
+ super(msg.merge("status" => status))
10
+ end
11
+ end
12
+
13
+ class Fatal < Base
14
+ end
15
+
16
+ class Authentication < Base
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,70 @@
1
+ require_relative "sdk"
2
+
3
+ module Clerk
4
+ class RackMiddleware
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ req = Rack::Request.new(env)
11
+ token = req.cookies["__session"]
12
+
13
+ if token
14
+ sess_id = req.params["_clerk_session_id"]
15
+ begin
16
+ env["clerk_session"] = fetch_session(token, sess_id)
17
+ rescue Errors::Authentication => e
18
+ env["clerk_error"] = e
19
+ end
20
+ end
21
+ if sess = env["clerk_session"]
22
+ env["clerk_user"] = fetch_user(sess["user_id"])
23
+ end
24
+ @app.call(env)
25
+ end
26
+
27
+ private
28
+ def clerk_sdk
29
+ SDK.new
30
+ end
31
+
32
+ def fetch_session(token, sess_id)
33
+ cache_key = token.split(".")[1]
34
+ if sess_id
35
+ session = cached_fetch("clerk_session:#{sess_id}:#{cache_key}") do
36
+ sdk = clerk_sdk
37
+ sdk.sessions.verify_token(sess_id, token)
38
+ end
39
+ else
40
+ session = cached_fetch("clerk_session:#{cache_key}") do
41
+ sdk = clerk_sdk
42
+ client = sdk.clients.verify_token(token)
43
+ sess_id = client["last_active_session_id"]
44
+ client["sessions"].find do |sess|
45
+ sess["id"] == sess_id
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def fetch_user(user_id)
52
+ sdk = clerk_sdk
53
+ cached_fetch("clerk_user:#{user_id}") do
54
+ sdk.users.find(user_id)
55
+ end
56
+ end
57
+
58
+ def cached_fetch(key, &block)
59
+ if store = Clerk.configuration.middleware_cache_store
60
+ store.fetch(key, expires_in: cache_ttl, &block)
61
+ else
62
+ yield
63
+ end
64
+ end
65
+
66
+ def cache_ttl
67
+ 60
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ require_relative "rack_middleware"
4
+
5
+ module Clerk
6
+ class Railtie < ::Rails::Railtie
7
+ initializer "clerk_railtie.configure_rails_initialization" do |app|
8
+ app.middleware.use Clerk::RackMiddleware
9
+ end
10
+
11
+ config.to_prepare do
12
+ Clerk.configure do |c|
13
+ c.middleware_cache_store ||= Rails.cache
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ require_relative "resources/allowlist_identifiers"
2
+ require_relative "resources/allowlist"
3
+ require_relative "resources/clients"
4
+ require_relative "resources/emails"
5
+ require_relative "resources/sessions"
6
+ require_relative "resources/sms_messages"
7
+ require_relative "resources/users"
@@ -0,0 +1,16 @@
1
+ require "forwardable"
2
+ require_relative "singular_resource"
3
+
4
+ module Clerk
5
+ module Resources
6
+ class Allowlist
7
+ extend Forwardable
8
+
9
+ def initialize(client)
10
+ @resource = SingularResource.new(client, "beta_features/allowlist")
11
+ end
12
+
13
+ def_delegators :@resource, :update
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require "forwardable"
2
+ require_relative "plural_resource"
3
+
4
+ module Clerk
5
+ module Resources
6
+ class AllowlistIdentifiers
7
+ extend Forwardable
8
+
9
+ def initialize(client)
10
+ @resource = PluralResource.new(client, "allowlist_identifiers")
11
+ end
12
+
13
+ def_delegators :@resource, :all, :create, :delete
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ require "forwardable"
2
+ require_relative "plural_resource"
3
+
4
+ module Clerk
5
+ module Resources
6
+ class Clients
7
+ extend Forwardable
8
+
9
+ def initialize(client)
10
+ @client = client
11
+ @resource = PluralResource.new(client, "clients")
12
+ end
13
+
14
+ def verify_token(token)
15
+ @client.request(:post, "#{@resource.collection_path}/verify",
16
+ body: {token: token})
17
+ end
18
+
19
+ def_delegators :@resource, :all, :find
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,16 @@
1
+ require "forwardable"
2
+ require_relative "plural_resource"
3
+
4
+ module Clerk
5
+ module Resources
6
+ class Emails
7
+ extend Forwardable
8
+
9
+ def initialize(client)
10
+ @resource = PluralResource.new(client, "emails")
11
+ end
12
+
13
+ def_delegators :@resource, :create
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,38 @@
1
+ module Clerk
2
+ module Resources
3
+ class PluralResource
4
+ def initialize(client, path_prefix)
5
+ @client = client
6
+ @path_prefix = path_prefix
7
+ end
8
+
9
+ def all(query_params = {})
10
+ @client.request(:get, collection_path, query: query_params)
11
+ end
12
+
13
+ def find(id)
14
+ @client.request(:get, resource_path(id))
15
+ end
16
+
17
+ def create(data = nil)
18
+ @client.request(:post, collection_path, body: data)
19
+ end
20
+
21
+ def update(id, changes = nil)
22
+ @client.request(:patch, resource_path(id), body: changes)
23
+ end
24
+
25
+ def delete(id)
26
+ @client.request(:delete, resource_path(id))
27
+ end
28
+
29
+ def collection_path
30
+ @path_prefix
31
+ end
32
+
33
+ def resource_path(id)
34
+ "#{@path_prefix}/#{id}"
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ require "forwardable"
2
+ require_relative "plural_resource"
3
+
4
+ module Clerk
5
+ module Resources
6
+ class Sessions
7
+ extend Forwardable
8
+
9
+ def initialize(client)
10
+ @client = client
11
+ @resource = PluralResource.new(client, "sessions")
12
+ end
13
+
14
+ def revoke(id)
15
+ @client.request(:post, "#{@resource.resource_path(id)}/revoke")
16
+ end
17
+
18
+ def verify_token(id, token)
19
+ @client.request(:post, "#{@resource.resource_path(id)}/verify",
20
+ body: {token: token})
21
+ end
22
+
23
+ def_delegators :@resource, :all, :find
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ module Clerk
2
+ module Resources
3
+ class SingularResource
4
+ def initialize(client, resource_path)
5
+ @client = client
6
+ @resource_path = resource_path
7
+ end
8
+
9
+ def update(changes = {})
10
+ @client.request(:patch, @resource_path, body:changes)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ require "forwardable"
2
+ require_relative "plural_resource"
3
+
4
+ module Clerk
5
+ module Resources
6
+ class SMSMessages
7
+ extend Forwardable
8
+
9
+ def initialize(client)
10
+ @resource = PluralResource.new(client, "sms_messages")
11
+ end
12
+
13
+ def_delegators :@resource, :create
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require "forwardable"
2
+ require_relative "plural_resource"
3
+
4
+ module Clerk
5
+ module Resources
6
+ class Users
7
+ extend Forwardable
8
+
9
+ def initialize(client)
10
+ @resource = PluralResource.new(client, "users")
11
+ end
12
+
13
+ def_delegators :@resource, :all, :find, :update, :delete
14
+ end
15
+ end
16
+ end
data/lib/clerk/sdk.rb ADDED
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "logger"
5
+ require "net/http"
6
+ require "json"
7
+
8
+ require_relative "resources/allowlist_identifiers"
9
+ require_relative "resources/allowlist"
10
+ require_relative "resources/clients"
11
+ require_relative "resources/emails"
12
+ require_relative "resources/sessions"
13
+ require_relative "resources/sms_messages"
14
+ require_relative "resources/users"
15
+ require_relative "errors"
16
+
17
+ module Clerk
18
+ class SDK
19
+ DEFAULT_HEADERS = {
20
+ "User-Agent" => "Clerk/#{Clerk::VERSION}; Faraday/#{Faraday::VERSION}; Ruby/#{RUBY_VERSION}"
21
+ }
22
+
23
+ def initialize(api_key: nil, base_url: nil, logger: nil, ssl_verify: true,
24
+ connection: nil)
25
+ if connection # Inject a Faraday::Connection for testing or full control over Faraday
26
+ @conn = connection
27
+ return
28
+ else
29
+ base_url = base_url || Clerk.configuration.base_url
30
+ base_uri = if !base_url.end_with?("/")
31
+ URI("#{base_url}/")
32
+ else
33
+ URI(base_url)
34
+ end
35
+ api_key = api_key || Clerk.configuration.api_key
36
+ logger = logger || Clerk.configuration.logger
37
+ @conn = Faraday.new(
38
+ url: base_uri, headers: DEFAULT_HEADERS, ssl: {verify: ssl_verify}
39
+ ) do |f|
40
+ f.request :url_encoded
41
+ f.request :authorization, "Bearer", api_key
42
+ if logger
43
+ f.response :logger, logger do |l|
44
+ l.filter(/(Authorization: "Bearer) (\w+)/, '\1 [SECRET]')
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def request(method, path, query: [], body: nil)
52
+ response = case method
53
+ when :get
54
+ @conn.get(path, query)
55
+ when :post
56
+ @conn.post(path, body)
57
+ when :patch
58
+ @conn.patch(path, body)
59
+ when :delete
60
+ @conn.delete(path)
61
+ end
62
+
63
+ body = if response["Content-Type"] == "application/json"
64
+ JSON.parse(response.body)
65
+ else
66
+ response.body
67
+ end
68
+
69
+ if response.success?
70
+ body
71
+ else
72
+ klass = case body.dig("errors", 0, "code")
73
+ when "cookie_invalid", "client_not_found", "resource_not_found"
74
+ Errors::Authentication
75
+ else
76
+ Errors::Fatal
77
+ end
78
+ raise klass.new(body, status: response.status)
79
+ end
80
+ end
81
+
82
+ def allowlist_identifiers
83
+ Resources::AllowlistIdentifiers.new(self)
84
+ end
85
+
86
+ def allowlist
87
+ Resources::Allowlist.new(self)
88
+ end
89
+
90
+ def clients
91
+ Resources::Clients.new(self)
92
+ end
93
+
94
+ def emails
95
+ Resources::Emails.new(self)
96
+ end
97
+
98
+ def sessions
99
+ Resources::Sessions.new(self)
100
+ end
101
+
102
+ def sms_messages
103
+ Resources::SMSMessages.new(self)
104
+ end
105
+
106
+ def users
107
+ Resources::Users.new(self)
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,11 @@
1
+ module Clerk
2
+ module Utils
3
+ module_function
4
+ def camelize(term)
5
+ string = term.to_s
6
+ string = string.sub(/^[a-z\d]*/) { match.capitalize }
7
+ string.gsub!(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }
8
+ string
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clerk
4
+ VERSION = "1.0.0"
5
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clerk-sdk-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Clerk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.1
27
+ description: Client SDK for the Clerk backend API.
28
+ email:
29
+ - ruby-sdk@clerk.dev
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".github/workflows/main.yml"
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - clerk-sdk-ruby.gemspec
44
+ - lib/clerk.rb
45
+ - lib/clerk/authenticatable.rb
46
+ - lib/clerk/errors.rb
47
+ - lib/clerk/rack_middleware.rb
48
+ - lib/clerk/railtie.rb
49
+ - lib/clerk/resources.rb
50
+ - lib/clerk/resources/allowlist.rb
51
+ - lib/clerk/resources/allowlist_identifiers.rb
52
+ - lib/clerk/resources/clients.rb
53
+ - lib/clerk/resources/emails.rb
54
+ - lib/clerk/resources/plural_resource.rb
55
+ - lib/clerk/resources/sessions.rb
56
+ - lib/clerk/resources/singular_resource.rb
57
+ - lib/clerk/resources/sms_messages.rb
58
+ - lib/clerk/resources/users.rb
59
+ - lib/clerk/sdk.rb
60
+ - lib/clerk/utils.rb
61
+ - lib/clerk/version.rb
62
+ homepage: https://github.com/clerkinc/clerk-sdk-ruby
63
+ licenses:
64
+ - MIT
65
+ metadata:
66
+ homepage_uri: https://github.com/clerkinc/clerk-sdk-ruby
67
+ source_code_uri: https://github.com/clerkinc/clerk-sdk-ruby
68
+ changelog_uri: https://github.com/clerkinc/clerk-sdk-ruby/blob/main/CHANGELOG.md
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.4.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.2.15
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Clerk SDK for Ruby.
88
+ test_files: []