polar_sh 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 371122cf10062244b6f2cafb31ac73d645e49841b0bad4ecac8a518c9af1b28f
4
+ data.tar.gz: ff56e2717b3ce2a12e049c0a03ab3e3e295380c407224f9cdface848af98a0e6
5
+ SHA512:
6
+ metadata.gz: f840ba134ff22fa70445c3c7c7fe8b5a0328f82c4be9b399ee72384d69f64a36ed5267e9e0dad021db0e67937a6a1f61c44952552ad2ddef3e5c8f8407de80a0
7
+ data.tar.gz: 53ceece89858ba6e827495753cd2a569084e5aa51f2edb4f518296fb3eff77740b1a6ce5819ba67175464fc841356929e3176929a3f21bdf0d402b5584f5fb30
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-12-20
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Mikkel Malmberg
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,39 @@
1
+ # PolarSh
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ 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/polar_sh`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mikker/polar_sh.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task(default: :spec)
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+
5
+ module Polar
6
+ class Client
7
+ class << self
8
+ def connection
9
+ @connection ||= HTTP
10
+ .persistent(Polar.config.endpoint)
11
+ .follow
12
+ .auth("Bearer #{Polar.config.api_key}")
13
+ .headers(
14
+ accept: "application/json",
15
+ content_type: "application/json",
16
+ user_agent: "polar_sh/v#{VERSION} (github.com/mikker/polar_sh)"
17
+ )
18
+ # .use(logging: {logger: Logger.new(STDOUT)})
19
+ end
20
+
21
+ def get_request(path, **params)
22
+ response = connection.get(path, params:)
23
+ return response.parse if response.status.success?
24
+ raise Error.from_response(response)
25
+ end
26
+
27
+ def post_request(path, **params)
28
+ response = connection.post(path, json: params)
29
+ return response.parse if response.status.success?
30
+ raise Error.from_response(response)
31
+ end
32
+
33
+ def patch_request(path, **params)
34
+ response = connection.patch(path, json: params)
35
+ return response.parse if response.status.success?
36
+ raise Error.from_response(response)
37
+ end
38
+
39
+ def delete_request(path, **params)
40
+ response = connection.delete(path, json: params)
41
+ return true if response.status.success?
42
+ raise Error.from_response(response)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class Configuration
5
+ attr_accessor :api_key
6
+ attr_accessor :sandbox
7
+
8
+ alias sandbox? sandbox
9
+
10
+ def endpoint
11
+ "https://#{sandbox? ? "sandbox-api" : "api"}.polar.sh"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class Error < StandardError
5
+ class PolarError < OpenStruct
6
+ end
7
+
8
+ def initialize(message, error: nil, status: nil)
9
+ @message = message
10
+ @error = error
11
+ @status = status
12
+ end
13
+
14
+ attr_reader :message, :error, :status
15
+
16
+ def self.from_response(response)
17
+ error = PolarError.new(response.parse)
18
+ new(name_for(response.status), error:, status: response.status)
19
+ end
20
+
21
+ def message
22
+ super + ": #{@error.inspect}"
23
+ end
24
+
25
+ class << self
26
+ private
27
+
28
+ def name_for(status)
29
+ case status
30
+ when 400
31
+ "Bad Request"
32
+ when 401
33
+ "Unauthorized"
34
+ when 403
35
+ "Forbidden"
36
+ when 404
37
+ "Not Found"
38
+ when 422
39
+ "Unprocessable Entity"
40
+ when 429
41
+ "Too Many Requests"
42
+ when 500
43
+ "Internal Server Error"
44
+ when 503
45
+ "Service Unavailable"
46
+ else
47
+ "Unknown Error"
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class Resource < OpenStruct
5
+ private
6
+
7
+ class << self
8
+ def handle_list(response, klass = nil)
9
+ klass ||= self
10
+ response.fetch("items").map { |attributes| klass.new(attributes) }
11
+ end
12
+
13
+ def handle_one(response, klass = nil)
14
+ klass ||= self
15
+ klass.new(response)
16
+ end
17
+
18
+ def handle_none(response)
19
+ response
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class Checkout
5
+ class Custom < Resource
6
+ def self.list(params = {})
7
+ response = Client.get_request("/v1/checkouts/custom/", **params)
8
+ handle_list(response, Checkout)
9
+ end
10
+
11
+ def self.create(params)
12
+ params[:payment_processor] ||= "stripe"
13
+ response = Client.post_request("/v1/checkouts/custom/", **params)
14
+ handle_one(response, Checkout)
15
+ end
16
+
17
+ def self.get(id)
18
+ response = Client.get_request("/v1/checkouts/custom/#{id}")
19
+ handle_one(response, Checkout)
20
+ end
21
+
22
+ def self.update(id, params)
23
+ response = Client.patch_request("/v1/checkouts/custom/#{id}", **params)
24
+ handle_one(response, Checkout)
25
+ end
26
+
27
+ def self.client_get(client_secret)
28
+ response = Client.get_request("/v1/checkouts/custom/client/#{client_secret}")
29
+ handle_one(response, Checkout)
30
+ end
31
+
32
+ def self.client_update(client_secret, params)
33
+ response = Client.patch_request("/v1/checkouts/custom/client/#{client_secret}", **params)
34
+ handle_one(response, Checkout)
35
+ end
36
+
37
+ def self.client_confirm(client_secret)
38
+ response = Client.post_request("/v1/checkouts/custom/client/#{client_secret}/confirm")
39
+ handle_one(response, Checkout)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class Checkout < Resource
5
+ autoload :Custom, "polar/resources/checkout/custom"
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class Customer < Resource
5
+ def self.list(params = {})
6
+ response = Client.get_request("/v1/customers", **params)
7
+ handle_list(response)
8
+ end
9
+
10
+ def self.create(params)
11
+ response = Client.post_request("/v1/customers", **params)
12
+ handle_one(response)
13
+ end
14
+
15
+ def self.get(id)
16
+ response = Client.get_request("/v1/customers/#{id}")
17
+ handle_one(response)
18
+ end
19
+
20
+ def self.update(id, params)
21
+ response = Client.patch_request("/v1/customers/#{id}", **params)
22
+ handle_one(response)
23
+ end
24
+
25
+ def self.delete(id)
26
+ response = Client.delete_request("/v1/customers/#{id}")
27
+ handle_none(response)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class CustomerSession < Resource
5
+ def self.create(params)
6
+ response = Client.post_request("/v1/customer-sessions", **params)
7
+ handle_one(response)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class Discount < Resource
5
+ def self.list(params = {})
6
+ response = Client.get_request("/v1/discounts", **params)
7
+ handle_list(response)
8
+ end
9
+
10
+ def self.create(params)
11
+ response = Client.post_request("/v1/discounts", **params)
12
+ handle_one(response)
13
+ end
14
+
15
+ def self.get(id)
16
+ response = Client.get_request("/v1/discounts/#{id}")
17
+ handle_one(response)
18
+ end
19
+
20
+ def self.update(id, params)
21
+ response = Client.patch_request("/v1/discounts/#{id}", **params)
22
+ handle_one(response)
23
+ end
24
+
25
+ def self.delete(id)
26
+ response = Client.delete_request("/v1/discounts/#{id}")
27
+ handle_none(response)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class LicenseKey < Resource
5
+ def self.list(params = {})
6
+ response = Client.get_request("/v1/license-keys", **params)
7
+ handle_list(response)
8
+ end
9
+
10
+ def self.get(id)
11
+ response = Client.get_request("/v1/license-keys/#{id}")
12
+ handle_one(response)
13
+ end
14
+
15
+ def self.update(id, params)
16
+ response = Client.patch_request("/v1/license-keys/#{id}", **params)
17
+ handle_one(response)
18
+ end
19
+
20
+ def self.get_activation(id, activation_id)
21
+ response = Client.get_request("/v1/license-keys/#{id}/activations/#{activation_id}")
22
+ handle_one(response)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class Organization < Resource
5
+ def self.list(params = {})
6
+ response = Client.get_request("/v1/organizations", **params)
7
+ handle_list(response)
8
+ end
9
+
10
+ def self.create(params = {})
11
+ response = Client.post_request("/v1/organizations", **params)
12
+ handle_one(response)
13
+ end
14
+
15
+ def self.get(id)
16
+ response = Client.get_request("/v1/organizations/#{id}")
17
+ handle_one(response)
18
+ end
19
+
20
+ def self.update(id, params = {})
21
+ response = Client.patch_request("/v1/organizations/#{id}", **params)
22
+ handle_one(response)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ class Product < Resource
5
+ def self.list(params = {})
6
+ response = Client.get_request("/v1/products", **params)
7
+ handle_list(response)
8
+ end
9
+
10
+ def self.create(params)
11
+ response = Client.post_request("/v1/products", **params)
12
+ handle_one(response)
13
+ end
14
+
15
+ def self.get(id)
16
+ response = Client.get_request("/v1/products/#{id}")
17
+ handle_one(response)
18
+ end
19
+
20
+ def self.update(id, params)
21
+ response = Client.patch_request("/v1/products/#{id}", **params)
22
+ handle_one(response)
23
+ end
24
+
25
+ def self.update_benefits(id, params)
26
+ response = Client.post_request("/v1/products/#{id}/benefits", **params)
27
+ handle_one(response)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ module Polar
2
+ class User < Resource
3
+ # def self.me
4
+ # response = Client.get_request("/v1/products")
5
+ # pp(JSON.parse(response))
6
+ # new(response.body) if response.status.success?
7
+ # end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polar
4
+ VERSION = "0.1.0"
5
+ end
data/lib/polar.rb ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ostruct"
4
+ require "http"
5
+
6
+ require_relative "polar/version"
7
+
8
+ module Polar
9
+ autoload :Configuration, "polar/configuration"
10
+ autoload :Client, "polar/client"
11
+ autoload :Error, "polar/error"
12
+ autoload :Resource, "polar/resource"
13
+
14
+ autoload :Customer, "polar/resources/customer"
15
+ autoload :CustomerSession, "polar/resources/customer_session"
16
+ autoload :Discount, "polar/resources/discount"
17
+ autoload :Checkout, "polar/resources/checkout"
18
+ autoload :LicenseKey, "polar/resources/license_key"
19
+ autoload :Organization, "polar/resources/organization"
20
+ autoload :Product, "polar/resources/product"
21
+ autoload :User, "polar/resources/user"
22
+
23
+ class << self
24
+ attr_writer :config
25
+ end
26
+
27
+ def self.configure
28
+ yield(config) if block_given?
29
+ end
30
+
31
+ def self.config
32
+ @config ||= Configuration.new
33
+ end
34
+ end
data/lib/polar_sh.rb ADDED
@@ -0,0 +1 @@
1
+ require "polar"
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polar_sh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ original_platform: ''
7
+ authors:
8
+ - Mikkel Malmberg
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ description: Interact with the Polar API
28
+ email:
29
+ - mikkel@brnbw.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - lib/polar.rb
39
+ - lib/polar/client.rb
40
+ - lib/polar/configuration.rb
41
+ - lib/polar/error.rb
42
+ - lib/polar/resource.rb
43
+ - lib/polar/resources/checkout.rb
44
+ - lib/polar/resources/checkout/custom.rb
45
+ - lib/polar/resources/customer.rb
46
+ - lib/polar/resources/customer_session.rb
47
+ - lib/polar/resources/discount.rb
48
+ - lib/polar/resources/license_key.rb
49
+ - lib/polar/resources/organization.rb
50
+ - lib/polar/resources/product.rb
51
+ - lib/polar/resources/user.rb
52
+ - lib/polar/version.rb
53
+ - lib/polar_sh.rb
54
+ homepage: https://github.com/mikker/polar_sh
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://github.com/mikker/polar_sh
59
+ source_code_uri: https://github.com/mikker/polar_sh
60
+ changelog_uri: https://github.com/mikker/polar_sh/blob/main/CHANGELOG.md
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.6.1
76
+ specification_version: 4
77
+ summary: API client for Polar.sh
78
+ test_files: []