casual_helper 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/lib/casual_helper.rb +165 -0
- metadata +63 -0
@@ -0,0 +1,165 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
require "casual_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
|
+
* log errors and return nil
|
15
|
+
|
16
|
+
todo
|
17
|
+
* improve error handling
|
18
|
+
* handle each exception type differently
|
19
|
+
|
20
|
+
=end
|
21
|
+
|
22
|
+
JSON_CONTENT = {:content_type => :json, :accept => :json}
|
23
|
+
|
24
|
+
module CasualHelper
|
25
|
+
class Api
|
26
|
+
|
27
|
+
def initialize(api_server)
|
28
|
+
@api_server = api_server
|
29
|
+
end
|
30
|
+
|
31
|
+
attr_reader :api_server
|
32
|
+
|
33
|
+
# return true when all environment variables are set
|
34
|
+
def self.validate_env_variables(env_vars)
|
35
|
+
p "Enivonrment variables at runtime"
|
36
|
+
env_vars.all? {|env_var|
|
37
|
+
if ENV.has_key? env_var
|
38
|
+
p " #{env_var}=#{ENV[env_var]}"
|
39
|
+
true
|
40
|
+
else
|
41
|
+
p " #{env_var} is not set! The application will not start."
|
42
|
+
false
|
43
|
+
end
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_drop(tap_id, drop_id)
|
48
|
+
return if are_invalid tap_id, drop_id
|
49
|
+
get "#{@api_server.to_s}/v0/taps/#{tap_id}/drops/#{drop_id}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def add_tap(options)
|
53
|
+
return if are_invalid tap_id
|
54
|
+
post "#{@api_server.to_s}/v0/taps", options
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_tap(tap_id)
|
58
|
+
return if are_invalid tap_id
|
59
|
+
get "#{@api_server.to_s}/v0/taps/#{tap_id}"
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_taps
|
63
|
+
get "#{@api_server.to_s}/v0/taps"
|
64
|
+
end
|
65
|
+
|
66
|
+
def add_drop(tap_id, options)
|
67
|
+
return if are_invalid tap_id, options
|
68
|
+
post "#{@api_server.to_s}/v0/taps/#{tap_id.to_s}/drops", options
|
69
|
+
end
|
70
|
+
|
71
|
+
# authenticates the user with the API and returns their Casual profile
|
72
|
+
def auth_user(auth)
|
73
|
+
return if are_invalid auth
|
74
|
+
# note - differences from 'post': (1) no .to_json (2) return response hash
|
75
|
+
response_json = RestClient.post "#{@api_server.to_s}/v0/users/authenticate", auth, JSON_CONTENT
|
76
|
+
response = JSON.parse(response_json)
|
77
|
+
response['response']
|
78
|
+
rescue => e
|
79
|
+
log_error url, e
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_user(id)
|
83
|
+
return if are_invalid id
|
84
|
+
get "#{@api_server.to_s}/v0/users/#{id}"
|
85
|
+
end
|
86
|
+
|
87
|
+
# add email address to signup list
|
88
|
+
# returns true if successful
|
89
|
+
def signup_email(email_address)
|
90
|
+
return if are_invalid email
|
91
|
+
post "#{@api_server.to_s}/v0/register", { :address => email_address.to_s }
|
92
|
+
end
|
93
|
+
|
94
|
+
def update_drop_feedback(tap_id, drop_id, update)
|
95
|
+
return if are_invalid tap_id, drop_id, update
|
96
|
+
post "#{@api_server.to_s}/v0/taps/#{tap_id}/drops/#{drop_id}/update", update
|
97
|
+
end
|
98
|
+
|
99
|
+
def lookup_alias(element_alias)
|
100
|
+
return if are_invalid element_alias
|
101
|
+
get "#{@api_server.to_s}/v0/aliases/#{element_alias}"
|
102
|
+
end
|
103
|
+
|
104
|
+
def update_location(drop_uri, longitude, latitude)
|
105
|
+
return if are_invalid drop_uri, longitude, latitude
|
106
|
+
put drop_uri, { :location => [longitude, latitude] }
|
107
|
+
end
|
108
|
+
|
109
|
+
def update_user(user_id, body)
|
110
|
+
return if are_invalid user_id, body
|
111
|
+
put "#{@api_server.to_s}/v0/users/#{user_id}", body.to_json
|
112
|
+
end
|
113
|
+
|
114
|
+
def add_comment_tap(tap_id, comment_string)
|
115
|
+
return if are_invalid tap_id, comment_string
|
116
|
+
post "#{@api_server.to_s}/v0/taps/#{tap_id}/update", { :feedback => { :comments => [comment_string] } }
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def get(url)
|
122
|
+
response = RestClient.get url
|
123
|
+
parsed_response = JSON.parse(response)
|
124
|
+
parsed_response['response']
|
125
|
+
rescue => e
|
126
|
+
log_error url, e
|
127
|
+
end
|
128
|
+
|
129
|
+
def put(url, body)
|
130
|
+
response_json = RestClient.put url, body, JSON_CONTEN
|
131
|
+
response = JSON.parse(response_json)
|
132
|
+
response['meta']['code'] >= 200 && response['meta']['code'] <= 300
|
133
|
+
rescue => e
|
134
|
+
log_error url, e
|
135
|
+
end
|
136
|
+
|
137
|
+
def post(url, body)
|
138
|
+
response_json = RestClient.post url, body.to_json, JSON_CONTENT
|
139
|
+
response = JSON.parse(response_json)
|
140
|
+
response['meta']['code'] >= 200 && response['meta']['code'] <= 300
|
141
|
+
rescue => e
|
142
|
+
log_error url, e
|
143
|
+
end
|
144
|
+
|
145
|
+
def are_invalid(*params)
|
146
|
+
if params.nil? || params.any? {|param| param.nil? }
|
147
|
+
p "Error in #{calling_method} - one or more parameters is nil - #{params.to_json}"
|
148
|
+
true
|
149
|
+
else
|
150
|
+
false
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# logs error with method name of caller
|
155
|
+
# always returns nil
|
156
|
+
def log_error(url, message)
|
157
|
+
calling_method = caller[1][/`.*'/][1..-2]
|
158
|
+
p "Error in #{self.class.name}.#{calling_method}"
|
159
|
+
p " url: #{url}"
|
160
|
+
p " message: #{message}"
|
161
|
+
nil
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: casual_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- matthewspivey
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-09-20 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Casual gem
|
22
|
+
email:
|
23
|
+
- spivey.mathew@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/casual_helper.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: ""
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.6
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: A casual gem, that probably isn't what you're looking for
|
62
|
+
test_files: []
|
63
|
+
|