xero-ruby 3.1.3 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9d0112cb2d0bb0b563d0f702794ac288faf75c964faefd7276c09bbed1ca70b
4
- data.tar.gz: 2ed673b2a449037d1bfb0d132ad84eeac7a814412e127b5d00a8694735288d56
3
+ metadata.gz: 3a94136a3e2a31cc4d5c7a162116bdd8705800ac2fc2732907a80286950fa376
4
+ data.tar.gz: 340b78c53b08a37f04b79d642bf0fc7e4e1c109595ef86b596398aa9c2b87be3
5
5
  SHA512:
6
- metadata.gz: 131b39dff4cc85df48f3a872d1dbbd3a7f3cdd5bb1808f88de897b4ae394b8ccc14549be94b5d51f66ef029713521e9447041a6f767ae0b954bffb6261972729
7
- data.tar.gz: 6311ad42dbddb39939fe10d9a2c9ee6e8ad9b0c4e83cd5976d1b86ff8b7db11b991890e050d9f0dcce2d39a72e4b4fd8f57a970dcc4845c7bc373feb2d6590d0
6
+ metadata.gz: 1275a668a399320fa11e6ccbcacd9eac6d4cf4ffb939108d1962ccede318376b1ee3236b4cdbb95e0db45e8e1695f860508861c4097da7c1d6a12e22cfe3672f
7
+ data.tar.gz: 49de788f087c577474ad556ed9188c0e75ccb46256f52ff6639e2fdf82ed732fceb9eebfb825a9d17cb2f6370c4b52c78c4b1ebc0b5484554e565089e5f5052d
data/README.md CHANGED
@@ -1,222 +1,297 @@
1
1
  # xero-ruby
2
- Xero Ruby SDK for OAuth 2.0 generated from [Xero API OpenAPI Spec](https://github.com/XeroAPI/Xero-OpenAPI).
3
-
4
- [![RubyGem](https://img.shields.io/badge/xero--ruby%20gem-brightgreen)](https://rubygems.org/gems/xero-ruby)
5
-
6
- # Documentation
7
- Xero Ruby SDK supports Xero's OAuth2.0 authentication and the following Xero API sets.
8
- ### API Client Documentation
9
- * [API client methods](https://xeroapi.github.io/xero-ruby/accounting/index.html)
10
- > This describes to ~200+ accounting API endpoints and their expected params. There are also method reference docs for the Assets, Files, Projects, and Payroll endpoints sets, though we are still working on accurately generating usable parameter examples for all! (feedback welcome)
11
- ### Model Docs
12
- * [Models](/docs/)
13
- > Directory of markdown files, describing the object models for the Accounting, Asset, Projects, Files, Payroll (AU, UK, NZ) Xero API sets.
14
- ## Sample Apps
15
- We have two sample apps showing SDK usage:
16
- * https://github.com/XeroAPI/xero-ruby-oauth2-starter (**Sinatra** - bare minimum to hello world and simple session based storage)
17
- * https://github.com/XeroAPI/xero-ruby-oauth2-app (**Rails** - token management with robust usage examples)
18
-
19
- ![sample-app](https://i.imgur.com/OOEn55G.png)
20
-
21
- ## Xero Pre-Requisites
22
- * Create a [free Xero user account](https://www.xero.com/us/signup/api/)
23
- * Login to your Xero developer [/myapps](https://developer.xero.com/myapps) dashboard & create an API application
24
- * Copy the credentials from your API app and store/access them using a secure ENV variable strategy
25
- * Resaearch and include the [neccesary scopes](https://developer.xero.com/documentation/oauth2/scopes) for your app's functionality as a space-seperated list, ex. "`SCOPES="openid profile email accounting.transactions accounting.settings"`"
26
- ## Installation
27
- To install this gem to your project:
2
+ [![Gem Version](https://badge.fury.io/rb/xero-ruby.svg)](https://badge.fury.io/rb/xero-ruby)
3
+ [![Github forks](https://img.shields.io/github/forks/XeroAPI/xero-ruby.svg)](https://github.com/XeroAPI/xero-ruby/network)
4
+ [![Github stars](https://img.shields.io/github/stars/XeroAPI/xero-ruby.svg)](https://github.com/XeroAPI/xero-ruby/stargazers)
5
+ ![total downloads](https://ruby-gem-downloads-badge.herokuapp.com/xero-ruby?type=total)
6
+
7
+ The xero-ruby SDK makes it easy for developers to access Xero's APIs in their Ruby code, and build robust applications and software using small business & general ledger accounting data.
8
+ # Table of Contents
9
+ - [API Client documentation](#api-client-documentation)
10
+ - [Sample Applications](#sample-applications)
11
+ - [Xero Account Requirements](#xero-account-requirements)
12
+ - [Installation](#installation)
13
+ - [Configuration](#configuration)
14
+ - [Authentication](#authentication)
15
+ - [Custom Connections](#custom-connections)
16
+ - [App Store Subscriptions](#app-store-subscriptions)
17
+ - [API Clients](#api-clients)
18
+ - [Helper Methods](#helper-methods)
19
+ - [Usage Examples](#usage-examples)
20
+ - [SDK conventions](#sdk-conventions)
21
+ - [Contributing](#contributing)
22
+
23
+ <hr>
24
+
25
+ ## API Client documentation
26
+ This SDK supports full method coverage for the following Xero API sets:
27
+
28
+ | API Set | Description |
29
+ | --- | --- |
30
+ | [`Accounting`](https://xeroapi.github.io/xero-ruby/accounting/index.html) | The Accounting API exposes accounting functions of the main Xero application *(most commonly used)*
31
+ | [Assets](https://xeroapi.github.io/xero-ruby/assets/index.html) | The Assets API exposes fixed asset related functions of the Xero Accounting application |
32
+ | [Files](https://xeroapi.github.io/xero-ruby/files/index.html) | The Files API provides access to the files, folders, and the association of files within a Xero organisation |
33
+ | [Projects](https://xeroapi.github.io/xero-ruby/projects/index.html) | Xero Projects allows businesses to track time and costs on projects/jobs and report on profitability |
34
+ | [Payroll (AU)](https://xeroapi.github.io/xero-ruby/payroll_au/index.html) | The (AU) Payroll API exposes payroll related functions of the payroll Xero application |
35
+ | [Payroll (UK)](https://xeroapi.github.io/xero-ruby/payroll_uk/index.html) | The (UK) Payroll API exposes payroll related functions of the payroll Xero application |
36
+ | [Payroll (NZ)](https://xeroapi.github.io/xero-ruby/payroll_nz/index.html) | The (NZ) Payroll API exposes payroll related functions of the payroll Xero application |
37
+ | [`Ruby Models`](/docs/) | Directory of markdown files, describing the object models for all supported API sets |
38
+
39
+ <img src="https://i.imgur.com/0MsvkGB.png" alt="drawing" width="350"/>
40
+
41
+ <hr>
42
+
43
+ ## Sample Applications
44
+ Sample apps can get you started quickly with simple auth flows to advanced usage.
45
+ | Sample App | Description | Screenshot |
46
+ | --- | --- | --- |
47
+ | [`xero-ruby-oauth2-starter`](https://github.com/XeroAPI/Xero-ruby-oauth2-starter) | A Sinatra application showing the basic getting started code to work with the sdk | <img src="https://i.imgur.com/9H4F98M.png" alt="drawing" width="300"/>
48
+ | [`xero-ruby-oauth2-app`](https://github.com/XeroAPI/Xero-ruby-oauth2-app) | Complete rails app with +95% of api set examples, complex filters, pagination, and user/token management in postgres | <img src="https://i.imgur.com/XsAp9Ww.png" alt="drawing" width="500"/>
49
+ | [`xero-ruby-custom-connections-starter`](https://github.com/XeroAPI/xero-ruby-custom-connections-starter) | A getting started Sinatra app showing Custom Connections - a Xero [premium option](https://developer.xero.com/documentation/oauth2/custom-connections) for building M2M integrations to a single org | <img src="https://i.imgur.com/YEkScui.png" alt="drawing" width="300"/>
50
+ | [`xero-ruby-sso-form`](https://github.com/XeroAPI/xero-ruby-sso-form) | A basic Sinatra app showing how to implement SSU to Lead | <img src="https://i.imgur.com/Nf95GVd.png" alt="drawing" width="300"/>
51
+
52
+ <hr>
53
+
54
+ ## Xero Account Requirements
55
+ - Create a [free Xero user account](https://www.xero.com/us/signup/api/)
56
+ - Login to your Xero developer [dashboard](https://developer.xero.com/app/manage) and create an API application
57
+ - Copy the credentials from your API app and store them using a secure ENV variable strategy
58
+ - Decide the [neccesary scopes](https://developer.xero.com/documentation/oauth2/scopes) for your app's functionality
59
+
60
+ # Installation
61
+ To install this gem to your project
28
62
  ```
29
63
  gem install 'xero-ruby'
30
64
  ```
31
- Or more commonly in Ruby on Rails usage add to your gemfile and run `bundle install`:
65
+ Or add to your gemfile and run `bundle install`
32
66
  ```
33
67
  gem 'xero-ruby'
34
68
  ```
35
69
 
36
70
  ---
37
- ## Usage
38
- ```
39
- require 'xero-ruby'
40
- ```
71
+ ## Configuration
41
72
  ```ruby
73
+ require 'xero-ruby'
74
+
42
75
  creds = {
43
76
  client_id: ENV['CLIENT_ID'],
44
77
  client_secret: ENV['CLIENT_SECRET'],
45
78
  redirect_uri: ENV['REDIRECT_URI'],
46
79
  scopes: ENV['SCOPES'],
47
- state: "this-can-be-a-custom-state-parameter" # optional
80
+ state: "Optional value to pass through auth flow"
48
81
  }
49
82
  xero_client ||= XeroRuby::ApiClient.new(credentials: creds)
50
83
  ```
51
84
 
52
- For additional logging or timeout, add or override any [config](/lib/xero-ruby/configuration.rb) option by passing an optional named parameter `config: {..}`.
85
+ For additional [config](/lib/xero-ruby/configuration.rb) options you can pass an optional named parameter `config: {}`
53
86
  ```ruby
54
87
  config = { timeout: 30, debugging: true }
55
88
  @xero_client ||= XeroRuby::ApiClient.new(credentials: creds, config: config)
56
89
  ```
90
+ ---
91
+ ## Authentication
92
+ All API requests go through Xero's OAuth2.0 gateway and require a valid `access_token` to be set on the `xero_client`, which autmoatically appends the `access_token` [JWT](https://jwt.io/) to the header of each request.
57
93
 
58
- ## OAuth2.0 Authorization & Callback
59
- All API requests require a valid access token to be set on the xero_client.
94
+ If you are making an API call for the first time:
60
95
 
61
- ### Step 1
62
- Send the user to the `authorization_url` after you have configured your xero_client
96
+ 1. Send the user to the Xero authorization URL
63
97
  ```ruby
64
98
  @authorization_url = xero_client.authorization_url
65
-
66
99
  redirect_to @authorization_url
67
100
  ```
68
-
69
- ### Step 2
70
- On successful authorization, Xero identity will redirect to the URI defined in your `redirect_uri` config which must match **exactly** with the variable in your /myapps dashboard.
71
- ```
72
- => /oauth/redirect_uri
101
+ 2. The user will authorize your API application and be sent to your `redirect_uri`
102
+ ```ruby
103
+ ENV['REDIRECT_URI']
104
+ => /xero-callback?code=xyz123
73
105
  ```
74
- ### Step 3
75
- In your server defined callback route, exchange the temporary code for a valid `token_set` that will get set on your client.
106
+ 3. You exchange a temporary `authorization_code` for a valid `token_set`
76
107
  ```ruby
77
- token_set = xero_client.get_token_set_from_callback(params)
108
+ @token_set = @xero_client.get_token_set_from_callback(params)
109
+ # save the @token_set
78
110
  ```
79
- At this point you should save the token_set as JSON in a datastore in relation to the authenticating user or entity.
80
111
 
81
- The sample [Rails app](https://github.com/XeroAPI/xero-ruby-oauth2-app/blob/master/app/controllers/application_controller.rb#L11) shows a solid pattern you can tweak to fit your needs:
82
- ```ruby
83
- # /oauth/redirect_uri -> 'application#callback'
84
- def callback
85
- @token_set = @xero_client.get_token_set_from_callback(params)
86
-
87
- current_user.token_set = @token_set
88
- current_user.token_set['connections'] = @xero_client.connections
89
- current_user.active_tenant_id = latest_connection(current_user.token_set['connections'])
90
- current_user.save!
91
- flash.notice = "Successfully authenticated with Xero!"
92
- end
112
+ It is reccomended that you store this token set JSON in a datastore in relation to the user who has authenticated the Xero API connection. Each time you want to call the Xero API, you will need to access the previously generated token set, initialize it on the SDK `xero_client`, and refresh the `access_token` prior to making API calls.
113
+
114
+ ### Token Set
115
+ | key | value | description |
116
+ | --- | --- | --- |
117
+ | id_token: | "xxx.yyy.zzz" | [OpenID Connect](https://openid.net/connect/) token returned if `openid profile email` scopes accepted |
118
+ | access_token: | "xxx.yyy.zzz" | [Bearer token](https://oauth.net/2/jwt/) with a 30 minute expiration required for all API calls |
119
+ | expires_in: | 1800 | Time in seconds till the token expires - 1800s is 30m |
120
+ | refresh_token: | "XXXXXXX" | Alphanumeric string used to obtain a new Token Set w/ a fresh access_token - 60 day expiry |
121
+ | scope: | "email profile openid accounting.transactions offline_access" | The Xero permissions that are embedded in the `access_token` |
122
+
123
+ Example Token Set JSON:
93
124
  ```
94
- ---
95
- ### What is a Token Set?
96
- A `token_set` is what we call the XeroAPI response that contains data about your API connection:
97
- ```json
98
125
  {
99
- "id_token": "xxx.yyy.zz", (if you requested `openid profile email` scope)
126
+ "id_token": "xxx.yyy.zz",
100
127
  "access_token": "xxx.yyy.zzz",
101
128
  "expires_in": 1800,
102
129
  "token_type": "Bearer",
103
- "refresh_token": "xxxxxx", (if you requested `offline_access` scope)
130
+ "refresh_token": "xxxxxxxxx",
104
131
  "scope": "email profile openid accounting.transactions offline_access"
105
132
  }
106
133
  ```
134
+ ---
135
+ ## Custom Connections
107
136
 
108
- Note that an `access_token` is valid for 30 minutes but a `refresh_token` can be used once in up to a 60 day window. If a refresh_token is used to refresh access you must replace the entire token_set.
137
+ Custom Connections are a Xero [premium option](https://developer.xero.com/documentation/oauth2/custom-connections) used for building M2M integrations to a single organisation. A custom connection uses OAuth2.0's [`client_credentials`](https://www.oauth.com/oauth2-servers/access-tokens/client-credentials/) grant which eliminates the step of exchanging the temporary code for a token set.
109
138
 
110
- Both the `id_token` & `access_token` are JWT's, and can be decoded to see additional metadata described in the Token Helpers section:
111
- ## Making API calls with a valid token_set
112
- After the initial user interaction you can simply setup a xero_client by passing the whole token_set to the client.
139
+ To use this SDK with a Custom Connections:
113
140
  ```ruby
114
- xero_client.set_token_set(user.token_set)
141
+ CREDENTIALS = {
142
+ client_id: ENV['CLIENT_ID'],
143
+ client_secret: ENV['CLIENT_SECRET'],
144
+ grant_type: 'client_credentials'
145
+ }
146
+
147
+ xero_client = XeroRuby::ApiClient.new(credentials: CREDENTIALS)
115
148
 
116
- xero_client.refresh_token_set(user.token_set)
149
+ @token_set = xero_client.get_client_credentials_token
150
+ # save @token_set
151
+
152
+ @invoices = xero_client.accounting_api.get_invoices('').invoices
117
153
  ```
118
- This sets the access_token on the client, and returns a refreshed `token_set` that you should save in your datastore for the next time you need to connect to Xero's API.
119
- ## Token Helpers
154
+
155
+ Because Custom Connections are only valid for a single organisation you don't need to pass the `xero-tenant-id` as the first parameter to every method.
156
+
157
+ However - due to the nature of how our SDK's are generated from our OpenAPI spec, the parameter remains which requires you to pass an empty string for now to use the SDK with a Custom Connection.
158
+
159
+ ---
160
+
161
+ ## App Store Subscriptions
162
+
163
+ If you are implementing subscriptions to participate in Xero's App Store you will need to setup [App Store subscriptions](https://developer.xero.com/documentation/guides/how-to-guides/xero-app-store-referrals/) endpoints.
164
+
165
+ When a plan is successfully purchased, the user is redirected back to the URL specified in the setup process. The Xero App Store appends the subscription Id to this URL so you can immediately determine what plan the user has subscribed to through the subscriptions API.
166
+
167
+ With your app credentials you can create a client via `client_credentials` grant_type with the `marketplace.billing` scope. This unique access_token will allow you to query any functions in `appStoreApi`. Client Credentials tokens to query app store endpoints will only work for apps that have completed the App Store on-boarding process.
168
+
120
169
  ```ruby
121
- xero_client.token_set
122
- =>
123
- {
124
- "id_token": "xxx.yyy.zz",
125
- "access_token": "xxx.yyy.zzz",
126
- "expires_in": 1800,
127
- "token_type": "Bearer",
128
- "refresh_token": "xxxxxx",
129
- "scope": "email profile openid accounting.transactions offline_access"
130
- }
170
+ // => /post-purchase-url?subscriptionId=03bc74f2-1237-4477-b782-2dfb1a6d8b21
131
171
 
132
- xero_client.access_token
133
- => "xxx.yyy.zzz"
134
-
135
- xero_client.decoded_access_token
136
- => {
137
- "exp": 1619715843,
138
- "xero_userid": "xero-user-uuid",
139
- "scope": [
140
- "email",
141
- "profile",
142
- "openid",
143
- "accounting.transactions",
144
- "offline_access"
145
- ]
146
- }
172
+ subscription_id = params[:subscriptionId]
147
173
 
174
+ xero_app_store_client ||= XeroRuby::ApiClient.new(credentials: {
175
+ client_id: ENV['CLIENT_ID'],
176
+ client_secret: ENV['CLIENT_SECRET'],
177
+ grant_type: 'client_credentials'
178
+ scopes: ['marketplace.billing']
179
+ })
148
180
 
149
- xero_client.id_token
150
- => "aaa.bbb.ccc"
181
+ xero_app_store_client.get_client_credentials_token
151
182
 
152
- xero_client.decoded_id_token
153
- => {
154
- "iss": "https://identity.xero.com",
155
- "email": "luca.pacioli@accounting-services.com",
156
- "given_name": "Luca",
157
- "family_name": "Pacioli"
158
- }
183
+ @subscription = xero_app_store_client.app_store_api.get_subscription(subscription_id)
159
184
 
160
- xero_client.set_token_set(token_set)
161
- => true
185
+ puts @subscription.to_attributes
186
+ {
187
+ :current_period_end => Thu, 02 Sep 2021 14:08:58 +0000,
188
+ :id => "03bc74f2-1237-4477-b782-2dfb1a6d8b21",
189
+ :organisation_id => "79e8b2e5-c63d-4dce-888f-e0f3e9eac647",
190
+ :plans => [
191
+ {
192
+ :id => "6abc26f3-9390-4194-8b25-ce8b9942fda9",
193
+ :name => "Small",
194
+ :status => "ACTIVE",
195
+ :subscription_items => [
196
+ {
197
+ :id => "834cff4c-b753-4de2-9e7a-3451e14fa17a",
198
+ :price => {
199
+ :amount => 0.1e0,
200
+ :currency => "NZD",
201
+ :id => "2310de92-c7c0-4bcb-b972-fb7612177bc7"
202
+ },
203
+ :product => {
204
+ :id => "9586421f-7325-4493-bac9-d93be06a6a38",
205
+ :name => "",
206
+ :type => "FIXED"
207
+ },
208
+ :start_date => Mon, 02 Aug 2021 14:08:58 +0000,
209
+ :test_mode => true
210
+ }
211
+ ]
212
+ }
213
+ ],
214
+ :start_date => Mon, 02 Aug 2021 14:08:58 +0000,
215
+ :status => "ACTIVE",
216
+ :test_mode => true
217
+ }
218
+ ```
219
+ You should use this subscription data to provision user access/permissions to your application.
220
+ ### App Store Subscription Webhooks
162
221
 
163
- xero_client.get_token_set_from_callback(callback_url_params)
164
- => new_xero_token_set
222
+ In additon to a subscription Id being passed through the URL, when a purchase or an upgrade takes place you will be notified via a webhook. You can then use the subscription Id in the webhook payload to query the AppStore endpoints and determine what plan the user purchased, upgraded, downgraded or cancelled.
165
223
 
166
- xero_client.refresh_token_set(token_set)
167
- => new_xero_token_set
224
+ Refer to Xero's documenation to learn more about setting up and receiving webhooks.
225
+ > https://developer.xero.com/documentation/guides/webhooks/overview/
168
226
 
169
- # These are automatically populated with `set_token_set`
170
- # But if you need to set just an access or id token on the client
171
- xero_client.set_access_token(access_token)
172
- xero_client.set_id_token(id_token)
227
+ ## API Clients
228
+ You can access the different API sets and their available methods through the following:
173
229
 
174
- # Automatically run on initial OAuth flow - can be called its own if desired
175
- # Read about why we have included this in the default library: https://auth0.com/docs/tokens/access-tokens/validate-access-tokens
176
- xero_client.validate_tokens(token_set)
177
- xero_client.decode_jwt(tkn)
230
+ ```ruby
231
+ xero_client = XeroRuby::ApiClient.new(credentials: creds)
232
+
233
+ xero_client.accounting_api
234
+ xero_client.asset_api
235
+ xero_client.project_api
236
+ xero_client.files_api
237
+ xero_client.payroll_au_api
238
+ xero_client.payroll_nz_api
239
+ xero_client.payroll_uk_api
178
240
  ```
179
- # Connection Helpers
241
+ ---
242
+ ## Helper Methods
243
+
244
+ Once you have a valid Token Set in your datastore, the next time you want to call the Xero API simply initialize a new `xero_client` and refresh the token set.
245
+
180
246
  ```ruby
181
- xero_client.authorization_url
182
- => # https://login.xero.com/identity/connect/authorize?response_type=code&client_id=<client_id>&redirect_uri=<redirect_uri>&scope=<scopes>&state=<my-state>
183
-
184
- # To completely Revoke a user's access token and all their connections
185
- xero_client.revoke_token(token_set)
186
-
187
- # In case there are > 1 tenants connected the `updatedDateUtc` will show you the most recently authorized tenant (aka organisation) - it is important to store the `tenantId` of the Org your user specified in their API authorization
188
- connections = xero_client.connections
189
- [{
190
- "id" => "xxx-yyy-zzz",
191
- "tenantId" => "xxx-yyy-zzz",
192
- "tenantType" => "ORGANISATION",
193
- "tenantName" => "Demo Company (US)",
194
- "createdDateUtc" => "2019-11-01T20:08:03.0766400",
195
- "updatedDateUtc" => "2020-04-15T22:37:10.4943410"
196
- }]
197
-
198
- # To disconnect a single org from a user's active connections pass the connection ['id'] (not ['tenantId'])
199
- # If you want to enforce only a single org connection per token do this prior to sending user through Xero authorize flow a 2nd time.
200
- remaining_connections = xero_client.disconnect(connections[0]['id'])
201
-
202
- xero_client.token_expired?
203
- => true || false
204
-
205
- # This will check against the following logic
206
- token_expiry = Time.at(decoded_access_token['exp'])
207
- token_expiry < Time.now
247
+ xero_client = XeroRuby::ApiClient.new(credentials: creds)
248
+
249
+ if xero_client.token_expired?
250
+ @token_set = xero_client.refresh_token_set(user.token_set)
251
+ else
252
+ @token_set = xero_client.set_token_set(user.token_set)
253
+ end
254
+
255
+ # example strategy
256
+ user.token_set = @token_set if !@token_set["error"]
257
+ user.xero_connections = xero_client.connections
258
+ user.active_tenant_id = xero_client.last_connection
259
+ user.save!
260
+
261
+ xero_client.accounting_api.get_invoices(xero_client.last_connection['tenantId']).invoices
208
262
  ```
209
263
 
210
- # API Usage
264
+ This will set the access_token on the client and return the refreshed `token_set` that you need to save in your datastore to keep the connection alive until you or the user disconnect.
265
+
266
+ A full list of the SDK client's methods:
267
+
268
+ | method | description |
269
+ | --- | --- |
270
+ | xero_client.`authorization_url` | returns the authorize URL string to send a new user to for API authorization |
271
+ | xero_client.`get_token_set_from_callback`(params) | returns and generates a `token_set` from a temporary code for an `authorization_code` configured client only. Params are the url params. |
272
+ | xero_client.`get_client_credentials_token` | returns and generates a `token_set` for a `client_credentials` configured client only |
273
+ | xero_client.`refresh_token_set`(token_set) | returns a refreshed token_set |
274
+ | xero_client.`revoke_token`(token_set) | removes all a user's org connections and revokes the refresh_token |
275
+ | xero_client.`disconnect`(connection_id) | disconnects an org connection from a user's token permissions |
276
+ | xero_client.`connections` | returns an array of the user's currently connected Xero orgs |
277
+ | xero_client.`last_connection` | returns the `xero-tenant-id` of the most recently connected Xero org |
278
+ | xero_client.`set_token_set`(token_set) | returns a boolean and sets a token on the client - `token_set` containing an access_token & refresh_token |
279
+ | xero_client.`set_access_token`(access_token) | How you can set an `access_token` by itself |
280
+ | xero_client.`set_id_token`(id_token) | How you can set an `id_token` by itself |
281
+ | xero_client.`token_expired?` | returns a boolean if token_set['access_token'] is expired |
282
+ | xero_client.`token_set` | returns the token set if one is set on the client |
283
+ | xero_client.`access_token` | returns the `access_token` if one is set on the client |
284
+ | xero_client.`id_token` | returns the `id_token` if one is set on the client |
285
+ | xero_client.`decoded_access_token` | Decoded JWT exposing OAuth2.`0` meta details about the token |
286
+ | xero_client.`decoded_id_token` | Decoded JWT containing the user meta details that can be used to implement SSO or SSU to Lead |
287
+ ---
288
+ ## Usage Examples
211
289
  ### Accounting API
212
- > https://xeroapi.github.io/xero-ruby/accounting/index.html
213
290
  ```ruby
214
291
  require 'xero-ruby'
215
292
 
216
- xero_client.refresh_token_set(user.token_set)
217
-
218
- tenant_id = user.active_tenant_id
219
- # Example 'active tenant' logic storage of the tenant the user specified, xero_client.connections[0] is not a safe assumption in case they authorized multiple orgs.
293
+ xero_client.refresh_token_set(user_token_set)
294
+ tenant_id = xero_client.last_connection
220
295
 
221
296
  # Get Accounts
222
297
  accounts = xero_client.accounting_api.get_accounts(tenant_id).accounts
@@ -225,7 +300,7 @@ accounts = xero_client.accounting_api.get_accounts(tenant_id).accounts
225
300
  invoices = { invoices: [{ type: XeroRuby::Accounting::Invoice::ACCREC, contact: { contact_id: contacts[0].contact_id }, line_items: [{ description: "Big Agency", quantity: BigDecimal("2.0"), unit_amount: BigDecimal("50.99"), account_code: "600", tax_type: XeroRuby::Accounting::TaxType::NONE }], date: "2019-03-11", due_date: "2018-12-10", reference: "Website Design", status: XeroRuby::Accounting::Invoice::DRAFT }]}
226
301
  invoice = xero_client.accounting_api.create_invoices(tenant_id, invoices).invoices.first
227
302
 
228
- # display out all the serialized data as a snake_case hash
303
+ # return data as a snake_case hash
229
304
  puts invoices.to_attributes
230
305
  => {type: 'ACCREC', line_items: [...]}
231
306
 
@@ -247,53 +322,9 @@ file = File.read(Rails.root.join('app/assets/images/xero-api.png'))
247
322
  attachment = xero_client.accounting_api.create_account_attachment_by_file_name(tenant_id, @account.account_id, file_name, file, opts)
248
323
  ```
249
324
 
250
- ### Assets API
251
- > https://github.com/XeroAPI/xero-ruby/blob/master/accounting/lib/xero-ruby/api/asset_api.rb
252
- ```ruby
253
- asset = {
254
- "assetName": "AssetName: #{rand(10000)}",
255
- "assetNumber": "Asset: #{rand(10000)}",
256
- "assetStatus": "DRAFT"
257
- }
258
- asset = xero_client.asset_api.create_asset(tenant_id, asset)
259
- ```
260
-
261
- ### Project API
262
- > https://github.com/XeroAPI/xero-ruby/blob/master/docs/projects/ProjectApi.md
263
- ```ruby
264
- projects = xero_client.project_api.get_projects(tenant_id).items
265
- ```
266
-
267
- ### Files API
268
- > https://github.com/XeroAPI/xero-ruby/blob/master/docs/files/FileApi.md
269
- ```ruby
270
- opts = {
271
- pagesize: 50, # Integer | pass an optional page size value
272
- page: 2, # Integer | number of records to skip for pagination
273
- sort: 'CreatedDateUTC DESC' # String | values to sort by
274
- }
275
-
276
- files = xero_client.files_api.get_files(tenant_id, opts).files
277
- ```
278
-
279
- ### Payroll API(s)
280
- ```ruby
281
- # https://github.com/XeroAPI/xero-ruby/blob/master/docs/payroll_au/PayrollAuApi.md
282
- employee_id = 'employee_uuid'
283
- employee = xero_client.payroll_au_api.get_employee(tenant_id, employee_id).employee
284
-
285
-
286
- # https://github.com/XeroAPI/xero-ruby/blob/master/docs/payroll_nz/PayrollNzApi.md
287
- timesheet_id = 'timesheeet_uuid'
288
- timesheet = xero_client.payroll_nz_api.approve_timesheet(tenant_id, timesheet_id).timesheets
289
-
290
-
291
- # https://github.com/XeroAPI/xero-ruby/blob/master/docs/payroll_uk/PayrollUkApi.md
292
- employee_id = 'employee_uuid'
293
- wages = xero_client.payroll_uk_api.get_employee_salary_and_wages(tenant_id, employee_id, opts).salary_and_wages
294
- ```
295
-
296
- ## BigDecimal
325
+ ---
326
+ ## SDK conventions
327
+ ### BigDecimal
297
328
  All monetary and fields and a couple quantity fields utilize BigDecimal
298
329
  ```ruby
299
330
  puts invoice.unit_amount
@@ -309,8 +340,8 @@ All monetary and fields and a couple quantity fields utilize BigDecimal
309
340
  number_to_currency(invoice.unit_amount, :unit => "$")
310
341
  ```
311
342
 
312
- ## Querying & Filtering
313
- Examples for the `opts` (_options_) parameters most endpoints support. This is an area of focus and improvement. If you have a complex filering/sorting/where usage that is not supported please [open an issue](https://github.com/XeroAPI/xero-ruby/issues).
343
+ ### Querying & Filtering
344
+ Examples for `opts` (aka _options_) parameters that most endpoints support. If you have a complex filering/sorting/where usage that is not supported please [open an issue](https://github.com/XeroAPI/xero-ruby/issues).
314
345
  ```ruby
315
346
  # Invoices
316
347
  opts = {
@@ -323,7 +354,7 @@ Examples for the `opts` (_options_) parameters most endpoints support. This is a
323
354
  invoice_number: ['=', "INV-0001"],
324
355
  contact_id: ['=', 'contact-uuid-xxxx-xxx-xxxxxxx'],
325
356
  contact_number: ['=', "the-contact-number"],
326
- # date: (DateTime.now - 2.year)..DateTime.now
357
+ date: (DateTime.now - 2.year)..DateTime.now
327
358
  # ▲ you can pass a range ▼ or a date & operator
328
359
  date: ['>=', DateTime.now - 2.year],
329
360
  status: ['=', XeroRuby::Accounting::Invoice::PAID]
@@ -344,9 +375,8 @@ opts = {
344
375
  }
345
376
  xero_client.accounting_api.get_contacts(tenant_id, opts).contacts
346
377
 
347
- # for more complex where filtering that requires a null check, pass those in as a string
378
+ # for more complex chained filtering that requires a null check pass those in as a string
348
379
  # see https://developer.xero.com/documentation/api/requests-and-responses for more
349
-
350
380
  opts = {
351
381
  where: {
352
382
  email_address: '!=null&&EmailAddress.StartsWith("chris.knight@")'
@@ -374,34 +404,39 @@ opts = {
374
404
  xero_client.accounting_api.get_bank_transfers(tenant_id, opts).bank_transfers
375
405
  ```
376
406
 
377
- 1) Not all `opts` parameter combinations are available for all endpoints, and there are likely some undiscovered edge cases. If you encounter a filter / sort / where clause that seems buggy open an issue and we will dig.
407
+ - Not all `opts` parameter combinations are available for all endpoints
408
+
409
+ - Some opts string values may need PascalCasing to match casing defined in our [core API docs](https://developer.xero.com/documentation/api/api-overview) like `opts = { order: 'UpdatedDateUtc DESC'}`
410
+
411
+ ---
412
+ ## Contributing
413
+ PRs, issues, and discussion are highly appreciated and encouraged. Note that the majority of this project is generated code based on [Xero's OpenAPI specs](https://github.com/XeroAPI/Xero-OpenAPI) - PR's will be evaluated and pre-merge will be incorporated into the root generation templates.
378
414
 
379
- 2) Some opts string values may need PascalCasing to match casing defined in our [core API docs](https://developer.xero.com/documentation/api/api-overview).
380
- * `opts = { order: 'UpdatedDateUtc DESC'}`
415
+ Please add tests for net new functionality and make existing test suite succeeds.
381
416
 
382
- 3) If you have use cases outside of these examples let us know.
383
- ## Developing locally
384
- To develop this gem locally against your project you can use the following development pattern:
417
+ `$ rspec spec/`
418
+ ### Versioning
419
+ We do our best to keep OS industry `semver` standards, but we can make mistakes! If something is not accurately reflected in a version's release notes please let the team know.
420
+ ### Developing locally
421
+ To develop or test this gem locally against your project we find the following development pattern easiest
385
422
 
386
- > xero-ruby
387
423
  ```bash
424
+ cd xero-ruby
388
425
  gem build
389
426
  mv xero-ruby-<vsn>.gem xero-ruby.gem
390
427
  pwd
391
428
  => /Users/your.user/code/sdks/xero-ruby/
392
- ```
393
429
 
394
- > https://github.com/XeroAPI/xero-ruby-oauth2-app
395
- Replace gem file with local path:
396
- ```bash
430
+ cd ..
431
+ cd xero-ruby-oauth2-app/
432
+
433
+ # xero-ruby-oauth2-app/Gemfile
397
434
  gem 'xero-ruby', path: '/Users/your.user/code/sdks/xero-ruby/'
398
435
  bundle install
399
436
  ```
400
437
 
401
- ## Testing
402
- * `rspec spec/`
403
-
404
438
  ## Participating in Xero’s developer community
439
+
405
440
  This SDK is one of a number of SDK’s that the Xero Developer team builds and maintains. We are grateful for all the contributions that the community makes.
406
441
 
407
442
  Here are a few things you should be aware of as a contributor: