Module: Subledger::Store::Api::CreateIdentity

Included in:
Store
Defined in:
lib/subledger/store/api/roles/create_identity.rb

Constant Summary collapse

ACTIVE_IDENTITY =
'active_identity'
ACTIVE_KEY =
'active_key'

Instance Method Summary collapse

Instance Method Details

#create_identity(identity) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/subledger/store/api/roles/create_identity.rb', line 8

def create_identity identity
  client = identity.client

  path = Path.for_collection :anchor => identity

  begin
    json_body = http.post do |req|
                  req.url    path
                  req.body = identity.post_hash
                end.body
  rescue Exception => e
    raise CreateError, "Cannot create #{identity.class}: #{e}"
  end

  response_hash = parse_json json_body

  identity_hash = { 'active_identity' => response_hash[ACTIVE_IDENTITY] }
  identity_json = MultiJson.dump identity_hash

  new_identity = new_or_initialize identity_json, identity

  identity.send :initialize, new_identity.attributes

  key_args = Rest.to_args response_hash[ACTIVE_KEY], client
  key      = client.active_keys key_args

  return identity, key
end