esriveter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .password
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in esriveter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jen Oslislo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Esriveter
2
+
3
+ A token manager for your all yer AGO token needs. Automagically manage
4
+ app tokens and device tokens.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'esriveter'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install esriveter
19
+
20
+ ## Usage
21
+
22
+ # Get a persisting app token
23
+ params = {client_id: 'client_id', client_secret: 'client_secret'}
24
+ t = Esriveter.new params
25
+ conn = Faraday.new(:url => geotriggers_url)
26
+ conn.get "/device/list?token=#{t.app_token}"
27
+
28
+ # Get a persisting device token
29
+ params = {client_id: 'client_id', client_secret: 'client_secret', device_id:
30
+ 'device_id', refresh_token: 'refresh_token'}
31
+ t = Esriveter.new params
32
+ conn = Faraday.new(:url => geotriggers_url)
33
+ conn.get "/device/list?token=#{t.device_token}"
34
+
35
+ # If you don't specify device_id and refresh token, they'll be
36
+ # created for you
37
+ params = {client_id: 'client_id', client_secret: 'client_secret'}
38
+ t = Esriveter.new params
39
+ conn = Faraday.new(:url => geotriggers_url)
40
+ conn.get "/device/list?token=#{t.device_token}"
41
+ puts "Device: #{t.device_id}"
42
+
43
+ # Don't got an app? No prob.
44
+ params = {username: 'me', password: 'password', title: 'My App'}
45
+ t = Esriveter.new params
46
+ app_stuff = t.create_app
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/esriveter.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'esriveter'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "esriveter"
8
+ gem.version = "0.0.1"
9
+ gem.authors = ["Jen Oslislo"]
10
+ gem.email = ["poeksweb@gmail.com"]
11
+ gem.description = %q{This gem creates new tokens and manages devices for you on Esri's Arcgis Online service.}
12
+ gem.summary = %q{This gem creates new tokens and manages devices for you on Esri's Arcgis Online service.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'faraday', '~>0.8.8'
21
+ gem.add_dependency 'multi_json', '~>1.7.9'
22
+ gem.add_development_dependency 'rspec'
23
+ gem.add_development_dependency 'faraday', '~>0.8.8'
24
+ gem.add_development_dependency 'multi_json', '~>1.7.9'
25
+ end
data/lib/esriveter.rb ADDED
@@ -0,0 +1,168 @@
1
+ class Esriveter
2
+
3
+ attr_accessor :device_token, :device_token_expiry, :refresh_token, :device_id
4
+ attr_accessor :app_token, :app_token_expiry
5
+ attr_accessor :client_id, :client_secret
6
+ attr_accessor :server, :debug, :force
7
+
8
+ ACCESS_TOKEN_KEY = 'access_token'
9
+ EXPIRES_KEY = 'expires_in'
10
+ DEVICE_KEY = 'device'
11
+ DEVICE_TOKEN_KEY = 'deviceToken'
12
+ DEVICE_ID_KEY = 'deviceId'
13
+ REFRESH_TOKEN_KEY = 'refresh_token'
14
+
15
+ def initialize(opts={})
16
+ set_variables opts
17
+
18
+ @server = self.default_server if not @server
19
+ @client = Faraday.new do |faraday|
20
+ faraday.use Faraday::Request::UrlEncoded
21
+ faraday.use Faraday::Response::Logger if @debug
22
+ faraday.adapter :net_http
23
+ end
24
+ self
25
+ end
26
+
27
+ def get_app_token
28
+ if not @app_token_expiry or (not @app_token or @force or @app_token_expiry < Time.now)
29
+ self.create_app_token
30
+ end
31
+ {app_token: @app_token, app_token_expiry: @app_token_expiry}
32
+ end
33
+
34
+ def get_device_token
35
+ if not @device_token_expiry or (not @device_token or @force or @device_token_expiry < Time.now)
36
+
37
+ dresponse = self.create_device_token
38
+ if dresponse.size == 2
39
+ {device_token: @device_token, device_token_expiry: @device_token_expiry}
40
+ else
41
+ {device_token: @device_token, device_token_expiry: @device_token_expiry, refresh_token: @refresh_token, device_id: @device_id}
42
+ end
43
+ else
44
+ {device_token: @device_token, device_token_expiry: @device_token_expiry}
45
+ end
46
+ end
47
+
48
+ def create_app username, password, title="Random Name #{Random.rand(11)}"
49
+
50
+ action = Proc.new { |uri, params|
51
+ puts params.inspect if @debug
52
+ self.generic_request uri, params
53
+ }
54
+
55
+ # Generate Token
56
+ params = {
57
+ 'username' => username,
58
+ 'password' => password,
59
+ 'expiration' => self.desired_expiration,
60
+ 'referer' => 'arcgis.com'
61
+ }
62
+ uri = "/rest/generateToken"
63
+ tresponse = action.call(uri, params)
64
+
65
+ # Add Item
66
+ params = {
67
+ 'token' => tresponse['token'],
68
+ 'type' => 'Application',
69
+ 'title' => title
70
+ }
71
+ uri = "/rest/content/users/#{username}/addItem"
72
+ iresponse = action.call(uri, params)
73
+
74
+ # Create Application
75
+ params = {
76
+ 'itemId' => iresponse['id'],
77
+ 'appType' => 'multiple',
78
+ 'redirect_uris' => '[]',
79
+ 'token' => tresponse['token']
80
+ }
81
+ uri = "/oauth2/registerApp"
82
+ aresponse = action.call(uri, params)
83
+
84
+ aresponse
85
+ end
86
+
87
+ protected
88
+
89
+ def create_app_token
90
+ params = {'grant_type' => 'client_credentials', 'client_id' => @client_id, 'client_secret' => @client_secret}
91
+ tresponse = self.generic_request "/oauth2/token", params
92
+ @app_token = tresponse[ACCESS_TOKEN_KEY]
93
+ @app_token_expiry = self.get_expiry tresponse[EXPIRES_KEY]
94
+
95
+ [@app_token, @app_token_expiry]
96
+ end
97
+
98
+ def create_device_token
99
+ if @refresh_token
100
+
101
+ params = {
102
+ 'client_id' => @client_id,
103
+ 'refresh_token' => @refresh_token,
104
+ 'grant_type' => 'refresh_token'
105
+ }
106
+ tresponse = self.generic_request "/oauth2/token", params
107
+ @device_token = tresponse[ACCESS_TOKEN_KEY]
108
+ @device_token_expiry = self.get_expiry tresponse[EXPIRES_KEY]
109
+ [@device_token, @device_token_expiry]
110
+ else
111
+ register_device
112
+ end
113
+ end
114
+
115
+ def register_device
116
+
117
+ params = {
118
+ 'client_id' => @client_id
119
+ }
120
+ tresponse = self.generic_request "/oauth2/registerDevice", params
121
+ @device_id = tresponse[DEVICE_KEY][DEVICE_ID_KEY]
122
+ @refresh_token = tresponse[DEVICE_TOKEN_KEY][REFRESH_TOKEN_KEY]
123
+ @device_token = tresponse[DEVICE_TOKEN_KEY][ACCESS_TOKEN_KEY]
124
+ @device_token_expiry = self.get_expiry tresponse[DEVICE_TOKEN_KEY][EXPIRES_KEY]
125
+
126
+ [@device_token, @device_token_expiry, @refresh_token, @device_id]
127
+ end
128
+
129
+ def generic_request path, params
130
+ uri = "#{self.base_uri}#{path}#{self.with_json}"
131
+ response = MultiJson.load @client.post(uri, params).body
132
+ puts response.inspect if @debug
133
+ response
134
+ end
135
+
136
+ def set_variables vars
137
+ return if not vars
138
+ vars.each do |key, value|
139
+ self.send("#{key}=", value) if self.respond_to?(key)
140
+ end
141
+ end
142
+
143
+ def default_server
144
+ ENV["ESRIVETER_DEFAULT_SERVER"]||"arcgis.com"
145
+ end
146
+
147
+ def base_path
148
+ ENV["ESRIVETER_BASE_PATH"]||"/sharing"
149
+ end
150
+
151
+ def base_uri
152
+ "https://#{ self.server }#{self.base_path}"
153
+ end
154
+
155
+ def with_json
156
+ "?f=json"
157
+ end
158
+
159
+ def get_expiry seconds
160
+ Time.now + (seconds.to_i)
161
+ end
162
+
163
+ def desired_expiration
164
+ 999999
165
+ end
166
+
167
+ end
168
+
@@ -0,0 +1,60 @@
1
+ require_relative 'spec_helper'
2
+ system("sh ./.password")
3
+
4
+ describe Esriveter do
5
+
6
+ before do
7
+ client_id = ENV["ESRIVETER_CLIENT_ID"]
8
+ client_secret = ENV["ESRIVETER_CLIENT_SECRET"]
9
+ debug = false
10
+
11
+ @params = {client_id: client_id, client_secret: client_secret, debug: debug, server: 'devext.arcgis.com'}
12
+ @t = Esriveter.new @params
13
+ end
14
+
15
+ it 'should get an app token' do
16
+ app_res = @t.get_app_token
17
+ app_res.class.should == Hash
18
+ app_res.keys.size.should == 2
19
+ @t.app_token.length.should > 0
20
+ end
21
+
22
+ it 'should save app token among requests' do
23
+ first = @t.get_app_token
24
+ second = @t.get_app_token
25
+ first[:app_token].should == second[:app_token]
26
+ end
27
+
28
+ it 'should be able to force fresh app tokens' do
29
+ first = @t.get_app_token
30
+ @t.force = true
31
+ second = @t.get_app_token
32
+ first[:app_token].should_not == second[:app_token]
33
+ end
34
+
35
+ it 'should get a device token' do
36
+ dev_res = @t.get_device_token
37
+ dev_res.class.should == Hash
38
+ dev_res.keys.size.should == 4
39
+ @t.device_token.length.should > 0
40
+ end
41
+
42
+ it 'should save device token among requests' do
43
+ @t.force = false
44
+ first = @t.get_device_token
45
+ second = @t.get_device_token
46
+ second.class.should == Hash
47
+ second.keys.size.should == 2
48
+ first[:device_token].should == second[:device_token]
49
+ end
50
+
51
+ it 'should be able to force fresh device tokens' do
52
+ first = @t.get_device_token
53
+ @t.force = true
54
+ second = @t.get_device_token
55
+ first.class.should == Hash
56
+ first.keys.size.should == 4
57
+ first[:device_token].should_not == second[:device_token]
58
+ end
59
+
60
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'faraday'
3
+ require 'multi_json'
4
+
5
+ require_relative '../lib/esriveter'
6
+
7
+
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: esriveter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jen Oslislo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: multi_json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.7.9
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.7.9
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: faraday
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.8.8
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.8.8
78
+ - !ruby/object:Gem::Dependency
79
+ name: multi_json
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.7.9
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.7.9
94
+ description: This gem creates new tokens and manages devices for you on Esri's Arcgis
95
+ Online service.
96
+ email:
97
+ - poeksweb@gmail.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - esriveter.gemspec
108
+ - lib/esriveter.rb
109
+ - spec/esriveter_spec.rb
110
+ - spec/spec_helper.rb
111
+ homepage: ''
112
+ licenses: []
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.25
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: This gem creates new tokens and manages devices for you on Esri's Arcgis
135
+ Online service.
136
+ test_files:
137
+ - spec/esriveter_spec.rb
138
+ - spec/spec_helper.rb
139
+ has_rdoc: