aux_helper 0.0.20
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.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/aux_helper.gemspec +20 -0
- data/lib/aux_helper/version.rb +3 -0
- data/lib/aux_helper.rb +288 -0
- data/readme.md +5 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d3ee692e128de8b1e9507e910144127bf78d1356
|
4
|
+
data.tar.gz: df1f2c16e500b9b45492b7e913db93a1e2ed5b73
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00a4283309084403afb0950eaf0c5035588aa45ee00f89949f79aa59d652fa75e45c839201b98603cc5973197aade5a1de01d59addf70ec4b2bd704b8d963b3a
|
7
|
+
data.tar.gz: 182967f328c80f0faeb1f33f0137b3a3a797b2190f2fead48321d6f6e7421d9f8f2a0fbbb88e9eeed59a945a708c6764f473bb385b4bb47f0b31cd388195f877
|
data/Gemfile
ADDED
data/Rakefile
ADDED
data/aux_helper.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/aux_helper/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["thor-aux"]
|
6
|
+
gem.email = ["gems@thorshammer.us "]
|
7
|
+
gem.description = %q{aUX gem}
|
8
|
+
gem.summary = %q{aUX API helper}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "aux_helper"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = AUXHelper::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "rest-client"
|
19
|
+
gem.add_runtime_dependency "json"
|
20
|
+
end
|
data/lib/aux_helper.rb
ADDED
@@ -0,0 +1,288 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
require "aux_helper/version"
|
4
|
+
|
5
|
+
=begin
|
6
|
+
|
7
|
+
assumptions
|
8
|
+
* helper users require that error information be logged, not returned
|
9
|
+
* helper users won't ever deal with API URIs
|
10
|
+
* the SDK should implement ActiveRecord classes
|
11
|
+
* model classes should define variables and set them to default values
|
12
|
+
|
13
|
+
design decisions
|
14
|
+
* on error: log error message and return nil
|
15
|
+
|
16
|
+
todo
|
17
|
+
* improve error handling
|
18
|
+
* handle each exception type differently
|
19
|
+
|
20
|
+
=end
|
21
|
+
|
22
|
+
module AUXHelper
|
23
|
+
##
|
24
|
+
# AUXHelper::Api makes it easier to use the Alpha UX backend REST API
|
25
|
+
#
|
26
|
+
# == Usage
|
27
|
+
#
|
28
|
+
# api = AUXHelper::Api.new 'http://localhost:9393'
|
29
|
+
#
|
30
|
+
class Api
|
31
|
+
|
32
|
+
def initialize(api_server, api_key=nil)
|
33
|
+
@api_server = api_server
|
34
|
+
@api_key = api_key
|
35
|
+
p " Running AUXHelper version #{AUXHelper::VERSION}"
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_reader :api_server
|
39
|
+
|
40
|
+
# return true when all environment variables are set
|
41
|
+
def self.validate_env_variables(env_vars)
|
42
|
+
p "Enivonrment variables at runtime"
|
43
|
+
env_vars.all? {|env_var|
|
44
|
+
if ENV.has_key? env_var
|
45
|
+
p " #{env_var}=#{ENV[env_var]}"
|
46
|
+
true
|
47
|
+
else
|
48
|
+
p " #{env_var} is not set! The application will not start."
|
49
|
+
false
|
50
|
+
end
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# == Description
|
56
|
+
#
|
57
|
+
# Fetch all recent taps from the Alpha UX API server
|
58
|
+
#
|
59
|
+
# Example:
|
60
|
+
#
|
61
|
+
# drops = api.get_taps #=> [{"id" => "0123456789abcdef01234567", ...}, ...]
|
62
|
+
#
|
63
|
+
# Returns nil on error.
|
64
|
+
#
|
65
|
+
# TODO - add limit parameter
|
66
|
+
#
|
67
|
+
def get_taps
|
68
|
+
get "#{@api_server.to_s}/v0/taps"
|
69
|
+
end
|
70
|
+
|
71
|
+
#
|
72
|
+
# == Description
|
73
|
+
#
|
74
|
+
# Fetch the tap with the specified ID
|
75
|
+
#
|
76
|
+
# @param tap_id If valid mongo ID string for an existing tap, the tap
|
77
|
+
# will be returned as a hash
|
78
|
+
#
|
79
|
+
# Example:
|
80
|
+
#
|
81
|
+
# tap = api.get_tap('0123456789abcdef01234567') #=> {"id" => "0123456789abcdef01234567", ...}
|
82
|
+
#
|
83
|
+
# Returns nil on error.
|
84
|
+
#
|
85
|
+
def get_tap(tap_id)
|
86
|
+
#return if are_invalid_mongo_key tap_id
|
87
|
+
get "#{@api_server.to_s}/v0/taps/#{tap_id}"
|
88
|
+
end
|
89
|
+
|
90
|
+
def update_tap(tap_id, options)
|
91
|
+
return if are_invalid options
|
92
|
+
put "#{@api_server.to_s}/v0/taps/#{tap_id}", options
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# == Description
|
97
|
+
#
|
98
|
+
# Fetch the drop with the specified IDs
|
99
|
+
#
|
100
|
+
# @param tap_id string containing the mongo ID of an existing tap
|
101
|
+
# @param drop_id string containing the mongo ID of an existing drop
|
102
|
+
#
|
103
|
+
# Example:
|
104
|
+
#
|
105
|
+
# tap = api.get_tap('0123456789abcdef01234567', 5) #=> {"id" => 5, "tap_id" => "7654321fedcba9876543210", ...}
|
106
|
+
#
|
107
|
+
# Returns nil on error.
|
108
|
+
#
|
109
|
+
def get_drop(tap_id, drop_id)
|
110
|
+
#return if are_invalid_mongo_key tap_id
|
111
|
+
return if are_invalid drop_id
|
112
|
+
get "#{@api_server.to_s}/v0/taps/#{tap_id}/drops/#{drop_id.to_i}"
|
113
|
+
end
|
114
|
+
|
115
|
+
def add_tap(options)
|
116
|
+
return if are_invalid options
|
117
|
+
post "#{@api_server.to_s}/v0/taps", options
|
118
|
+
end
|
119
|
+
|
120
|
+
def add_drop(tap_id, options)
|
121
|
+
return if are_invalid tap_id, options
|
122
|
+
post "#{@api_server.to_s}/v0/taps/#{tap_id.to_s}/drops", options
|
123
|
+
end
|
124
|
+
|
125
|
+
def update_drop(tap_id, drop_id, options)
|
126
|
+
return if are_invalid tap_id, options
|
127
|
+
put "#{@api_server.to_s}/v0/taps/#{tap_id.to_s}/drops/#{drop_id.to_s}", options
|
128
|
+
end
|
129
|
+
|
130
|
+
# authenticates the user with the API and returns their profile
|
131
|
+
def auth_user(auth)
|
132
|
+
return if are_invalid auth
|
133
|
+
# note - differences from 'post': (1) no .to_json (2) return response hash
|
134
|
+
response_json = RestClient.post "#{@api_server.to_s}/v0/users/authenticate", auth, compute_headers(true)
|
135
|
+
response = JSON.parse(response_json)
|
136
|
+
response['response']
|
137
|
+
rescue => e
|
138
|
+
log_error auth, e
|
139
|
+
end
|
140
|
+
|
141
|
+
def get_user(id)
|
142
|
+
#return if are_invalid_mongo_key id
|
143
|
+
get "#{@api_server.to_s}/v0/users/#{id}"
|
144
|
+
end
|
145
|
+
|
146
|
+
# add email address to signup list
|
147
|
+
# returns true if successful
|
148
|
+
def signup_email(email_address)
|
149
|
+
return if are_invalid email
|
150
|
+
post "#{@api_server.to_s}/v0/register", { :address => email_address.to_s }
|
151
|
+
end
|
152
|
+
|
153
|
+
def update_drop_feedback(tap_id, drop_id, update)
|
154
|
+
return if are_invalid tap_id, drop_id, update
|
155
|
+
post "#{@api_server.to_s}/v0/taps/#{tap_id}/drops/#{drop_id}/update", update
|
156
|
+
end
|
157
|
+
|
158
|
+
def lookup_alias(element_alias)
|
159
|
+
return if are_invalid element_alias
|
160
|
+
get "#{@api_server.to_s}/v0/aliases/#{element_alias}"
|
161
|
+
end
|
162
|
+
|
163
|
+
def update_location(drop_uri, longitude, latitude)
|
164
|
+
return if are_invalid drop_uri, longitude, latitude
|
165
|
+
put drop_uri, { :location => [longitude, latitude] }
|
166
|
+
end
|
167
|
+
|
168
|
+
def update_user(user_id, body)
|
169
|
+
return if are_invalid user_id, body
|
170
|
+
put "#{@api_server.to_s}/v0/users/#{user_id}", body
|
171
|
+
end
|
172
|
+
|
173
|
+
def add_comment_tap(tap_id, comment_string)
|
174
|
+
return if are_invalid tap_id, comment_string
|
175
|
+
post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", { :feedback => { :comments => [comment_string] } }
|
176
|
+
end
|
177
|
+
|
178
|
+
#COUNTERS
|
179
|
+
|
180
|
+
def update_counters_tap(tap_id, counter_id, amount)
|
181
|
+
return if are_invalid tap_id, counter_id, amount
|
182
|
+
counters_hash = Hash.new
|
183
|
+
counters_hash[counter_id] = amount
|
184
|
+
post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", { :feedback => { :counters => counters_hash } }
|
185
|
+
end
|
186
|
+
|
187
|
+
#TAGS
|
188
|
+
def set_tags_tap(tap_id, tags)
|
189
|
+
return if are_invalid tap_id, tags
|
190
|
+
post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", {:feedback => { :set_tags => tags}}
|
191
|
+
end
|
192
|
+
|
193
|
+
def unset_tags_tap(tap_id, tags)
|
194
|
+
return if are_invalid tap_id, tags
|
195
|
+
post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", {:feedback => { :unset_tags => tags}}
|
196
|
+
end
|
197
|
+
|
198
|
+
# @return hash with attributes passed in and readable_id
|
199
|
+
def get_config(id)
|
200
|
+
return if are_invalid id
|
201
|
+
get "#{@api_server.to_s}/v0/configs/#{id}"
|
202
|
+
end
|
203
|
+
|
204
|
+
# @param config is a hash
|
205
|
+
def put_config(id, config)
|
206
|
+
return if are_invalid id, config
|
207
|
+
put "#{@api_server.to_s}/v0/configs/#{id}", config
|
208
|
+
end
|
209
|
+
|
210
|
+
private
|
211
|
+
|
212
|
+
def compute_headers(isJson=false)
|
213
|
+
headers = Hash.new
|
214
|
+
if @api_key != nil
|
215
|
+
headers[:headers] = {"Casual-API-Key" => @api_key}
|
216
|
+
headers[:headers] = {"AUX-API-Key" => @api_key}
|
217
|
+
end
|
218
|
+
if isJson
|
219
|
+
headers[:content_type] = :json
|
220
|
+
headers[:accept] = :json
|
221
|
+
end
|
222
|
+
return headers
|
223
|
+
end
|
224
|
+
|
225
|
+
def get(url)
|
226
|
+
response = RestClient.get url, compute_headers
|
227
|
+
parsed_response = JSON.parse(response)
|
228
|
+
parsed_response['response']
|
229
|
+
rescue => e
|
230
|
+
log_error url, e
|
231
|
+
end
|
232
|
+
|
233
|
+
def put(url, body)
|
234
|
+
response_json = RestClient.put url, body.to_json, compute_headers(true)
|
235
|
+
response = JSON.parse(response_json)
|
236
|
+
response['meta']['code'] >= 200 && response['meta']['code'] <= 300
|
237
|
+
rescue => e
|
238
|
+
log_error url, e
|
239
|
+
end
|
240
|
+
|
241
|
+
def post(url, body)
|
242
|
+
response_json = RestClient.post url, body.to_json, compute_headers(true)
|
243
|
+
response = JSON.parse(response_json)
|
244
|
+
response['response']
|
245
|
+
rescue => e
|
246
|
+
log_error url, e
|
247
|
+
end
|
248
|
+
|
249
|
+
def are_invalid(*params)
|
250
|
+
if params.nil? || params.any? {|param| param.nil? }
|
251
|
+
calling_method = caller[1][/`.*'/][1..-2]
|
252
|
+
p "Error in #{calling_method} - one or more parameters is nil - #{params.to_json}"
|
253
|
+
true
|
254
|
+
else
|
255
|
+
false
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def create_alias
|
260
|
+
base64_charaters = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
|
261
|
+
(0..5).map{base64_charaters.shuffle[0]}.join
|
262
|
+
end
|
263
|
+
|
264
|
+
# TODO broken
|
265
|
+
=begin
|
266
|
+
def are_invalid_mongo_key(*params)
|
267
|
+
if params.nil? || params.any? {|param| param.nil? || !param.is_a?(String) || param.count != 24}
|
268
|
+
calling_method = caller[1][/`.*'/][1..-2]
|
269
|
+
p "Error in #{calling_method} - one or more parameters is nil - #{params.to_json}"
|
270
|
+
true
|
271
|
+
else
|
272
|
+
false
|
273
|
+
end
|
274
|
+
end
|
275
|
+
=end
|
276
|
+
|
277
|
+
# logs error with method name of caller
|
278
|
+
# always returns nil
|
279
|
+
def log_error(url, message)
|
280
|
+
calling_method = caller[1][/`.*'/][1..-2]
|
281
|
+
p "Error in #{self.class.name}.#{calling_method}\n" <<
|
282
|
+
" url: #{url}\n" <<
|
283
|
+
" message: #{message}"
|
284
|
+
nil
|
285
|
+
end
|
286
|
+
|
287
|
+
end
|
288
|
+
end
|
data/readme.md
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aux_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.20
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- thor-aux
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: aUX gem
|
42
|
+
email:
|
43
|
+
- "gems@thorshammer.us\t"
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- Gemfile
|
49
|
+
- Rakefile
|
50
|
+
- aux_helper.gemspec
|
51
|
+
- lib/aux_helper.rb
|
52
|
+
- lib/aux_helper/version.rb
|
53
|
+
- readme.md
|
54
|
+
homepage: ''
|
55
|
+
licenses: []
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.4.6
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: aUX API helper
|
77
|
+
test_files: []
|