Class: SelfSDK::RestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, app_id, app_key, env) ⇒ RestClient

RestClient initializer

Parameters:

  • url (string)

    self-messaging url

  • token (string)

    jwt token identifying the authenticated user



15
16
17
18
19
20
# File 'lib/client.rb', line 15

def initialize(url, app_id, app_key, env)
  SelfSDK.logger.info "client setup with #{url}"
  @self_url = url
  @env = env
  @jwt = SelfSDK::JwtService.new(app_id, app_key)
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



9
10
11
# File 'lib/client.rb', line 9

def env
  @env
end

#jwtObject (readonly)

Returns the value of attribute jwt.



9
10
11
# File 'lib/client.rb', line 9

def jwt
  @jwt
end

#self_urlObject (readonly)

Returns the value of attribute self_url.



9
10
11
# File 'lib/client.rb', line 9

def self_url
  @self_url
end

Instance Method Details

#app(id) ⇒ Object

Get app details

Parameters:

  • id (string)

    app self_id.



32
33
34
# File 'lib/client.rb', line 32

def app(id)
  get_identity "/v1/apps/#{id}"
end

#device_public_key(id, did) ⇒ Object

Get the active public key for a device

Parameters:

  • id (string)

    identity id



110
111
112
113
114
# File 'lib/client.rb', line 110

def device_public_key(id, did)
  i = entity(id)
  sg = SelfSDK::SignatureGraph.new(i[:history])
  sg.key_by_device(did)
end

#devices(id) ⇒ Object

Lists all devices assigned to the given identity

Parameters:

  • id (string)

    identity id



51
52
53
54
55
56
57
58
59
# File 'lib/client.rb', line 51

def devices(id)
  res = get "/v1/identities/#{id}/devices"
  body = JSON.parse(res.body, symbolize_names: true)
  if res.code != 200
    SelfSDK.logger.error "identity response : #{body[:message]}"
    raise "you need connection permissions"
  end
  body
end

#entity(id) ⇒ Object

Get app/identity details

Parameters:

  • id (string)

    app/identity self_id.



39
40
41
42
43
44
45
46
# File 'lib/client.rb', line 39

def entity(id)
  #TODO : Consider a better check for this conditional
  if id.length == 11
    return identity(id)
  else
    return app(id)
  end
end

#get(endpoint) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/client.rb', line 85

def get(endpoint)
  res = nil
  loop do
    res = HTTParty.get("#{@self_url}#{endpoint}", headers: {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer #{@jwt.auth_token}"
    })
    break if res.code != 503
    sleep 2
  end
  return res
end

#identity(id) ⇒ Object

Get identity details

Parameters:

  • id (string)

    identity self_id.



25
26
27
# File 'lib/client.rb', line 25

def identity(id)
  get_identity "/v1/identities/#{id}"
end

#post(endpoint, body) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/client.rb', line 70

def post(endpoint, body)
  res = nil
  loop do
    res = HTTParty.post("#{@self_url}#{endpoint}",
                  headers: {
                      'Content-Type' => 'application/json',
                      'Authorization' => "Bearer #{@jwt.auth_token}"
                  },
                  body: body)
    break if res.code != 503
    sleep 2
  end
  return res
end

#public_key(id, kid) ⇒ Object

Lists all public keys stored on self for the given ID

Parameters:

  • id (string)

    identity id



101
102
103
104
105
# File 'lib/client.rb', line 101

def public_key(id, kid)
  i = entity(id)
  sg = SelfSDK::SignatureGraph.new(i[:history])
  sg.key_by_id(kid)
end

#public_keys(id) ⇒ Object

Lists all public keys stored on self for the given ID

DEPRECATED

Parameters:

  • id (string)

    identity id



65
66
67
68
# File 'lib/client.rb', line 65

def public_keys(id)
  i = entity(id)
  i[:public_keys]
end