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.



5
6
7
8
# File 'lib/pirsch_api/resources/token.rb', line 5

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

Instance Method Details

#parse_response(body) ⇒ Object



21
22
23
24
# 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_bodyObject



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

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

#request_urlObject



10
11
12
# File 'lib/pirsch_api/resources/token.rb', line 10

def request_url
  "token"
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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