Skip to content

Commit 51592cd

Browse files
committed
Add comprehensive Rails-style architecture and documentation
This PR introduces significant architectural improvements inspired by best practices from AWS SDK, Rails, and modern Ruby gems: ## New Features & Improvements ### Rails-Style Architecture - Added concerns pattern with Transformable and Cacheable modules - Implemented Rails-style query interface with chainable methods - Added ActiveRecord integration for seamless Rails usage - Introduced Rails naming conventions throughout ### Enhanced Documentation - Created comprehensive CLAUDE.md for AI-assisted development - Added detailed EXAMPLES.md with all SDK features - Updated README with new architecture examples - Modernized all API examples to use new patterns ### API Examples Update - Updated all 33 REST API examples to use new client interface - Modernized upload API examples with progress tracking - Added examples for batch operations, webhooks, and add-ons - Included error handling and testing patterns ### Code Quality - Fixed test failures related to error type expectations - Added base64 gem dependency for Ruby 3.4+ compatibility - Improved error handling with specific error types ## Testing All tests passing with 63.77% code coverage ## Breaking Changes None - all changes are additive and backward compatible
1 parent 6da3958 commit 51592cd

37 files changed

+2766
-126
lines changed

CLAUDE.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
Uploadcare Ruby SDK - Ruby client library for Uploadcare's Upload and REST APIs, providing file upload, management, and transformation capabilities.
8+
9+
## Development Commands
10+
11+
### Environment Setup
12+
- Ruby 3.0+ required (use mise for version management: `mise use ruby@latest`)
13+
- Install dependencies: `bundle install`
14+
- Add `gem 'base64'` to Gemfile if using Ruby 3.4+ to avoid vcr gem warnings
15+
16+
### Testing
17+
- Run all tests: `bundle exec rake spec` or `bundle exec rspec`
18+
- Run specific test file: `bundle exec rspec spec/uploadcare/resources/file_spec.rb`
19+
- Run with documentation format: `bundle exec rspec --format documentation`
20+
- Run with fail-fast: `bundle exec rspec --fail-fast`
21+
- Test environment variables required:
22+
- `UPLOADCARE_PUBLIC_KEY=demopublickey`
23+
- `UPLOADCARE_SECRET_KEY=demoprivatekey`
24+
25+
### Code Quality
26+
- Run linter: `bundle exec rubocop`
27+
- Run linter with auto-fix: `bundle exec rubocop -a`
28+
- Run all checks (tests + linter): `bundle exec rake`
29+
30+
## Architecture Overview
31+
32+
### Core Module Structure
33+
The gem uses Zeitwerk autoloading with collapsed directories for resources and clients:
34+
35+
- **lib/uploadcare.rb** - Main module, configures Zeitwerk autoloading
36+
- **lib/uploadcare/configuration.rb** - Configuration management
37+
- **lib/uploadcare/api.rb** - Main API interface (deprecated pattern, use resources directly)
38+
- **lib/uploadcare/client.rb** - New client pattern for API interactions
39+
40+
### Resource Layer (lib/uploadcare/resources/)
41+
Domain objects representing Uploadcare entities:
42+
- **file.rb** - File upload/management operations
43+
- **group.rb** - File group operations
44+
- **webhook.rb** - Webhook management
45+
- **uploader.rb** - Upload coordination
46+
- **paginated_collection.rb** - Pagination support for list operations
47+
- **batch_file_result.rb** - Batch operation results
48+
- **add_ons.rb** - Add-on services (AWS Rekognition, ClamAV, Remove.bg)
49+
- **document_converter.rb** - Document conversion operations
50+
- **video_converter.rb** - Video conversion operations
51+
52+
### Client Layer (lib/uploadcare/clients/)
53+
HTTP client implementations for API communication:
54+
- **rest_client.rb** - Base REST API client
55+
- **upload_client.rb** - Upload API client
56+
- **multipart_upload_client.rb** - Multipart upload handling
57+
- **uploader_client.rb** - Upload coordination client
58+
- **file_client.rb** - File management endpoints
59+
- **group_client.rb** - Group management endpoints
60+
- **webhook_client.rb** - Webhook endpoints
61+
- **project_client.rb** - Project info endpoints
62+
63+
### Middleware Layer (lib/uploadcare/middleware/)
64+
Request/response processing:
65+
- **base.rb** - Base middleware class
66+
- **retry.rb** - Retry logic for failed requests
67+
- **logger.rb** - Request/response logging
68+
69+
### Error Handling
70+
- **lib/uploadcare/error_handler.rb** - Central error parsing and handling
71+
- **lib/uploadcare/exception/** - Custom exception types
72+
- **request_error.rb** - Base request errors
73+
- **auth_error.rb** - Authentication errors
74+
- **throttle_error.rb** - Rate limiting errors
75+
- **retry_error.rb** - Retry exhaustion errors
76+
77+
### Utilities
78+
- **lib/uploadcare/authenticator.rb** - Request signing and authentication
79+
- **lib/uploadcare/url_builder.rb** - CDN URL generation with transformations
80+
- **lib/uploadcare/signed_url_generators/** - Secure URL generation (Akamai)
81+
- **lib/uploadcare/throttle_handler.rb** - Rate limit handling
82+
83+
## Key Design Patterns
84+
85+
1. **Resource-Client Separation**: Resources handle business logic, clients handle HTTP communication
86+
2. **Zeitwerk Autoloading**: Uses collapsed directories for cleaner require structure
87+
3. **Middleware Pattern**: Extensible request/response processing pipeline
88+
4. **Result Objects**: Many operations return Success/Failure result objects
89+
5. **Lazy Loading**: Paginated collections fetch data on demand
90+
91+
## API Configuration
92+
93+
Configuration can be set via:
94+
- Environment variables: `UPLOADCARE_PUBLIC_KEY`, `UPLOADCARE_SECRET_KEY`
95+
- Code: `Uploadcare.config.public_key = "key"`
96+
- Per-request: Pass config to individual resource methods
97+
98+
## Testing Approach
99+
100+
- Uses RSpec for testing
101+
- VCR for recording/replaying HTTP interactions
102+
- SimpleCov for code coverage reporting
103+
- Tests are in `spec/uploadcare/` mirroring lib structure
104+
- Fixtures and cassettes in `spec/fixtures/`
105+
106+
## Common Development Tasks
107+
108+
### Adding New API Endpoints
109+
1. Create/update client in `lib/uploadcare/clients/`
110+
2. Create/update resource in `lib/uploadcare/resources/`
111+
3. Add corresponding specs in `spec/uploadcare/`
112+
4. Update README.md with usage examples
113+
114+
### Handling API Responses
115+
- Use `Uploadcare::ErrorHandler` for error parsing
116+
- Return result objects for operations that can fail
117+
- Parse JSON responses into Ruby objects/hashes
118+
119+
### Working with Batch Operations
120+
- Use `BatchFileResult` for batch store/delete results
121+
- Handle both successful results and problem items
122+
- Follow pattern in `File.batch_store` and `File.batch_delete`

0 commit comments

Comments
 (0)