Class: PassaporteWeb::IdentityService

Inherits:
Object
  • Object
show all
Includes:
Attributable
Defined in:
lib/passaporte_web/identity_service.rb

Overview

The IdentityService objct represents the relationship between an Identity and a Service on PassaporteWeb. This is only relevant if you wish to add information (via service_data attribute) about the Identity and Service on PassaporteWeb. It does not mean that the Identity has an ServiceAccount on the Service.

Constant Summary collapse

ATTRIBUTES =
[:identity, :slug, :is_active, :service_data]
UPDATABLE_ATTRIBUTES =
[:is_active, :service_data]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identity, attributes = {}) ⇒ IdentityService

Instanciates a new IdentityService object for the supplied Identity. The slug attribute is required. The service_data attribute should be a Hash that will be converted to a JSON object. As so, it should only contain strings, symbols, integers, floats, arrays and hashes (the last two with only the same kind of simple objects).

Example:

identity = PassaporteWeb::Identity.find('some-uuid')
PassaporteWeb::IdentityService.new(identity, slug: 'identity_client', is_active: true, service_data: {foo: 'bar'})


26
27
28
29
30
# File 'lib/passaporte_web/identity_service.rb', line 26

def initialize(identity, attributes={})
  set_attributes(attributes)
  @identity = identity
  @errors = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



15
16
17
# File 'lib/passaporte_web/identity_service.rb', line 15

def errors
  @errors
end

Class Method Details

.find(identity, slug) ⇒ Object

Finds the IdentityService representing the relationship of the Identity with the Service. Returns an IdentityService object or nil if no relationship is found.

API method: GET /accounts/api/service-info/:uuid/:service_slug/

API documentation: app.passaporteweb.com.br/static/docs/servicos.html#get-accounts-api-service-info-uuid-service-slug



74
75
76
77
78
79
# File 'lib/passaporte_web/identity_service.rb', line 74

def self.find(identity, slug)
  response = Http.get("/accounts/api/service-info/#{identity.uuid}/#{slug}/")
  return if response.code == 204
  attributes_hash = MultiJson.decode(response.body)
  load_identity_service(identity, attributes_hash)
end

Instance Method Details

#attributesObject



61
62
63
64
65
66
# File 'lib/passaporte_web/identity_service.rb', line 61

def attributes
  ATTRIBUTES.inject({}) do |hash, attribute|
    hash[attribute] = self.send(attribute)
    hash
  end
end

#is_active?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/passaporte_web/identity_service.rb', line 57

def is_active?
  @is_active == true
end

#persisted?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/passaporte_web/identity_service.rb', line 53

def persisted?
  @persisted == true
end

#saveObject

Creates or updates the IdentityService. The service_data attribute should be a Hash that will be converted to a JSON object. As so, it should only contain strings, symbols, integers, floats, arrays and hashes (the last two with only the same kind of simple objects).

API method: PUT /accounts/api/service-info/:uuid/:service_slug/

API documentation: app.passaporteweb.com.br/static/docs/servicos.html#put-accounts-api-service-info-uuid-service-slug



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/passaporte_web/identity_service.rb', line 39

def save
  # TODO validar atributos?
  response = Http.put("/accounts/api/service-info/#{identity.uuid}/#{slug}/", save_body)
  raise "unexpected response: #{response.code} - #{response.body}" unless response.code == 200
  attributes_hash = MultiJson.decode(response.body)
  set_attributes(attributes_hash)
  @errors = {}
  @persisted = true
  true
rescue *[RestClient::Conflict, RestClient::BadRequest] => e
  @errors = MultiJson.decode(e.response.body)
  false
end