Class: Blockmason::Link::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/blockmason/link/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, http:) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
# File 'lib/blockmason/link/connection.rb', line 12

def initialize(base_url:, http:)
  @base_url = base_url
  @http = http
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



10
11
12
# File 'lib/blockmason/link/connection.rb', line 10

def base_url
  @base_url
end

#httpObject

Returns the value of attribute http.



10
11
12
# File 'lib/blockmason/link/connection.rb', line 10

def http
  @http
end

Instance Method Details

#authenticate!(client_id:, client_secret:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blockmason/link/connection.rb', line 17

def authenticate!(client_id:, client_secret:)
  request = Net::HTTP::Post.new("#{@base_url}/oauth2/token")

  request['Content-Type'] = 'application/json'

  response = @http.request(request, {
    client_id: client_id,
    client_secret: client_secret,
    grant_type: 'client_credentials'
  }.to_json)

  grant = JSON.parse(response.body)

  raise response.body if grant.has_key?('errors')

  session = ::Blockmason::Link::Session.new(access_token: grant['access_token'], base_url: @base_url, http: @http, refresh_token: grant['refresh_token'])

  ::Blockmason::Link::ManagedSession.new(session: session)
end