nexaas_id-client 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.env.test +5 -0
  4. data/.gitignore +25 -0
  5. data/.hound.yml +1063 -0
  6. data/.rspec +2 -0
  7. data/.travis.yml +4 -0
  8. data/Gemfile +4 -0
  9. data/Gemfile.lock +130 -0
  10. data/LICENSE +202 -0
  11. data/README.md +157 -0
  12. data/Rakefile +16 -0
  13. data/lib/nexaas_id/client/application.rb +35 -0
  14. data/lib/nexaas_id/client/exception.rb +30 -0
  15. data/lib/nexaas_id/client/exception_wrapper.rb +23 -0
  16. data/lib/nexaas_id/client/identity.rb +67 -0
  17. data/lib/nexaas_id/client/oauth.rb +20 -0
  18. data/lib/nexaas_id/client.rb +2 -0
  19. data/lib/nexaas_id/configuration.rb +13 -0
  20. data/lib/nexaas_id/entities/base.rb +5 -0
  21. data/lib/nexaas_id/entities/collection.rb +53 -0
  22. data/lib/nexaas_id/entities/profile/contacts.rb +4 -0
  23. data/lib/nexaas_id/entities/profile/emails.rb +4 -0
  24. data/lib/nexaas_id/entities/profile/professional_info.rb +6 -0
  25. data/lib/nexaas_id/entities/profile.rb +14 -0
  26. data/lib/nexaas_id/entities/sign_up.rb +5 -0
  27. data/lib/nexaas_id/entities.rb +2 -0
  28. data/lib/nexaas_id/resources/base.rb +34 -0
  29. data/lib/nexaas_id/resources/profile.rb +67 -0
  30. data/lib/nexaas_id/resources/sign_up.rb +30 -0
  31. data/lib/nexaas_id/resources/widget.rb +28 -0
  32. data/lib/nexaas_id/resources.rb +2 -0
  33. data/lib/nexaas_id/version.rb +4 -0
  34. data/lib/nexaas_id.rb +40 -0
  35. data/nexaas_id-client.gemspec +43 -0
  36. data/spec/nexaas_id/client/application_spec.rb +13 -0
  37. data/spec/nexaas_id/client/exception_spec.rb +15 -0
  38. data/spec/nexaas_id/client/exception_wrapper_spec.rb +48 -0
  39. data/spec/nexaas_id/client/identity_spec.rb +29 -0
  40. data/spec/nexaas_id/configuration_spec.rb +38 -0
  41. data/spec/nexaas_id/resources/profile_spec.rb +54 -0
  42. data/spec/nexaas_id/resources/sign_up_spec.rb +33 -0
  43. data/spec/nexaas_id/resources/widget_spec.rb +18 -0
  44. data/spec/nexaas_id_spec.rb +30 -0
  45. data/spec/spec_helper.rb +43 -0
  46. data/spec/support/authorization.rb +62 -0
  47. metadata +331 -0
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format progress
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.4
4
+ - 2.2.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nexaas-id-client.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,130 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nexaas_id-client (0.5.0)
5
+ multi_json (~> 1.11)
6
+ oauth2 (~> 1.4.0)
7
+ virtus (~> 1.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.5.2)
13
+ public_suffix (>= 2.0.2, < 4.0)
14
+ awesome_print (1.8.0)
15
+ axiom-types (0.1.1)
16
+ descendants_tracker (~> 0.0.4)
17
+ ice_nine (~> 0.11.0)
18
+ thread_safe (~> 0.3, >= 0.3.1)
19
+ byebug (9.0.6)
20
+ coderay (1.1.1)
21
+ coercible (1.0.0)
22
+ descendants_tracker (~> 0.0.1)
23
+ coveralls (0.8.21)
24
+ json (>= 1.8, < 3)
25
+ simplecov (~> 0.14.1)
26
+ term-ansicolor (~> 1.3)
27
+ thor (~> 0.19.4)
28
+ tins (~> 1.6)
29
+ crack (0.4.3)
30
+ safe_yaml (~> 1.0.0)
31
+ descendants_tracker (0.0.4)
32
+ thread_safe (~> 0.3, >= 0.3.1)
33
+ diff-lcs (1.3)
34
+ docile (1.1.5)
35
+ domain_name (0.5.20180417)
36
+ unf (>= 0.0.5, < 1.0.0)
37
+ dotenv (2.5.0)
38
+ equalizer (0.0.11)
39
+ faraday (0.12.2)
40
+ multipart-post (>= 1.2, < 3)
41
+ faraday-cookie_jar (0.0.6)
42
+ faraday (>= 0.7.4)
43
+ http-cookie (~> 1.0.0)
44
+ hashdiff (0.3.7)
45
+ http-cookie (1.0.3)
46
+ domain_name (~> 0.5)
47
+ ice_nine (0.11.2)
48
+ json (2.1.0)
49
+ jwt (1.5.6)
50
+ method_source (0.8.2)
51
+ multi_json (1.13.1)
52
+ multi_xml (0.6.0)
53
+ multipart-post (2.0.0)
54
+ oauth2 (1.4.0)
55
+ faraday (>= 0.8, < 0.13)
56
+ jwt (~> 1.0)
57
+ multi_json (~> 1.3)
58
+ multi_xml (~> 0.5)
59
+ rack (>= 1.2, < 3)
60
+ pry (0.10.4)
61
+ coderay (~> 1.1.0)
62
+ method_source (~> 0.8.1)
63
+ slop (~> 3.4)
64
+ pry-byebug (3.4.2)
65
+ byebug (~> 9.0)
66
+ pry (~> 0.10)
67
+ public_suffix (3.0.3)
68
+ rack (2.0.5)
69
+ rake (12.0.0)
70
+ rdoc (6.0.4)
71
+ rspec (3.6.0)
72
+ rspec-core (~> 3.6.0)
73
+ rspec-expectations (~> 3.6.0)
74
+ rspec-mocks (~> 3.6.0)
75
+ rspec-core (3.6.0)
76
+ rspec-support (~> 3.6.0)
77
+ rspec-expectations (3.6.0)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.6.0)
80
+ rspec-mocks (3.6.0)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.6.0)
83
+ rspec-support (3.6.0)
84
+ safe_yaml (1.0.4)
85
+ simplecov (0.14.1)
86
+ docile (~> 1.1.0)
87
+ json (>= 1.8, < 3)
88
+ simplecov-html (~> 0.10.0)
89
+ simplecov-html (0.10.1)
90
+ slop (3.6.0)
91
+ term-ansicolor (1.6.0)
92
+ tins (~> 1.0)
93
+ thor (0.19.4)
94
+ thread_safe (0.3.6)
95
+ tins (1.15.0)
96
+ unf (0.1.4)
97
+ unf_ext
98
+ unf_ext (0.0.7.5)
99
+ vcr (2.9.3)
100
+ virtus (1.0.5)
101
+ axiom-types (~> 0.1)
102
+ coercible (~> 1.0)
103
+ descendants_tracker (~> 0.0, >= 0.0.3)
104
+ equalizer (~> 0.0, >= 0.0.9)
105
+ webmock (3.4.2)
106
+ addressable (>= 2.3.6)
107
+ crack (>= 0.3.2)
108
+ hashdiff
109
+
110
+ PLATFORMS
111
+ ruby
112
+
113
+ DEPENDENCIES
114
+ awesome_print (~> 1.8)
115
+ bundler (~> 1.9)
116
+ coveralls (~> 0.8)
117
+ dotenv (~> 2.5.0)
118
+ faraday-cookie_jar (~> 0.0.6)
119
+ json (~> 2.1)
120
+ nexaas_id-client!
121
+ pry-byebug (~> 3.4)
122
+ rake (~> 12.0)
123
+ rdoc (~> 6.0)
124
+ rspec (~> 3.6)
125
+ simplecov (~> 0.14)
126
+ vcr (~> 2.4)
127
+ webmock (~> 3.4)
128
+
129
+ BUNDLED WITH
130
+ 1.16.4
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2013 Myfreecomm - Free Communities Consultoria Em Informatica Ltda
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,157 @@
1
+ # nexaas-id-client-ruby
2
+
3
+ This is the official Ruby client for the [Nexaas ID](https://id.nexaas.com) API.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/nexaas-id-client.png)](https://rubygems.org/gems/nexaas-id-client)
6
+ [![Build Status](https://travis-ci.org/myfreecomm/nexaas-id-client-ruby.png?branch=master)](https://travis-ci.org/myfreecomm/nexaas-id-client-ruby)
7
+ [![Test Coverage](https://coveralls.io/repos/myfreecomm/nexaas-id-client-ruby/badge.png?branch=master)](https://coveralls.io/r/myfreecomm/nexaas-id-client-ruby)
8
+ [![Code Climate Grade](https://codeclimate.com/github/myfreecomm/nexaas-id-client-ruby.png)](https://codeclimate.com/github/myfreecomm/nexaas-id-client-ruby)
9
+ [![Inline docs](http://inch-ci.org/github/myfreecomm/nexaas-id-client-ruby.svg)](http://inch-ci.org/github/myfreecomm/nexaas-id-client-ruby)
10
+
11
+ Nexaas ID API docs: https://id.nexaas.com/static/docs/
12
+
13
+ nexaas-id-client-ruby RDoc documentation: http://rubydoc.info/github/myfreecomm/nexaas-id-client-ruby/frames/
14
+
15
+ The [RDoc](http://rubydoc.info/github/myfreecomm/nexaas-id-client-ruby/frames/) is the best place to learn how to use this client. A few example uses are listed below. See the mapping of API endpoints to this client code below as well to find what you need.
16
+
17
+ This client only uses the API of Nexaas ID. To authenticate users via OAuth2 in Ruby, see the [omni_auth_nexaas_id gem](https://rubygems.org/gems/omni_auth_nexaas_id) ([code](https://github.com/myfreecomm/omniauth-nexaas_id) and [example of use](https://github.com/myfreecomm/nexaas-id-demo-apps)).
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ gem 'nexaas_id-client', require: 'nexaas_id'
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install nexaas_id-client
32
+
33
+ ## Support
34
+
35
+ This gem supports Ruby 2.1 or superior.
36
+
37
+ ## Configuration
38
+
39
+ ### Create a new application
40
+
41
+ Go to https://id.nexaas.com/applications and create a new application in your Nexaas ID account.
42
+
43
+ ### Use NexaasID.configure to setup your environment
44
+
45
+ ```ruby
46
+ require 'nexaas_id'
47
+
48
+ NexaasID.configure do |c|
49
+ c.url = 'https://sandbox.id.nexaas.com' # defaults to 'https://id.nexaas.com' if omitted
50
+ c.user_agent = 'My App v1.0' # optional, but you should pass a custom user-agent identifying your app
51
+ c.application_token = 'your-application-token'
52
+ c.application_secret = 'your-application-secret'
53
+ end
54
+ ```
55
+
56
+ ## Usage
57
+
58
+ The API can be used to access resources owned by an `Identity`, which requires previous authorization from the
59
+ corresponding user (see the {omni_auth_nexaas_id gem}[https://rubygems.org/gems/omni_auth_nexaas_id]),
60
+ or resources owned by an `Application`, which only requires the application's credentials.
61
+
62
+ ### Resources owned by an Identity
63
+
64
+ #### Create an instance of NexaasID::Client::Identity, as below:
65
+
66
+ ```ruby
67
+ client = NexaasID::Client::Identity.new(user_credentials)
68
+ ```
69
+
70
+ Here, `user_crendentials` is an object that must have the following attributes available for reading/writing:
71
+ * access_token
72
+ * refresh_token
73
+ * expires_in
74
+ * expires_at
75
+
76
+ As long as these attributes are available, your object can be of any class (an `Active Record` object or a
77
+ simple `OpenStruct`, for instance); the client won't make any assumptions about its nature. Your application is responsible
78
+ for obtaining the initial values for these attributes (through the OAuth2 Authorization Flow, using the
79
+ {omni_auth_nexaas_id gem}[https://rubygems.org/gems/omni_auth_nexaas_id]) and storing them as appropriate
80
+ (you might store them using a Users table for instance, or even in your user's session). The client WILL updated these
81
+ attributes if the token has to be refreshed (Nexaas ID uses a TTL of 2 hours for access tokens) and your application
82
+ needs to update its storage when that happens.
83
+
84
+ #### Now you have access to the following endpoints:
85
+
86
+ * [Profile API](TODO: doc link) as `client.profile`
87
+ * [SignUp API](TODO: doc link) as `client.signup`
88
+ * [Widget API](TODO: doc link) as `client.widget`
89
+
90
+ #### Examples
91
+
92
+ ```ruby
93
+ client = NexaasID::Client::Identity.new(user_credentials)
94
+
95
+ profile_resource = client.profile
96
+
97
+ profile = profile_resource.get # Obtains user's profile
98
+ profile.id # '57bb5938-d0c5-439a-9986-e5c565124beb'
99
+ profile.email # 'john.doe@example.com'
100
+ profile.name # 'John Doe'
101
+
102
+ contacts = profile_resource.professional_info # Obtains user's professional information
103
+ contacts.id # '57bb5938-d0c5-439a-9986-e5c565124beb'
104
+ contacts.phone_numbers # ['+55 21 12345678']
105
+
106
+ sign_up_resource = client.sign_up
107
+
108
+ # Invites another user to Nexaas ID on behalf of the current user
109
+ sign_up = sign_up_resource.create('another.john@example.com')
110
+ sign_up.id # '1061a775-b86c-4082-b801-767f651fa4c7'
111
+ sign_up.email # 'another.john@example.com'
112
+ sign_up.requester # '57bb5938-d0c5-439a-9986-e5c565124beb'
113
+
114
+ widget_resource = client.widget
115
+
116
+ # Obtains navigation bar URL with current user information and logout button
117
+ navbar_url = widget_resource.navbar_url
118
+ ```
119
+
120
+ ### Resources not owned by an Identity
121
+
122
+ #### Create an instance of NexaasID::Client::Application, as below:
123
+
124
+ ```ruby
125
+ client = NexaasID::Client::Application.new
126
+ ```
127
+
128
+ #### Now you have access to the following endpoints:
129
+
130
+ * [SignUp API](TODO: doc link) as `client.signup`
131
+
132
+ #### Examples
133
+
134
+ ```ruby
135
+ client = NexaasID::Client::Application.new
136
+
137
+ sign_up_resource = client.sign_up
138
+
139
+ # Invites another user to Nexaas ID on behalf of the application
140
+ sign_up = sign_up_resource.create('another.john@example.com')
141
+ sign_up.id # '1061a775-b86c-4082-b801-767f651fa4c7'
142
+ sign_up.email # 'another.john@example.com'
143
+ sign_up.requester # nil
144
+ ```
145
+
146
+ ### Error handling
147
+
148
+ In case of a transport or OAuth error, an instance of NexaasID::Client::Exception will be raised by the client.
149
+ This exception can be inspected using the methods `status`, `headers` and `body`.
150
+
151
+ ## Contributing
152
+
153
+ 1. Fork it
154
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
155
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
156
+ 4. Push to the branch (`git push origin my-new-feature`)
157
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rdoc/task'
4
+ Rake::RDocTask.new do |rdoc|
5
+ rdoc.title = 'nexaas-id-client-ruby'
6
+ rdoc.main = "README.rdoc"
7
+ rdoc.rdoc_dir = 'doc'
8
+ rdoc.rdoc_files.include("README.rdoc","lib/**/*.rb")
9
+ # rdoc.generator = 'darkfish'
10
+ end
11
+
12
+ require 'rspec/core/rake_task'
13
+ RSpec::Core::RakeTask.new(:spec)
14
+
15
+ task :test => :spec
16
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ # Nexaas ID Client for resources not owned by an Identity
2
+ #
3
+ # [API]
4
+ # Documentation:
5
+ #
6
+ # @example Inviting a new user:
7
+ # client = NexaasID::Client::Application.new
8
+ # client.sign_up.create(invited: 'john.doe@example.com')
9
+ #
10
+ class NexaasID::Client::Application
11
+
12
+ def initialize
13
+ @tokens = {}
14
+ end
15
+
16
+ # Provides a SignUp resource.
17
+ # @return [NexaasID::Resources::SignUp] the signup resource.
18
+ def sign_up
19
+ NexaasID::Resources::SignUp.new(token(:invite))
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :tokens, :credentials
25
+
26
+ def client
27
+ @client ||= NexaasID::Client::OAuth.build
28
+ end
29
+
30
+ def token(scope = nil)
31
+ token = tokens[scope]
32
+ return token unless token.nil? || token.expired?
33
+ tokens[scope] = NexaasID::Client::ExceptionWrapper.new(client.client_credentials.get_token(scope: scope))
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ # Exception class raised whenever the underlying client receives an error response.
2
+ #
3
+ # [API]
4
+ # Documentation:
5
+ #
6
+ class NexaasID::Client::Exception < StandardError
7
+ # Creates an instance of this exception.
8
+ #
9
+ # @param [String] message The exception message.
10
+ # @param [#status, #headers, #body] response The client's HTTP response object.
11
+ def initialize(message, response)
12
+ super(message)
13
+ @response = response
14
+ end
15
+
16
+ # @return [Integer] HTTP response's status.
17
+ def status
18
+ @response.status if @response
19
+ end
20
+
21
+ # @return [Integer] HTTP response's headers.
22
+ def headers
23
+ @response.headers if @response
24
+ end
25
+
26
+ # @return [Integer] HTTP response's body.
27
+ def body
28
+ @response ? @response.body : ''
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ # Delegator class which intercepts exceptions raised by OAuth::AccessToken methods
2
+ # and wraps them in [NexaasID::Client::Exception] exceptions.
3
+ #
4
+ # [API]
5
+ # Documentation:
6
+ #
7
+ class NexaasID::Client::ExceptionWrapper < SimpleDelegator
8
+ def refresh(*args)
9
+ call_with_rescue { super }
10
+ end
11
+
12
+ def request(*args, &block)
13
+ call_with_rescue { super }
14
+ end
15
+
16
+ private
17
+
18
+ def call_with_rescue
19
+ yield
20
+ rescue Faraday::ClientError, OAuth2::Error => exception
21
+ raise NexaasID::Client::Exception.new(exception.message, exception.response)
22
+ end
23
+ end
@@ -0,0 +1,67 @@
1
+ # Nexaas ID Client for resources owned by an Identity
2
+ #
3
+ # [API]
4
+ # Documentation:
5
+ #
6
+ # @example Obtaining a user's profile:
7
+ # client = NexaasID::Client::Identity.new(user_credentials)
8
+ # client.profile.get
9
+ #
10
+ class NexaasID::Client::Identity
11
+ # Creates an instance of this client.
12
+ #
13
+ # @param [
14
+ # #access_token, #access_token=,
15
+ # #refresh_token, #refresh_token=,
16
+ # #expires_at, #expires_at=
17
+ # #expires_in, #expires_in=] The user credentials, obtained through the OAuth2 authorization flow.
18
+ def initialize(credentials)
19
+ @credentials = credentials
20
+ @token = NexaasID::Client::ExceptionWrapper.new(OAuth2::AccessToken.from_hash(client, hash))
21
+ end
22
+
23
+ # Provides a Profile resource.
24
+ # @return [NexaasID::Resources::Profile] the profile resource.
25
+ def profile
26
+ NexaasID::Resources::Profile.new(api)
27
+ end
28
+
29
+ # Provides a SignUp resource.
30
+ # @return [NexaasID::Resources::SignUp] the signup resource.
31
+ def sign_up
32
+ NexaasID::Resources::SignUp.new(api)
33
+ end
34
+
35
+ # Provides a Widget resource.
36
+ # @return [NexaasID::Resources::Widget] the widget resource.
37
+ def widget
38
+ NexaasID::Resources::Widget.new(api)
39
+ end
40
+
41
+ private
42
+
43
+ attr_reader :credentials
44
+ attr_accessor :token
45
+
46
+ ATTRIBUTES = %i[access_token refresh_token expires_at expires_in].freeze
47
+
48
+ def api
49
+ token.expired? ? refresh_token : token
50
+ end
51
+
52
+ def client
53
+ @client ||= NexaasID::Client::OAuth.build
54
+ end
55
+
56
+ def hash
57
+ ATTRIBUTES.map { |attr| [attr, credentials.send(attr)] }.to_h
58
+ end
59
+
60
+ def refresh_token
61
+ token.refresh!.tap do |token|
62
+ self.token = NexaasID::Client::ExceptionWrapper.new(token)
63
+ credentials.access_token = token.token
64
+ (ATTRIBUTES - [:access_token]).each { |attr| credentials.send("#{attr}=", token.send(attr)) }
65
+ end
66
+ end
67
+ end