lemonsqueezy 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: 159648c0a2258cb867fc0b4119e816aceccb4584fd45fc690e91b3db2f760865
4
+ data.tar.gz: d56a260c987b0039d7a8c6376d8686d1cdb32128b0039f61787644eaa34e9707
5
+ SHA512:
6
+ metadata.gz: 3c8b6936badab690f9e73cf97777074ef32724382135de8699258f9ca87ba3cc5a6f8fef0b385f86645436967ba329ab388d8e9128ad5ec7451073f4187283de
7
+ data.tar.gz: 829dd9b1560c6d6a7bb534332ccb4af2ae3c0333e06c4bbed0aee8666bc5139480064c4a8358c5703c385a3e2747cf7085e47f529880d91730dfa7518dce63d0
data/.env.example ADDED
@@ -0,0 +1 @@
1
+ ACCESS_TOKEN=
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in lemonsqueezy.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+ gem "dotenv"
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lemonsqueezy (0.1.0)
5
+ faraday (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ dotenv (2.7.6)
11
+ faraday (2.5.2)
12
+ faraday-net_http (>= 2.0, < 3.1)
13
+ ruby2_keywords (>= 0.0.4)
14
+ faraday-net_http (3.0.0)
15
+ minitest (5.15.0)
16
+ rake (13.0.6)
17
+ ruby2_keywords (0.0.5)
18
+
19
+ PLATFORMS
20
+ x86_64-linux
21
+
22
+ DEPENDENCIES
23
+ dotenv
24
+ lemonsqueezy!
25
+ minitest (~> 5.0)
26
+ rake (~> 13.0)
27
+
28
+ BUNDLED WITH
29
+ 2.3.22
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # LemonSqueezy
2
+
3
+ This is a Ruby library for interacting with the Lemon Squeezy API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "lemonsqueezy"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Set Access Token
16
+
17
+ Firstly you'll need to create an API Access Token on your
18
+ [settings page](https://app.lemonsqueezy.com/settings/api)
19
+
20
+ ```ruby
21
+ @client = LemonSqueezy::Client.new(access_token: "")
22
+ ```
23
+
24
+ ### Stores
25
+
26
+ ```ruby
27
+ # Retrieves a list of Stores
28
+ @client.stores.list
29
+
30
+ # Retrieves a Store
31
+ @client.stores.get(id: "123")
32
+ ```
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/deanpcmad/lemonsqueezy.
37
+
38
+ ## License
39
+
40
+ 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,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,20 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "lemon_squeezy"
6
+
7
+ # Load environment variables from .env file
8
+ require 'dotenv/load'
9
+
10
+ # You can add fixtures and/or initialization code here to make experimenting
11
+ # with your gem easier. You can also use a different console, if you like.
12
+
13
+ # (If you use this, don't forget to add pry to your Gemfile!)
14
+ # require "pry"
15
+ # Pry.start
16
+
17
+ @client = LemonSqueezy::Client.new(access_token: ENV["ACCESS_TOKEN"])
18
+
19
+ require "irb"
20
+ 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/lemon_squeezy/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lemonsqueezy"
7
+ spec.version = LemonSqueezy::VERSION
8
+ spec.authors = ["Dean Perry"]
9
+ spec.email = ["dean@deanpcmad.com"]
10
+
11
+ spec.summary = "Ruby library for interacting with the Lemon Squeezy API"
12
+ spec.homepage = "https://github.com/deanpcmad/lemonsqueezy"
13
+ spec.required_ruby_version = ">= 2.6.0"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/deanpcmad/lemonsqueezy"
17
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
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", "~> 2.0"
31
+ end
@@ -0,0 +1,31 @@
1
+ module LemonSqueezy
2
+ class Client
3
+ BASE_URL = "https://api.lemonsqueezy.com/v1"
4
+
5
+ attr_reader :access_token, :adapter
6
+
7
+ def initialize(access_token:, adapter: Faraday.default_adapter, stubs: nil)
8
+ @access_token = access_token
9
+ @adapter = adapter
10
+
11
+ # Test stubs for requests
12
+ @stubs = stubs
13
+ end
14
+
15
+ def stores
16
+ StoresResource.new(self)
17
+ end
18
+
19
+ def connection
20
+ @connection ||= Faraday.new(BASE_URL) do |conn|
21
+ conn.request :authorization, :Bearer, access_token
22
+
23
+ conn.request :json
24
+
25
+ conn.response :json, content_type: "application/vnd.api+json"
26
+
27
+ conn.adapter adapter, @stubs
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module LemonSqueezy
2
+ class Collection
3
+ attr_reader :data, :total
4
+
5
+ def self.from_response(response, type:, key: nil)
6
+ body = response.body
7
+
8
+ if key.is_a?(String)
9
+ data = body["data"][key].map { |attrs| type.new(attrs) }
10
+ total = body["data"]["total"]
11
+ else
12
+ data = body["data"].map { |attrs| type.new(attrs) }
13
+ total = body["data"].count
14
+ end
15
+
16
+ new(
17
+ data: data,
18
+ total: total
19
+ )
20
+ end
21
+
22
+ def initialize(data:, total:)
23
+ @data = data
24
+ @total = total
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ module LemonSqueezy
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+
3
+ module LemonSqueezy
4
+ class Object < OpenStruct
5
+ def initialize(attributes)
6
+ super to_ostruct(attributes)
7
+ end
8
+
9
+ def to_ostruct(obj)
10
+ if obj.is_a?(Hash)
11
+ OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
12
+ elsif obj.is_a?(Array)
13
+ obj.map { |o| to_ostruct(o) }
14
+ else # Assumed to be a primitive value
15
+ obj
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module LemonSqueezy
2
+ class Store < Object
3
+
4
+ def initialize(options = {})
5
+ options.delete "relationships"
6
+ options.delete "links"
7
+
8
+ super options
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,60 @@
1
+ module LemonSqueezy
2
+ class Resource
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ private
10
+
11
+ def get_request(url, params: {}, headers: {})
12
+ handle_response client.connection.get(url, params, headers)
13
+ end
14
+
15
+ def post_request(url, body: {}, headers: {})
16
+ handle_response client.connection.post(url, body, headers)
17
+ end
18
+
19
+ def patch_request(url, body:, headers: {})
20
+ handle_response client.connection.patch(url, body, headers)
21
+ end
22
+
23
+ def put_request(url, body:, headers: {})
24
+ handle_response client.connection.put(url, body, headers)
25
+ end
26
+
27
+ def delete_request(url, params: {}, headers: {})
28
+ handle_response client.connection.delete(url, params, headers)
29
+ end
30
+
31
+ def handle_response(response)
32
+ case response.status
33
+ when 400
34
+ raise Error, "Error 400: Your request was malformed. '#{response.body["message"]}'"
35
+ when 401
36
+ raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["error"]}'"
37
+ when 403
38
+ raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["error"]}'"
39
+ when 404
40
+ raise Error, "Error 404: No results were found for your request. '#{response.body["error"]}'"
41
+ when 409
42
+ raise Error, "Error 409: Your request was a conflict. '#{response.body["message"]}'"
43
+ when 429
44
+ raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["error"]}'"
45
+ when 500
46
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["error"]}'"
47
+ when 503
48
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["error"]}'"
49
+ when 501
50
+ raise Error, "Error 501: This resource has not been implemented. '#{response.body["error"]}'"
51
+ end
52
+
53
+ if response.body && response.body["error"]
54
+ raise Error, "Error #{response.body["error"]["code"]} - #{response.body["error"]["message"]}"
55
+ end
56
+
57
+ response
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,16 @@
1
+ module LemonSqueezy
2
+ class StoresResource < Resource
3
+
4
+ def list
5
+ response = get_request("stores")
6
+ # response.body["data"]
7
+ Collection.from_response(response, type: Store)
8
+ end
9
+
10
+ def get(id:)
11
+ response = get_request("stores/#{id}")
12
+ Store.new(response.body["data"]) if response.success?
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LemonSqueezy
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+
5
+ require_relative "lemon_squeezy/version"
6
+
7
+ module LemonSqueezy
8
+
9
+ autoload :Client, "lemon_squeezy/client"
10
+ autoload :Collection, "lemon_squeezy/collection"
11
+ autoload :Error, "lemon_squeezy/error"
12
+ autoload :Resource, "lemon_squeezy/resource"
13
+ autoload :Object, "lemon_squeezy/object"
14
+
15
+ autoload :StoresResource, "lemon_squeezy/resources/stores"
16
+
17
+ autoload :Store, "lemon_squeezy/objects/store"
18
+
19
+ end
@@ -0,0 +1 @@
1
+ require "lemon_squeezy"
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lemonsqueezy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dean Perry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-02 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: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description:
28
+ email:
29
+ - dean@deanpcmad.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".env.example"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - README.md
38
+ - Rakefile
39
+ - bin/console
40
+ - bin/setup
41
+ - lemonsqueezy.gemspec
42
+ - lib/lemon_squeezy.rb
43
+ - lib/lemon_squeezy/client.rb
44
+ - lib/lemon_squeezy/collection.rb
45
+ - lib/lemon_squeezy/error.rb
46
+ - lib/lemon_squeezy/object.rb
47
+ - lib/lemon_squeezy/objects/store.rb
48
+ - lib/lemon_squeezy/resource.rb
49
+ - lib/lemon_squeezy/resources/stores.rb
50
+ - lib/lemon_squeezy/version.rb
51
+ - lib/lemonsqueezy.rb
52
+ homepage: https://github.com/deanpcmad/lemonsqueezy
53
+ licenses: []
54
+ metadata:
55
+ homepage_uri: https://github.com/deanpcmad/lemonsqueezy
56
+ source_code_uri: https://github.com/deanpcmad/lemonsqueezy
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.6.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.3.7
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Ruby library for interacting with the Lemon Squeezy API
76
+ test_files: []