Class: Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/arcgis_vrps/auth.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ Auth

Returns a new instance of Auth.



5
6
7
8
9
10
11
12
# File 'lib/arcgis_vrps/auth.rb', line 5

def initialize(client_id, client_secret)
  if client_id.nil? || client_id.empty? || client_secret.nil? || client_secret.empty?
        raise ArgumentError, "Both Client ID & Client Secret has to be set"
    end

  @client_id = client_id
  @client_secret = client_secret
end

Instance Method Details

#generateTokenObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/arcgis_vrps/auth.rb', line 14

def generateToken
    begin

    url = "https://www.arcgis.com/sharing/rest/oauth2/token"
    escaped_url = URI.encode(url)
    uri = URI.parse(escaped_url)
    req = Net::HTTP::Post.new(uri.to_s)
     
    # Expiration is set to 20160 minutes = 2 weeks

    req.set_form_data(
      f: 'json',
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials',
      expiration: 20160
      )

    http = Net::HTTP.new(uri.hostname, uri.port)
    http.use_ssl = true
     
    res = http.request(req)

    token = JSON.parse(res.body)['access_token']
    puts "\n"+res.body
    return token
  rescue => e
    puts "==== Error in generating token: "
    puts e
  end
end