|
| 1 | +# Square Ruby Library |
| 2 | + |
| 3 | +[](https://github.com/square/square-ruby-sdk/actions/workflows/ruby.yml) |
| 4 | +[](https://badge.fury.io/rb/square.rb) |
| 5 | + |
| 6 | +The Square Ruby library provides convenient access to the Square API from Ruby. |
| 7 | + |
| 8 | +## Requirements |
| 9 | + |
| 10 | +Use of the Square Ruby SDK requires: |
| 11 | + |
| 12 | +* Ruby 2.7+ |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +For more information, see [Set Up Your Square SDK for a Ruby Project](https://developer.squareup.com/docs/sdks/ruby/setup-project). |
| 17 | + |
| 18 | +## Reference |
| 19 | + |
| 20 | +For more information, see [Full Ruby Guide](https://developer.squareup.com/docs/sdks/ruby). |
| 21 | + |
| 22 | +## Versioning |
| 23 | + |
| 24 | +The Square Ruby SDK version is managed through the gem version. To use a specific version: |
| 25 | + |
| 26 | +```bash |
| 27 | +gem install square.rb -v 44.0.0 |
| 28 | +``` |
| 29 | + |
| 30 | +Or in your Gemfile: |
| 31 | +```ruby |
| 32 | +gem 'square.rb', '~> 44.0.0' |
| 33 | +``` |
| 34 | +``` |
| 35 | +
|
| 36 | +## Usage |
| 37 | +
|
| 38 | +For more information, see [Using the Square Ruby SDK](https://developer.squareup.com/docs/sdks/ruby/using-ruby-sdk). |
| 39 | +
|
| 40 | +## Legacy SDK |
| 41 | +
|
| 42 | +While the new SDK has a lot of improvements, we at Square understand that it takes time to upgrade when there are breaking changes. |
| 43 | +To make the migration easier, the new SDK also exports the legacy SDK as `square_legacy`. Here's an example of how you can use the |
| 44 | +legacy SDK alongside the new SDK inside a single file: |
| 45 | +
|
| 46 | +```ruby |
| 47 | +# Load both SDKs |
| 48 | +require 'square' |
| 49 | +require 'square_legacy' |
| 50 | +
|
| 51 | +# Initialize new SDK client |
| 52 | +new_client = Square::Client.new( |
| 53 | + access_token: 'YOUR_SQUARE_ACCESS_TOKEN' |
| 54 | +) |
| 55 | +
|
| 56 | +# Initialize legacy SDK client |
| 57 | +legacy_client = SquareLegacy::Client.new( |
| 58 | + bearer_auth_credentials: { |
| 59 | + access_token: 'YOUR_SQUARE_ACCESS_TOKEN' |
| 60 | + } |
| 61 | +) |
| 62 | +
|
| 63 | +# Use new SDK for newer features |
| 64 | +locations = new_client.locations.get_locations.data.locations |
| 65 | +
|
| 66 | +# Use legacy SDK for specific legacy functionality |
| 67 | +legacy_payment = legacy_client.payments_api.create_payment( |
| 68 | + body: { |
| 69 | + source_id: 'example_1234567890', |
| 70 | + idempotency_key: SecureRandom.uuid, |
| 71 | + amount_money: { |
| 72 | + amount: 100, |
| 73 | + currency: 'USD' |
| 74 | + } |
| 75 | + } |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +We recommend migrating to the new SDK using the following steps: |
| 80 | + |
| 81 | +1. Update your square.rb: `gem update square.rb` |
| 82 | +2. Search and replace all requires from `'square'` to `'square_legacy'` |
| 83 | +3. Update all client initializations from |
| 84 | +```ruby |
| 85 | +client = Square::Client.new(access_token: 'token') |
| 86 | +``` |
| 87 | +to |
| 88 | +```ruby |
| 89 | +client = SquareLegacy::Client.new( |
| 90 | + bearer_auth_credentials: { access_token: 'token' } |
| 91 | +) |
| 92 | +``` |
| 93 | +4. Gradually migrate over to the new SDK by importing it from the `'square'` import. |
| 94 | + |
| 95 | +## Request And Response Types |
| 96 | + |
| 97 | +The SDK exports all request and response types as Ruby classes. Simply require them with the |
| 98 | +following namespace: |
| 99 | + |
| 100 | +```ruby |
| 101 | +require 'square' |
| 102 | + |
| 103 | +# Create a request object |
| 104 | +request = Square::CreateMobileAuthorizationCodeRequest.new( |
| 105 | + location_id: 'YOUR_LOCATION_ID' |
| 106 | +) |
| 107 | +``` |
| 108 | + |
| 109 | +## Exception Handling |
| 110 | + |
| 111 | +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error |
| 112 | +will be raised. |
| 113 | + |
| 114 | +```ruby |
| 115 | +require 'square' |
| 116 | + |
| 117 | +begin |
| 118 | + response = client.payments.create(...) |
| 119 | +rescue Square::ApiError => e |
| 120 | + puts "Status Code: #{e.status_code}" |
| 121 | + puts "Message: #{e.message}" |
| 122 | + puts "Body: #{e.body}" |
| 123 | +end |
| 124 | +``` |
| 125 | + |
| 126 | +## Pagination |
| 127 | + |
| 128 | +List endpoints are paginated. The SDK provides methods to handle pagination: |
| 129 | + |
| 130 | +```ruby |
| 131 | +require 'square' |
| 132 | + |
| 133 | +client = Square::Client.new(access_token: "YOUR_TOKEN") |
| 134 | + |
| 135 | +# Get all items using pagination |
| 136 | +response = client.bank_accounts.list |
| 137 | +all_bank_accounts = [] |
| 138 | + |
| 139 | +while response.data.bank_accounts.any? |
| 140 | + all_bank_accounts.concat(response.data.bank_accounts) |
| 141 | + |
| 142 | + # Check if there are more pages |
| 143 | + if response.cursor |
| 144 | + response = client.bank_accounts.list(cursor: response.cursor) |
| 145 | + else |
| 146 | + break |
| 147 | + end |
| 148 | +end |
| 149 | + |
| 150 | +puts "Total bank accounts: #{all_bank_accounts.length}" |
| 151 | +``` |
| 152 | + |
| 153 | +## Advanced |
| 154 | + |
| 155 | +### Additional Headers |
| 156 | + |
| 157 | +If you would like to send additional headers as part of the request, use the `headers` request option. |
| 158 | + |
| 159 | +```ruby |
| 160 | +response = client.payments.create(..., { |
| 161 | + headers: { |
| 162 | + 'X-Custom-Header' => 'custom value' |
| 163 | + } |
| 164 | +}) |
| 165 | +``` |
| 166 | + |
| 167 | +### Receive Extra Properties |
| 168 | + |
| 169 | +Every response includes any extra properties in the JSON response that were not specified in the type. |
| 170 | +This can be useful for API features not present in the SDK yet. |
| 171 | + |
| 172 | +You can receive and interact with the extra properties by accessing the raw response data: |
| 173 | + |
| 174 | +```ruby |
| 175 | +response = client.locations.create(...) |
| 176 | + |
| 177 | +# Access the raw response data |
| 178 | +location_data = response.data.to_h |
| 179 | + |
| 180 | +# Then access the extra property by its name |
| 181 | +undocumented_property = location_data['undocumentedProperty'] |
| 182 | +``` |
| 183 | + |
| 184 | +### Retries |
| 185 | + |
| 186 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 187 | +as the request is deemed retriable and the number of retry attempts has not grown larger than the configured |
| 188 | +retry limit (default: 2). |
| 189 | + |
| 190 | +A request is deemed retriable when any of the following HTTP status codes is returned: |
| 191 | + |
| 192 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 193 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 194 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 195 | + |
| 196 | +Use the `max_retries` request option to configure this behavior. |
| 197 | + |
| 198 | +```ruby |
| 199 | +response = client.payments.create(..., { |
| 200 | + max_retries: 0 # override max_retries at the request level |
| 201 | +}) |
| 202 | +``` |
| 203 | + |
| 204 | +### Timeouts |
| 205 | + |
| 206 | +The SDK defaults to a 60 second timeout. Use the `timeout_in_seconds` option to configure this behavior. |
| 207 | + |
| 208 | +```ruby |
| 209 | +response = client.payments.create(..., { |
| 210 | + timeout_in_seconds: 30 # override timeout to 30s |
| 211 | +}) |
| 212 | +``` |
| 213 | + |
| 214 | +## Contributing |
| 215 | + |
| 216 | +While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us! |
| 217 | + |
| 218 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments