lemonsqueezy 0.2.2 → 1.0.0

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.
Files changed (57) 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 +193 -68
  6. data/bin/console +3 -1
  7. data/lib/lemon_squeezy/client.rb +57 -63
  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 -29
  31. metadata +23 -29
  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/subscription_invoice.rb +0 -12
  44. data/lib/lemon_squeezy/objects/variant.rb +0 -12
  45. data/lib/lemon_squeezy/resource.rb +0 -60
  46. data/lib/lemon_squeezy/resources/checkouts.rb +0 -42
  47. data/lib/lemon_squeezy/resources/customers.rb +0 -15
  48. data/lib/lemon_squeezy/resources/discounts.rb +0 -15
  49. data/lib/lemon_squeezy/resources/files.rb +0 -15
  50. data/lib/lemon_squeezy/resources/license_key_instances.rb +0 -15
  51. data/lib/lemon_squeezy/resources/license_keys.rb +0 -15
  52. data/lib/lemon_squeezy/resources/orders.rb +0 -20
  53. data/lib/lemon_squeezy/resources/products.rb +0 -15
  54. data/lib/lemon_squeezy/resources/stores.rb +0 -15
  55. data/lib/lemon_squeezy/resources/subscription_invoices.rb +0 -15
  56. data/lib/lemon_squeezy/resources/subscriptions.rb +0 -92
  57. data/lib/lemon_squeezy/resources/variants.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d3696e1ba538e89d4ea65a77f90a20c024f79790700981191437e09c22bf183
4
- data.tar.gz: 11186062f312505a6dbe326c9ff27a8c940573475f788b5d44cabbb3e27c95ac
3
+ metadata.gz: '0900634c79d4767c4035474bf0c842e8a08f4ae6a967d8111e2c20b0da65321d'
4
+ data.tar.gz: 65fe9091c594652c165f771c72dd763b0496163eac3d2bc6d5fb6b26a2ed2d85
5
5
  SHA512:
6
- metadata.gz: 47b685c7e6d41eaf056789dbdfbe58b2e080626cc20fe2fc997d78cc16d95545547d25efd1b54fbc51fc7c296cc6bd532b95a6193b94a43d97e2c4749bf44057
7
- data.tar.gz: 1894f19062e99ea38437dd0fc9c2691e90148c2974d8ca7390f1daf8a96de1291d455551a97f29b64cbf6f7dc955b6bc444bd4863b77a422f8e74281b826b9c3
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,220 +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
115
163
  ```
116
164
 
117
165
  ### Subscription Invoices
118
166
 
119
167
  ```ruby
120
168
  # Retrieves a list of Subscription Invoices
121
- @client.subscription_invoices.list
169
+ LemonSqueezy::SubscriptionInvoice.list
122
170
 
123
171
  # Retrieves a list of Subscription Invoices for a specified Store
124
- @client.subscription_invoices.list store_id: 123
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
125
176
 
126
177
  # Retrieves a Subscription Invoice
127
- @client.subscription_invoices.get id: "123"
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
128
201
  ```
129
202
 
130
203
  ### Discounts
131
204
 
132
205
  ```ruby
133
206
  # Retrieves a list of Discounts
134
- @client.discounts.list
207
+ LemonSqueezy::Discount.list
135
208
 
136
209
  # Retrieves a list of Discounts for a store
137
- @client.discounts.list store_id: 123
210
+ LemonSqueezy::Discount.list store_id: 123
138
211
 
139
212
  # Retrieves a Discount
140
- @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
141
221
  ```
142
222
 
143
- ### License Keys
223
+ ### Discount Redemptions
144
224
 
145
225
  ```ruby
146
- # Retrieves a list of License Keys
147
- @client.license_keys.list
226
+ # Retrieves a list of Discount Redemptions
227
+ LemonSqueezy::DiscountRedemption.list
148
228
 
149
- # Retrieves a list of License Keys for a store
150
- @client.license_keys.list store_id: 123
229
+ # Retrieves a list of Discount Redemptions for a Discount
230
+ LemonSqueezy::DiscountRedemption.list discount_id: 123
151
231
 
152
- # Retrieves a list of License Keys for an order
153
- @client.license_keys.list order_id: 123
232
+ # Retrieves a list of Discount Redemptions for a Order
233
+ LemonSqueezy::DiscountRedemption.list order_id: 123
154
234
 
155
- # Retrieves a Discount
156
- @client.license_keys.get id: "123"
235
+ # Retrieves a Discount Redemption
236
+ LemonSqueezy::DiscountRedemption.retrieve id: 123
157
237
  ```
158
238
 
159
- ### 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)
160
242
 
161
243
  ```ruby
162
- # Retrieves a list of Files
163
- @client.files.list
244
+ # Validate a license key
245
+ LemonSqueezy::License.validate key: "abc123-abc123-abc123"
164
246
 
165
- # Retrieves a File
166
- @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"
167
254
  ```
168
255
 
169
- ### Customers
256
+ ### License Keys
170
257
 
171
258
  ```ruby
172
- # Retrieves a list of Customers
173
- @client.customers.list
259
+ # Retrieves a list of License Keys
260
+ LemonSqueezy::LicenseKey.list
174
261
 
175
- # Retrieves a list of Customers for a store
176
- @client.customers.list store_id: 123
262
+ # Retrieves a list of License Keys for a store
263
+ LemonSqueezy::LicenseKey.list store_id: 123
177
264
 
178
- # Retrieves a list of Customers that contain the supplied email
179
- @client.customers.list email: "test@hello.com"
265
+ # Retrieves a list of License Keys for an order
266
+ LemonSqueezy::LicenseKey.list order_id: 123
180
267
 
181
- # Retrieves a Customer
182
- @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
183
274
  ```
184
275
 
185
276
  ### License Key Instances
186
277
 
187
278
  ```ruby
188
279
  # Retrieves a list of License Key Instances
189
- @client.license_key_instances.list
280
+ LemonSqueezy::LicenseKeyInstances.list
190
281
 
191
282
  # Retrieves a list of License Key Instances for a license key
192
- @client.license_key_instances.list license_key_id: 123
283
+ LemonSqueezy::LicenseKeyInstances.list license_key_id: 123
193
284
 
194
285
  # Retrieves a License Key Instance
195
- @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
196
297
  ```
197
298
 
198
299
  ### Checkouts
199
300
 
200
301
  ```ruby
201
302
  # Retrieves a list of Checkouts
202
- @client.checkouts.list
303
+ LemonSqeezy::Checkout.list
203
304
 
204
305
  # Retrieves a list of Checkouts for a store
205
- @client.checkouts.list store_id: 123
306
+ LemonSqeezy::Checkout.list store_id: 123
206
307
 
207
308
  # Retrieves a list of Checkouts for a variant
208
- @client.checkouts.list variant_id: 123
309
+ LemonSqeezy::Checkout.list variant_id: 123
209
310
 
210
311
  # Retrieves a Checkout
211
- @client.checkouts.get id: "123"
312
+ LemonSqeezy::Checkout.retrieve id: 123
212
313
 
213
314
  # Creates a Checkout
214
315
  # View docs for more info: https://docs.lemonsqueezy.com/api/checkouts#create-a-checkout
215
316
  # store_id and variant_id are required
216
317
  # Any other parameters are send as "attributes" to the API
217
- @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
218
343
  ```
219
344
 
220
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,80 +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
18
-
19
- def products
20
- ProductsResource.new(self)
21
- end
22
-
23
- def variants
24
- VariantsResource.new(self)
25
- 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
+ }
26
15
 
27
- def orders
28
- OrdersResource.new(self)
29
- end
30
-
31
- def subscriptions
32
- SubscriptionsResource.new(self)
33
- end
16
+ conn.request :json
34
17
 
35
- def subscription_invoices
36
- SubscriptionInvoicesResource.new(self)
37
- end
18
+ conn.response :json
19
+ end
20
+ end
38
21
 
39
- def discounts
40
- DiscountsResource.new(self)
41
- end
22
+ def get_request(url, params: {}, headers: {})
23
+ handle_response connection.get(url, params, headers)
24
+ end
42
25
 
43
- def license_keys
44
- LicenseKeysResource.new(self)
45
- end
26
+ def post_request(url, body: {}, headers: {})
27
+ handle_response connection.post(url, body, headers)
28
+ end
46
29
 
47
- def files
48
- FilesResource.new(self)
49
- end
30
+ def patch_request(url, body:, headers: {})
31
+ handle_response connection.patch(url, body, headers)
32
+ end
50
33
 
51
- def customers
52
- CustomersResource.new(self)
53
- end
34
+ def delete_request(url, headers: {})
35
+ handle_response connection.delete(url, headers)
36
+ end
54
37
 
55
- def license_key_instances
56
- LicenseKeyInstancesResource.new(self)
57
- 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
58
70
 
59
- def checkouts
60
- CheckoutsResource.new(self)
61
71
  end
62
72
 
63
- def connection
64
- @connection ||= Faraday.new(BASE_URL) do |conn|
65
- conn.request :authorization, :Bearer, access_token
66
-
67
- conn.headers = {
68
- "User-Agent" => "lemonsqueezy/v#{VERSION} (github.com/deanpcmad/lemonsqueezy)",
69
- "Accept" => "application/vnd.api+json",
70
- "Content-Type" => "application/vnd.api+json",
71
- }
72
-
73
- conn.request :json
74
- conn.response :json
75
-
76
- conn.adapter adapter, @stubs
77
- end
78
- end
79
73
  end
80
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