elasticfin 0.1.1

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: fa62ade8d1ea1c964a0b8f9a46e6cae7502e17871fbfc08e5fae24b2bd984458
4
+ data.tar.gz: bf8a4cb710510e55c29b1872f66c20b8c089992d9178e77eef3eaad258f13e66
5
+ SHA512:
6
+ metadata.gz: 47926b533b42ef70a7c28f7d491eb95624b088c674d9e5411f6eb05a669538ba524e1500ce11a44e993f5562d269a0e8dcec9c9ca1d4313b26f11e99bbeeb0a6
7
+ data.tar.gz: edf02101e4f8624d7ec47d50c934a96777194733c746001c7f6d42d66909f341f968a155e019677f4d72434d804052e522eb008687bc8e5e7177495d118a6695
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ # Omakase Ruby styling for Rails
2
+ inherit_gem: { rubocop-rails-omakase: rubocop.yml }
3
+ # Overwrite or add rules to create your own house style
4
+ #
5
+ # # Use `[a, [b, c]]` not `[ a, [ b, c ] ]`
6
+ # Layout/SpaceInsideArrayLiteralBrackets:
7
+ # Enabled: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Elasticfin
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,50 @@
1
+ # Elasticfin
2
+
3
+ Ruby wrapper for ElasticFin API
4
+
5
+ ## Setup
6
+
7
+ Add to Gemfile:
8
+
9
+ ```ruby
10
+ gem 'elasticfin'
11
+ ```
12
+
13
+ Set your API key in env:
14
+
15
+ ```
16
+ ELASTICFIN_KEY=your_key_here
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Check entitlements
22
+
23
+ ```ruby
24
+ Elasticfin.customer.has_entitlement?(team_id, "premium")
25
+ ```
26
+
27
+ ### Create checkout
28
+
29
+ ```ruby
30
+ url = Elasticfin.checkout.create_session(team_id, email, price_id)
31
+ ```
32
+
33
+ ### Billing portal
34
+
35
+ ```ruby
36
+ url = Elasticfin.billing.portal_url(team_id, return_url)
37
+ ```
38
+
39
+ ### Change plan
40
+
41
+ ```ruby
42
+ url = Elasticfin.billing.change_plan_url(team_id, price_id, return_url)
43
+ ```
44
+
45
+ ### Get customer info
46
+
47
+ ```ruby
48
+ provider = Elasticfin.customer.provider(team_id)
49
+ interval = Elasticfin.customer.interval(team_id)
50
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+ require "cgi"
6
+
7
+ module Elasticfin
8
+ class Client
9
+ attr_reader :api_key, :base_url
10
+
11
+ def initialize(api_key:)
12
+ @api_key = api_key
13
+ @base_url = "api.elasticfin.com"
14
+ end
15
+
16
+ def customer
17
+ @customer ||= Resources::Customer.new(self)
18
+ end
19
+
20
+ def checkout
21
+ @checkout ||= Resources::Checkout.new(self)
22
+ end
23
+
24
+ def billing
25
+ @billing ||= Resources::Billing.new(self)
26
+ end
27
+
28
+ def get(path, params = {})
29
+ uri_path = path
30
+ unless params.empty?
31
+ query_string = params.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&")
32
+ uri_path = "#{path}?#{query_string}"
33
+ end
34
+
35
+ request = build_request(:get, uri_path)
36
+ make_request(request)
37
+ end
38
+
39
+ def post(path, body = {})
40
+ request = build_request(:post, path)
41
+ request["Content-Type"] = "application/json"
42
+ request.body = body.to_json
43
+ make_request(request)
44
+ end
45
+
46
+ private
47
+
48
+ def build_request(method, path)
49
+ request_class = case method
50
+ when :get
51
+ Net::HTTP::Get
52
+ when :post
53
+ Net::HTTP::Post
54
+ when :put
55
+ Net::HTTP::Put
56
+ when :delete
57
+ Net::HTTP::Delete
58
+ end
59
+
60
+ request = request_class.new(path)
61
+ request["Authorization"] = "Bearer #{@api_key}"
62
+ request
63
+ end
64
+
65
+ def make_request(request)
66
+ Net::HTTP.start(@base_url, 443, use_ssl: true) do |http|
67
+ response = http.request(request)
68
+ handle_response(response)
69
+ end
70
+ end
71
+
72
+ def handle_response(response)
73
+ case response.code.to_i
74
+ when 200..299
75
+ if response.body.present?
76
+ JSON.parse(response.body)
77
+ else
78
+ response.body
79
+ end
80
+ when 404
81
+ raise Error, "Resource not found"
82
+ when 401
83
+ raise Error, "Unauthorized"
84
+ else
85
+ raise Error, "Request failed with status #{response.code}: #{response.body}"
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elasticfin
4
+ class Configuration
5
+ attr_accessor :api_key
6
+
7
+ def initialize
8
+ @api_key = ENV["ELASTICFIN_KEY"]
9
+ end
10
+ end
11
+
12
+ class << self
13
+ def configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def configure
18
+ yield(configuration)
19
+ end
20
+
21
+ def reset_configuration!
22
+ @configuration = Configuration.new
23
+ end
24
+
25
+ def client
26
+ @client ||= Client.new(api_key: configuration.api_key)
27
+ end
28
+
29
+ def customer
30
+ client.customer
31
+ end
32
+
33
+ def checkout
34
+ client.checkout
35
+ end
36
+
37
+ def billing
38
+ client.billing
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elasticfin
4
+ module Resources
5
+ class Billing
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def portal_url(team_id, return_url)
11
+ response = @client.get("/customer/billing_portal_url/#{team_id}", { return_url: return_url })
12
+ response["url"]
13
+ rescue
14
+ nil
15
+ end
16
+
17
+ def change_plan_url(team_id, price_id, return_url)
18
+ response = @client.get("/customer/change_plan_url/#{team_id}", { price_id: price_id, return_url: return_url })
19
+ response["url"]
20
+ rescue
21
+ nil
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elasticfin
4
+ module Resources
5
+ class Checkout
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def create_session(team_id, billing_email, price_id)
11
+ body = {
12
+ external_id: team_id.to_s,
13
+ email: billing_email,
14
+ price_id: price_id
15
+ }
16
+
17
+ response = @client.post("/checkout", body)
18
+ response["checkout_url"]
19
+ rescue
20
+ nil
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elasticfin
4
+ module Resources
5
+ class Customer
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def has_entitlement?(team_id, entitlement_name)
11
+ response = @client.get("/customer/has-entitlement/#{team_id}/#{entitlement_name}")
12
+ response == true
13
+ rescue
14
+ false
15
+ end
16
+
17
+ def provider(team_id)
18
+ response = @client.get("/customer/provider/#{team_id}")
19
+ response["provider"]
20
+ rescue
21
+ nil
22
+ end
23
+
24
+ def interval(team_id)
25
+ response = @client.get("/customer/interval/#{team_id}")
26
+ response["interval"]
27
+ rescue
28
+ nil
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elasticfin
4
+ VERSION = "0.1.1"
5
+ end
data/lib/elasticfin.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "elasticfin/version"
4
+ require_relative "elasticfin/client"
5
+ require_relative "elasticfin/resources/customer"
6
+ require_relative "elasticfin/resources/checkout"
7
+ require_relative "elasticfin/resources/billing"
8
+ require_relative "elasticfin/configuration"
9
+
10
+ module Elasticfin
11
+ class Error < StandardError; end
12
+ end
@@ -0,0 +1,4 @@
1
+ module Elasticfin
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elasticfin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - elasticfin
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ email:
13
+ - development@elasticfin.com
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - ".rubocop.yml"
19
+ - LICENSE.txt
20
+ - README.md
21
+ - Rakefile
22
+ - lib/elasticfin.rb
23
+ - lib/elasticfin/client.rb
24
+ - lib/elasticfin/configuration.rb
25
+ - lib/elasticfin/resources/billing.rb
26
+ - lib/elasticfin/resources/checkout.rb
27
+ - lib/elasticfin/resources/customer.rb
28
+ - lib/elasticfin/version.rb
29
+ - sig/elasticfin.rbs
30
+ homepage: https://elasticfin.com
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://elasticfin.com
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.1.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.7.2
50
+ specification_version: 4
51
+ summary: Ruby gem to connect to Elasticfin API to manage subscriptions and payments
52
+ test_files: []