module Subledger::Store::Api::CreateIdentity

Constants

ACTIVE_IDENTITY
ACTIVE_KEY

Public Instance Methods

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