Class: CirroIOV2::RequestClients::Jwt

Inherits:
Base
  • Object
show all
Defined in:
lib/cirro_io_v2/request_clients/jwt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#request

Constructor Details

#initialize(base_url:, private_key:, client_id:) ⇒ Jwt

Returns a new instance of Jwt.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cirro_io_v2/request_clients/jwt.rb', line 9

def initialize(base_url:, private_key:, client_id:)
  @base_url = base_url
  @private_key = private_key
  @client_id = client_id

  @connection = Faraday.new(url: base_url) do |conn|
    conn.request :json
    conn.response :raise_error
    conn.response :json
    conn.adapter Faraday.default_adapter # testIO App is on older version of faraday and needs this line
  end
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



7
8
9
# File 'lib/cirro_io_v2/request_clients/jwt.rb', line 7

def base_url
  @base_url
end

#client_idObject (readonly)

Returns the value of attribute client_id.



7
8
9
# File 'lib/cirro_io_v2/request_clients/jwt.rb', line 7

def client_id
  @client_id
end

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/cirro_io_v2/request_clients/jwt.rb', line 7

def connection
  @connection
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



7
8
9
# File 'lib/cirro_io_v2/request_clients/jwt.rb', line 7

def private_key
  @private_key
end

Instance Method Details

#bearer_tokenObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/cirro_io_v2/request_clients/jwt.rb', line 30

def bearer_token
  payload = {
    exp: Time.now.to_i + (10 * 60),
    sub: client_id,
  }

  token = JWT.encode(payload, private_key, 'RS256')

  "Bearer #{token}"
end

#make_request(http_method, url, body: nil, params: nil, _headers: {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/cirro_io_v2/request_clients/jwt.rb', line 22

def make_request(http_method, url, body: nil, params: nil, _headers: {})
  @connection.send(http_method, url) do |request|
    request.params = params if params
    request.body = body.to_json if body
    request.headers['Authorization'] = bearer_token
  end
end