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, options) ⇒ Object



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

def create_record(text, options)
  create_session if expired_token?

  feed_post = FeedPost.new(text, options)

  body = {
    collection: Tinysky::Lexicon::FEED_POST,
    record: feed_post.to_hash,
    repo: @credentials[:handle]
  }

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

  path = Tinysky::Endpoints::REPO_CREATE_RECORD
  @connection.post(path, body, headers)
end

#create_sessionObject



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

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

  path = Tinysky::Endpoints::SERVER_CREATE_SESSION
  response = @connection.post(path, body)

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

  response
end

#upload_blob(blob_data, content_type) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tinysky/client.rb', line 46

def upload_blob(blob_data, content_type)
  create_session if expired_token?

  headers = {
    "Authorization" => "Bearer #{@access_jwt}",
    "Content-Type" => content_type
  }

  path = Tinysky::Endpoints::REPO_UPLOAD_BLOB
  @connection.post(path, blob_data, headers)
end