Skip to content
This repository has been archived by the owner on Sep 11, 2018. It is now read-only.

Commit

Permalink
Merge 8511331 into 1720e4d
Browse files Browse the repository at this point in the history
  • Loading branch information
buiatte committed Aug 21, 2018
2 parents 1720e4d + 8511331 commit f5ef472
Show file tree
Hide file tree
Showing 57 changed files with 407 additions and 476 deletions.
2 changes: 1 addition & 1 deletion .env.test
@@ -1,4 +1,4 @@
PASSAPORTE_WEB_URL=https://sandbox.v2.passaporteweb.com.br
NEXAAS_ID_URL=https://sandbox.id.nexaas.com
APPLICATION_TOKEN=QMKAZVAZ6NBR3IO2MCEHOGKVCY
APPLICATION_SECRET=HXKQYGAQ4JHR5HR73SKOQ5NI6E
USER_NAME=luiz.buiatte+pw.api.test@nexaas.com
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,4 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in passaporteweb-client.gemspec
# Specify your gem's dependencies in nexaas-id-client.gemspec
gemspec
10 changes: 5 additions & 5 deletions Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
passaporteweb-client (0.5.0)
nexaas-id-client (0.5.0)
multi_json (~> 1.11)
oauth2 (~> 1.4.0)
virtus (~> 1.0)
Expand Down Expand Up @@ -64,7 +64,7 @@ GEM
pry-byebug (3.4.2)
byebug (~> 9.0)
pry (~> 0.10)
public_suffix (3.0.1)
public_suffix (3.0.3)
rack (2.0.5)
rake (12.0.0)
rdoc (6.0.4)
Expand Down Expand Up @@ -102,7 +102,7 @@ GEM
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
webmock (3.1.1)
webmock (3.4.2)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
Expand All @@ -117,14 +117,14 @@ DEPENDENCIES
dotenv (~> 2.5.0)
faraday-cookie_jar (~> 0.0.6)
json (~> 2.1)
passaporteweb-client!
nexaas-id-client!
pry-byebug (~> 3.4)
rake (~> 12.0)
rdoc (~> 6.0)
rspec (~> 3.6)
simplecov (~> 0.14)
vcr (~> 2.4)
webmock (~> 3.1.0, >= 3.1.1)
webmock (~> 3.4)

BUNDLED WITH
1.16.3
60 changes: 30 additions & 30 deletions README.md
@@ -1,34 +1,34 @@
# passaporteweb-client-ruby
# nexaas-id-client-ruby

This is the official Ruby client for the [PassaporteWeb 2](https://v2.passaporteweb.com.br) API.
This is the official Ruby client for the [Nexaas ID](https://id.nexaas.com) API.

[![Gem Version](https://badge.fury.io/rb/passaporteweb-client.png)](https://rubygems.org/gems/passaporteweb-client)
[![Build Status](https://travis-ci.org/myfreecomm/passaporteweb-client-ruby.png?branch=master)](https://travis-ci.org/myfreecomm/passaporteweb-client-ruby)
[![Test Coverage](https://coveralls.io/repos/myfreecomm/passaporteweb-client-ruby/badge.png?branch=master)](https://coveralls.io/r/myfreecomm/passaporteweb-client-ruby)
[![Code Climate Grade](https://codeclimate.com/github/myfreecomm/passaporteweb-client-ruby.png)](https://codeclimate.com/github/myfreecomm/passaporteweb-client-ruby)
[![Inline docs](http://inch-ci.org/github/myfreecomm/passaporteweb-client-ruby.svg)](http://inch-ci.org/github/myfreecomm/passaporteweb-client-ruby)
[![Gem Version](https://badge.fury.io/rb/nexaas-id-client.png)](https://rubygems.org/gems/nexaas-id-client)
[![Build Status](https://travis-ci.org/myfreecomm/nexaas-id-client-ruby.png?branch=master)](https://travis-ci.org/myfreecomm/nexaas-id-client-ruby)
[![Test Coverage](https://coveralls.io/repos/myfreecomm/nexaas-id-client-ruby/badge.png?branch=master)](https://coveralls.io/r/myfreecomm/nexaas-id-client-ruby)
[![Code Climate Grade](https://codeclimate.com/github/myfreecomm/nexaas-id-client-ruby.png)](https://codeclimate.com/github/myfreecomm/nexaas-id-client-ruby)
[![Inline docs](http://inch-ci.org/github/myfreecomm/nexaas-id-client-ruby.svg)](http://inch-ci.org/github/myfreecomm/nexaas-id-client-ruby)

PassaporteWeb API docs: https://app.passaporteweb.com.br/static/docs/
Nexaas ID API docs: https://id.nexaas.com/static/docs/

passaporteweb-client-ruby RDoc documentation: http://rubydoc.info/github/myfreecomm/passaporteweb-client-ruby/frames/
nexaas-id-client-ruby RDoc documentation: http://rubydoc.info/github/myfreecomm/nexaas-id-client-ruby/frames/

The {RDoc}[http://rubydoc.info/github/myfreecomm/passaporteweb-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.
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.

This client only uses the API of PassaporteWeb. To authenticate users via OAuth2 in Ruby, see the {omni_auth_passaporte_web gem}[https://rubygems.org/gems/omni_auth_passaporte_web] ({code}[https://github.com/myfreecomm/omniauth-passaporte_web] and {example of use}[https://github.com/myfreecomm/passaporte-web-2-demo-apps]).
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)).

## Installation

Add this line to your application's Gemfile:

gem 'passaporteweb-client', require: 'passaporte_web'
gem 'nexaas-id-client', require: 'nexaas_id'

And then execute:

$ bundle

Or install it yourself as:

$ gem install passaporteweb-client
$ gem install nexaas-id-client

## Support

Expand All @@ -38,15 +38,15 @@ This gem supports Ruby 2.1 or superior.

### Create a new application

Go to https://v2.passaporteweb.com.br/applications and create a new application in your PassaporteWeb account.
Go to https://id.nexaas.com/applications and create a new application in your Nexaas ID account.

### Use PassaporteWeb.configure to setup your environment
### Use NexaasID.configure to setup your environment

```ruby
require 'passaporte_web'
require 'nexaas_id'

PassaporteWeb.configure do |c|
c.url = 'http://v2.sandbox.passaporteweb.com.br' # defaults to 'https://v2.passaporteweb.com.br' if omitted
NexaasID.configure do |c|
c.url = 'https://sandbox.id.nexaas.com' # defaults to 'https://id.nexaas.com' if omitted
c.user_agent = 'My App v1.0' # optional, but you should pass a custom user-agent identifying your app
c.application_token = 'your-application-token'
c.application_secret = 'your-application-secret'
Expand All @@ -56,15 +56,15 @@ end
## Usage

The API can be used to access resources owned by an `Identity`, which requires previous authorization from the
corresponding user (see the {omni_auth_passaporte_web gem}[https://rubygems.org/gems/omni_auth_passaporte_web]),
corresponding user (see the {omni_auth_nexaas_id gem}[https://rubygems.org/gems/omni_auth_nexaas_id]),
or resources owned by an `Application`, which only requires the application's credentials.

### Resources owned by an Identity

#### Create an instance of PassaporteWeb::Client::Identity, as below:
#### Create an instance of NexaasID::Client::Identity, as below:

```ruby
client = PassaporteWeb::Client::Identity.new(user_credentials)
client = NexaasID::Client::Identity.new(user_credentials)
```

Here, `user_crendentials` is an object that must have the following attributes available for reading/writing:
Expand All @@ -76,9 +76,9 @@ Here, `user_crendentials` is an object that must have the following attributes a
As long as these attributes are available, your object can be of any class (an `Active Record` object or a
simple `OpenStruct`, for instance); the client won't make any assumptions about its nature. Your application is responsible
for obtaining the initial values for these attributes (through the OAuth2 Authorization Flow, using the
{omni_auth_passaporte_web gem}[https://rubygems.org/gems/omni_auth_passaporte_web]) and storing them as appropriate
{omni_auth_nexaas_id gem}[https://rubygems.org/gems/omni_auth_nexaas_id]) and storing them as appropriate
(you might store them using a Users table for instance, or even in your user's session). The client WILL updated these
attributes if the token has to be refreshed (PassaporteWeb uses a TTL of 2 hours for access tokens) and your application
attributes if the token has to be refreshed (Nexaas ID uses a TTL of 2 hours for access tokens) and your application
needs to update its storage when that happens.

#### Now you have access to the following endpoints:
Expand All @@ -90,7 +90,7 @@ needs to update its storage when that happens.
#### Examples

```ruby
client = PassaporteWeb::Client::Identity.new(user_credentials)
client = NexaasID::Client::Identity.new(user_credentials)

profile_resource = client.profile

Expand All @@ -105,7 +105,7 @@ contacts.phone_numbers # ['+55 21 12345678']

sign_up_resource = client.sign_up

# Invites another user to PassaporteWeb on behalf of the current user
# Invites another user to Nexaas ID on behalf of the current user
sign_up = sign_up_resource.create('another.john@example.com')
sign_up.id # '1061a775-b86c-4082-b801-767f651fa4c7'
sign_up.email # 'another.john@example.com'
Expand All @@ -119,10 +119,10 @@ navbar_url = widget_resource.navbar_url

### Resources not owned by an Identity

#### Create an instance of PassaporteWeb::Client::Application, as below:
#### Create an instance of NexaasID::Client::Application, as below:

```ruby
client = PassaporteWeb::Client::Application.new
client = NexaasID::Client::Application.new
```

#### Now you have access to the following endpoints:
Expand All @@ -132,11 +132,11 @@ client = PassaporteWeb::Client::Application.new
#### Examples

```ruby
client = PassaporteWeb::Client::Application.new
client = NexaasID::Client::Application.new

sign_up_resource = client.sign_up

# Invites another user to PassaporteWeb on behalf of the application
# Invites another user to Nexaas ID on behalf of the application
sign_up = sign_up_resource.create('another.john@example.com')
sign_up.id # '1061a775-b86c-4082-b801-767f651fa4c7'
sign_up.email # 'another.john@example.com'
Expand All @@ -145,7 +145,7 @@ sign_up.requester # nil

### Error handling

In case of a transport or OAuth error, an instance of PassaporteWeb::Client::Exception will be raised by the client.
In case of a transport or OAuth error, an instance of NexaasID::Client::Exception will be raised by the client.
This exception can be inspected using the methods `status`, `headers` and `body`.

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -2,7 +2,7 @@ require "bundler/gem_tasks"

require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
rdoc.title = 'passaporteweb-client-ruby'
rdoc.title = 'nexaas-id-client-ruby'
rdoc.main = "README.rdoc"
rdoc.rdoc_dir = 'doc'
rdoc.rdoc_files.include("README.rdoc","lib/**/*.rb")
Expand Down
40 changes: 40 additions & 0 deletions lib/nexaas_id.rb
@@ -0,0 +1,40 @@
# encoding: utf-8

require 'oauth2'
require 'multi_json'

require "nexaas_id/version"
require "nexaas_id/configuration"

require "nexaas_id/client"
require "nexaas_id/client/application"
require "nexaas_id/client/exception"
require "nexaas_id/client/exception_wrapper"
require "nexaas_id/client/identity"
require "nexaas_id/client/oauth"

require "nexaas_id/entities"
require "nexaas_id/entities/base"

require "nexaas_id/entities/profile"
require "nexaas_id/entities/profile/professional_info"
require "nexaas_id/entities/profile/contacts"
require "nexaas_id/entities/profile/emails"

require "nexaas_id/entities/sign_up"

require "nexaas_id/resources"
require "nexaas_id/resources/base"
require "nexaas_id/resources/profile"
require "nexaas_id/resources/sign_up"
require "nexaas_id/resources/widget"

module NexaasID
def self.configuration
@configuration ||= Configuration.new
end

def self.configure
yield(configuration) if block_given?
end
end
2 changes: 2 additions & 0 deletions lib/nexaas_id/client.rb
@@ -0,0 +1,2 @@
module NexaasID::Client
end
35 changes: 35 additions & 0 deletions lib/nexaas_id/client/application.rb
@@ -0,0 +1,35 @@
# NexaasID Client for resources not owned by an Identity
#
# [API]
# Documentation:
#
# @example Inviting a new user:
# client = NexaasID::Client::Application.new
# client.sign_up.create(invited: 'john.doe@example.com')
#
class NexaasID::Client::Application

def initialize
@tokens = {}
end

# Provides a SignUp resource.
# @return [NexaasID::Resources::SignUp] the signup resource.
def sign_up
NexaasID::Resources::SignUp.new(token(:invite))
end

private

attr_reader :tokens, :credentials

def client
@client ||= NexaasID::Client::OAuth.build
end

def token(scope = nil)
token = tokens[scope]
return token unless token.nil? || token.expired?
tokens[scope] = NexaasID::Client::ExceptionWrapper.new(client.client_credentials.get_token(scope: scope))
end
end
Expand Up @@ -3,7 +3,7 @@
# [API]
# Documentation:
#
class PassaporteWeb::Client::Exception < StandardError
class NexaasID::Client::Exception < StandardError
# Creates an instance of this exception.
#
# @param [String] message The exception message.
Expand Down
@@ -1,10 +1,10 @@
# Delegator class which intercepts exceptions raised by OAuth::AccessToken methods
# and wraps them in [PassaporteWeb::Client::Exception] exceptions.
# and wraps them in [NexaasID::Client::Exception] exceptions.
#
# [API]
# Documentation:
#
class PassaporteWeb::Client::ExceptionWrapper < SimpleDelegator
class NexaasID::Client::ExceptionWrapper < SimpleDelegator
def refresh(*args)
call_with_rescue { super }
end
Expand All @@ -18,6 +18,6 @@ def request(*args, &block)
def call_with_rescue
yield
rescue Faraday::ClientError, OAuth2::Error => exception
raise PassaporteWeb::Client::Exception.new(exception.message, exception.response)
raise NexaasID::Client::Exception.new(exception.message, exception.response)
end
end
@@ -1,13 +1,13 @@
# PassaporteWeb Client for resources owned by an Identity
# NexaasID Client for resources owned by an Identity
#
# [API]
# Documentation:
#
# @example Obtaining a user's profile:
# client = PassaporteWeb::Client::Identity.new(user_credentials)
# client = NexaasID::Client::Identity.new(user_credentials)
# client.profile.get
#
class PassaporteWeb::Client::Identity
class NexaasID::Client::Identity
# Creates an instance of this client.
#
# @param [
Expand All @@ -17,25 +17,25 @@ class PassaporteWeb::Client::Identity
# #expires_in, #expires_in=] The user credentials, obtained through the OAuth2 authorization flow.
def initialize(credentials)
@credentials = credentials
@token = PassaporteWeb::Client::ExceptionWrapper.new(OAuth2::AccessToken.from_hash(client, hash))
@token = NexaasID::Client::ExceptionWrapper.new(OAuth2::AccessToken.from_hash(client, hash))
end

# Provides a Profile resource.
# @return [PassaporteWeb::Resources::Profile] the profile resource.
# @return [NexaasID::Resources::Profile] the profile resource.
def profile
PassaporteWeb::Resources::Profile.new(api)
NexaasID::Resources::Profile.new(api)
end

# Provides a SignUp resource.
# @return [PassaporteWeb::Resources::SignUp] the signup resource.
# @return [NexaasID::Resources::SignUp] the signup resource.
def sign_up
PassaporteWeb::Resources::SignUp.new(api)
NexaasID::Resources::SignUp.new(api)
end

# Provides a Widget resource.
# @return [PassaporteWeb::Resources::Widget] the widget resource.
# @return [NexaasID::Resources::Widget] the widget resource.
def widget
PassaporteWeb::Resources::Widget.new(api)
NexaasID::Resources::Widget.new(api)
end

private
Expand All @@ -50,7 +50,7 @@ def api
end

def client
@client ||= PassaporteWeb::Client::OAuth.build
@client ||= NexaasID::Client::OAuth.build
end

def hash
Expand All @@ -59,7 +59,7 @@ def hash

def refresh_token
token.refresh!.tap do |token|
self.token = PassaporteWeb::Client::ExceptionWrapper.new(token)
self.token = NexaasID::Client::ExceptionWrapper.new(token)
credentials.access_token = token.token
(ATTRIBUTES - [:access_token]).each { |attr| credentials.send("#{attr}=", token.send(attr)) }
end
Expand Down

0 comments on commit f5ef472

Please sign in to comment.