Class: Tortoise::ApiHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/tortoise/services/api_helper.rb

Constant Summary collapse

ACCESS_TOKEN_CACHE_KEY =
'access_token'
DATABASE_CACHE_KEY =
'database'
CACHE_EXPIRES =
60

Instance Method Summary collapse

Instance Method Details

#databaseObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tortoise/services/api_helper.rb', line 24

def database
  return cached_database unless cached_database.nil?
  return if public_informations_path.nil?

  response = RestClient.get(
    public_informations_path,
    {
      Authorization: "Bearer #{token}"
    }
  )
  cache.set(
    DATABASE_CACHE_KEY,
    JSON.parse(response.body)['database'],
    expires_in: CACHE_EXPIRES
  )
end

#tokenObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tortoise/services/api_helper.rb', line 7

def token
  return cached_token unless cached_token.nil?
  return if token_path.nil? || token_params.nil?

  response = RestClient.post(
    token_path,
    token_params,
    token_header
  )

  cache.set(
    ACCESS_TOKEN_CACHE_KEY,
    JSON.parse(response.body)['access_token'],
    expires_in: CACHE_EXPIRES
  )
end

#upload(file) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tortoise/services/api_helper.rb', line 41

def upload(file)
  return if upload_path.nil? || token.nil?

  RestClient.post(
    upload_path,
    {
      multipart: true,
      file: File.new(file)
    },
    {
      'User-access' => database,
      Authorization: "Bearer #{token}"
    }
  )
end