class PirschApi::TokenResource

Public Class Methods

new(client_id, client_secret) click to toggle source
# File lib/pirsch_api/resources/token.rb, line 5
def initialize(client_id, client_secret)
  @client_id     = client_id
  @client_secret = client_secret
end

Public Instance Methods

parse_response(body) click to toggle source
# File lib/pirsch_api/resources/token.rb, line 21
def parse_response(body)
  puts "[Pirsch API] Token received"
  Token.new JSON.parse(body)
end
request_body() click to toggle source
# File lib/pirsch_api/resources/token.rb, line 14
def request_body
  {
    "client_id" => @client_id,
    "client_secret" => @client_secret
  }.to_json
end
request_url() click to toggle source
# File lib/pirsch_api/resources/token.rb, line 10
def request_url
  "token"
end
run() click to toggle source
# File lib/pirsch_api/resources/token.rb, line 26
def run
  uri = URI.parse "#{PirschApi::Client::BASE_URL}/#{request_url}"
  puts "[Pirsch API] Requesting Token..."
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  
  header = { 'Content-Type': 'text/json' }
  request = Net::HTTP::Post.new(uri.request_uri, header)
  request.body = request_body
  
  response = http.request(request)

  raise Error.new "Token request failed. (#{response.body})" unless response.code == "200"
  
  parse_response(response.body)
end