|
1 | | -# button-client-ruby |
2 | | -ruby client for the Button Order API |
| 1 | +# button-client-ruby [](https://travis-ci.org/button/button-client-ruby) |
| 2 | + |
| 3 | +This module is a thin client for interacting with Button's API. |
| 4 | + |
| 5 | +Please see the full [API Docs](https://www.usebutton.com/developers/api-reference) for more information. |
| 6 | + |
| 7 | +#### Supported runtimes |
| 8 | + |
| 9 | +* Ruby (MRI) `>=1.9.3` |
| 10 | + |
| 11 | +#### Dependencies |
| 12 | + |
| 13 | +* None |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```bash |
| 18 | +gem install button |
| 19 | +``` |
| 20 | + |
| 21 | +To create a client capable of making network requests, instantiate a `Button::Client` with your [API key](https://app.usebutton.com/settings/organization). |
| 22 | + |
| 23 | +```ruby |
| 24 | +require 'button' |
| 25 | + |
| 26 | +client = Button::Client.new('sk-XXX') |
| 27 | +``` |
| 28 | + |
| 29 | +The client will always attempt to raise a `Button::ButtonClientError` in an error condition. |
| 30 | + |
| 31 | +All API requests will return a `Button::Response` instance, which supports accessing data properties from the API response as methods. To access the raw response hash, use `#to_hash`. For instance: |
| 32 | + |
| 33 | +```ruby |
| 34 | +require 'button' |
| 35 | + |
| 36 | +client = Button::Client.new('sk-XXX') |
| 37 | + |
| 38 | +begin |
| 39 | + response = client.orders.get('btnorder-XXX') |
| 40 | +rescue Button::ButtonClientError => error |
| 41 | + puts error |
| 42 | +end |
| 43 | + |
| 44 | +puts response |
| 45 | +# => Button::Response(button_order_id: btnorder-XXX, total: 60, ... ) |
| 46 | + |
| 47 | +puts response.button_order_id |
| 48 | +# => btnorder-XXX |
| 49 | + |
| 50 | +puts response.to_hash() |
| 51 | +# => {:button_order_id=>'btnorder-29de0b1436075ea6', :total=>60, ... } |
| 52 | +``` |
| 53 | + |
| 54 | +n.b. the keys of the response hash will always be symbols. |
| 55 | + |
| 56 | +## Resources |
| 57 | + |
| 58 | +We currently expose only one resource to manage, `Orders`. |
| 59 | + |
| 60 | +### Orders |
| 61 | + |
| 62 | +**n.b: all currency values should be reported in the smallest possible unit of that denomination, i.e. $1.00 should be reported as `100` (i.e. 100 pennies)** |
| 63 | + |
| 64 | +##### Create |
| 65 | + |
| 66 | +```ruby |
| 67 | +require 'button' |
| 68 | + |
| 69 | +client = Button::Client.new('sk-XXX') |
| 70 | + |
| 71 | +response = client.orders.create({ |
| 72 | + total: 50, |
| 73 | + currency: 'USD', |
| 74 | + order_id: '1994', |
| 75 | + finalization_date: '2017-08-02T19:26:08Z' |
| 76 | +}) |
| 77 | + |
| 78 | +puts response |
| 79 | +# => Button::Response(button_order_id: btnorder-XXX, total: 50, ... ) |
| 80 | +``` |
| 81 | + |
| 82 | +##### Get |
| 83 | + |
| 84 | +```ruby |
| 85 | +require 'button' |
| 86 | + |
| 87 | +client = Button::Client.new('sk-XXX') |
| 88 | + |
| 89 | +response = client.orders.get('btnorder-XXX') |
| 90 | + |
| 91 | +puts response |
| 92 | +# => Button::Response(button_order_id: btnorder-XXX, total: 50, ... ) |
| 93 | +``` |
| 94 | +##### Update |
| 95 | + |
| 96 | +```ruby |
| 97 | +require 'button' |
| 98 | + |
| 99 | +client = Button::Client.new('sk-XXX') |
| 100 | + |
| 101 | +response = client.orders.update('btnorder-XXX', total: 60) |
| 102 | + |
| 103 | +puts response |
| 104 | +# => Button::Response(button_order_id: btnorder-XXX, total: 60, ... ) |
| 105 | +``` |
| 106 | + |
| 107 | +##### Delete |
| 108 | + |
| 109 | +```ruby |
| 110 | +require 'button' |
| 111 | + |
| 112 | +client = Button::Client.new('sk-XXX') |
| 113 | + |
| 114 | +response = client.orders.delete('btnorder-XXX') |
| 115 | + |
| 116 | +puts response |
| 117 | +# => Button::Response() |
| 118 | +``` |
| 119 | + |
| 120 | +## Contributing |
| 121 | + |
| 122 | +* Building the gem: `gem build button.gemspec` |
| 123 | +* Installing locally: `gem install ./button-X.Y.Z.gem` |
| 124 | +* Installing development dependencies: `bundle install` |
| 125 | +* Running tests: `bundle exec rake` |
0 commit comments