Class: Vis::Api

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

Instance Method Summary collapse

Constructor Details

#initialize(server_url: "https://identity.dhamma.org", client_id:, client_secret:) ⇒ Api

Returns a new instance of Api.



7
8
9
10
11
12
# File 'lib/vis/api.rb', line 7

def initialize(server_url: "https://identity.dhamma.org", client_id:, client_secret:)
  @client_id = client_id
  @client_secret = client_secret
  @vis_app_url = server_url
  @use_ssl = !Rails.env.development?
end

Instance Method Details

#get(path) ⇒ Object



42
43
44
45
46
# File 'lib/vis/api.rb', line 42

def get(path)
  http_client, uri = http_client_and_uri path
  response = http_client.get(uri, headers)
  return_response(response)
end

#post(path, post_params_hash) ⇒ Object



48
49
50
51
52
# File 'lib/vis/api.rb', line 48

def post(path, post_params_hash)
  http_client, uri = http_client_and_uri path
  response = http_client.post(uri, post_params_hash.to_json, headers)
  return_response(response)
end

#tokenObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/vis/api.rb', line 14

def token
  return @token if @token && @expiry && Time.now.utc < @expiry

  response = token_post
  result = JSON.parse(response.body)
  check_error!(response.code, result)

  @expiry = (Time.now.utc + result["expires_in"] - 1)
  @token = result["access_token"]
end