|
| 1 | +# Happi - AI Coding Instructions |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +Happi is a lightweight Ruby gem that provides a pre-configured Faraday HTTP client wrapper for RESTful APIs. It assumes URL patterns like `https://hostname.com/api/v1/something` and handles OAuth2 authentication, multipart file uploads, and JSON/form-encoded requests. |
| 5 | + |
| 6 | +## Architecture & Design Patterns |
| 7 | + |
| 8 | +### Class-Based Configuration (Critical) |
| 9 | +**Never use `Happi::Client` directly** - always create a derived class. Configuration is stored as class-level state, so using the base class directly causes cross-contamination between different API endpoints. |
| 10 | + |
| 11 | +```ruby |
| 12 | +# CORRECT: Derive your own client |
| 13 | +class MyClient < Happi::Client; end |
| 14 | +MyClient.configure { |config| config.host = 'http://api.example.com' } |
| 15 | + |
| 16 | +# WRONG: Never use base class directly |
| 17 | +Happi::Client.configure { ... } # Will pollute all clients |
| 18 | +``` |
| 19 | + |
| 20 | +This pattern is tested extensively in [spec/client_spec.rb](spec/client_spec.rb#L5-L85) which validates config isolation between base/derived classes at both class and instance levels. |
| 21 | + |
| 22 | +### Configuration Hierarchy |
| 23 | +Config precedence: instance-level > class-level > defaults (from `Happi::Configuration.defaults`). |
| 24 | + |
| 25 | +Instance config is set via initializer: `MyClient.new(oauth_token: 'abc123', timeout: 30)` |
| 26 | + |
| 27 | +## Key Components |
| 28 | + |
| 29 | +- **[lib/happi/client.rb](lib/happi/client.rb)**: Main HTTP client with REST verbs (get/post/patch/delete), error handling, OAuth2 middleware integration |
| 30 | +- **[lib/happi/configuration.rb](lib/happi/configuration.rb)**: Config object with defaults for host, port, timeout, version, use_json, log_level, token_type |
| 31 | +- **[lib/happi/file.rb](lib/happi/file.rb)**: Handles file uploads with MIME type detection, converts to Faraday::UploadIO for multipart requests |
| 32 | +- **[lib/happi/error.rb](lib/happi/error.rb)**: HTTP error hierarchy mapping status codes (400-504) to specific exception classes |
| 33 | + |
| 34 | +## Developer Workflows |
| 35 | + |
| 36 | +### Testing |
| 37 | +```bash |
| 38 | +# Run all specs |
| 39 | +bundle exec rspec |
| 40 | + |
| 41 | +# Run specific spec file |
| 42 | +bundle exec rspec spec/client_spec.rb |
| 43 | +``` |
| 44 | + |
| 45 | +RSpec is configured with SimpleCov for coverage reports (see [coverage/index.html](coverage/index.html)). |
| 46 | + |
| 47 | +### Gemfile Variations |
| 48 | +- `Gemfile`: Standard dependencies |
| 49 | +- `Gemfile.rails32`, `Gemfile.rails41`: Legacy Rails compatibility testing (historical, not actively maintained) |
| 50 | + |
| 51 | +## Critical Conventions |
| 52 | + |
| 53 | +### Multipart File Handling |
| 54 | +When a hash value responds to `#multipart`, it's automatically converted by `param_check` method. This enables seamless file uploads: |
| 55 | + |
| 56 | +```ruby |
| 57 | +client.post('templates', template: { |
| 58 | + name: 'test', |
| 59 | + file: Happi::File.new('/path/to/file.docx') # Automatically becomes multipart |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +### JSON vs Form Encoding |
| 64 | +Controlled by `config.use_json` flag: |
| 65 | +- `false` (default): Uses multipart/form-encoded requests |
| 66 | +- `true`: Encodes requests as JSON and parses JSON responses |
| 67 | + |
| 68 | +### OAuth2 Token Types |
| 69 | +Set `token_type: 'bearer'` to pass OAuth tokens only as Authorization header (not as query param). This avoids faraday_middleware warnings. See [CHANGELOG.md](CHANGELOG.md#L30) for context. |
| 70 | + |
| 71 | +### Logging Behavior |
| 72 | +- `log_level: :debug` logs full request bodies/params (can generate large logs) |
| 73 | +- `log_level: :info` (default) logs only HTTP method and URL |
| 74 | +- Rails logger auto-detected via `Rails.try(:logger)`, falls back to STDOUT |
| 75 | + |
| 76 | +### URL Construction |
| 77 | +URLs automatically prefixed with `/api/#{version}/`. The `version` config defaults to `'v1'`. Example: |
| 78 | +```ruby |
| 79 | +client.get('templates') # Requests /api/v1/templates |
| 80 | +``` |
| 81 | + |
| 82 | +## Dependencies |
| 83 | +- **faraday ~> 2.13**: Core HTTP client |
| 84 | +- **oauth2 ~> 2.0**: Authentication (via FaradayMiddleware::OAuth2) |
| 85 | +- **activemodel >= 6.0**: Provides `with_indifferent_access` for response hashes |
| 86 | +- **mime-types**: File MIME type detection |
| 87 | +- Ruby >= 3.2.0 |
| 88 | + |
| 89 | +## Testing Patterns |
| 90 | +Specs extensively test configuration isolation, multipart param checking, and connection options. When adding features: |
| 91 | +1. Test class-level vs instance-level config behavior |
| 92 | +2. Verify derived classes don't pollute base class state |
| 93 | +3. Use fixtures from [spec/fixtures/](spec/fixtures/) for file upload tests |
0 commit comments