polar-ruby 0.1.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 (42) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/CHANGELOG.md +50 -0
  4. data/DEVELOPMENT.md +329 -0
  5. data/EXAMPLES.md +385 -0
  6. data/Gemfile +12 -0
  7. data/Gemfile.lock +115 -0
  8. data/LICENSE +23 -0
  9. data/PROJECT_SUMMARY.md +256 -0
  10. data/README.md +635 -0
  11. data/Rakefile +24 -0
  12. data/examples/demo.rb +106 -0
  13. data/lib/polar/authentication.rb +83 -0
  14. data/lib/polar/client.rb +144 -0
  15. data/lib/polar/configuration.rb +46 -0
  16. data/lib/polar/customer_portal/benefit_grants.rb +41 -0
  17. data/lib/polar/customer_portal/customers.rb +69 -0
  18. data/lib/polar/customer_portal/license_keys.rb +70 -0
  19. data/lib/polar/customer_portal/orders.rb +82 -0
  20. data/lib/polar/customer_portal/subscriptions.rb +51 -0
  21. data/lib/polar/errors.rb +96 -0
  22. data/lib/polar/http_client.rb +150 -0
  23. data/lib/polar/pagination.rb +133 -0
  24. data/lib/polar/resources/base.rb +47 -0
  25. data/lib/polar/resources/benefits.rb +64 -0
  26. data/lib/polar/resources/checkouts.rb +75 -0
  27. data/lib/polar/resources/customers.rb +120 -0
  28. data/lib/polar/resources/events.rb +45 -0
  29. data/lib/polar/resources/files.rb +57 -0
  30. data/lib/polar/resources/license_keys.rb +81 -0
  31. data/lib/polar/resources/metrics.rb +30 -0
  32. data/lib/polar/resources/oauth2.rb +61 -0
  33. data/lib/polar/resources/orders.rb +54 -0
  34. data/lib/polar/resources/organizations.rb +41 -0
  35. data/lib/polar/resources/payments.rb +29 -0
  36. data/lib/polar/resources/products.rb +58 -0
  37. data/lib/polar/resources/subscriptions.rb +55 -0
  38. data/lib/polar/resources/webhooks.rb +81 -0
  39. data/lib/polar/version.rb +5 -0
  40. data/lib/polar/webhooks.rb +174 -0
  41. data/lib/polar.rb +65 -0
  42. metadata +239 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e57f786b73e94872a77124c5865633c7a31b1b88b1e94bfe0bb0e01a1cfe7dec
4
+ data.tar.gz: b046f3c91cedef35d450d9e8029575e10a9b23a6447b8bab2be6d38feaf2afa4
5
+ SHA512:
6
+ metadata.gz: 3cbed2cb510eaf3febbdad873e228692452599abf9755768f1499ea56f66fce9778502e8d281c0aa92998ddc5e6884469a6c1ee63f6af6a9321f75138d28fb41
7
+ data.tar.gz: 4169e320b9884fa6e4308edc3ffc6168869f67225aa13508f178e2a3e7751dd78ac4848e144c0ef65eb77d49404ecc5c7b8466ea026160e2137b95aa64a5ed3d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,50 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2024-10-12
11
+
12
+ ### Added
13
+ - Initial release of Polar Ruby SDK
14
+ - Complete API coverage for Polar.sh API
15
+ - Organization Access Token (OAT) authentication
16
+ - Customer session authentication
17
+ - Core API resources:
18
+ - Organizations
19
+ - Products
20
+ - Customers
21
+ - Orders
22
+ - Payments
23
+ - Subscriptions
24
+ - Checkouts
25
+ - Benefits
26
+ - License Keys
27
+ - Files
28
+ - Metrics
29
+ - Events
30
+ - Webhooks
31
+ - OAuth2
32
+ - Customer Portal API resources:
33
+ - Customer management
34
+ - Order management
35
+ - Subscription management
36
+ - Benefit grants
37
+ - License key operations
38
+ - Comprehensive error handling with specific error classes
39
+ - Automatic pagination support with iterator pattern
40
+ - Webhook signature verification
41
+ - HTTP client with retry logic and exponential backoff
42
+ - Production and sandbox environment support
43
+ - Configurable timeouts and retry policies
44
+ - Debug logging support
45
+ - Ruby 2.7+ compatibility
46
+
47
+ ### Security
48
+ - Secure webhook signature verification using HMAC-SHA256
49
+ - Automatic token validation and format checking
50
+ - Safe string comparison to prevent timing attacks
data/DEVELOPMENT.md ADDED
@@ -0,0 +1,329 @@
1
+ # Polar Ruby SDK Development Guide
2
+
3
+ This guide covers development setup, testing, and contribution guidelines for the Polar Ruby SDK.
4
+
5
+ ## Development Setup
6
+
7
+ ### Prerequisites
8
+
9
+ - Ruby 2.7 or higher
10
+ - Bundler 2.0+
11
+ - Git
12
+
13
+ ### Initial Setup
14
+
15
+ 1. Clone the repository:
16
+ ```bash
17
+ git clone https://github.com/polarsource/polar-ruby.git
18
+ cd polar-ruby
19
+ ```
20
+
21
+ 2. Install dependencies:
22
+ ```bash
23
+ bundle install
24
+ ```
25
+
26
+ 3. Set up environment variables (optional):
27
+ ```bash
28
+ cp .env.example .env
29
+ # Edit .env with your test API keys
30
+ ```
31
+
32
+ 4. Run tests to verify setup:
33
+ ```bash
34
+ bundle exec rake spec
35
+ ```
36
+
37
+ ## Project Structure
38
+
39
+ ```
40
+ polar-ruby/
41
+ ├── lib/
42
+ │ └── polar/
43
+ │ ├── resources/ # Core API resource classes
44
+ │ ├── customer_portal/ # Customer portal API classes
45
+ │ ├── authentication.rb # Authentication classes
46
+ │ ├── client.rb # Main client class
47
+ │ ├── configuration.rb # Configuration management
48
+ │ ├── errors.rb # Error classes
49
+ │ ├── http_client.rb # HTTP client with retry logic
50
+ │ ├── pagination.rb # Pagination support
51
+ │ ├── webhooks.rb # Webhook verification
52
+ │ └── version.rb # Version constant
53
+ ├── spec/ # Test files
54
+ ├── examples/ # Usage examples
55
+ ├── polar-ruby.gemspec # Gem specification
56
+ ├── Gemfile # Development dependencies
57
+ ├── Rakefile # Rake tasks
58
+ └── README.md # Main documentation
59
+ ```
60
+
61
+ ## Testing
62
+
63
+ ### Running Tests
64
+
65
+ ```bash
66
+ # Run all tests
67
+ bundle exec rake spec
68
+
69
+ # Run specific test file
70
+ bundle exec rspec spec/polar/client_spec.rb
71
+
72
+ # Run tests with coverage
73
+ bundle exec rake test_with_coverage
74
+
75
+ # Run quality checks (tests + rubocop)
76
+ bundle exec rake quality
77
+ ```
78
+
79
+ ### Test Structure
80
+
81
+ Tests are organized using RSpec with the following conventions:
82
+
83
+ - Unit tests for each class in `spec/polar/`
84
+ - Integration tests in `spec/integration/`
85
+ - VCR cassettes for HTTP request recording in `spec/vcr_cassettes/`
86
+ - Test helpers and shared examples in `spec/support/`
87
+
88
+ ### Writing Tests
89
+
90
+ Example test structure:
91
+
92
+ ```ruby
93
+ # spec/polar/resources/customers_spec.rb
94
+ RSpec.describe Polar::Resources::Customers do
95
+ let(:client) { create_test_client }
96
+ let(:customers) { client.customers }
97
+
98
+ describe "#list" do
99
+ it "returns paginated customers", :vcr do
100
+ stub_polar_request(:get, "/customers", response_body: {
101
+ data: [{ id: "cust_123", email: "test@example.com" }]
102
+ })
103
+
104
+ result = customers.list
105
+ expect(result).to be_a(Polar::PaginatedResponse)
106
+ end
107
+ end
108
+
109
+ describe "#create" do
110
+ it "creates a customer", :vcr do
111
+ customer_data = {
112
+ email: "new@example.com",
113
+ name: "New Customer",
114
+ organization_id: "org_123"
115
+ }
116
+
117
+ stub_polar_request(:post, "/customers",
118
+ response_body: { id: "cust_456" }.merge(customer_data)
119
+ )
120
+
121
+ result = customers.create(customer_data)
122
+ expect(result["id"]).to eq("cust_456")
123
+ expect(result["email"]).to eq("new@example.com")
124
+ end
125
+ end
126
+ end
127
+ ```
128
+
129
+ ### Test Helpers
130
+
131
+ Common test helpers are available in `spec_helper.rb`:
132
+
133
+ - `create_test_client(options = {})` - Creates a test client instance
134
+ - `stub_polar_request(method, path, ...)` - Stubs HTTP requests to Polar API
135
+
136
+ ### VCR Cassettes
137
+
138
+ For integration tests that make real HTTP requests:
139
+
140
+ 1. Set up test API credentials
141
+ 2. Use `:vcr` metadata on test examples
142
+ 3. Run tests to generate cassettes
143
+ 4. Commit cassettes to version control
144
+
145
+ ```ruby
146
+ it "creates real customer", :vcr do
147
+ # This will record actual HTTP request/response
148
+ customer = client.customers.create(email: "test@example.com")
149
+ expect(customer["id"]).to start_with("cust_")
150
+ end
151
+ ```
152
+
153
+ ## Code Style
154
+
155
+ ### RuboCop
156
+
157
+ The project uses RuboCop for code style enforcement:
158
+
159
+ ```bash
160
+ # Check code style
161
+ bundle exec rubocop
162
+
163
+ # Auto-fix issues
164
+ bundle exec rubocop -A
165
+
166
+ # Check specific files
167
+ bundle exec rubocop lib/polar/client.rb
168
+ ```
169
+
170
+ ### Style Guidelines
171
+
172
+ - Follow standard Ruby conventions
173
+ - Use 2 spaces for indentation
174
+ - Prefer double quotes for strings
175
+ - Add frozen string literal comment to all files
176
+ - Document public methods with YARD format
177
+ - Keep line length under 120 characters
178
+
179
+ ### Documentation
180
+
181
+ Document public methods using YARD syntax:
182
+
183
+ ```ruby
184
+ # Get a customer by ID
185
+ # @param id [String] Customer ID
186
+ # @return [Hash] Customer data
187
+ # @raise [Polar::NotFoundError] If customer doesn't exist
188
+ def get(id)
189
+ response = get("/customers/#{id}")
190
+ response.body
191
+ end
192
+ ```
193
+
194
+ ## Release Process
195
+
196
+ ### Version Management
197
+
198
+ 1. Update version in `lib/polar/version.rb`
199
+ 2. Update `CHANGELOG.md` with changes
200
+ 3. Commit changes
201
+ 4. Create git tag: `git tag v0.1.1`
202
+ 5. Push tag: `git push origin v0.1.1`
203
+
204
+ ### Publishing to RubyGems
205
+
206
+ ```bash
207
+ # Build gem
208
+ bundle exec rake build
209
+
210
+ # Publish to RubyGems (requires credentials)
211
+ bundle exec rake release
212
+ ```
213
+
214
+ ## Contributing
215
+
216
+ ### Pull Request Process
217
+
218
+ 1. Fork the repository
219
+ 2. Create feature branch: `git checkout -b feature/new-feature`
220
+ 3. Make changes with tests
221
+ 4. Run quality checks: `bundle exec rake quality`
222
+ 5. Commit changes with descriptive messages
223
+ 6. Push branch and create pull request
224
+
225
+ ### Commit Messages
226
+
227
+ Use conventional commit format:
228
+
229
+ - `feat: add customer portal support`
230
+ - `fix: handle rate limiting in HTTP client`
231
+ - `docs: update README examples`
232
+ - `test: add webhook verification tests`
233
+ - `refactor: simplify pagination logic`
234
+
235
+ ### Code Review Guidelines
236
+
237
+ - All code must have tests
238
+ - Maintain backward compatibility
239
+ - Follow existing patterns and conventions
240
+ - Update documentation for public API changes
241
+ - Ensure CI passes before merging
242
+
243
+ ## Debugging
244
+
245
+ ### Debug Logging
246
+
247
+ Enable debug logging to see HTTP requests/responses:
248
+
249
+ ```ruby
250
+ client = Polar.new(
251
+ access_token: "your_token",
252
+ debug: true,
253
+ logger: Logger.new(STDOUT)
254
+ )
255
+ ```
256
+
257
+ ### Common Issues
258
+
259
+ **Authentication Errors:**
260
+ - Verify token format starts with `polar_oat_`
261
+ - Check token permissions and expiration
262
+ - Ensure correct environment (production vs sandbox)
263
+
264
+ **Network Errors:**
265
+ - Check internet connection
266
+ - Verify API endpoint URLs
267
+ - Review timeout and retry settings
268
+
269
+ **Rate Limiting:**
270
+ - Implement exponential backoff
271
+ - Reduce request frequency
272
+ - Contact Polar for rate limit increases
273
+
274
+ ### Development Tools
275
+
276
+ Useful development tools:
277
+
278
+ ```bash
279
+ # Interactive console
280
+ bin/console
281
+
282
+ # Generate documentation
283
+ bundle exec yard doc
284
+
285
+ # Check gem dependencies
286
+ bundle outdated
287
+
288
+ # Security audit
289
+ bundle audit
290
+ ```
291
+
292
+ ## Architecture Notes
293
+
294
+ ### HTTP Client Design
295
+
296
+ The HTTP client uses Faraday with:
297
+ - JSON request/response handling
298
+ - Automatic retries with exponential backoff
299
+ - Timeout configuration
300
+ - Rate limiting respect
301
+ - Comprehensive error handling
302
+
303
+ ### Pagination Strategy
304
+
305
+ Pagination follows the iterator pattern:
306
+ - Lazy loading of pages
307
+ - Support for auto-pagination
308
+ - Memory efficient streaming
309
+ - Configurable page sizes
310
+
311
+ ### Error Handling Philosophy
312
+
313
+ - Specific error classes for different scenarios
314
+ - Preserve original HTTP response data
315
+ - Meaningful error messages
316
+ - Consistent error interface
317
+
318
+ ### Security Considerations
319
+
320
+ - Secure webhook signature verification
321
+ - Token validation and format checking
322
+ - Safe string comparison for cryptographic operations
323
+ - No sensitive data in logs (when debug disabled)
324
+
325
+ For questions or help with development, please:
326
+ - Check existing GitHub issues
327
+ - Review documentation
328
+ - Ask in Polar Discord community
329
+ - Create new GitHub issue for bugs/features