|
| 1 | +# fastcomments-ruby |
| 2 | +The FastComments Ruby SDK. You can use this to build secure and scalable backend applications that interact with FastComments, or build reactive client applications. |
| 3 | + |
| 4 | +## Installation |
| 5 | + |
| 6 | +Add this line to your application's Gemfile: |
| 7 | + |
| 8 | +```ruby |
| 9 | +gem 'fastcomments-ruby' |
| 10 | +``` |
| 11 | + |
| 12 | +And then execute: |
| 13 | + |
| 14 | +```bash |
| 15 | +bundle install |
| 16 | +``` |
| 17 | + |
| 18 | +Or install it yourself as: |
| 19 | + |
| 20 | +```bash |
| 21 | +gem install fastcomments-ruby |
| 22 | +``` |
| 23 | + |
| 24 | +### Library Contents |
| 25 | + |
| 26 | +This library contains the generated API client and the SSO utilities to make working with the API easier. |
| 27 | + |
| 28 | +- [API Client Library Docs](./client/README.md) |
| 29 | + |
| 30 | +### Public vs Secured APIs |
| 31 | + |
| 32 | +For the API client, there are two classes, `DefaultAPI` and `PublicAPI`. The `DefaultAPI` contains methods that require your API key, and `PublicAPI` contains api calls |
| 33 | +that can be made directly from a browser/mobile device/etc without authentication. |
| 34 | + |
| 35 | +## Quick Start |
| 36 | + |
| 37 | +### Using Authenticated APIs (DefaultAPI) |
| 38 | + |
| 39 | +**Important:** You must set your API key on the ApiClient before making authenticated requests. If you don't, requests will fail with a 401 error. |
| 40 | + |
| 41 | +```ruby |
| 42 | +require 'fastcomments-client' |
| 43 | + |
| 44 | +# Create and configure the API client |
| 45 | +config = FastCommentsClient::Configuration.new |
| 46 | +api_client = FastCommentsClient::ApiClient.new(config) |
| 47 | + |
| 48 | +# REQUIRED: Set your API key (get this from your FastComments dashboard) |
| 49 | +config.api_key['ApiKeyAuth'] = 'YOUR_API_KEY_HERE' |
| 50 | + |
| 51 | +# Create the API instance with the configured client |
| 52 | +api = FastCommentsClient::DefaultAPI.new(api_client) |
| 53 | + |
| 54 | +# Now you can make authenticated API calls |
| 55 | +begin |
| 56 | + # Example: Add an SSO user |
| 57 | + user_data = { |
| 58 | + id: 'user-123', |
| 59 | + |
| 60 | + displayName: 'John Doe' |
| 61 | + } |
| 62 | + |
| 63 | + response = api.add_sso_user('YOUR_TENANT_ID', user_data) |
| 64 | + puts "User created: #{response}" |
| 65 | + |
| 66 | +rescue FastCommentsClient::ApiError => e |
| 67 | + puts "Error: #{e.response_body}" |
| 68 | + # Common errors: |
| 69 | + # - 401: API key is missing or invalid |
| 70 | + # - 400: Request validation failed |
| 71 | +end |
| 72 | +``` |
| 73 | + |
| 74 | +### Using Public APIs (PublicAPI) |
| 75 | + |
| 76 | +Public endpoints don't require authentication: |
| 77 | + |
| 78 | +```ruby |
| 79 | +require 'fastcomments-client' |
| 80 | + |
| 81 | +public_api = FastCommentsClient::PublicAPI.new |
| 82 | + |
| 83 | +begin |
| 84 | + response = public_api.get_comments_public( |
| 85 | + tenant_id: 'YOUR_TENANT_ID', |
| 86 | + url_id: 'page-url-id' |
| 87 | + ) |
| 88 | + puts response |
| 89 | +rescue FastCommentsClient::ApiError => e |
| 90 | + puts e.message |
| 91 | +end |
| 92 | +``` |
| 93 | + |
| 94 | +### Common Issues |
| 95 | + |
| 96 | +1. **401 "missing-api-key" error**: Make sure you set `config.api_key['ApiKeyAuth'] = 'YOUR_KEY'` before creating the DefaultAPI instance. |
| 97 | +2. **Wrong API class**: Use `DefaultAPI` for server-side authenticated requests, `PublicAPI` for client-side/public requests. |
| 98 | +3. **Null API key**: The SDK will silently skip authentication if the API key is null, leading to 401 errors. |
| 99 | + |
| 100 | +## Notes |
| 101 | + |
| 102 | +### Broadcast Ids |
| 103 | + |
| 104 | +You'll see you're supposed to pass a `broadcastId` in some API calls. When you receive events, you'll get this ID back, so you know to ignore the event if you plan to optimistically apply changes on the client |
| 105 | +(which you'll probably want to do since it offers the best experience). Pass a UUID here. The ID should be unique enough to not occur twice in a browser session. |
| 106 | + |
| 107 | +### SSO (Single Sign-On) |
| 108 | + |
| 109 | +For SSO examples, see below. |
| 110 | + |
| 111 | +## SSO Usage |
| 112 | + |
| 113 | +### Simple SSO |
| 114 | + |
| 115 | +```ruby |
| 116 | +require 'fastcomments' |
| 117 | +require 'fastcomments-client' |
| 118 | + |
| 119 | +# Create Simple SSO token |
| 120 | +user = FastComments::SSO::SimpleSSOUserData.new( |
| 121 | + user_id: 'user-123', |
| 122 | + |
| 123 | + avatar: 'https://example.com/avatar.jpg' |
| 124 | +) |
| 125 | + |
| 126 | +sso = FastComments::SSO::FastCommentsSSO.new_simple(user) |
| 127 | +token = sso.create_token |
| 128 | + |
| 129 | +puts "SSO Token: #{token}" |
| 130 | + |
| 131 | +# Use the SSO token to make an authenticated API call |
| 132 | +config = FastCommentsClient::Configuration.new |
| 133 | +api_client = FastCommentsClient::ApiClient.new(config) |
| 134 | +public_api = FastCommentsClient::PublicAPI.new(api_client) |
| 135 | + |
| 136 | +response = public_api.get_comments_public( |
| 137 | + tenant_id: 'your-tenant-id', |
| 138 | + url_id: 'your-page-url-id', |
| 139 | + sso: token |
| 140 | +) |
| 141 | + |
| 142 | +puts "Status: #{response}" |
| 143 | +``` |
| 144 | + |
| 145 | +### Secure SSO |
| 146 | + |
| 147 | +```ruby |
| 148 | +require 'fastcomments' |
| 149 | +require 'fastcomments-client' |
| 150 | + |
| 151 | +# Create Secure SSO token |
| 152 | +user = FastComments::SSO::SecureSSOUserData.new( |
| 153 | + user_id: 'user-123', |
| 154 | + |
| 155 | + username: 'johndoe', |
| 156 | + avatar: 'https://example.com/avatar.jpg' |
| 157 | +) |
| 158 | + |
| 159 | +api_key = 'your-api-key' |
| 160 | +sso = FastComments::SSO::FastCommentsSSO.new_secure(api_key, user) |
| 161 | +token = sso.create_token |
| 162 | + |
| 163 | +puts "Secure SSO Token: #{token}" |
| 164 | + |
| 165 | +# Use the SSO token to make an authenticated API call |
| 166 | +config = FastCommentsClient::Configuration.new |
| 167 | +api_client = FastCommentsClient::ApiClient.new(config) |
| 168 | +public_api = FastCommentsClient::PublicAPI.new(api_client) |
| 169 | + |
| 170 | +response = public_api.get_comments_public( |
| 171 | + tenant_id: 'your-tenant-id', |
| 172 | + url_id: 'your-page-url-id', |
| 173 | + sso: token |
| 174 | +) |
| 175 | + |
| 176 | +puts "Status: #{response}" |
| 177 | +``` |
| 178 | + |
| 179 | +## Testing |
| 180 | + |
| 181 | +Set the required environment variables: |
| 182 | + |
| 183 | +```bash |
| 184 | +export FASTCOMMENTS_API_KEY="your-api-key" |
| 185 | +export FASTCOMMENTS_TENANT_ID="your-tenant-id" |
| 186 | +``` |
| 187 | + |
| 188 | +Run the tests: |
| 189 | + |
| 190 | +```bash |
| 191 | +bundle exec rspec |
| 192 | +``` |
| 193 | + |
| 194 | +## Development |
| 195 | + |
| 196 | +To update the generated client from the OpenAPI spec: |
| 197 | + |
| 198 | +```bash |
| 199 | +./update.sh |
| 200 | +``` |
| 201 | + |
| 202 | +This will download the latest OpenAPI spec from a running FastComments server (or use a local copy) and regenerate the client code. |
| 203 | + |
| 204 | +## License |
| 205 | + |
| 206 | +MIT License - see LICENSE file for details |
| 207 | + |
| 208 | +## Support |
| 209 | + |
| 210 | +For support, please visit https://fastcomments.com/auth/my-account/help or email [email protected] |
0 commit comments