Class: PirschApi::TokenResource

Inherits:
Object
  • Object
show all
Defined in:
lib/pirsch_api/resources/token.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ TokenResource

Returns a new instance of TokenResource.



3
4
5
6
# File 'lib/pirsch_api/resources/token.rb', line 3

def initialize(client_id, client_secret)
  @client_id = client_id
  @client_secret = client_secret
end

Instance Method Details

#parse_response(body) ⇒ Object



19
20
21
22
# File 'lib/pirsch_api/resources/token.rb', line 19

def parse_response(body)
  puts "[Pirsch API] Token received"
  Token.new JSON.parse(body)
end

#request_bodyObject



12
13
14
15
16
17
# File 'lib/pirsch_api/resources/token.rb', line 12

def request_body
  {
    "client_id" => @client_id,
    "client_secret" => @client_secret
  }.to_json
end

#request_urlObject



8
9
10
# File 'lib/pirsch_api/resources/token.rb', line 8

def request_url
  "token"
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pirsch_api/resources/token.rb', line 24

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