Class: PassaporteWeb::Identity
- Inherits:
-
Object
- Object
- PassaporteWeb::Identity
- Includes:
- Attributable
- Defined in:
- lib/passaporte_web/identity.rb
Overview
Represents an Identity on PassaporteWeb, i.e. a person. When you sign up for PassaporteWeb, your ‘account’ there is an Identity.
Constant Summary collapse
- ATTRIBUTES =
[:accounts, :birth_date, :country, :cpf, :email, :first_name, :gender, :id_token, :is_active, :language, :last_name, :nickname, :notifications, :send_myfreecomm_news, :send_partner_news, :services, :timezone, :update_info_url, :uuid, :password, :password2, :must_change_password, :inhibit_activation_message, :tos, :bio, :position, :city, :company, :profession, :identity_info_url, :state, :email_list]
- UPDATABLE_ATTRIBUTES =
[:first_name, :last_name, :nickname, :cpf, :birth_date, :gender, :send_myfreecomm_news, :send_partner_news, :country, :language, :timezone, :bio, :position, :city, :company, :profession, :state]
- CREATABLE_ATTRIBUTES =
(UPDATABLE_ATTRIBUTES + [:email, :password, :password2, :must_change_password, :tos, :inhibit_activation_message])
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
-
.authenticate(email, password) ⇒ Object
Checks if an Identity exists on PassaporteWeb and if the password is correct.
-
.find(uuid, include_expired_accounts = false, include_other_services = false) ⇒ Object
Finds an Identity by it’s UUID.
-
.find_by_email(email, include_expired_accounts = false, include_other_services = false) ⇒ Object
Finds an Identity by it’s email (emails are unique on PassaporteWeb).
-
.profile(uuid) ⇒ Object
Finds an Identity Profile by it’s UUID (UUIDs are unique on PassaporteWeb).
-
.profile_by_email(email) ⇒ Object
Finds an Profile by it’s email (primary or secondary) using the ***DEPRECATED*** profile endpoint.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares one Identity with another, returns true if they have the same UUID.
-
#===(other) ⇒ Object
Returns true if both Identity are the same object.
-
#attributes ⇒ Object
Returns a hash with all attribures of the identity.
-
#authenticate(password) ⇒ Object
Checks if the supplied password is correct for the current Identity.
-
#initialize(attributes = {}) ⇒ Identity
constructor
Instanciates a new Identity with the supplied attributes.
- #persisted? ⇒ Boolean
-
#save ⇒ Object
Saves the Identity on PassaporteWeb, creating it if new or updating it if existing.
Constructor Details
#initialize(attributes = {}) ⇒ Identity
Instanciates a new Identity with the supplied attributes. Only the attributes listed on Identity::CREATABLE_ATTRIBUTES
are used when creating an Identity and on Identity::UPDATABLE_ATTRIBUTES
are used when updating an Identity.
Example:
identity = PassaporteWeb::Identity.new(
email: '[email protected]',
password: '123456',
password2: '123456',
must_change_password: false,
tos: true,
first_name: 'Fulano',
last_name: 'de Tal',
nickname: 'Fulaninho',
cpf: '342.766.570-40',
birth_date: '1983-04-19',
gender: 'M',
send_myfreecomm_news: true,
send_partner_news: false,
country: 'Brasil',
language: 'pt_BR',
timezone: 'GMT-3'
)
154 155 156 157 |
# File 'lib/passaporte_web/identity.rb', line 154 def initialize(attributes={}) set_attributes(attributes) @errors = {} end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
15 16 17 |
# File 'lib/passaporte_web/identity.rb', line 15 def errors @errors end |
Class Method Details
.authenticate(email, password) ⇒ Object
Checks if an Identity exists on PassaporteWeb and if the password is correct. Returns an instance of Identity for the supplied email if the password is correct (although with only basic attributes set). Returns false
if the password is wrong or if no Identity exists on PassaporteWeb with the supplied email. Use it to validate that a user is who he says he is.
API method: GET /accounts/api/auth/
API documentation: app.passaporteweb.com.br/static/docs/usuarios.html#get-accounts-api-auth
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/passaporte_web/identity.rb', line 97 def self.authenticate(email, password) response = Http.custom_auth_get( email, password, "/accounts/api/auth/" ) raise "unexpected response: #{response.code} - #{response.body}" unless response.code == 200 attributes_hash = MultiJson.decode(response.body) load_identity(attributes_hash) rescue *[RestClient::Unauthorized] => e false end |
.find(uuid, include_expired_accounts = false, include_other_services = false) ⇒ Object
Finds an Identity by it’s UUID. Returns the Identity instance with all fields set if successful. Raises a RestClient::ResourceNotFound
exception if no Identity exists with the supplied UUID.
If include_expired_accounts
is passed as true
, brings information about all accounts the Identity is related, regardless of the account’s expiration date.
If include_other_services
is passed as true
, brings information about accounts of all services the Identity is related to (not just the current logged in service / application).
API method: /accounts/api/identities/:uuid/
API documentation: app.passaporteweb.com.br/static/docs/usuarios.html#get-accounts-api-identities-uuid
30 31 32 33 34 35 36 37 |
# File 'lib/passaporte_web/identity.rb', line 30 def self.find(uuid, include_expired_accounts=false, include_other_services=false) response = Http.get( "/accounts/api/identities/#{uuid}/", {include_expired_accounts: include_expired_accounts, include_other_services: include_other_services} ) attributes_hash = MultiJson.decode(response.body) load_identity(attributes_hash) end |
.find_by_email(email, include_expired_accounts = false, include_other_services = false) ⇒ Object
Finds an Identity by it’s email (emails are unique on PassaporteWeb). Returns the Identity instance with all fields set if successful. Raises a RestClient::ResourceNotFound
exception if no Identity exists with the supplied email.
If include_expired_accounts
is passed as true
, brings information about all accounts the Identity is related, regardless of the account’s expiration date.
If include_other_services
is passed as true
, brings information about accounts of all services the Identity is related to (not just the current logged in service / application).
API method: GET /accounts/api/identities/?email=:email
API documentation: app.passaporteweb.com.br/static/docs/usuarios.html#get-accounts-api-identities-email-email
52 53 54 55 56 57 58 59 |
# File 'lib/passaporte_web/identity.rb', line 52 def self.find_by_email(email, include_expired_accounts=false, include_other_services=false) response = Http.get( "/accounts/api/identities/", {email: email, include_expired_accounts: include_expired_accounts, include_other_services: include_other_services} ) attributes_hash = MultiJson.decode(response.body) load_identity(attributes_hash) end |
.profile(uuid) ⇒ Object
Finds an Identity Profile by it’s UUID (UUIDs are unique on PassaporteWeb). Returns the Identity instance with all fields set if successful, including the email list (in case user has more than one email activated). Raises a RestClient::ResourceNotFound
exception if no Identity exists with the supplied UUID.
API method: GET /accounts/api/identities/:uuid/profile
API documentation: myfreecomm.github.io/passaporte-web/pweb/api/usuarios.html#get-accounts-api-identities-uuid-profile
69 70 71 72 73 |
# File 'lib/passaporte_web/identity.rb', line 69 def self.profile(uuid) response = Http.get("/accounts/api/identities/#{uuid}/profile") attributes_hash = MultiJson.decode(response.body) load_identity(attributes_hash) end |
.profile_by_email(email) ⇒ Object
Finds an Profile by it’s email (primary or secondary) using the ***DEPRECATED*** profile endpoint. Returns the Identity instance, with the primary email used by the Identity. Raises a RestClient::ResourceNotFound
exception if no Identity exists with the supplied email.
API method: GET /profile/api/info/?email=:email
API documentation: myfreecomm.github.io/passaporte-web/pweb/api/perfil.html#get-profile-api-info-email-email
83 84 85 86 87 |
# File 'lib/passaporte_web/identity.rb', line 83 def self.profile_by_email(email) response = Http.get("/profile/api/info/", email: email) attributes_hash = MultiJson.decode(response.body) load_identity(attributes_hash) end |
Instance Method Details
#==(other) ⇒ Object
Compares one Identity with another, returns true if they have the same UUID.
168 169 170 171 172 |
# File 'lib/passaporte_web/identity.rb', line 168 def ==(other) if self.respond_to?(:uuid) && other.respond_to?(:uuid) self.uuid == other.uuid end end |
#===(other) ⇒ Object
Returns true if both Identity are the same object.
175 176 177 |
# File 'lib/passaporte_web/identity.rb', line 175 def ===(other) self.object_id == other.object_id end |
#attributes ⇒ Object
Returns a hash with all attribures of the identity.
160 161 162 163 164 165 |
# File 'lib/passaporte_web/identity.rb', line 160 def attributes ATTRIBUTES.inject({}) do |hash, attribute| hash[attribute] = self.send(attribute) hash end end |
#authenticate(password) ⇒ Object
Checks if the supplied password is correct for the current Identity. Returns true
if the password matches or false
if the password is wrong. Use it to validate that a user is who he says he is.
API method: GET /accounts/api/auth/
API documentation: app.passaporteweb.com.br/static/docs/usuarios.html#get-accounts-api-auth
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/passaporte_web/identity.rb', line 117 def authenticate(password) raise ArgumentError, "email must be set" if (self.email.nil? || self.email.to_s.empty?) response = Http.custom_auth_get( self.email, password, "/accounts/api/auth/" ) raise "unexpected response: #{response.code} - #{response.body}" unless response.code == 200 true rescue *[RestClient::Unauthorized] => e false end |
#persisted? ⇒ Boolean
216 217 218 |
# File 'lib/passaporte_web/identity.rb', line 216 def persisted? !self.uuid.nil? && @persisted == true end |
#save ⇒ Object
Saves the Identity on PassaporteWeb, creating it if new or updating it if existing. Returns true if successfull or false if not. In case of failure, it will fill the errors
attribute with the reason for the failure to save the object.
The attributes first_name
, last_name
, cpf
and inhibit_activation_message
are optional. password2
and password
fields are required even if the parameter must_change_password
is used.
API methods:
-
POST /accounts/api/create/
(on create) -
PUT /accounts/api/identities/:uuid/
(on update)
API documentation:
-
app.passaporteweb.com.br/static/docs/usuarios.html#post-accounts-api-create
-
app.passaporteweb.com.br/static/docs/usuarios.html#get-accounts-api-identities-email-email
Example:
identity = Identity.find_by_email('[email protected]')
identity.save # => true
identity.cpf = '12'
identity.save # => false
identity.errors # => {"cpf" => ["Certifique-se de que o valor tenha no mínimo 11 caracteres (ele possui 2)."]}
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/passaporte_web/identity.rb', line 202 def save # TODO validar atributos? response = (persisted? ? update : create) raise "unexpected response: #{response.code} - #{response.body}" unless [200,201].include?(response.code) attributes_hash = MultiJson.decode(response.body) set_attributes(attributes_hash) @persisted = true @errors = {} true rescue *[RestClient::Conflict, RestClient::BadRequest] => e @errors = MultiJson.decode(e.response.body) false end |