Class: Tinysky::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
# File 'lib/tinysky/client.rb', line 5

def initialize(credentials)
  @credentials = credentials
  @connection = Tinysky.generate_connection

  @access_jwt = nil
  @expires_at = nil
end

Instance Attribute Details

#access_jwtObject (readonly)

Returns the value of attribute access_jwt.



3
4
5
# File 'lib/tinysky/client.rb', line 3

def access_jwt
  @access_jwt
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



3
4
5
# File 'lib/tinysky/client.rb', line 3

def expires_at
  @expires_at
end

Instance Method Details

#create_record(text) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tinysky/client.rb', line 28

def create_record(text)
  create_session if expired_token?

  record = {
    "$type" => POST_LEXICON_TYPE,
    "createdAt" => DateTime.now.iso8601,
    "langs" => ["en-US"],
    "text" => text
  }

  body = {
    collection: POST_LEXICON_TYPE,
    record: record,
    repo: @credentials[:handle]
  }

  headers = {"Authorization" => "Bearer #{@access_jwt}"}

  @connection.post(CREATE_RECORD_PATH, body, headers)
end

#create_sessionObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tinysky/client.rb', line 13

def create_session
  body = {
    identifier: @credentials[:handle],
    password: @credentials[:app_password]
  }

  response = @connection.post(CREATE_SESSION_PATH, body)

  @access_jwt = response.body["accessJwt"]
  payload, _header = JWT.decode(@access_jwt, nil, false)
  @expires_at = Time.at(payload["exp"])

  response
end