lemonsqueezy 0.2.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.env.example +2 -1
  3. data/Gemfile +2 -1
  4. data/Gemfile.lock +13 -9
  5. data/README.md +203 -65
  6. data/bin/console +3 -1
  7. data/lib/lemon_squeezy/client.rb +57 -59
  8. data/lib/lemon_squeezy/configuration.rb +12 -0
  9. data/lib/lemon_squeezy/models/checkout.rb +46 -0
  10. data/lib/lemon_squeezy/models/customer.rb +52 -0
  11. data/lib/lemon_squeezy/models/discount.rb +47 -0
  12. data/lib/lemon_squeezy/models/discount_redemption.rb +19 -0
  13. data/lib/lemon_squeezy/models/file.rb +19 -0
  14. data/lib/lemon_squeezy/models/license.rb +24 -0
  15. data/lib/lemon_squeezy/models/license_key.rb +32 -0
  16. data/lib/lemon_squeezy/models/license_key_instance.rb +19 -0
  17. data/lib/lemon_squeezy/models/order.rb +24 -0
  18. data/lib/lemon_squeezy/models/price.rb +19 -0
  19. data/lib/lemon_squeezy/models/product.rb +19 -0
  20. data/lib/lemon_squeezy/models/store.rb +19 -0
  21. data/lib/lemon_squeezy/models/subscription.rb +99 -0
  22. data/lib/lemon_squeezy/models/subscription_invoice.rb +19 -0
  23. data/lib/lemon_squeezy/models/subscription_item.rb +38 -0
  24. data/lib/lemon_squeezy/models/subscription_usage.rb +4 -0
  25. data/lib/lemon_squeezy/models/user.rb +14 -0
  26. data/lib/lemon_squeezy/models/variant.rb +19 -0
  27. data/lib/lemon_squeezy/models/webhook.rb +58 -0
  28. data/lib/lemon_squeezy/object.rb +15 -1
  29. data/lib/lemon_squeezy/version.rb +1 -1
  30. data/lib/lemon_squeezy.rb +34 -27
  31. metadata +23 -27
  32. data/lib/lemon_squeezy/objects/checkout.rb +0 -12
  33. data/lib/lemon_squeezy/objects/customer.rb +0 -12
  34. data/lib/lemon_squeezy/objects/discount.rb +0 -12
  35. data/lib/lemon_squeezy/objects/file.rb +0 -12
  36. data/lib/lemon_squeezy/objects/license_key.rb +0 -12
  37. data/lib/lemon_squeezy/objects/license_key_instance.rb +0 -12
  38. data/lib/lemon_squeezy/objects/order.rb +0 -12
  39. data/lib/lemon_squeezy/objects/order_item.rb +0 -12
  40. data/lib/lemon_squeezy/objects/product.rb +0 -12
  41. data/lib/lemon_squeezy/objects/store.rb +0 -12
  42. data/lib/lemon_squeezy/objects/subscription.rb +0 -12
  43. data/lib/lemon_squeezy/objects/variant.rb +0 -12
  44. data/lib/lemon_squeezy/resource.rb +0 -60
  45. data/lib/lemon_squeezy/resources/checkouts.rb +0 -42
  46. data/lib/lemon_squeezy/resources/customers.rb +0 -15
  47. data/lib/lemon_squeezy/resources/discounts.rb +0 -15
  48. data/lib/lemon_squeezy/resources/files.rb +0 -15
  49. data/lib/lemon_squeezy/resources/license_key_instances.rb +0 -15
  50. data/lib/lemon_squeezy/resources/license_keys.rb +0 -15
  51. data/lib/lemon_squeezy/resources/orders.rb +0 -20
  52. data/lib/lemon_squeezy/resources/products.rb +0 -15
  53. data/lib/lemon_squeezy/resources/stores.rb +0 -15
  54. data/lib/lemon_squeezy/resources/subscriptions.rb +0 -92
  55. data/lib/lemon_squeezy/resources/variants.rb +0 -15
@@ -0,0 +1,52 @@
1
+ module LemonSqueezy
2
+ class Customer < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("customers", params: {filter: params})
8
+ Collection.from_response(response, type: Customer)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("customers/#{id}")
13
+ Customer.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ def create(store_id:, name:, email:, **params)
17
+ body = {
18
+ data: {
19
+ type: "customers",
20
+ attributes: {
21
+ name: name,
22
+ email: email
23
+ }.merge(params),
24
+ relationships: {
25
+ store: {
26
+ data: {
27
+ type: "stores",
28
+ id: store_id.to_s
29
+ }
30
+ },
31
+ }
32
+ }
33
+ }
34
+ response = Client.post_request("customers", body: body.to_json)
35
+ Customer.new(response.body["data"]) if response.success?
36
+ end
37
+
38
+ def update(id:, **params)
39
+ body = {
40
+ data: {
41
+ type: "customers",
42
+ id: id.to_s,
43
+ attributes: params
44
+ }
45
+ }
46
+ response = Client.patch_request("customers/#{id}", body: body.to_json)
47
+ Customer.new(response.body["data"]) if response.success?
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,47 @@
1
+ module LemonSqueezy
2
+ class Discount < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("discounts", params: {filter: params})
8
+ Collection.from_response(response, type: Discount)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("discounts/#{id}")
13
+ Discount.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ def create(store_id:, name:, code:, amount:, amount_type:, **params)
17
+ body = {
18
+ data: {
19
+ type: "discounts",
20
+ attributes: {
21
+ name: name,
22
+ code: code,
23
+ amount: amount,
24
+ amount_type: amount_type
25
+ }.merge(params),
26
+ relationships: {
27
+ store: {
28
+ data: {
29
+ type: "stores",
30
+ id: store_id.to_s
31
+ }
32
+ },
33
+ }
34
+ }
35
+ }
36
+ response = Client.post_request("discounts", body: body.to_json)
37
+ Discount.new(response.body["data"]) if response.success?
38
+ end
39
+
40
+ def delete(id:)
41
+ Client.delete_request("discounts/#{id}")
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,19 @@
1
+ module LemonSqueezy
2
+ class DiscountRedemption < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("discount-redemptions", params: {filter: params})
8
+ Collection.from_response(response, type: DiscountRedemption)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("discount-redemptions/#{id}")
13
+ DiscountRedemption.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module LemonSqueezy
2
+ class File < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("files", params: {filter: params})
8
+ Collection.from_response(response, type: File)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("files/#{id}")
13
+ File.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ module LemonSqueezy
2
+ class License < Object
3
+
4
+ class << self
5
+
6
+ def validate(key:, instance: nil)
7
+ response = Client.post_request("licenses/validate", body: { license_key: key, instance_id: instance })
8
+ License.new(response.body) if response.success?
9
+ end
10
+
11
+ def activate(key:, instance:)
12
+ response = Client.post_request("licenses/activate", body: { license_key: key, instance_name: instance })
13
+ License.new(response.body) if response.success?
14
+ end
15
+
16
+ def deactivate(key:, instance:)
17
+ response = Client.post_request("licenses/deactivate", body: { license_key: key, instance_id: instance })
18
+ License.new(response.body) if response.success?
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ module LemonSqueezy
2
+ class LicenseKey < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("license-keys", params: {filter: params})
8
+ Collection.from_response(response, type: LicenseKey)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("license-keys/#{id}")
13
+ LicenseKey.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ def update(id:, **attributes)
17
+ body = {
18
+ data: {
19
+ type: "license-keys",
20
+ id: id.to_s,
21
+ attributes: attributes
22
+ }
23
+ }
24
+
25
+ response = Client.patch_request("license-keys/#{id}", body: body.to_json)
26
+ LicenseKey.new(response.body["data"]) if response.success?
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ module LemonSqueezy
2
+ class LicenseKeyInstance < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("license-key-instances", params: {filter: params})
8
+ Collection.from_response(response, type: LicenseKeyInstance)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("license-key-instances/#{id}")
13
+ LicenseKeyInstance.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ module LemonSqueezy
2
+ class Order < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("orders", params: {filter: params})
8
+ Collection.from_response(response, type: Order)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("orders/#{id}")
13
+ Order.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ def order_items(id:)
17
+ response = Client.get_request("order-items", params: {order_id: id})
18
+ Collection.from_response(response, type: OrderItem)
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module LemonSqueezy
2
+ class Price < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("prices", params: {filter: params})
8
+ Collection.from_response(response, type: Price)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("prices/#{id}")
13
+ Price.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module LemonSqueezy
2
+ class Product < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("products", params: {filter: params})
8
+ Collection.from_response(response, type: Product)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("products/#{id}")
13
+ Product.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module LemonSqueezy
2
+ class Store < Object
3
+
4
+ class << self
5
+
6
+ def list
7
+ response = Client.get_request("stores")
8
+ Collection.from_response(response, type: Store)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("stores/#{id}")
13
+ Store.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,99 @@
1
+ module LemonSqueezy
2
+ class Subscription < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("subscriptions", params: {filter: params})
8
+ Collection.from_response(response, type: Subscription)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("subscriptions/#{id}")
13
+ Subscription.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ def update(id:, **attributes)
17
+ body = {
18
+ data: {
19
+ type: "subscriptions",
20
+ id: id.to_s,
21
+ attributes: attributes
22
+ }
23
+ }
24
+
25
+ response = Client.patch_request("subscriptions/#{id}", body: body.to_json)
26
+ Subscription.new(response.body["data"]) if response.success?
27
+ end
28
+
29
+ # Kind: void or free
30
+ def pause(id:, kind:, resumes_at: nil)
31
+ body = {
32
+ data: {
33
+ type: "subscriptions",
34
+ id: id.to_s,
35
+ attributes: {
36
+ pause: {mode: kind, resumes_at: resumes_at}
37
+ }
38
+ }
39
+ }
40
+
41
+ response = Client.patch_request("subscriptions/#{id}", body: body.to_json)
42
+ Subscription.new(response.body["data"]) if response.success?
43
+ end
44
+
45
+ def unpause(id:)
46
+ body = {
47
+ data: {
48
+ type: "subscriptions",
49
+ id: id.to_s,
50
+ attributes: {
51
+ pause: nil
52
+ }
53
+ }
54
+ }
55
+
56
+ response = Client.patch_request("subscriptions/#{id}", body: body.to_json)
57
+ Subscription.new(response.body["data"]) if response.success?
58
+ end
59
+
60
+ def cancel(id:)
61
+ response = Client.delete_request("subscriptions/#{id}")
62
+ Subscription.new(response.body["data"]) if response.success?
63
+ end
64
+
65
+ def uncancel(id:)
66
+ body = {
67
+ data: {
68
+ type: "subscriptions",
69
+ id: id.to_s,
70
+ attributes: {
71
+ cancelled: false
72
+ }
73
+ }
74
+ }
75
+
76
+ response = Client.patch_request("subscriptions/#{id}", body: body.to_json)
77
+ Subscription.new(response.body["data"]) if response.success?
78
+ end
79
+
80
+ def change_plan(id:, plan_id:, variant_id:)
81
+ body = {
82
+ data: {
83
+ type: "subscriptions",
84
+ id: id.to_s,
85
+ attributes: {
86
+ product_id: plan_id,
87
+ variant_id: variant_id
88
+ }
89
+ }
90
+ }
91
+
92
+ response = Client.patch_request("subscriptions/#{id}", body: body.to_json)
93
+ Subscription.new(response.body["data"]) if response.success?
94
+ end
95
+
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,19 @@
1
+ module LemonSqueezy
2
+ class SubscriptionInvoice < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("subscription-invoices", params: {filter: params})
8
+ Collection.from_response(response, type: SubscriptionInvoice)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("subscription-invoices/#{id}")
13
+ SubscriptionInvoice.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ module LemonSqueezy
2
+ class SubscriptionItem < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("subscription-items", params: {filter: params})
8
+ Collection.from_response(response, type: SubscriptionItem)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("subscription-items/#{id}")
13
+ SubscriptionItem.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ def current_usage(id:)
17
+ response = Client.get_request("subscription-items/#{id}/current-usage")
18
+ SubscriptionUsage.new(response.body["meta"]) if response.success?
19
+ end
20
+
21
+ def update(id:, quantity:)
22
+ body = {
23
+ data: {
24
+ type: "subscription-items",
25
+ id: id.to_s,
26
+ attributes: {
27
+ quantity: quantity
28
+ }
29
+ }
30
+ }
31
+ response = Client.patch_request("subscription-items/#{id}", body: body.to_json)
32
+ SubscriptionItem.new(response.body["data"]) if response.success?
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,4 @@
1
+ module LemonSqueezy
2
+ class SubscriptionUsage < Object
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ module LemonSqueezy
2
+ class User < Object
3
+
4
+ class << self
5
+
6
+ def me
7
+ response = Client.get_request("users/me")
8
+ User.new(response.body["data"]) if response.success?
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ module LemonSqueezy
2
+ class Variant < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("variants", params: {filter: params})
8
+ Collection.from_response(response, type: Variant)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("variants/#{id}")
13
+ Variant.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,58 @@
1
+ module LemonSqueezy
2
+ class Webhook < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("webhooks", params: {filter: params})
8
+ Collection.from_response(response, type: Webhook)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("webhooks/#{id}")
13
+ Webhook.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ def create(store_id:, url:, events:, secret:, **params)
17
+ body = {
18
+ data: {
19
+ type: "webhooks",
20
+ attributes: {
21
+ url: url,
22
+ events: events,
23
+ secret: secret
24
+ }.merge(params),
25
+ relationships: {
26
+ store: {
27
+ data: {
28
+ type: "stores",
29
+ id: store_id.to_s
30
+ }
31
+ },
32
+ }
33
+ }
34
+ }
35
+ response = Client.post_request("webhooks", body: body.to_json)
36
+ Webhook.new(response.body["data"]) if response.success?
37
+ end
38
+
39
+ def update(id:, **params)
40
+ body = {
41
+ data: {
42
+ type: "webhooks",
43
+ id: id.to_s,
44
+ attributes: params
45
+ }
46
+ }
47
+ response = Client.patch_request("webhooks/#{id}", body: body.to_json)
48
+ Webhook.new(response.body["data"]) if response.success?
49
+ end
50
+
51
+ def delete(id:)
52
+ Client.delete_request("webhooks/#{id}")
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
@@ -3,7 +3,21 @@ require "ostruct"
3
3
  module LemonSqueezy
4
4
  class Object < OpenStruct
5
5
  def initialize(attributes)
6
- super to_ostruct(attributes)
6
+ # Remove relationships and links from responses as they are not required
7
+ attributes.delete "relationships"
8
+ attributes.delete "links"
9
+
10
+ attrs = {}
11
+
12
+ if attributes["attributes"]
13
+ # Add ID from response to attributes
14
+ attrs[:id] = attributes["id"] if attributes["id"]
15
+ attrs.merge!(attributes["attributes"])
16
+ else
17
+ attrs.merge!(attributes)
18
+ end
19
+
20
+ super to_ostruct(attrs)
7
21
  end
8
22
 
9
23
  def to_ostruct(obj)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LemonSqueezy
4
- VERSION = "0.2.1"
4
+ VERSION = "1.0.0"
5
5
  end
data/lib/lemon_squeezy.rb CHANGED
@@ -5,36 +5,43 @@ require "faraday"
5
5
  require_relative "lemon_squeezy/version"
6
6
 
7
7
  module LemonSqueezy
8
-
8
+
9
+ autoload :Configuration, "lemon_squeezy/configuration"
9
10
  autoload :Client, "lemon_squeezy/client"
10
11
  autoload :Collection, "lemon_squeezy/collection"
11
12
  autoload :Error, "lemon_squeezy/error"
12
- autoload :Resource, "lemon_squeezy/resource"
13
13
  autoload :Object, "lemon_squeezy/object"
14
14
 
15
- autoload :StoresResource, "lemon_squeezy/resources/stores"
16
- autoload :ProductsResource, "lemon_squeezy/resources/products"
17
- autoload :VariantsResource, "lemon_squeezy/resources/variants"
18
- autoload :OrdersResource, "lemon_squeezy/resources/orders"
19
- autoload :SubscriptionsResource, "lemon_squeezy/resources/subscriptions"
20
- autoload :DiscountsResource, "lemon_squeezy/resources/discounts"
21
- autoload :LicenseKeysResource, "lemon_squeezy/resources/license_keys"
22
- autoload :LicenseKeyInstancesResource, "lemon_squeezy/resources/license_key_instances"
23
- autoload :FilesResource, "lemon_squeezy/resources/files"
24
- autoload :CustomersResource, "lemon_squeezy/resources/customers"
25
- autoload :CheckoutsResource, "lemon_squeezy/resources/checkouts"
26
-
27
- autoload :Store, "lemon_squeezy/objects/store"
28
- autoload :Product, "lemon_squeezy/objects/product"
29
- autoload :Variant, "lemon_squeezy/objects/variant"
30
- autoload :Order, "lemon_squeezy/objects/order"
31
- autoload :OrderItem, "lemon_squeezy/objects/order_item"
32
- autoload :Subscription, "lemon_squeezy/objects/subscription"
33
- autoload :Discount, "lemon_squeezy/objects/discount"
34
- autoload :LicenseKey, "lemon_squeezy/objects/license_key"
35
- autoload :LicenseKeyInstance, "lemon_squeezy/objects/license_key_instance"
36
- autoload :File, "lemon_squeezy/objects/file"
37
- autoload :Customer, "lemon_squeezy/objects/customer"
38
- autoload :Checkout, "lemon_squeezy/objects/checkout"
39
-
15
+ class << self
16
+ attr_writer :config
17
+ end
18
+
19
+ def self.configure
20
+ yield(config) if block_given?
21
+ end
22
+
23
+ def self.config
24
+ @config ||= Configuration.new
25
+ end
26
+
27
+ autoload :User, "lemon_squeezy/models/user"
28
+ autoload :Store, "lemon_squeezy/models/store"
29
+ autoload :Product, "lemon_squeezy/models/product"
30
+ autoload :Variant, "lemon_squeezy/models/variant"
31
+ autoload :Order, "lemon_squeezy/models/order"
32
+ autoload :Subscription, "lemon_squeezy/models/subscription"
33
+ autoload :SubscriptionInvoice, "lemon_squeezy/models/subscription_invoice"
34
+ autoload :SubscriptionItem, "lemon_squeezy/models/subscription_item"
35
+ autoload :SubscriptionUsage, "lemon_squeezy/models/subscription_usage"
36
+ autoload :Discount, "lemon_squeezy/models/discount"
37
+ autoload :DiscountRedemption, "lemon_squeezy/models/discount_redemption"
38
+ autoload :License, "lemon_squeezy/models/license"
39
+ autoload :LicenseKey, "lemon_squeezy/models/license_key"
40
+ autoload :LicenseKeyInstance, "lemon_squeezy/models/license_key_instance"
41
+ autoload :File, "lemon_squeezy/models/file"
42
+ autoload :Customer, "lemon_squeezy/models/customer"
43
+ autoload :Checkout, "lemon_squeezy/models/checkout"
44
+ autoload :Price, "lemon_squeezy/models/price"
45
+ autoload :Webhook, "lemon_squeezy/models/webhook"
46
+
40
47
  end