Class: PassaporteWeb::ServiceAccount
- Inherits:
-
Object
- Object
- PassaporteWeb::ServiceAccount
- Includes:
- Attributable
- Defined in:
- lib/passaporte_web/service_account.rb
Overview
Represents a ServiceAccount on PassaporteWeb, which is the ‘account’ of an Identity within a Service. A Service may have many ServiceAccount s and many Identity ies via it’s ServiceAccount s. A Identity may belong to serveral Service s via it’s ServiceAccount s.
Constant Summary collapse
- ATTRIBUTES =
[:plan_slug, :expiration, :identity, :roles, :member_uuid, :role, :include_expired_accounts, :name, :members_data, :url, :service_data, :account_data, :add_member_url]
- UPDATABLE_ATTRIBUTES =
[:plan_slug, :expiration]
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
-
.find(uuid) ⇒ Object
Instanciates an ServiceAccount identified by it’s UUID, with all the details.
-
.find_all(page = 1, limit = 20) ⇒ Object
Finds all ServiceAccounts that the current authenticated application has access to, paginated.
Instance Method Summary collapse
-
#activate(identity) ⇒ Object
Activates an existing ServiceAccount.
- #attributes ⇒ Object
-
#initialize(attributes = {}) ⇒ ServiceAccount
constructor
A new instance of ServiceAccount.
- #persisted? ⇒ Boolean
-
#save ⇒ Object
Updates an existing ServiceAccount, changing it’s plan_slug and/or expiration date.
- #uuid ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ ServiceAccount
Returns a new instance of ServiceAccount.
17 18 19 20 |
# File 'lib/passaporte_web/service_account.rb', line 17 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/service_account.rb', line 15 def errors @errors end |
Class Method Details
.find(uuid) ⇒ Object
Instanciates an ServiceAccount identified by it’s UUID, with all the details. Only service accounts related to the current authenticated application are available. Returns the ServiceAccount instance if successful, or raises a RestClient::ResourceNotFound
exception if no ServiceAccount exists with that UUID (or if it is not related to the current authenticated application).
API method: GET /organizations/api/accounts/:uuid/
API documentation: app.passaporteweb.com.br/static/docs/account_manager.html#get-organizations-api-accounts-uuid
63 64 65 66 67 |
# File 'lib/passaporte_web/service_account.rb', line 63 def self.find(uuid) response = Http.get("/organizations/api/accounts/#{uuid}/") attributes_hash = MultiJson.decode(response.body) load_service_account(attributes_hash) end |
.find_all(page = 1, limit = 20) ⇒ Object
Finds all ServiceAccounts that the current authenticated application has access to, paginated. By default finds 20 ServiceAccounts per request, starting at “page” 1. Returns an OpenStruct object with two attributes service_accounts
and meta
. service_accounts
is an array of ServiceAccount instances or an empty array if no ServiceAccounts are found. meta
is an OpenStruct object with information about limit and available pagination values, to use in subsequent calls to .find_all
. Raises a RestClient::ResourceNotFound
exception if the requested page does not exist.
API method: GET /organizations/api/accounts/
API documentation: app.passaporteweb.com.br/static/docs/account_manager.html#get-organizations-api-accounts
Example:
data = PassaporteWeb::ServiceAccount.find_all
data.service_accounts # => [account1, account2, ...]
data. # => #<OpenStruct limit=20, next_page=2, prev_page=nil, first_page=1, last_page=123>
data..limit # => 20
data..next_page # => 2
data..prev_page # => nil
data..first_page # => 1
data..last_page # => 123
46 47 48 49 50 51 52 53 |
# File 'lib/passaporte_web/service_account.rb', line 46 def self.find_all(page=1, limit=20) response = Http.get("/organizations/api/accounts/?page=#{Integer(page)}&limit=#{Integer(limit)}") raw_accounts = MultiJson.decode(response.body) result_hash = {} result_hash[:service_accounts] = raw_accounts.map { |raw_account| load_service_account(raw_account) } result_hash[:meta] = PassaporteWeb::Helpers.(response.headers[:link]) PassaporteWeb::Helpers.convert_to_ostruct_recursive(result_hash) end |
Instance Method Details
#activate(identity) ⇒ Object
Activates an existing ServiceAccount. 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.
API method: PUT /organizations/api/activate/
API documentation: myfreecomm.github.io/passaporte-web/pweb/api/account_manager.html#put-organizations-api-activate
106 107 108 109 110 111 112 113 114 |
# File 'lib/passaporte_web/service_account.rb', line 106 def activate(identity) response = Http.put("/organizations/api/activate/", {slug: self.plan_slug, identity: identity, global_account: self.uuid}) raise "unexpected response: #{response.code} - #{response.body}" unless response.code == 200 @errors = {} true rescue *[RestClient::Conflict, RestClient::BadRequest] => e @errors = MultiJson.decode(e.response.body) false end |
#attributes ⇒ Object
89 90 91 92 93 94 |
# File 'lib/passaporte_web/service_account.rb', line 89 def attributes ATTRIBUTES.inject({}) do |hash, attribute| hash[attribute] = self.send(attribute) hash end end |
#persisted? ⇒ Boolean
96 97 98 |
# File 'lib/passaporte_web/service_account.rb', line 96 def persisted? @persisted == true end |
#save ⇒ Object
Updates an existing ServiceAccount, changing it’s plan_slug and/or expiration date. 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.
API method: PUT /organizations/api/accounts/:uuid/
API documentation: app.passaporteweb.com.br/static/docs/account_manager.html#put-organizations-api-accounts-uuid
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/passaporte_web/service_account.rb', line 76 def save # TODO validar atributos? response = update raise "unexpected response: #{response.code} - #{response.body}" unless response.code == 200 attributes_hash = MultiJson.decode(response.body) set_attributes(attributes_hash) @errors = {} true rescue *[RestClient::Conflict, RestClient::BadRequest] => e @errors = MultiJson.decode(e.response.body) false end |
#uuid ⇒ Object
22 23 24 |
# File 'lib/passaporte_web/service_account.rb', line 22 def uuid self.account_data['uuid'] if self.account_data end |