class PassaporteWeb::ServiceAccount

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.

Constants

ATTRIBUTES
UPDATABLE_ATTRIBUTES

Attributes

errors[R]

Public Class Methods

find(uuid) click to toggle source

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

# 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) click to toggle source

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.meta # => #<OpenStruct limit=20, next_page=2, prev_page=nil, first_page=1, last_page=123>
data.meta.limit      # => 20
data.meta.next_page  # => 2
data.meta.prev_page  # => nil
data.meta.first_page # => 1
data.meta.last_page  # => 123
# 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.meta_links_from_header(response.headers[:link])
  PassaporteWeb::Helpers.convert_to_ostruct_recursive(result_hash)
end
new(attributes={}) click to toggle source
# File lib/passaporte_web/service_account.rb, line 17
def initialize(attributes={})
  set_attributes(attributes)
  @errors = {}
end

Private Class Methods

load_service_account(attributes_hash) click to toggle source
# File lib/passaporte_web/service_account.rb, line 126
def self.load_service_account(attributes_hash)
  service_account = self.new(attributes_hash)
  service_account.instance_variable_set(:@persisted, true)
  service_account
end

Public Instance Methods

activate(identity) click to toggle source

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

# 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() click to toggle source
# 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?() click to toggle source
# File lib/passaporte_web/service_account.rb, line 96
def persisted?
  @persisted == true
end
save() click to toggle source

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

# 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() click to toggle source
# File lib/passaporte_web/service_account.rb, line 22
def uuid
  self.account_data['uuid'] if self.account_data
end

Private Instance Methods

update() click to toggle source
# File lib/passaporte_web/service_account.rb, line 118
def update
  Http.put("/organizations/api/accounts/#{self.uuid}/", update_body)
end
update_body() click to toggle source
# File lib/passaporte_web/service_account.rb, line 122
def update_body
  self.attributes.select { |key, value| UPDATABLE_ATTRIBUTES.include?(key) && !value.nil? }
end