dronetrack 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +7 -0
- data/README.md +179 -0
- data/Rakefile +19 -0
- data/dronetrack.gemspec +21 -0
- data/lib/dronetrack.rb +4 -0
- data/lib/dronetrack/drone.rb +35 -0
- data/lib/dronetrack/oauthhelper.rb +19 -0
- data/lib/dronetrack/restservice.rb +63 -0
- data/lib/dronetrack/track.rb +34 -0
- data/lib/dronetrack/version.rb +18 -0
- data/spec/auth.js +59 -0
- data/spec/auth.rb +29 -0
- data/spec/config.yml.sample +9 -0
- data/spec/dronetrack/drone_spec.rb +132 -0
- data/spec/dronetrack/oauthhelper_spec.rb +9 -0
- data/spec/dronetrack/test1.csv +3 -0
- data/spec/dronetrack/test1.kml +47 -0
- data/spec/dronetrack/test2.csv +7 -0
- data/spec/dronetrack/track_spec.rb +136 -0
- data/spec/helper.rb +28 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ac551bf3a5aab22aed4bd8e92435c106059882b
|
4
|
+
data.tar.gz: 5cf4712aa4455872ca96a246bb9de82dd17017e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 079964ba054e51e1f83f8a84653f6473b38a35a2536210f23a3453886ea654b21cf77f73bb3e135a75757d6adbe82fd73ee54c0c1752b820d1a8407a0641269c
|
7
|
+
data.tar.gz: 315301a5c7766a8a57047e6988ca88e1934b439c8fbc52a6b7413980bc48cba7892cc92fd0af83dbb1b74a151b243472ba39f7499f7fd3e3982836b10e242a35
|
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (C) 2013 Scott Barstow
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
# Dronetrack API
|
2
|
+
|
3
|
+
This is a simple ruby gem for integrating with the Dronetrack API
|
4
|
+
|
5
|
+
# Pre-Requisites and Installation
|
6
|
+
## Prerequisites
|
7
|
+
|
8
|
+
You will need to create an account on Dronetrack and get clientId, clientSecret and redirect url to receive access token value.
|
9
|
+
Also you need bundler to install gems.
|
10
|
+
If you are going to run tests you have to install [PhantomJS](http://phantomjs.org/) too.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add to your project gem file line
|
15
|
+
|
16
|
+
```
|
17
|
+
gem 'dronetrack'
|
18
|
+
|
19
|
+
```
|
20
|
+
|
21
|
+
and run then
|
22
|
+
|
23
|
+
```shell
|
24
|
+
bundle
|
25
|
+
|
26
|
+
```
|
27
|
+
|
28
|
+
|
29
|
+
# Using Dronetrack Client
|
30
|
+
|
31
|
+
```
|
32
|
+
require 'dronetrack'
|
33
|
+
```
|
34
|
+
|
35
|
+
## Drone
|
36
|
+
|
37
|
+
Type Drone allows user to manage user's drones
|
38
|
+
|
39
|
+
```
|
40
|
+
drone_api = Dronetrack::Drone.new('baseUrl', 'accessToken') # you can get accessToken with Dronetrack::OAuthHelper
|
41
|
+
|
42
|
+
```
|
43
|
+
|
44
|
+
### Getting drones
|
45
|
+
```
|
46
|
+
drones = drone_api.all()
|
47
|
+
```
|
48
|
+
|
49
|
+
### Getting drone by id
|
50
|
+
```
|
51
|
+
drone = drone_api.get(id)
|
52
|
+
```
|
53
|
+
|
54
|
+
### Creating drone
|
55
|
+
```
|
56
|
+
drone = drone_api.create('name' => name})
|
57
|
+
```
|
58
|
+
|
59
|
+
### Updating drone
|
60
|
+
```
|
61
|
+
data = {'id' => id, 'name' => name}
|
62
|
+
drone = drone_api.update(data)
|
63
|
+
```
|
64
|
+
|
65
|
+
### Removing drone
|
66
|
+
```
|
67
|
+
drone_api.remove(id)
|
68
|
+
```
|
69
|
+
|
70
|
+
### Adding points to new or exisiting track of drone
|
71
|
+
```
|
72
|
+
# Adding points to new track
|
73
|
+
res = drone_api.add_points(id, [{latitude: latitude, longitude: longitude, timestamp: timestamp}, ...])
|
74
|
+
# res.trackId will be contain id of created track
|
75
|
+
|
76
|
+
# Adding points to exisiting track
|
77
|
+
drone_api.add_points(id, trackId, [{latitude: latitude, longitude: longitude, timestamp: timestamp }, ...])
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
### Import points from CSV or KML files to new tracks of drone
|
82
|
+
|
83
|
+
```
|
84
|
+
# Import points to new tracks from csv files
|
85
|
+
# New track will be created for each file. File name without extension will be used as track name.
|
86
|
+
drone_api.import_points_from_files(id, ['/path/to/file1', ...], :csv)
|
87
|
+
|
88
|
+
# Import points to new tracks from kml files
|
89
|
+
drone_api.import_points_from_files(id, ['/path/to/file1', ...], :kml)
|
90
|
+
|
91
|
+
|
92
|
+
```
|
93
|
+
|
94
|
+
|
95
|
+
## Track
|
96
|
+
|
97
|
+
Type Track allows user to manage tracks tracks
|
98
|
+
|
99
|
+
```
|
100
|
+
track_api = Dronetrack::Track.new('baseUrl', 'accessToken'); # you can get accessToken with Dronetrack::OAuthHelper
|
101
|
+
|
102
|
+
```
|
103
|
+
|
104
|
+
### Getting tracks
|
105
|
+
```
|
106
|
+
tracks = track_api.all()
|
107
|
+
```
|
108
|
+
|
109
|
+
### Getting track by id
|
110
|
+
```
|
111
|
+
track = track_api.get(id)
|
112
|
+
```
|
113
|
+
|
114
|
+
### Creating track
|
115
|
+
```
|
116
|
+
data = {'name' => name, 'deviceId' => droneId}
|
117
|
+
track = track_api.create(data)
|
118
|
+
```
|
119
|
+
|
120
|
+
### Updating track
|
121
|
+
```
|
122
|
+
data = {'id' => id, 'name' => name}
|
123
|
+
track_api.update(data)
|
124
|
+
```
|
125
|
+
|
126
|
+
### Removing track
|
127
|
+
```
|
128
|
+
track_api.remove(id)
|
129
|
+
```
|
130
|
+
|
131
|
+
### Getting points
|
132
|
+
```
|
133
|
+
points = track_api.get_points(id)
|
134
|
+
```
|
135
|
+
|
136
|
+
### Adding points to track
|
137
|
+
```
|
138
|
+
points = track_api.add_points(id, [{latitude: latitude, longitude: longitude, timestamp: timestamp}, ...])
|
139
|
+
|
140
|
+
```
|
141
|
+
|
142
|
+
### Import points from CSV or KML files to track
|
143
|
+
|
144
|
+
```
|
145
|
+
# Import points to new tracks from csv files
|
146
|
+
# All exisiting points will be overwriten
|
147
|
+
track_api.import_points_from_files(id, ['/path/to/file1', ...], :csv)
|
148
|
+
|
149
|
+
# Import points to new tracks from kml files
|
150
|
+
track_api.import_points_from_files(id, ['/path/to/file1', ...], :kml)
|
151
|
+
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
## OAuthHelper
|
156
|
+
|
157
|
+
Dronetrack API uses OAuth 2.0 to authorize user. To get access token you can use type OAuthHelper
|
158
|
+
|
159
|
+
```
|
160
|
+
auth = Dronetrack::OAuthHelper.new('baseUrl', 'clientId', 'clientSecret', 'redirectUrl') # you must receive clientId, clientSecret and redirectUrl from Dronetrack site
|
161
|
+
```
|
162
|
+
|
163
|
+
### Getting OAuth 2.0 access token
|
164
|
+
|
165
|
+
```
|
166
|
+
auth.getAccessToken lambda { |url, callback|
|
167
|
+
# You should redirect to this 'url' (for web apps) or open this 'url' in browser (for other apps)
|
168
|
+
# User will sign in on Dronetrack site (if need) and confirm decision to allow your app to work with Dronetrack API.
|
169
|
+
# Then Dronetrack site will redirect user to 'redirectUrl' putting pin code value as query parameter 'code' (for web apps)
|
170
|
+
# If your 'redirectUrl' is 'http://localhost' (for non-web apps) pin code will be shown to user. You should promt to enter this value by user.
|
171
|
+
# Anyway you should pass this code as first parameter of callback function (like callback(code))
|
172
|
+
} do |accessToken|
|
173
|
+
# use access token
|
174
|
+
end
|
175
|
+
|
176
|
+
# You should save accessToken value to use it in future.
|
177
|
+
|
178
|
+
```
|
179
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
task :default => :spec
|
8
|
+
task :test => :spec
|
9
|
+
|
10
|
+
namespace :doc do
|
11
|
+
require 'rdoc/task'
|
12
|
+
require File.expand_path('../lib/dronetrack/version', __FILE__)
|
13
|
+
RDoc::Task.new do |rdoc|
|
14
|
+
rdoc.rdoc_dir = 'rdoc'
|
15
|
+
rdoc.title = "dronetrack #{Dronetrack::Version}"
|
16
|
+
rdoc.main = 'README.md'
|
17
|
+
rdoc.rdoc_files.include('README.md', 'LICENSE', 'lib/**/*.rb')
|
18
|
+
end
|
19
|
+
end
|
data/dronetrack.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dronetrack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.add_development_dependency 'bundler', '~> 1.0'
|
8
|
+
spec.add_dependency 'oauth2', '~> 0.9.2'
|
9
|
+
spec.authors = ["Andrey Belchikov"]
|
10
|
+
spec.description = %q{This is a simple ruby gem for integrating with the Dronetrack API.}
|
11
|
+
spec.files = %w(LICENSE README.md Rakefile dronetrack.gemspec)
|
12
|
+
spec.files += Dir.glob("lib/**/*.rb")
|
13
|
+
spec.files += Dir.glob("spec/**/*")
|
14
|
+
spec.homepage = 'https://github.com/scottbarstow/ruby-dronetrack'
|
15
|
+
spec.name = 'dronetrack'
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
spec.required_rubygems_version = '>= 1.3.5'
|
18
|
+
spec.summary = %q{A Ruby wrapper for Dronetrack API.}
|
19
|
+
spec.test_files = Dir.glob("spec/**/*")
|
20
|
+
spec.version = Dronetrack::Version
|
21
|
+
end
|
data/lib/dronetrack.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Dronetrack
|
2
|
+
class Drone < RestService
|
3
|
+
def initialize (baseUrl, accessToken)
|
4
|
+
super(baseUrl, "/drone", accessToken)
|
5
|
+
end
|
6
|
+
|
7
|
+
def add_points (id, trackId, points=[])
|
8
|
+
if trackId.is_a? Array or trackId.is_a? Hash
|
9
|
+
points = trackId
|
10
|
+
trackId = nil
|
11
|
+
end
|
12
|
+
makeRequest "#{@path}/#{id}/points", :post, {:body => {:trackId => trackId, :points => points}.to_json, :headers=>{'Content-Type' => 'application/json'}}
|
13
|
+
end
|
14
|
+
|
15
|
+
def import_points_from_files (id, files, format=:csv)
|
16
|
+
f = format.to_s().upcase()
|
17
|
+
raise ArgumentError, "Format #{f} is not supported" if f != "CSV" && f != "KML"
|
18
|
+
body = {}
|
19
|
+
i = 1
|
20
|
+
mime = "application/vnd.google-earth.kml+xml"
|
21
|
+
mime = "text/csv" if format == :csv
|
22
|
+
files.each do |file|
|
23
|
+
body["file#{i}".to_sym] = Faraday::UploadIO.new(file, mime)
|
24
|
+
i = i + 1
|
25
|
+
end
|
26
|
+
r = createNewRequest do |con|
|
27
|
+
con.request :multipart
|
28
|
+
con.request :url_encoded
|
29
|
+
con.adapter :net_http
|
30
|
+
end
|
31
|
+
r.post "#{@path}/#{id}/import#{f}", {:body => body, :headers => {"Accept" => "application/json"}}
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Dronetrack
|
2
|
+
class OAuthHelper
|
3
|
+
def initialize (baseUrl, clientId, clientSecret, redirectUrl)
|
4
|
+
@client = OAuth2::Client.new(clientId, clientSecret, {:site => baseUrl, :authorize_url => "#{@baseUrl}/auth/dialog", :token_url => "#{@baseUrl}/auth/token"})
|
5
|
+
@redirectUrl = redirectUrl
|
6
|
+
end
|
7
|
+
|
8
|
+
def getAccessToken (getCode, &block)
|
9
|
+
url = @client.auth_code.authorize_url(:redirect_uri => @redirectUrl)
|
10
|
+
b = if block_given? then block else Proc.new() end
|
11
|
+
complete = Proc.new do |code|
|
12
|
+
token = @client.auth_code.get_token(code, :redirect_uri => @redirectUrl)
|
13
|
+
b.call token.token
|
14
|
+
end
|
15
|
+
|
16
|
+
getCode.call url, complete
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'oauth2'
|
2
|
+
require 'json'
|
3
|
+
module Dronetrack
|
4
|
+
class RestService
|
5
|
+
def initialize (baseUrl, path, accessToken)
|
6
|
+
@path = path
|
7
|
+
if baseUrl.length > 0 and baseUrl[baseUrl.length - 1] == "/"
|
8
|
+
baseUrl = baseUrl[0, baseUrl.length - 1]
|
9
|
+
end
|
10
|
+
@baseUrl = baseUrl
|
11
|
+
@accessToken = accessToken
|
12
|
+
@client = OAuth2::Client.new("", "", :site => baseUrl)
|
13
|
+
@token = OAuth2::AccessToken.from_hash(@client, :access_token => accessToken)
|
14
|
+
end
|
15
|
+
|
16
|
+
def all (query=nil)
|
17
|
+
opts = if query.nil? then {} else {:params => query} end
|
18
|
+
makeRequest @path, :get, opts
|
19
|
+
end
|
20
|
+
|
21
|
+
def get (id)
|
22
|
+
makeRequest "#{@path}/#{id}", :get
|
23
|
+
end
|
24
|
+
|
25
|
+
def create (item)
|
26
|
+
makeRequest @path, :post, {:body => item}
|
27
|
+
end
|
28
|
+
|
29
|
+
def update (item)
|
30
|
+
makeRequest "#{@path}/#{item['id']}", :put, {:body => item}
|
31
|
+
end
|
32
|
+
|
33
|
+
def remove (id)
|
34
|
+
makeRequest "#{@path}/#{id}", :delete
|
35
|
+
end
|
36
|
+
|
37
|
+
alias :destroy :remove
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
def makeRequest (url, method, opts = {}, &block)
|
42
|
+
if opts[:headers].nil?
|
43
|
+
opts[:headers] = {}
|
44
|
+
end
|
45
|
+
opts[:headers]["Accept"] = "application/json"
|
46
|
+
res = @token.request(method, url, opts, &block)
|
47
|
+
r = res.parsed
|
48
|
+
if r.kind_of?(Hash) and r.has_key?(:error)
|
49
|
+
raise StandardError, r[:error]
|
50
|
+
end
|
51
|
+
r
|
52
|
+
end
|
53
|
+
|
54
|
+
def createNewRequest (&block)
|
55
|
+
client = OAuth2::Client.new("", "", :site => @baseUrl) do |conn|
|
56
|
+
yield(conn) if block_given?
|
57
|
+
end
|
58
|
+
OAuth2::AccessToken.from_hash(client, :access_token => @accessToken)
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Dronetrack
|
2
|
+
class Track < RestService
|
3
|
+
def initialize (baseUrl, accessToken)
|
4
|
+
super(baseUrl, "/track", accessToken)
|
5
|
+
end
|
6
|
+
|
7
|
+
def get_points (id)
|
8
|
+
makeRequest "#{@path}/#{id}/points", :get
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_points (id, points=[])
|
12
|
+
makeRequest "#{@path}/#{id}/points", :post, {:body => points.to_json, :headers=>{'Content-Type' => 'application/json'}}
|
13
|
+
end
|
14
|
+
|
15
|
+
def import_points_from_files (id, files, format=:csv)
|
16
|
+
f = format.to_s().upcase()
|
17
|
+
raise ArgumentError, "Format #{f} is not supported" if f != "CSV" && f != "KML"
|
18
|
+
body = {}
|
19
|
+
i = 1
|
20
|
+
mime = "application/vnd.google-earth.kml+xml"
|
21
|
+
mime = "text/csv" if format == :csv
|
22
|
+
files.each do |file|
|
23
|
+
body["file#{i}".to_sym] = Faraday::UploadIO.new(file, mime)
|
24
|
+
i = i + 1
|
25
|
+
end
|
26
|
+
r = createNewRequest do |con|
|
27
|
+
con.request :multipart
|
28
|
+
con.request :url_encoded
|
29
|
+
con.adapter :net_http
|
30
|
+
end
|
31
|
+
r.post "#{@path}/#{id}/points/import#{f}", {:body => body, :headers => {"Accept" => "application/json"}}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Dronetrack
|
2
|
+
class Version
|
3
|
+
MAJOR = 0 unless defined? MAJOR
|
4
|
+
MINOR = 0 unless defined? MINOR
|
5
|
+
PATCH = 1 unless defined? PATCH
|
6
|
+
PRE = nil unless defined? PRE
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
# @return [String]
|
11
|
+
def to_s
|
12
|
+
[MAJOR, MINOR, PATCH, PRE].compact.join('.')
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
data/spec/auth.js
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
var system = require('system');
|
2
|
+
var args = system.args;
|
3
|
+
|
4
|
+
var showError = function (err){
|
5
|
+
system.stderr.writeLine("Error: " + err);
|
6
|
+
phantom.exit();
|
7
|
+
};
|
8
|
+
|
9
|
+
if(args.length < 5 ){
|
10
|
+
showError("Invalid PhantomJS args");
|
11
|
+
}
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
var baseUrl = args[1];
|
17
|
+
var authUrl = args[2];
|
18
|
+
var userName = args[3];
|
19
|
+
var password = args[4];
|
20
|
+
|
21
|
+
|
22
|
+
var page = require('webpage').create();
|
23
|
+
page.open(baseUrl + "/auth/signin", function (status) {
|
24
|
+
if(status == "fail") return showError("Error on load /auth/signin");
|
25
|
+
page.onLoadFinished = function(status){
|
26
|
+
if(status == "fail") return showError("Error on user signing in");
|
27
|
+
page.onLoadFinished = null;
|
28
|
+
setTimeout(function() {
|
29
|
+
page.open(authUrl, function(status){
|
30
|
+
if(status == "fail") return showError("Error on loading authorization page");
|
31
|
+
setTimeout(function() {
|
32
|
+
page.onLoadFinished = function(status){
|
33
|
+
if(status == "fail") return showError("Error on loading page with pin code");
|
34
|
+
setTimeout(function() {
|
35
|
+
var code = page.evaluate(function() {
|
36
|
+
return $(".pin-code").text();
|
37
|
+
});
|
38
|
+
code = (code || "").trim();
|
39
|
+
if(code.length > 0){
|
40
|
+
console.log(code);
|
41
|
+
phantom.exit();
|
42
|
+
}
|
43
|
+
}, 200);
|
44
|
+
};
|
45
|
+
page.evaluate(function() {
|
46
|
+
$("button[type='submit'].btn-primary").click();
|
47
|
+
});
|
48
|
+
}, 200);
|
49
|
+
});
|
50
|
+
}, 200);
|
51
|
+
};
|
52
|
+
setTimeout(function() {
|
53
|
+
page.evaluate(function(user, pwd) {
|
54
|
+
$("input[name='userNameOrEmail']").val(user);
|
55
|
+
$("input[name='password']").val(pwd);
|
56
|
+
$("button[type='submit']").click();
|
57
|
+
}, userName, password);
|
58
|
+
}, 200);
|
59
|
+
});
|
data/spec/auth.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'phantomjs.rb'
|
2
|
+
|
3
|
+
$accessToken = nil
|
4
|
+
|
5
|
+
def getAccessToken
|
6
|
+
helper = OAuthHelper.new $config['baseUrl'], $config['clientId'], $config['clientSecret'], 'http://localhost'
|
7
|
+
get_code = lambda do |url, done|
|
8
|
+
code = Phantomjs.run(File.expand_path('../auth.js', __FILE__), "\"#{$config['baseUrl']}\"", "\"#{url}\"", $config['userName'], $config['password'])
|
9
|
+
code = '' if code.nil?
|
10
|
+
code.strip!
|
11
|
+
done.call(code)
|
12
|
+
end
|
13
|
+
|
14
|
+
helper.getAccessToken get_code do |token|
|
15
|
+
raise "Invalid access token #{token}" if not token.is_a?(String) or token.length == 0
|
16
|
+
yield(token) if block_given?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def authorize
|
21
|
+
if $accessToken.nil?
|
22
|
+
getAccessToken do |token|
|
23
|
+
$accessToken = token
|
24
|
+
yield(token) if block_given?
|
25
|
+
end
|
26
|
+
else
|
27
|
+
yield($accessToken) if block_given?
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
def create_drone
|
4
|
+
before (:all) do
|
5
|
+
@droneId = @drone.create({:name => 'new Drone'})['id']
|
6
|
+
end
|
7
|
+
after (:all) do
|
8
|
+
@drone.remove @droneId
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
authorize do |access_token|
|
14
|
+
describe Dronetrack::Drone do
|
15
|
+
|
16
|
+
before(:all) do
|
17
|
+
@drone = Drone.new($config['baseUrl'], access_token)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#all' do
|
21
|
+
it 'should return all drone records of current user' do
|
22
|
+
items = @drone.all
|
23
|
+
expect(items.kind_of?(Array)).to be_true
|
24
|
+
expect(items.length).to be >= 0
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#create' do
|
29
|
+
it 'should create new drone' do
|
30
|
+
items = @drone.all
|
31
|
+
count = items.length
|
32
|
+
item = @drone.create :name => 'new Drone'
|
33
|
+
expect(item.has_key?('id')).to be_true
|
34
|
+
expect(item['name']).to eq('new Drone')
|
35
|
+
items = @drone.all
|
36
|
+
expect(items.length).to eq(count+1)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#get' do
|
41
|
+
it 'should return drone with given id' do
|
42
|
+
i = @drone.create :name => 'new Drone'
|
43
|
+
item = @drone.get i['id']
|
44
|
+
expect(item['id']).to eq(i['id'])
|
45
|
+
expect(item['name']).to eq(i['name'])
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should fail if drone is not exists' do
|
49
|
+
expect(lambda {@drone.get('id')}).to raise_error
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#update' do
|
54
|
+
it 'should update drone with given id' do
|
55
|
+
i = @drone.create :name => 'new Drone'
|
56
|
+
item = @drone.get i['id']
|
57
|
+
name= "Name#{(0...20).map{ ('a'..'z').to_a[rand(26)] }.join}"
|
58
|
+
expect(item['name']).to_not eq(name)
|
59
|
+
item['name'] = name
|
60
|
+
it = @drone.update item
|
61
|
+
expect(it['name']).to eq name
|
62
|
+
it = @drone.get i['id']
|
63
|
+
expect(it['name']).to eq name
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should fail if drone is not exists' do
|
67
|
+
item = {:id => 'id', :name => 'Drone'}
|
68
|
+
expect(lambda {@drone.update item}).to raise_error
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#remove' do
|
73
|
+
it 'should remove drone with given id' do
|
74
|
+
i = @drone.create :name => 'new Drone'
|
75
|
+
items = @drone.all
|
76
|
+
count = items.length
|
77
|
+
@drone.remove i['id']
|
78
|
+
items = @drone.all
|
79
|
+
expect(items.length).to be(count-1)
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should fail if drone is not exists' do
|
83
|
+
expect(lambda {@drone.remove 'id'}).to raise_error
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe '#add_points' do
|
88
|
+
create_drone()
|
89
|
+
it 'should create new track and add points to it if trackId is missing' do
|
90
|
+
data = [{latitude: 1, longitude: 1}, {latitude: 2, longitude: 2}]
|
91
|
+
res = @drone.add_points @droneId, data
|
92
|
+
expect(res.has_key?('trackId')).to be_true
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should add points to existing track' do
|
96
|
+
data = [{latitude: 1, longitude: 1}, {latitude: 2, longitude: 2}]
|
97
|
+
res = @drone.add_points @droneId, data
|
98
|
+
expect(res.has_key?('trackId')).to be_true
|
99
|
+
trackId = res['trackId']
|
100
|
+
data = [{latitude: 3, longitude: 3}, {latitude: 4, longitude: 4}]
|
101
|
+
res = @drone.add_points @droneId, trackId, data
|
102
|
+
expect(res['trackId']).to eq(trackId)
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'should fail for non-existing drone' do
|
106
|
+
data = [{latitude: 1, longitude: 1}, {latitude: 2, longitude: 2}]
|
107
|
+
expect(lambda {@drone.add_points 'id', data}).to raise_error
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'should fail for non-existing track' do
|
111
|
+
data = [{latitude: 1, longitude: 1}, {latitude: 2, longitude: 2}]
|
112
|
+
expect(lambda {@drone.add_points @droneId, 'trackId', data}).to raise_error
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
describe '#import_points_from_files' do
|
118
|
+
create_drone()
|
119
|
+
|
120
|
+
it 'should create new tracks and add points for each csv file' do
|
121
|
+
files = [File.expand_path('../test1.csv', __FILE__), File.expand_path('../test2.csv', __FILE__)]
|
122
|
+
@drone.import_points_from_files @droneId, files, :csv
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'should create new tracks and add points for each kml file' do
|
126
|
+
files = [File.expand_path('../test1.kml', __FILE__)]
|
127
|
+
@drone.import_points_from_files @droneId, files, :kml
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<kml xmlns="http://www.opengis.net/kml/2.2">
|
3
|
+
<Document>
|
4
|
+
<name>2012-09-15</name>
|
5
|
+
<description>21.1 km Running</description>
|
6
|
+
<Style id="redLine">
|
7
|
+
<LineStyle>
|
8
|
+
<color>ff0000ff</color>
|
9
|
+
<width>4</width>
|
10
|
+
</LineStyle>
|
11
|
+
<PolyStyle>
|
12
|
+
<color>7f00ff00</color>
|
13
|
+
</PolyStyle>
|
14
|
+
</Style>
|
15
|
+
<Placemark>
|
16
|
+
<name>2012-09-15</name>
|
17
|
+
<description>21.1 km Running</description>
|
18
|
+
<styleUrl>#redLine</styleUrl>
|
19
|
+
<LineString>
|
20
|
+
<extrude>1</extrude>
|
21
|
+
<tessellate>1</tessellate>
|
22
|
+
<altitudeMode>clampToGround</altitudeMode>
|
23
|
+
<coordinates>
|
24
|
+
-71.083963,42.363458,3.0
|
25
|
+
-71.083828,42.363452,3.0
|
26
|
+
-71.083668,42.363443,3.0
|
27
|
+
-71.083522,42.363429,3.22222222222222
|
28
|
+
-71.083399,42.363401,3.4
|
29
|
+
-71.083284,42.36337,3.54545454545455
|
30
|
+
-71.083136,42.363322,3.72727272727273
|
31
|
+
-71.082963,42.363273,3.90909090909091
|
32
|
+
-71.08279,42.363218,4.09090909090909
|
33
|
+
-71.082675,42.363181,4.18181818181818
|
34
|
+
-71.082526,42.363148,4.27272727272727
|
35
|
+
-71.082408,42.36311,4.36363636363636
|
36
|
+
-71.079023,42.362639,9.27272727272727
|
37
|
+
-71.078902,42.362675,8.27272727272727
|
38
|
+
-71.078808,42.362748,7.09090909090909
|
39
|
+
-71.078682,42.362799,5.90909090909091
|
40
|
+
-71.078261,42.362788,0.0
|
41
|
+
-71.078363,42.362849,0.0
|
42
|
+
-71.078363,42.36285,0.0
|
43
|
+
</coordinates>
|
44
|
+
</LineString>
|
45
|
+
</Placemark>
|
46
|
+
</Document>
|
47
|
+
</kml>
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
def create_simple_track
|
4
|
+
drone = Drone.new($config['baseUrl'], @access_token)
|
5
|
+
track = Track.new($config['baseUrl'], @access_token)
|
6
|
+
d = drone.create :name => 'new Drone'
|
7
|
+
item = {:name => 'new Track', :deviceId => d['id']}
|
8
|
+
track.create item
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_track
|
12
|
+
before (:all) do
|
13
|
+
t = create_simple_track
|
14
|
+
@trackId = t['id']
|
15
|
+
@droneId = t['deviceId']
|
16
|
+
end
|
17
|
+
|
18
|
+
after (:all) do
|
19
|
+
drone = Drone.new($config['baseUrl'], @access_token)
|
20
|
+
drone.remove @droneId
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
authorize do |access_token|
|
25
|
+
describe Dronetrack::Track do
|
26
|
+
|
27
|
+
before(:all) do
|
28
|
+
@track = Track.new($config['baseUrl'], access_token)
|
29
|
+
@access_token = access_token
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#all' do
|
33
|
+
it 'should return all track records of current user' do
|
34
|
+
items = @track.all
|
35
|
+
expect(items.kind_of?(Array)).to be_true
|
36
|
+
expect(items.length).to be >= 0
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#create' do
|
41
|
+
it 'should create new track' do
|
42
|
+
items = @track.all
|
43
|
+
count = items.length
|
44
|
+
item = create_simple_track()
|
45
|
+
expect(item.has_key?('id')).to be_true
|
46
|
+
expect(item['name']).to eq('new Track')
|
47
|
+
items = @track.all
|
48
|
+
expect(items.length).to eq(count+1)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#get' do
|
53
|
+
it 'should return track with given id' do
|
54
|
+
i = create_simple_track()
|
55
|
+
item = @track.get i['id']
|
56
|
+
expect(item['id']).to eq(i['id'])
|
57
|
+
expect(item['name']).to eq(i['name'])
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should fail if track is not exists' do
|
61
|
+
expect(lambda {@track.get('id')}).to raise_error
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#update' do
|
66
|
+
it 'should update track with given id' do
|
67
|
+
i = create_simple_track()
|
68
|
+
item = @track.get i['id']
|
69
|
+
name= "Name#{(0...20).map{ ('a'..'z').to_a[rand(26)] }.join}"
|
70
|
+
expect(item['name']).to_not eq(name)
|
71
|
+
item['name'] = name
|
72
|
+
it = @track.update item
|
73
|
+
expect(it['name']).to eq name
|
74
|
+
it = @track.get i['id']
|
75
|
+
expect(it['name']).to eq name
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should fail if track is not exists' do
|
79
|
+
item = {:id => 'id', :name => 'Drone'}
|
80
|
+
expect(lambda {@track.update item}).to raise_error
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#remove' do
|
85
|
+
it 'should remove track with given id' do
|
86
|
+
i = create_simple_track()
|
87
|
+
items = @track.all
|
88
|
+
count = items.length
|
89
|
+
@track.remove i['id']
|
90
|
+
items = @track.all
|
91
|
+
expect(items.length).to be(count-1)
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should fail if track is not exists' do
|
95
|
+
expect(lambda {@track.remove 'id'}).to raise_error
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#add_points' do
|
100
|
+
create_track()
|
101
|
+
it 'should add points' do
|
102
|
+
data = [{latitude: 1, longitude: 1}, {latitude: 2, longitude: 2}]
|
103
|
+
@track.add_points @trackId, data
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should fail for non-existing track' do
|
107
|
+
data = [{latitude: 1, longitude: 1}, {latitude: 2, longitude: 2}]
|
108
|
+
expect(lambda {@track.add_points 'id', data}).to raise_error
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe '#import_points_from_files' do
|
113
|
+
create_track()
|
114
|
+
it 'should create new tracks and add points for each csv file' do
|
115
|
+
files = [File.expand_path('../test1.csv', __FILE__), File.expand_path('../test2.csv', __FILE__)]
|
116
|
+
@track.import_points_from_files @trackId, files, :csv
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'should create new tracks and add points for each kml file' do
|
120
|
+
files = [File.expand_path('../test1.kml', __FILE__)]
|
121
|
+
@track.import_points_from_files @trackId, files, :kml
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '#get_points' do
|
126
|
+
create_track()
|
127
|
+
it 'should return points of the track' do
|
128
|
+
data = [{latitude: 1, longitude: 1}, {latitude: 2, longitude: 2}]
|
129
|
+
@track.add_points @trackId, data
|
130
|
+
pts = @track.get_points @trackId
|
131
|
+
expect(pts.kind_of?(Array)).to be_true
|
132
|
+
expect(pts.length).to be >= 2
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'coveralls'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
6
|
+
SimpleCov::Formatter::HTMLFormatter,
|
7
|
+
Coveralls::SimpleCov::Formatter
|
8
|
+
]
|
9
|
+
SimpleCov.start
|
10
|
+
|
11
|
+
$config = YAML.load_file(File.expand_path('../config.yml', __FILE__))
|
12
|
+
|
13
|
+
require 'dronetrack'
|
14
|
+
require 'rspec'
|
15
|
+
require 'rspec/autorun'
|
16
|
+
require_relative './auth'
|
17
|
+
|
18
|
+
RSpec.configure do |config|
|
19
|
+
config.expect_with :rspec do |c|
|
20
|
+
c.syntax = :expect
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
RSpec.configure do |conf|
|
26
|
+
include Dronetrack
|
27
|
+
end
|
28
|
+
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dronetrack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrey Belchikov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oauth2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.9.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.9.2
|
41
|
+
description: This is a simple ruby gem for integrating with the Dronetrack API.
|
42
|
+
email:
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- dronetrack.gemspec
|
51
|
+
- lib/dronetrack/drone.rb
|
52
|
+
- lib/dronetrack/oauthhelper.rb
|
53
|
+
- lib/dronetrack/restservice.rb
|
54
|
+
- lib/dronetrack/track.rb
|
55
|
+
- lib/dronetrack/version.rb
|
56
|
+
- lib/dronetrack.rb
|
57
|
+
- spec/auth.js
|
58
|
+
- spec/auth.rb
|
59
|
+
- spec/config.yml.sample
|
60
|
+
- spec/dronetrack/drone_spec.rb
|
61
|
+
- spec/dronetrack/oauthhelper_spec.rb
|
62
|
+
- spec/dronetrack/test1.csv
|
63
|
+
- spec/dronetrack/test1.kml
|
64
|
+
- spec/dronetrack/test2.csv
|
65
|
+
- spec/dronetrack/track_spec.rb
|
66
|
+
- spec/helper.rb
|
67
|
+
homepage: https://github.com/scottbarstow/ruby-dronetrack
|
68
|
+
licenses: []
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 1.3.5
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 2.0.3
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: A Ruby wrapper for Dronetrack API.
|
90
|
+
test_files:
|
91
|
+
- spec/auth.js
|
92
|
+
- spec/auth.rb
|
93
|
+
- spec/config.yml.sample
|
94
|
+
- spec/dronetrack/drone_spec.rb
|
95
|
+
- spec/dronetrack/oauthhelper_spec.rb
|
96
|
+
- spec/dronetrack/test1.csv
|
97
|
+
- spec/dronetrack/test1.kml
|
98
|
+
- spec/dronetrack/test2.csv
|
99
|
+
- spec/dronetrack/track_spec.rb
|
100
|
+
- spec/helper.rb
|