Class: InceApi::CreateAccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/ince_api/create_access_token.rb

Constant Summary collapse

URL =
URI("https://api.1nce.com/management-api/oauth/token")

Instance Method Summary collapse

Constructor Details

#initialize(username:, password:) ⇒ CreateAccessToken

Returns a new instance of CreateAccessToken.



5
6
7
8
# File 'lib/ince_api/create_access_token.rb', line 5

def initialize(username:, password:)
  @username = username
  @password = password
end

Instance Method Details

#connectionObject



26
27
28
29
30
# File 'lib/ince_api/create_access_token.rb', line 26

def connection
  @connection ||= Net::HTTP.new(URL.host, URL.port).tap do |http|
    http.use_ssl = true
  end
end

#create_tokenObject



12
13
14
15
# File 'lib/ince_api/create_access_token.rb', line 12

def create_token
  response = connection.request(request)
  JSON.parse(response.body)
end

#encoded_credentialsObject



32
33
34
# File 'lib/ince_api/create_access_token.rb', line 32

def encoded_credentials
  Base64.encode64("#{@username}:#{@password}").strip
end

#requestObject



17
18
19
20
21
22
23
24
# File 'lib/ince_api/create_access_token.rb', line 17

def request
  Net::HTTP::Post.new(URL).tap do |request|
    request["Accept"] = 'application/json'
    request["Content-Type"] = 'application/x-www-form-urlencoded'
    request["Authorization"] = "Basic #{encoded_credentials}"
    request.body = "grant_type=client_credentials"
  end
end