File: esri_token.rb

package info (click to toggle)
ruby-geocoder 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 900 kB
  • sloc: ruby: 8,419; makefile: 3
file content (38 lines) | stat: -rw-r--r-- 975 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
module Geocoder
  class EsriToken
    attr_accessor :value, :expires_at

    def initialize(value, expires_at)
      @value = value
      @expires_at = expires_at
    end

    def to_s
        @value
    end

    def active?
      @expires_at > Time.now
    end

    def self.generate_token(client_id, client_secret, expires=1440)
      # creates a new token that will expire in 1 day by default
      getToken = Net::HTTP.post_form URI('https://www.arcgis.com/sharing/rest/oauth2/token'),
        f: 'json',
        client_id: client_id,
        client_secret: client_secret,
        grant_type: 'client_credentials',
        expiration: expires # (minutes) max: 20160, default: 1 day

      response = JSON.parse(getToken.body)

      if response['error']
        Geocoder.log(:warn, response['error'])
      else
        token_value = response['access_token']
        expires_at = Time.now + (expires * 60)
        new(token_value, expires_at)
      end
    end
  end
end