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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca29faa717ccfdc417a761a105fa39b1a3ee8ca7d61f03b1ac9b83f1f2a0785a
4
- data.tar.gz: 1480cfb125ae58d1295bdb0aca4603ca8440309314079b23b96dd79fd6162623
3
+ metadata.gz: '0900634c79d4767c4035474bf0c842e8a08f4ae6a967d8111e2c20b0da65321d'
4
+ data.tar.gz: 65fe9091c594652c165f771c72dd763b0496163eac3d2bc6d5fb6b26a2ed2d85
5
5
  SHA512:
6
- metadata.gz: 26e001c1de7c812783602d68d821999e874cbf46301810883e953773f4b4418103b8beede64158fd3e521cea0eb9d0101bc8339657211389daa913f3b7d93337
7
- data.tar.gz: 00faf6c801928c90bcbbf6e6c21c35960a1f323d50b8494d789c0adead95ef1434e533458436f13fa7dfa5c1cdf7bb74efa52a1cfbc9c4d60dfc94ae60421e6a
6
+ metadata.gz: 876df20f2d4528f04e0c1dbbde80043879c17dd45a209b4ad7f1ac8a099f50e77df5e4b16b198b3ab14247208df2b8e3eb64794d18f364c96ff6c399610b9223
7
+ data.tar.gz: 78175a9305d0453b2bf755a516a2fbd8a308ca6f2976c946863f6d84d2bd8ebb9d2a36d5c3fb9130ac2551a290d32618def69365e53437635da51fe50d4a1c55
data/.env.example CHANGED
@@ -1 +1,2 @@
1
- ACCESS_TOKEN=
1
+ LEMON_SQUEEZY_API_KEY=""
2
+ LEMON_SQUEEZY_API_KEY_TEST=""
data/Gemfile CHANGED
@@ -8,4 +8,5 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
 
10
10
  gem "minitest", "~> 5.0"
11
- gem "dotenv"
11
+ gem "dotenv"
12
+ gem "vcr"
data/Gemfile.lock CHANGED
@@ -1,20 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lemonsqueezy (0.2.1)
4
+ lemonsqueezy (1.0.0)
5
5
  faraday (~> 2.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- dotenv (2.7.6)
11
- faraday (2.7.4)
12
- faraday-net_http (>= 2.0, < 3.1)
13
- ruby2_keywords (>= 0.0.4)
14
- faraday-net_http (3.0.2)
15
- minitest (5.15.0)
16
- rake (13.0.6)
17
- ruby2_keywords (0.0.5)
10
+ dotenv (2.8.1)
11
+ faraday (2.9.0)
12
+ faraday-net_http (>= 2.0, < 3.2)
13
+ faraday-net_http (3.1.0)
14
+ net-http
15
+ minitest (5.20.0)
16
+ net-http (0.4.1)
17
+ uri
18
+ rake (13.1.0)
19
+ uri (0.13.0)
20
+ vcr (6.2.0)
18
21
 
19
22
  PLATFORMS
20
23
  x86_64-linux
@@ -24,6 +27,7 @@ DEPENDENCIES
24
27
  lemonsqueezy!
25
28
  minitest (~> 5.0)
26
29
  rake (~> 13.0)
30
+ vcr
27
31
 
28
32
  BUNDLED WITH
29
33
  2.3.22
data/README.md CHANGED
@@ -1,207 +1,345 @@
1
- # LemonSqueezy
1
+ # LemonSqueezy Ruby Library
2
2
 
3
- **This Library is a work in progress**
4
-
5
- This is a Ruby library for interacting with the Lemon Squeezy API.
3
+ A Ruby library for the Lemon Squeezy API.
6
4
 
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
9
  ```ruby
12
- gem "lemonsqueezy"
10
+ gem "lemonsqueezy", "~> 1.0"
13
11
  ```
14
12
 
15
13
  ## Usage
16
14
 
17
- ### Set Access Token
15
+ ### Set API Key
16
+
17
+ Firstly you'll need to create an API Key on your [settings page](https://app.lemonsqueezy.com/settings/api).
18
+
19
+ ```ruby
20
+ LemonSqueezy.configure do |config|
21
+ config.api_key = ENV["LEMON_SQUEEZY_API_KEY"]
22
+ end
23
+ ```
18
24
 
19
- Firstly you'll need to create an API Access Token on your [settings page](https://app.lemonsqueezy.com/settings/api).
25
+ ### User
20
26
 
21
27
  ```ruby
22
- @client = LemonSqueezy::Client.new(access_token: "")
28
+ # Retrieve the authenticated user
29
+ LemonSqueezy::User.me
23
30
  ```
24
31
 
25
32
  ### Stores
26
33
 
27
34
  ```ruby
28
35
  # Retrieves a list of Stores
29
- @client.stores.list
36
+ LemonSqueezy::Store.list
30
37
 
31
38
  # Retrieves a Store
32
- @client.stores.get id: "123"
39
+ LemonSqueezy::Store.retrieve id: 123
40
+ ```
41
+
42
+ ### Customers
43
+
44
+ ```ruby
45
+ # Retrieves a list of Customers
46
+ LemonSqueezy::Customer.list
47
+
48
+ # Retrieves a list of Customers for a store
49
+ LemonSqueezy::Customer.list store_id: 123
50
+
51
+ # Retrieves a list of Customers that contain the supplied email
52
+ LemonSqueezy::Customer.list email: "test@hello.com"
53
+
54
+ # Retrieves a Customer
55
+ LemonSqueezy::Customer.retrieve id: 123
56
+
57
+ # Create a customer
58
+ # https://docs.lemonsqueezy.com/api/customers#create-a-customer
59
+ LemonSqueezy::Customer.create store_id: 123, name: "Customer name", email: "test@hello.com"
60
+
61
+ # Update a customer
62
+ # https://docs.lemonsqueezy.com/api/customers#update-a-customer
63
+ LemonSqueezy::Customer.update id: 123, name: "Customer name", status: "archived"
33
64
  ```
34
65
 
35
66
  ### Products
36
67
 
37
68
  ```ruby
38
69
  # Retrieves a list of Products
39
- @client.products.list
70
+ LemonSqueezy::Product.list
40
71
 
41
72
  # Retrieves a list of Products for a specified Store
42
- @client.products.list store_id: 123
73
+ LemonSqueezy::Product.list store_id: 123
43
74
 
44
75
  # Retrieves a Product
45
- @client.products.get id: "123"
76
+ LemonSqueezy::Product.retrieve id: 123
46
77
  ```
47
78
 
48
79
  ### Product Variants
49
80
 
50
81
  ```ruby
51
82
  # Retrieves a list of Variants
52
- @client.variants.list
83
+ LemonSqueezy::Variant.list
53
84
 
54
85
  # Retrieves a list of Variants for a Product
55
- @client.variants.list product_id: 123
86
+ LemonSqueezy::Variant.list product_id: 123
56
87
 
57
88
  # Retrieves a Variant
58
- @client.variants.get id: "123"
89
+ LemonSqueezy::Variant.retrieve id: 123
90
+ ```
91
+
92
+ ### Prices
93
+
94
+ ```ruby
95
+ # Retrieves a list of Prices
96
+ LemonSqueezy::Price.list
97
+
98
+ # Retrieves a list of Prices for a Variant
99
+ LemonSqueezy::Price.list variant_id: 123
100
+
101
+ # Retrieves a Price
102
+ LemonSqueezy::Price.retrieve id: 123
59
103
  ```
60
104
 
61
105
  ### Orders
62
106
 
63
107
  ```ruby
64
108
  # Retrieves a list of Orders
65
- @client.orders.list
109
+ LemonSqueezy::Order.list
66
110
 
67
111
  # Retrieves a list of Orders for a specified store
68
- @client.orders.list product_id: 123
112
+ LemonSqueezy::Order.list product_id: 123
69
113
 
70
114
  # Retrieves a list of Orders for an email address
71
- @client.orders.list email: "hello@test.com"
115
+ LemonSqueezy::Order.list email: "hello@test.com"
72
116
 
73
117
  # Retrieves an Order
74
- @client.orders.get id: "123"
118
+ LemonSqueezy::Order.retrieve id: 123
75
119
 
76
120
  # Retrieves the items on an Order
77
- @client.orders.get order_items: "123"
121
+ LemonSqueezy::Order.retrieve order_items: 123
78
122
  ```
79
123
 
80
124
  ### Subscriptions
81
125
 
82
126
  ```ruby
83
127
  # Retrieves a list of Subscriptions
84
- @client.subscriptions.list
128
+ LemonSqueezy::Subscription.list
85
129
 
86
130
  # Retrieves a list of Subscriptions for a store
87
- @client.subscriptions.list store_id: 123
131
+ LemonSqueezy::Subscription.list store_id: 123
88
132
 
89
133
  # Retrieves a list of Subscriptions for an Order
90
- @client.subscriptions.list order_id: 123
134
+ LemonSqueezy::Subscription.list order_id: 123
91
135
 
92
136
  # Retrieves a list of Subscriptions for a product
93
- @client.subscriptions.list product_id: 123
137
+ LemonSqueezy::Subscription.list product_id: 123
94
138
 
95
139
  # Retrieves a Subscription
96
- @client.subscriptions.get id: 123
140
+ LemonSqueezy::Subscription.retrieve id: 123
141
+
142
+ # Updates a Subscription
143
+ # https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription
144
+ LemonSqueezy::Subscription.update id: 123, variant_id: 123
97
145
 
98
146
  # Pauses a Subscription
99
147
  # Kind should be void or free
100
148
  # resumes_at can be nil or an ISO-8601 formatted date-time string indicating
101
149
  # when the subscription will continue collecting payments.
102
- @client.subscriptions.pause id: 123, kind: "void", resumes_at: nil
150
+ LemonSqueezy::Subscription.pause id: 123, kind: "void", resumes_at: nil
103
151
 
104
152
  # Un-Pause a Subscription
105
- @client.subscriptions.unpause id: 123
153
+ LemonSqueezy::Subscription.unpause id: 123
106
154
 
107
155
  # Cancel a Subscription
108
- @client.subscriptions.cancel id: 123
156
+ LemonSqueezy::Subscription.cancel id: 123
109
157
 
110
158
  # Un-Cancel a Subscription
111
- @client.subscriptions.uncancel id: 123
159
+ LemonSqueezy::Subscription.uncancel id: 123
112
160
 
113
161
  # Change the Plan for a Subscription
114
- @client.subscriptions.change_plan id: 123, plan_id: 111, variant_id: 111
162
+ LemonSqueezy::Subscription.change_plan id: 123, plan_id: 111, variant_id: 111
163
+ ```
164
+
165
+ ### Subscription Invoices
166
+
167
+ ```ruby
168
+ # Retrieves a list of Subscription Invoices
169
+ LemonSqueezy::SubscriptionInvoice.list
170
+
171
+ # Retrieves a list of Subscription Invoices for a specified Store
172
+ LemonSqueezy::SubscriptionInvoice.list store_id: 123
173
+
174
+ # Retrieves a list of Subscription Invoices for a specified Subscription
175
+ LemonSqueezy::SubscriptionInvoice.list subscription_id: 123
176
+
177
+ # Retrieves a Subscription Invoice
178
+ LemonSqueezy::SubscriptionInvoice.retrieve id: 123
179
+ ```
180
+
181
+ ### Subscription Items
182
+
183
+ ```ruby
184
+ # Retrieves a list of Subscription Items
185
+ LemonSqueezy::SubscriptionItem.list
186
+
187
+ # Retrieves a list of Subscription Items for a specified Subscription
188
+ LemonSqueezy::SubscriptionItem.list subscription_id: 123
189
+
190
+ # Retrieves a Subscription Item
191
+ LemonSqueezy::SubscriptionItem.retrieve id: 123
192
+
193
+ # Updates a Subscription Item
194
+ # https://docs.lemonsqueezy.com/api/subscription-items#update-a-subscription-item
195
+ LemonSqueezy::SubscriptionItem.update id: 123, quantity: 2
196
+
197
+ # Retrieve subscription item's current usage
198
+ # Returns 404 if the product/variant does not have usage based billing enabled
199
+ # https://docs.lemonsqueezy.com/api/subscription-items#retrieve-a-subscription-item-s-current-usage
200
+ LemonSqueezy::SubscriptionItem.current_usage id: 123
115
201
  ```
116
202
 
117
203
  ### Discounts
118
204
 
119
205
  ```ruby
120
206
  # Retrieves a list of Discounts
121
- @client.discounts.list
207
+ LemonSqueezy::Discount.list
122
208
 
123
209
  # Retrieves a list of Discounts for a store
124
- @client.discounts.list store_id: 123
210
+ LemonSqueezy::Discount.list store_id: 123
125
211
 
126
212
  # Retrieves a Discount
127
- @client.discounts.get id: "123"
213
+ LemonSqueezy::Discount.retrieve id: 123
214
+
215
+ # Create a Discount
216
+ # https://docs.lemonsqueezy.com/api/discounts#create-a-discount
217
+ LemonSqueezy::Discount.create store_id: 123, name: "20 Off", code: "20OFF", amount: 20, amount_type: "percent"
218
+
219
+ # Delete a Discount
220
+ LemonSqueezy::Discount.delete id: 123
128
221
  ```
129
222
 
130
- ### License Keys
223
+ ### Discount Redemptions
131
224
 
132
225
  ```ruby
133
- # Retrieves a list of License Keys
134
- @client.license_keys.list
226
+ # Retrieves a list of Discount Redemptions
227
+ LemonSqueezy::DiscountRedemption.list
135
228
 
136
- # Retrieves a list of License Keys for a store
137
- @client.license_keys.list store_id: 123
229
+ # Retrieves a list of Discount Redemptions for a Discount
230
+ LemonSqueezy::DiscountRedemption.list discount_id: 123
138
231
 
139
- # Retrieves a list of License Keys for an order
140
- @client.license_keys.list order_id: 123
232
+ # Retrieves a list of Discount Redemptions for a Order
233
+ LemonSqueezy::DiscountRedemption.list order_id: 123
141
234
 
142
- # Retrieves a Discount
143
- @client.license_keys.get id: "123"
235
+ # Retrieves a Discount Redemption
236
+ LemonSqueezy::DiscountRedemption.retrieve id: 123
144
237
  ```
145
238
 
146
- ### Files
239
+ ### Licenses
240
+
241
+ This API is used to manage licenses generated by Lemon Squeezy. [Docs](https://docs.lemonsqueezy.com/help/licensing/license-api)
147
242
 
148
243
  ```ruby
149
- # Retrieves a list of Files
150
- @client.files.list
244
+ # Validate a license key
245
+ LemonSqueezy::License.validate key: "abc123-abc123-abc123"
151
246
 
152
- # Retrieves a File
153
- @client.files.get id: "123"
247
+ # Activate a license key
248
+ # Instance is a label to identify this activation
249
+ LemonSqueezy::License.activate key: "abc123-abc123-abc123", instance: "Activation 1"
250
+
251
+ # Deactivate a license key
252
+ # Instance is the License Key Instance ID
253
+ LemonSqueezy::License.deactivate key: "abc123-abc123-abc123", instance: "abc123"
154
254
  ```
155
255
 
156
- ### Customers
256
+ ### License Keys
157
257
 
158
258
  ```ruby
159
- # Retrieves a list of Customers
160
- @client.customers.list
259
+ # Retrieves a list of License Keys
260
+ LemonSqueezy::LicenseKey.list
161
261
 
162
- # Retrieves a list of Customers for a store
163
- @client.customers.list store_id: 123
262
+ # Retrieves a list of License Keys for a store
263
+ LemonSqueezy::LicenseKey.list store_id: 123
164
264
 
165
- # Retrieves a list of Customers that contain the supplied email
166
- @client.customers.list email: "test@hello.com"
265
+ # Retrieves a list of License Keys for an order
266
+ LemonSqueezy::LicenseKey.list order_id: 123
167
267
 
168
- # Retrieves a Customer
169
- @client.customers.get id: "123"
268
+ # Retrieves a License Key
269
+ LemonSqueezy::LicenseKey.retrieve id: 123
270
+
271
+ # Updates a License Key
272
+ # https://docs.lemonsqueezy.com/api/license-keys#update-a-license-key
273
+ LemonSqueezy::LicenseKey.update id: 123, activation_limit: 10, disabled: false
170
274
  ```
171
275
 
172
276
  ### License Key Instances
173
277
 
174
278
  ```ruby
175
279
  # Retrieves a list of License Key Instances
176
- @client.license_key_instances.list
280
+ LemonSqueezy::LicenseKeyInstances.list
177
281
 
178
282
  # Retrieves a list of License Key Instances for a license key
179
- @client.license_key_instances.list license_key_id: 123
283
+ LemonSqueezy::LicenseKeyInstances.list license_key_id: 123
180
284
 
181
285
  # Retrieves a License Key Instance
182
- @client.license_key_instances.get id: "123"
286
+ LemonSqueezy::LicenseKeyInstances.retrieve id: 123
287
+ ```
288
+
289
+ ### Files
290
+
291
+ ```ruby
292
+ # Retrieves a list of Files
293
+ LemonSqueezy::File.list
294
+
295
+ # Retrieves a File
296
+ LemonSqueezy::File.retrieve id: 123
183
297
  ```
184
298
 
185
299
  ### Checkouts
186
300
 
187
301
  ```ruby
188
302
  # Retrieves a list of Checkouts
189
- @client.checkouts.list
303
+ LemonSqeezy::Checkout.list
190
304
 
191
305
  # Retrieves a list of Checkouts for a store
192
- @client.checkouts.list store_id: 123
306
+ LemonSqeezy::Checkout.list store_id: 123
193
307
 
194
308
  # Retrieves a list of Checkouts for a variant
195
- @client.checkouts.list variant_id: 123
309
+ LemonSqeezy::Checkout.list variant_id: 123
196
310
 
197
311
  # Retrieves a Checkout
198
- @client.checkouts.get id: "123"
312
+ LemonSqeezy::Checkout.retrieve id: 123
199
313
 
200
314
  # Creates a Checkout
201
315
  # View docs for more info: https://docs.lemonsqueezy.com/api/checkouts#create-a-checkout
202
316
  # store_id and variant_id are required
203
317
  # Any other parameters are send as "attributes" to the API
204
- @client.checkouts.create store_id: 123, variant_id: 321, custom_price: 500, product_options: {name: "a test name"}
318
+ LemonSqueezy::Checkout.create store_id: 123, variant_id: 321, custom_price: 500, product_options: {name: "a test name"}
319
+ ```
320
+
321
+ ### Webhooks
322
+
323
+ ```ruby
324
+ # List all Webhooks
325
+ LemonSqueezy::Webhook.list
326
+
327
+ # List all Webhooks for a Store
328
+ LemonSqueezy::Webhook.list store_id: 123
329
+
330
+ # Retrieve a Webhook
331
+ LemonSqueezy::Webhook.retrieve id: 123
332
+
333
+ # Create a Webhook
334
+ # https://docs.lemonsqueezy.com/api/webhooks#create-a-webhook
335
+ LemonSqueezy::Webhook.create store_id: 123, url: "", secret: "abc", events: ["order_created"]
336
+
337
+ # Update a Webhook
338
+ # https://docs.lemonsqueezy.com/api/webhooks#update-a-webhook
339
+ LemonSqueezy::Webhook.update id: 123, events: ["order_created", "subscription_created"]
340
+
341
+ # Delete a Webhook
342
+ LemonSqueezy::Webhook.delete id: 123
205
343
  ```
206
344
 
207
345
 
data/bin/console CHANGED
@@ -14,7 +14,9 @@ require 'dotenv/load'
14
14
  # require "pry"
15
15
  # Pry.start
16
16
 
17
- @client = LemonSqueezy::Client.new(access_token: ENV["ACCESS_TOKEN"])
17
+ LemonSqueezy.configure do |config|
18
+ config.api_key = ENV["LEMON_SQUEEZY_API_KEY"]
19
+ end
18
20
 
19
21
  require "irb"
20
22
  IRB.start(__FILE__)
@@ -1,76 +1,74 @@
1
1
  module LemonSqueezy
2
2
  class Client
3
- BASE_URL = "https://api.lemonsqueezy.com/v1"
4
3
 
5
- attr_reader :access_token, :adapter
4
+ class << self
6
5
 
7
- def initialize(access_token:, adapter: Faraday.default_adapter, stubs: nil)
8
- @access_token = access_token
9
- @adapter = adapter
6
+ def connection
7
+ @connection ||= Faraday.new("https://api.lemonsqueezy.com/v1") do |conn|
8
+ conn.request :authorization, :Bearer, LemonSqueezy.config.api_key
10
9
 
11
- # Test stubs for requests
12
- @stubs = stubs
13
- end
14
-
15
- def stores
16
- StoresResource.new(self)
17
- end
10
+ conn.headers = {
11
+ "User-Agent" => "lemonsqueezy/v#{VERSION} (github.com/deanpcmad/lemonsqueezy)",
12
+ "Accept" => "application/vnd.api+json",
13
+ "Content-Type" => "application/vnd.api+json"
14
+ }
18
15
 
19
- def products
20
- ProductsResource.new(self)
21
- end
16
+ conn.request :json
22
17
 
23
- def variants
24
- VariantsResource.new(self)
25
- end
26
-
27
- def orders
28
- OrdersResource.new(self)
29
- end
30
-
31
- def subscriptions
32
- SubscriptionsResource.new(self)
33
- end
18
+ conn.response :json
19
+ end
20
+ end
34
21
 
35
- def discounts
36
- DiscountsResource.new(self)
37
- end
22
+ def get_request(url, params: {}, headers: {})
23
+ handle_response connection.get(url, params, headers)
24
+ end
38
25
 
39
- def license_keys
40
- LicenseKeysResource.new(self)
41
- end
26
+ def post_request(url, body: {}, headers: {})
27
+ handle_response connection.post(url, body, headers)
28
+ end
42
29
 
43
- def files
44
- FilesResource.new(self)
45
- end
30
+ def patch_request(url, body:, headers: {})
31
+ handle_response connection.patch(url, body, headers)
32
+ end
46
33
 
47
- def customers
48
- CustomersResource.new(self)
49
- end
34
+ def delete_request(url, headers: {})
35
+ handle_response connection.delete(url, headers)
36
+ end
50
37
 
51
- def license_key_instances
52
- LicenseKeyInstancesResource.new(self)
53
- end
38
+ def handle_response(response)
39
+ case response.status
40
+ when 400
41
+ raise Error, "Error 400: Your request was malformed. '#{response.body["errors"]}'"
42
+ when 401
43
+ raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["errors"]}'"
44
+ when 403
45
+ raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["errors"]}'"
46
+ when 404
47
+ raise Error, "Error 404: No results were found for your request. '#{response.body["errors"]}'"
48
+ when 409
49
+ raise Error, "Error 409: Your request was a conflict. '#{response.body["errors"]}'"
50
+ when 429
51
+ raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["errors"]}'"
52
+ when 422
53
+ raise Error, "Error 422: Unprocessable Entity. '#{response.body["errors"]}'"
54
+ when 500
55
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["errors"]}'"
56
+ when 503
57
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["errors"]}'"
58
+ when 501
59
+ raise Error, "Error 501: This resource has not been implemented. '#{response.body["errors"]}'"
60
+ when 204
61
+ return true
62
+ end
63
+
64
+ if response.body && response.body["error"]
65
+ raise Error, "Error #{response.body["error"]["code"]} - #{response.body["errors"]["message"]}"
66
+ end
67
+
68
+ response
69
+ end
54
70
 
55
- def checkouts
56
- CheckoutsResource.new(self)
57
71
  end
58
72
 
59
- def connection
60
- @connection ||= Faraday.new(BASE_URL) do |conn|
61
- conn.request :authorization, :Bearer, access_token
62
-
63
- conn.headers = {
64
- "User-Agent" => "lemonsqueezy/v#{VERSION} (github.com/deanpcmad/lemonsqueezy)",
65
- "Accept" => "application/vnd.api+json",
66
- "Content-Type" => "application/vnd.api+json",
67
- }
68
-
69
- conn.request :json
70
- conn.response :json
71
-
72
- conn.adapter adapter, @stubs
73
- end
74
- end
75
73
  end
76
74
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LemonSqueezy
4
+ class Configuration
5
+
6
+ attr_accessor :api_key
7
+
8
+ def initialize
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,46 @@
1
+ module LemonSqueezy
2
+ class Checkout < Object
3
+
4
+ class << self
5
+
6
+ def list(**params)
7
+ response = Client.get_request("checkouts", params: {filter: params})
8
+ Collection.from_response(response, type: Checkout)
9
+ end
10
+
11
+ def retrieve(id:)
12
+ response = Client.get_request("checkouts/#{id}")
13
+ Checkout.new(response.body["data"]) if response.success?
14
+ end
15
+
16
+ def create(store_id:, variant_id:, **attrs)
17
+ data = {}
18
+
19
+ data["type"] = "checkouts"
20
+
21
+ data["relationships"] = {
22
+ store: {
23
+ data: {
24
+ type: "stores",
25
+ id: store_id.to_s
26
+ }
27
+ },
28
+ variant: {
29
+ data: {
30
+ type: "variants",
31
+ id: variant_id.to_s
32
+ }
33
+ }
34
+ }
35
+
36
+ data["attributes"] = attrs
37
+
38
+ response = Client.post_request("checkouts", body: {data: data}.to_json)
39
+
40
+ Checkout.new(response.body["data"]) if response.success?
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end