bubbles-rest-client 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +45 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +85 -0
- data/LICENSE +373 -0
- data/README.md +24 -0
- data/Rakefile +10 -0
- data/bubbles.gemspec +45 -0
- data/fixtures/vcr_cassettes/get_students_authenticated.yml +46 -0
- data/fixtures/vcr_cassettes/get_version_unauthenticated.yml +42 -0
- data/lib/bubbles.rb +650 -0
- data/lib/bubbles/config.rb +201 -0
- data/lib/bubbles/endpoint.rb +181 -0
- data/lib/bubbles/rest_client_resources.rb +124 -0
- data/lib/bubbles/rest_environment.rb +68 -0
- data/lib/bubbles/version.rb +22 -0
- metadata +201 -0
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# bubbles
|
2
|
+
A gem for easily defining client REST interfaces in ruby
|
3
|
+
|
4
|
+
## Project Goals
|
5
|
+
When working in an Android environment, [Retrofit](https://square.github.io/retrofit/) provides a simple and effective way of annotating methods so that you can define your REST interface:
|
6
|
+
```java
|
7
|
+
public interface GitHubService {
|
8
|
+
@GET("users/{user}/repos")
|
9
|
+
Call<List<Repo>> listRepos(@Path("user") String user);
|
10
|
+
}
|
11
|
+
```
|
12
|
+
|
13
|
+
You can then instantiate your REST client as follows:
|
14
|
+
```java
|
15
|
+
Retrofit retrofit = new Retrofit.Builder()
|
16
|
+
.baseUrl("https://api.github.com/")
|
17
|
+
.build();
|
18
|
+
|
19
|
+
GitHubService service = retrofit.create(GitHubService.class);
|
20
|
+
```
|
21
|
+
|
22
|
+
What this does is allow you to focus on your _handling_ of the REST responses, rather than worrying about the boilerplate code required to set up the client side of the REST API.
|
23
|
+
|
24
|
+
_bubbles_ is a Gem that seeks to provide this same behavior.
|
data/Rakefile
ADDED
data/bubbles.gemspec
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bubbles/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = Bubbles::VersionInformation.package_name
|
8
|
+
spec.version = Bubbles::VersionInformation.version_name
|
9
|
+
spec.date = Date.today.strftime("%Y-%m-%d")
|
10
|
+
spec.summary = 'FoamFactory REST Client'
|
11
|
+
spec.homepage = 'http://www.foamfactorybrewing.com'
|
12
|
+
spec.authors = ['Scott Johnson']
|
13
|
+
spec.email = 'jaywir3@gmail.com'
|
14
|
+
spec.files = %w(lib/bubbles.rb lib/bubbles/rest_environment.rb lib/bubbles/version.rb)
|
15
|
+
spec.license = 'MPL-2.0'
|
16
|
+
spec.summary = %q{A gem for easily defining client REST interfaces in Ruby}
|
17
|
+
spec.description = %q{Retrofit, by Square, allows you to easily define annoations that will generate the necessary boilerplate code for your REST interfaces. Bubbles is a Gem that seeks to bring a similar style of boilerplate generation to Ruby.}
|
18
|
+
|
19
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
20
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
21
|
+
if spec.respond_to?(:metadata)
|
22
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
23
|
+
else
|
24
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
25
|
+
"public gem pushes."
|
26
|
+
end
|
27
|
+
|
28
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
29
|
+
f.match(%r{^(test|spec|features)/})
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
38
|
+
spec.add_development_dependency "minitest-reporters", "~> 1.1"
|
39
|
+
spec.add_development_dependency "simplecov", "~> 0.16"
|
40
|
+
spec.add_development_dependency "webmock", "~> 3.0"
|
41
|
+
spec.add_development_dependency "vcr", "~> 3.0"
|
42
|
+
spec.add_development_dependency "rspec", "~> 3.8"
|
43
|
+
spec.add_dependency "addressable", "~> 2.5"
|
44
|
+
spec.add_dependency "rest-client", "~> 2.0"
|
45
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://127.0.0.1:1234/students
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- "*/*"
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip, deflate
|
14
|
+
User-Agent:
|
15
|
+
- rest-client/2.0.2 (darwin16.1.0 x86_64) ruby/2.4.0p0
|
16
|
+
Authorization:
|
17
|
+
- Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjcmVhdGlvbl9kYXRlIjoiMjAxNy0xMC0xNVQxMToyNjozMS0wNTowMCIsImV4cGlyYXRpb25fZGF0ZSI6IjIwMTctMTEtMTRUMTE6MjY6MzEtMDU6MDAiLCJ1c2VyX2lkIjoxfQ.dyCWwE4wk7aTfjnGncsqp_jq5QyICKYQPkBh5nLQwFU
|
18
|
+
Content-Type:
|
19
|
+
- application/json
|
20
|
+
Host:
|
21
|
+
- 127.0.0.1:1234
|
22
|
+
response:
|
23
|
+
status:
|
24
|
+
code: 200
|
25
|
+
message: OK
|
26
|
+
headers:
|
27
|
+
Content-Type:
|
28
|
+
- application/json; charset=utf-8
|
29
|
+
Etag:
|
30
|
+
- W/"9d4b7d98d38caa096dee4e7844662cf6"
|
31
|
+
Cache-Control:
|
32
|
+
- max-age=0, private, must-revalidate
|
33
|
+
X-Request-Id:
|
34
|
+
- de8e2f1a-4eb7-44a5-9129-84df77d3b938
|
35
|
+
X-Runtime:
|
36
|
+
- '0.014508'
|
37
|
+
Transfer-Encoding:
|
38
|
+
- chunked
|
39
|
+
body:
|
40
|
+
encoding: UTF-8
|
41
|
+
string: '{"students":[{"id":1,"name":"Joe Blow","address":"2234 Bubble Gum Ave.
|
42
|
+
#127","city":"Sometown","state":"CA","zip":"90263","phone":"5558764566","email":"bubblegumbanshee987@bazooka.org","emergencyContactName":"Some
|
43
|
+
Guy","emergencyContactPhone":"5554339182","joinDate":"2017-10-14T00:00:00.000Z","lastAdvancementDate":"2017-10-14T00:00:00.000Z","waiverSigned":true,"created_at":"2017-10-14T21:46:42.826Z","updated_at":"2017-10-14T21:46:42.826Z","preferredContact":"phone","rank":"green"}]}'
|
44
|
+
http_version:
|
45
|
+
recorded_at: Sun, 15 Oct 2017 16:51:12 GMT
|
46
|
+
recorded_with: VCR 3.0.3
|
@@ -0,0 +1,42 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://127.0.0.1:1234/version
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- "*/*"
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip, deflate
|
14
|
+
User-Agent:
|
15
|
+
- rest-client/2.0.2 (darwin16.1.0 x86_64) ruby/2.4.0p0
|
16
|
+
Content-Type:
|
17
|
+
- application/json
|
18
|
+
Host:
|
19
|
+
- 127.0.0.1:1234
|
20
|
+
response:
|
21
|
+
status:
|
22
|
+
code: 200
|
23
|
+
message: OK
|
24
|
+
headers:
|
25
|
+
Content-Type:
|
26
|
+
- application/json; charset=utf-8
|
27
|
+
Etag:
|
28
|
+
- W/"939eb8a8e26be1cdc231493379b6d17c"
|
29
|
+
Cache-Control:
|
30
|
+
- max-age=0, private, must-revalidate
|
31
|
+
X-Request-Id:
|
32
|
+
- fab0bd54-b471-46d4-8f1f-9165c1549c8f
|
33
|
+
X-Runtime:
|
34
|
+
- '0.263693'
|
35
|
+
Transfer-Encoding:
|
36
|
+
- chunked
|
37
|
+
body:
|
38
|
+
encoding: UTF-8
|
39
|
+
string: '{"name":"Sinking Moon API","versionName":"2.0.0","versionCode":8,"deployDate":"2018-01-02T22:51:29-06:00"}'
|
40
|
+
http_version:
|
41
|
+
recorded_at: Wed, 03 Jan 2018 04:51:29 GMT
|
42
|
+
recorded_with: VCR 3.0.3
|
data/lib/bubbles.rb
ADDED
@@ -0,0 +1,650 @@
|
|
1
|
+
require "bubbles/version"
|
2
|
+
require 'bubbles/config'
|
3
|
+
require 'base64'
|
4
|
+
require 'bubbles/rest_client_resources'
|
5
|
+
require 'bubbles/rest_environment'
|
6
|
+
require 'bubbles/version'
|
7
|
+
# require 'exceptions'
|
8
|
+
require 'rest-client'
|
9
|
+
require 'json'
|
10
|
+
|
11
|
+
module Bubbles
|
12
|
+
class Resources < RestClientResources
|
13
|
+
def initialize
|
14
|
+
# def initialize(env, api_key)
|
15
|
+
# @environment = get_environment env
|
16
|
+
# @api_key = api_key
|
17
|
+
# @auth_token = nil
|
18
|
+
@packageName = Bubbles::VersionInformation.package_name
|
19
|
+
@versionName = Bubbles::VersionInformation.version_name
|
20
|
+
@versionCode = Bubbles::VersionInformation.version_code
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_version_info
|
24
|
+
{
|
25
|
+
:name => @packageName,
|
26
|
+
:versionName => @versionName,
|
27
|
+
:versionCode => @versionCode
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
# def local_environment
|
32
|
+
# LOCAL_ENVIRONMENT
|
33
|
+
# end
|
34
|
+
|
35
|
+
###### BEGIN API METHODS ######
|
36
|
+
|
37
|
+
def version
|
38
|
+
execute_get_unauthenticated(@environment.version_endpoint)
|
39
|
+
end
|
40
|
+
|
41
|
+
# def login(username, password)
|
42
|
+
# encoded_auth = Base64.strict_encode64(username + ':' + password)
|
43
|
+
#
|
44
|
+
# response = execute_post_unauthenticated(@environment.login_endpoint, nil,
|
45
|
+
# {
|
46
|
+
# :authorization => 'Basic ' + encoded_auth
|
47
|
+
# })
|
48
|
+
#
|
49
|
+
# @auth_token = JSON.parse(response, object_class: OpenStruct).auth_token
|
50
|
+
#
|
51
|
+
# response
|
52
|
+
# end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# module SinkingMoonRestClient
|
57
|
+
# class Resources
|
58
|
+
# attr_accessor :environment, :api_key
|
59
|
+
#
|
60
|
+
# def check_for_expired_token
|
61
|
+
# begin
|
62
|
+
# yield
|
63
|
+
# rescue RestClient::Forbidden => e
|
64
|
+
# response_json = JSON.parse(e.response.to_s, object_class: OpenStruct)
|
65
|
+
# if response_json.error == 'Auth token is expired; You will need to login again'
|
66
|
+
# raise SinkingMoonRestClient::AuthTokenExpiredException.new
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# raise e
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
#
|
74
|
+
# ###### BEGIN API METHODS ######
|
75
|
+
#
|
76
|
+
# def version
|
77
|
+
# execute_get_unauthenticated(@environment.version_endpoint)
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# def login(username, password)
|
81
|
+
# encoded_auth = Base64.strict_encode64(username + ':' + password)
|
82
|
+
#
|
83
|
+
# response = execute_post_unauthenticated(@environment.login_endpoint, nil,
|
84
|
+
# {
|
85
|
+
# :authorization => 'Basic ' + encoded_auth
|
86
|
+
# })
|
87
|
+
#
|
88
|
+
# @auth_token = JSON.parse(response, object_class: OpenStruct).auth_token
|
89
|
+
#
|
90
|
+
# response
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# def list_students(auth_token=nil)
|
94
|
+
# check_for_expired_token do
|
95
|
+
# response = execute_get_authenticated(@environment.students_endpoint, auth_token)
|
96
|
+
# return response
|
97
|
+
# end
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# def get_student(id, auth_token=nil)
|
101
|
+
# check_for_expired_token do
|
102
|
+
# url_components = [@environment.students_endpoint, id]
|
103
|
+
# url = url_components.join('/')
|
104
|
+
# execute_get_authenticated(url, auth_token)
|
105
|
+
# end
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
# def forgot_password(email=nil)
|
109
|
+
# unless email
|
110
|
+
# raise 'Method requires a valid email address to be passed in'
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# response = execute_post_unauthenticated(@environment.forgot_password_endpoint,
|
114
|
+
# {
|
115
|
+
# :email => email
|
116
|
+
# })
|
117
|
+
# response
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# def change_password(login_hash, password, password_confirm)
|
121
|
+
# unless login_hash and password and password_confirm
|
122
|
+
# raise 'Method requires a valid login hash, password, and password confirmation to be passed in'
|
123
|
+
# end
|
124
|
+
#
|
125
|
+
# unless password == password_confirm
|
126
|
+
# raise 'Password and password confirmation do not match'
|
127
|
+
# end
|
128
|
+
#
|
129
|
+
# response = execute_put_unauthenticated(@environment.change_password_endpoint,
|
130
|
+
# {
|
131
|
+
# :one_time_login_hash => login_hash,
|
132
|
+
# :new_password => password,
|
133
|
+
# :password_confirmation => password_confirm
|
134
|
+
# })
|
135
|
+
# response
|
136
|
+
# end
|
137
|
+
#
|
138
|
+
# def create_student(student, auth_token = nil)
|
139
|
+
# check_for_expired_token do
|
140
|
+
# token = get_auth_token(auth_token)
|
141
|
+
#
|
142
|
+
# unless student
|
143
|
+
# raise 'Unable to create student with empty body'
|
144
|
+
# end
|
145
|
+
#
|
146
|
+
# response = execute_post_authenticated(@environment.students_endpoint, token,
|
147
|
+
# {
|
148
|
+
# :name => student[:name],
|
149
|
+
# :address => student[:address],
|
150
|
+
# :city => student[:city],
|
151
|
+
# :state => student[:state],
|
152
|
+
# :zip => student[:zip],
|
153
|
+
# :phone => student[:phone],
|
154
|
+
# :email => student[:email],
|
155
|
+
# :preferredContact => student[:preferredContact].downcase,
|
156
|
+
# :emergencyContactName => student[:emergencyContactName],
|
157
|
+
# :emergencyContactPhone => student[:emergencyContactPhone],
|
158
|
+
# :rank => student[:rank].downcase,
|
159
|
+
# :joinDate => student[:joinDate],
|
160
|
+
# :lastAdvancementDate => student[:lastAdvancementDate],
|
161
|
+
# :waiverSigned => student[:waiverSigned]
|
162
|
+
# })
|
163
|
+
#
|
164
|
+
# response
|
165
|
+
# end
|
166
|
+
# end
|
167
|
+
#
|
168
|
+
# def delete_student(id, auth_token = nil)
|
169
|
+
# check_for_expired_token do
|
170
|
+
# token = get_auth_token(auth_token)
|
171
|
+
#
|
172
|
+
# unless id
|
173
|
+
# raise 'Unable to delete a student without an id'
|
174
|
+
# end
|
175
|
+
#
|
176
|
+
# url_components = [@environment.students_endpoint, id]
|
177
|
+
# execute_delete_authenticated(url_components.join('/'), token)
|
178
|
+
# end
|
179
|
+
# end
|
180
|
+
#
|
181
|
+
# def update_student(id, new_record, auth_token = nil)
|
182
|
+
# check_for_expired_token do
|
183
|
+
# token = get_auth_token(auth_token)
|
184
|
+
#
|
185
|
+
# unless id
|
186
|
+
# raise 'Unable to update a student without the student id'
|
187
|
+
# end
|
188
|
+
#
|
189
|
+
# unless new_record
|
190
|
+
# raise 'Unable to update a student record without the record body'
|
191
|
+
# end
|
192
|
+
#
|
193
|
+
# url_components = [@environment.students_endpoint, id]
|
194
|
+
# execute_put_authenticated(url_components.join('/'), token, new_record)
|
195
|
+
# end
|
196
|
+
# end
|
197
|
+
#
|
198
|
+
# ##
|
199
|
+
# # :method: activate_student
|
200
|
+
# #
|
201
|
+
# # Activate a student by his/her student id. This method requires administrative privileges to call.
|
202
|
+
# #
|
203
|
+
# # :param: [Integer] id The identifier of the student to activate
|
204
|
+
# # :param: [String] auth_token The authentication token to use to identify yourself as an admin to the server.
|
205
|
+
# def activate_student(id, auth_token = nil)
|
206
|
+
# check_for_expired_token do
|
207
|
+
# token = get_auth_token(auth_token)
|
208
|
+
#
|
209
|
+
# unless id
|
210
|
+
# raise 'Unable to activate a student without an id'
|
211
|
+
# end
|
212
|
+
#
|
213
|
+
# url_components = [@environment.students_endpoint, 'activate', id]
|
214
|
+
# execute_patch_authenticated(url_components.join('/'), token)
|
215
|
+
# end
|
216
|
+
# end
|
217
|
+
#
|
218
|
+
# ##
|
219
|
+
# # :method: deactivate_student
|
220
|
+
# #
|
221
|
+
# # Deactivate a student by his/her student id. This method requires administrative privileges to call.
|
222
|
+
# #
|
223
|
+
# # :param: [Integer] id The identifier of the student to deactivate
|
224
|
+
# # :param: [String] auth_token The authentication token to use to identify yourself as an admin to the server.
|
225
|
+
# def deactivate_student(id, auth_token = nil)
|
226
|
+
# check_for_expired_token do
|
227
|
+
# token = get_auth_token(auth_token)
|
228
|
+
#
|
229
|
+
# unless id
|
230
|
+
# raise 'Unable to deactivate a student without an id'
|
231
|
+
# end
|
232
|
+
#
|
233
|
+
# url_components = [@environment.students_endpoint, 'deactivate', id]
|
234
|
+
# execute_patch_authenticated(url_components.join('/'), token)
|
235
|
+
# end
|
236
|
+
# end
|
237
|
+
#
|
238
|
+
# def get_user(id, auth_token = nil)
|
239
|
+
# check_for_expired_token do
|
240
|
+
# token = get_auth_token(auth_token);
|
241
|
+
# url_components = [@environment.users_endpoint, id]
|
242
|
+
# url = url_components.join('/')
|
243
|
+
# execute_get_authenticated(url, token)
|
244
|
+
# end
|
245
|
+
# end
|
246
|
+
#
|
247
|
+
# def create_user(user, auth_token = nil)
|
248
|
+
# check_for_expired_token do
|
249
|
+
# token = get_auth_token(auth_token)
|
250
|
+
#
|
251
|
+
# unless user
|
252
|
+
# raise SinkingMoonRestClient::InvalidBodyException.new
|
253
|
+
# end
|
254
|
+
#
|
255
|
+
# response = execute_post_authenticated(@environment.users_endpoint, token,
|
256
|
+
# {
|
257
|
+
# :name => user[:name],
|
258
|
+
# :email => user[:email],
|
259
|
+
# :username => user[:username],
|
260
|
+
# :role => user[:role]
|
261
|
+
# })
|
262
|
+
#
|
263
|
+
# response
|
264
|
+
# end
|
265
|
+
# end
|
266
|
+
#
|
267
|
+
# def update_user(id, new_record, auth_token = nil)
|
268
|
+
# check_for_expired_token do
|
269
|
+
# token = get_auth_token(auth_token)
|
270
|
+
#
|
271
|
+
# unless id
|
272
|
+
# raise 'Unable to update a user record without the user id'
|
273
|
+
# end
|
274
|
+
#
|
275
|
+
# unless new_record
|
276
|
+
# raise 'Unable to update a user record without the record body'
|
277
|
+
# end
|
278
|
+
#
|
279
|
+
# url_components = [@environment.users_endpoint, id]
|
280
|
+
# execute_put_authenticated(url_components.join('/'), token, new_record)
|
281
|
+
# end
|
282
|
+
# end
|
283
|
+
#
|
284
|
+
# def list_advancements(auth_token = nil)
|
285
|
+
# check_for_expired_token do
|
286
|
+
# token = get_auth_token(auth_token)
|
287
|
+
# response = execute_get_authenticated(@environment.advancements_endpoint, token)
|
288
|
+
# return response
|
289
|
+
# end
|
290
|
+
# end
|
291
|
+
#
|
292
|
+
# def create_advancement (advancement, auth_token = nil)
|
293
|
+
# check_for_expired_token do
|
294
|
+
# token = get_auth_token(auth_token)
|
295
|
+
#
|
296
|
+
# unless advancement
|
297
|
+
# raise 'Unable to create advancement with empty body'
|
298
|
+
# end
|
299
|
+
#
|
300
|
+
# response = execute_post_authenticated(@environment.advancements_endpoint, token,
|
301
|
+
# {
|
302
|
+
# :date => advancement[:date],
|
303
|
+
# :student_id => advancement[:student_id],
|
304
|
+
# :rank => advancement[:rank]
|
305
|
+
# })
|
306
|
+
#
|
307
|
+
# response
|
308
|
+
# end
|
309
|
+
# end
|
310
|
+
#
|
311
|
+
# def delete_advancement(id, auth_token = nil)
|
312
|
+
# check_for_expired_token do
|
313
|
+
# token = get_auth_token(auth_token)
|
314
|
+
#
|
315
|
+
# unless id
|
316
|
+
# raise 'Unable to delete an advancement without an id'
|
317
|
+
# end
|
318
|
+
#
|
319
|
+
# url_components = [@environment.advancements_endpoint, id]
|
320
|
+
# execute_delete_authenticated(url_components.join('/'), token)
|
321
|
+
# end
|
322
|
+
# end
|
323
|
+
#
|
324
|
+
# def update_advancement(id, new_record, auth_token = nil)
|
325
|
+
# check_for_expired_token do
|
326
|
+
# token = get_auth_token(auth_token)
|
327
|
+
#
|
328
|
+
# unless id
|
329
|
+
# raise 'Unable to update an advancement record without the advancement id'
|
330
|
+
# end
|
331
|
+
#
|
332
|
+
# unless new_record
|
333
|
+
# raise 'Unable to update an advancement record without the record body'
|
334
|
+
# end
|
335
|
+
#
|
336
|
+
# url_components = [@environment.advancements_endpoint, id]
|
337
|
+
# execute_put_authenticated(url_components.join('/'), token, new_record)
|
338
|
+
# end
|
339
|
+
# end
|
340
|
+
#
|
341
|
+
# def get_advancement(id, auth_token = nil)
|
342
|
+
# check_for_expired_token do
|
343
|
+
# token = get_auth_token(auth_token)
|
344
|
+
#
|
345
|
+
# unless id
|
346
|
+
# raise 'Unable to retrieve an advancement record without the advancement id'
|
347
|
+
# end
|
348
|
+
#
|
349
|
+
# url_components = [@environment.advancements_endpoint, id]
|
350
|
+
# execute_get_authenticated(url_components.join('/'), token)
|
351
|
+
# end
|
352
|
+
# end
|
353
|
+
#
|
354
|
+
# def list_tournament_registrations(auth_token = nil)
|
355
|
+
# check_for_expired_token do
|
356
|
+
# token = get_auth_token(auth_token)
|
357
|
+
#
|
358
|
+
# url_components = [@environment.tournament_registrations_endpoint]
|
359
|
+
# execute_get_authenticated(url_components.join('/'), token)
|
360
|
+
# end
|
361
|
+
# end
|
362
|
+
#
|
363
|
+
# def delete_tournament_registration(id, auth_token = nil)
|
364
|
+
# check_for_expired_token do
|
365
|
+
# token = get_auth_token(auth_token)
|
366
|
+
#
|
367
|
+
# unless id
|
368
|
+
# raise 'Unable to delete a tournament registration without an id'
|
369
|
+
# end
|
370
|
+
#
|
371
|
+
# url_components = [@environment.tournament_registrations_endpoint, id]
|
372
|
+
# execute_delete_authenticated(url_components.join('/'), token)
|
373
|
+
# end
|
374
|
+
# end
|
375
|
+
#
|
376
|
+
# def create_tournament_registration(registration)
|
377
|
+
# check_for_expired_token do
|
378
|
+
#
|
379
|
+
# unless registration
|
380
|
+
# raise 'Unable to create tournament registration with empty body'
|
381
|
+
# end
|
382
|
+
#
|
383
|
+
# begin
|
384
|
+
# data =
|
385
|
+
# {
|
386
|
+
# :name => registration[:name],
|
387
|
+
# :address => registration[:address],
|
388
|
+
# :city => registration[:city],
|
389
|
+
# :state => registration[:state],
|
390
|
+
# :zip => registration[:zip],
|
391
|
+
# :email => registration[:email],
|
392
|
+
# :birthDate => registration[:birthDate],
|
393
|
+
# :tShirtSize => registration[:tShirtSize],
|
394
|
+
# :registrationType => registration[:registrationType],
|
395
|
+
# :studentsExpected => registration[:studentsExpected],
|
396
|
+
# :noPrivateLessons => registration[:noPrivateLessons],
|
397
|
+
# :rank => registration[:rank],
|
398
|
+
# :permissionToSpar => registration[:permissionToSpar],
|
399
|
+
# :school => registration[:school],
|
400
|
+
# :formsCompetitionParticipant => registration[:formsCompetitionParticipant],
|
401
|
+
# :skillsCombineParticipant => registration[:skillsCombineParticipant],
|
402
|
+
# :sparringParticipant => registration[:sparringParticipant],
|
403
|
+
# :liabilityWaiverNeeded => registration[:liabilityWaiverNeeded],
|
404
|
+
# :privateLessonDesired => registration[:privateLessonDesired],
|
405
|
+
# :spectatorConfirmation => registration[:spectatorConfirmation],
|
406
|
+
# :donationAmount => registration[:donationAmount],
|
407
|
+
# :paymentToken => registration[:paymentToken]
|
408
|
+
# }
|
409
|
+
#
|
410
|
+
# response = execute_post_unauthenticated(@environment.tournament_registrations_endpoint, data)
|
411
|
+
# rescue RestClient::Exception => e
|
412
|
+
# responseObj = JSON.parse(e.response, object_class: OpenStruct)
|
413
|
+
# raise InsufficientDataException.new(responseObj.error)
|
414
|
+
# end
|
415
|
+
#
|
416
|
+
# response
|
417
|
+
# end
|
418
|
+
# end
|
419
|
+
#
|
420
|
+
# ###### End API Methods ######
|
421
|
+
#
|
422
|
+
# def get_auth_token(auth_token = nil)
|
423
|
+
# if @auth_token.nil? and auth_token.nil?
|
424
|
+
# raise 'Method requires authentication. Use login() first or pass in an authorization token.'
|
425
|
+
# end
|
426
|
+
#
|
427
|
+
# token = auth_token
|
428
|
+
# token = @auth_token if auth_token == nil
|
429
|
+
#
|
430
|
+
# token
|
431
|
+
# end
|
432
|
+
#
|
433
|
+
# private
|
434
|
+
#
|
435
|
+
# def execute_post_unauthenticated(endpoint, data, headers=nil)
|
436
|
+
# additionalHeaders = {
|
437
|
+
# 'X-Api-Key' => @api_key,
|
438
|
+
# :content_type => :json,
|
439
|
+
# :accept => :json
|
440
|
+
# }
|
441
|
+
#
|
442
|
+
# unless headers.nil?
|
443
|
+
# headers.each { |nextHeader|
|
444
|
+
# additionalHeaders[nextHeader[0]] = nextHeader[1]
|
445
|
+
# }
|
446
|
+
# end
|
447
|
+
#
|
448
|
+
# begin
|
449
|
+
# if @environment.scheme == 'https'
|
450
|
+
# response = RestClient::Resource.new(endpoint, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
|
451
|
+
# .post(data.to_json, additionalHeaders)
|
452
|
+
# else
|
453
|
+
# response = RestClient.post endpoint, data.to_json, additionalHeaders
|
454
|
+
# end
|
455
|
+
# rescue Errno::ECONNREFUSED
|
456
|
+
# return { :error => 'Unable to connect to host ' + @environment.host.to_s + ":" + @environment.port.to_s }.to_json
|
457
|
+
# end
|
458
|
+
#
|
459
|
+
# response
|
460
|
+
# end
|
461
|
+
#
|
462
|
+
# def execute_post_authenticated(endpoint, token, data)
|
463
|
+
# if token.nil?
|
464
|
+
# raise 'Cannot execute an authenticated POST request with no auth_token'
|
465
|
+
# end
|
466
|
+
#
|
467
|
+
# if data.nil?
|
468
|
+
# raise 'Cannot execute POST command with an empty data set'
|
469
|
+
# end
|
470
|
+
#
|
471
|
+
# begin
|
472
|
+
# if @environment.scheme == 'https'
|
473
|
+
# response = RestClient::Resource.new(endpoint, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
|
474
|
+
# .post(data.to_json,
|
475
|
+
# {
|
476
|
+
# :content_type => :json,
|
477
|
+
# :accept => :json,
|
478
|
+
# :authorization => 'Bearer ' + token
|
479
|
+
# })
|
480
|
+
#
|
481
|
+
# else
|
482
|
+
# response = RestClient.post(endpoint,
|
483
|
+
# data.to_json,
|
484
|
+
# {
|
485
|
+
# :content_type => :json,
|
486
|
+
# :accept => :json,
|
487
|
+
# :authorization => 'Bearer ' + token
|
488
|
+
# })
|
489
|
+
# end
|
490
|
+
# rescue Errno::ECONNREFUSED
|
491
|
+
# return { :error => 'Unable to connect to host ' + @environment.host.to_s + ':' + @environment.port.to_s }.to_json
|
492
|
+
# end
|
493
|
+
#
|
494
|
+
# response
|
495
|
+
# end
|
496
|
+
#
|
497
|
+
#
|
498
|
+
# def execute_get_authenticated(endpoint, auth_token)
|
499
|
+
# token = get_auth_token(auth_token)
|
500
|
+
#
|
501
|
+
# begin
|
502
|
+
# if @environment.scheme == 'https'
|
503
|
+
# response = RestClient::Resource.new(endpoint, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
|
504
|
+
# .get({
|
505
|
+
# :authorization => 'Bearer ' + token,
|
506
|
+
# :content_type => :json,
|
507
|
+
# :accept => :json
|
508
|
+
# })
|
509
|
+
# else
|
510
|
+
# response = RestClient.get(endpoint,
|
511
|
+
# {
|
512
|
+
# :authorization => 'Bearer ' + token,
|
513
|
+
# :content_type => :json
|
514
|
+
# })
|
515
|
+
# end
|
516
|
+
# rescue Errno::ECONNREFUSED
|
517
|
+
# return {:error => 'Unable to connect to host ' + @environment.host.to_s + ':' + @environment.port.to_s}.to_json
|
518
|
+
# end
|
519
|
+
#
|
520
|
+
# response
|
521
|
+
# end
|
522
|
+
#
|
523
|
+
# def execute_put_unauthenticated(endpoint, data)
|
524
|
+
# begin
|
525
|
+
# if @environment.scheme == 'https'
|
526
|
+
# response = RestClient::Resource.new(endpoint, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
|
527
|
+
# .put(data.to_json,
|
528
|
+
# {
|
529
|
+
# :content_type => :json,
|
530
|
+
# :accept => :json
|
531
|
+
# })
|
532
|
+
#
|
533
|
+
# else
|
534
|
+
# response = RestClient.put(endpoint,
|
535
|
+
# data.to_json,
|
536
|
+
# {
|
537
|
+
# :content_type => :json,
|
538
|
+
# :accept => :json
|
539
|
+
# })
|
540
|
+
# end
|
541
|
+
# rescue Errno::ECONNREFUSED
|
542
|
+
# return {:error => 'Unable to connect to host ' + @environment.host.to_s + ':' + @environment.port.to_s}.to_json
|
543
|
+
# end
|
544
|
+
#
|
545
|
+
# response
|
546
|
+
# end
|
547
|
+
#
|
548
|
+
# def execute_put_authenticated(endpoint, token, data)
|
549
|
+
# if token.nil?
|
550
|
+
# raise 'Cannot execute an authenticated PUT request with no auth_token'
|
551
|
+
# end
|
552
|
+
#
|
553
|
+
# if data.nil?
|
554
|
+
# raise 'Cannot execute PUT command with an empty data set'
|
555
|
+
# end
|
556
|
+
#
|
557
|
+
# begin
|
558
|
+
# if @environment.scheme == 'https'
|
559
|
+
# response = RestClient::Resource.new(endpoint, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
|
560
|
+
# .put(data.to_json,
|
561
|
+
# {
|
562
|
+
# :content_type => :json,
|
563
|
+
# :accept => :json,
|
564
|
+
# :authorization => 'Bearer ' + token
|
565
|
+
# })
|
566
|
+
#
|
567
|
+
# else
|
568
|
+
# response = RestClient.put(endpoint,
|
569
|
+
# data.to_json,
|
570
|
+
# {
|
571
|
+
# :content_type => :json,
|
572
|
+
# :accept => :json,
|
573
|
+
# :authorization => 'Bearer ' + token
|
574
|
+
# })
|
575
|
+
# end
|
576
|
+
# rescue Errno::ECONNREFUSED
|
577
|
+
# return { :error => 'Unable to connect to host ' + @environment.host.to_s + ':' + @environment.port.to_s }.to_json
|
578
|
+
# end
|
579
|
+
#
|
580
|
+
# response
|
581
|
+
# end
|
582
|
+
#
|
583
|
+
# def execute_patch_authenticated(endpoint, token)
|
584
|
+
# if token.nil?
|
585
|
+
# raise 'Cannot execute an authenticated PATCH request with no auth_token'
|
586
|
+
# end
|
587
|
+
#
|
588
|
+
# begin
|
589
|
+
# if @environment.scheme == 'https'
|
590
|
+
# response = RestClient::Resource.new(endpoint, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
|
591
|
+
# .patch(nil,
|
592
|
+
# {
|
593
|
+
# :content_type => :json,
|
594
|
+
# :accept => :json,
|
595
|
+
# :authorization => 'Bearer ' + token
|
596
|
+
# })
|
597
|
+
#
|
598
|
+
# else
|
599
|
+
# response = RestClient.patch(endpoint, nil,
|
600
|
+
# {
|
601
|
+
# :content_type => :json,
|
602
|
+
# :accept => :json,
|
603
|
+
# :authorization => 'Bearer ' + token
|
604
|
+
# })
|
605
|
+
# end
|
606
|
+
# rescue Errno::ECONNREFUSED
|
607
|
+
# return { :error => 'Unable to connect to host ' + @environment.host.to_s + ':' + @environment.port.to_s }.to_json
|
608
|
+
# end
|
609
|
+
#
|
610
|
+
# response
|
611
|
+
# end
|
612
|
+
#
|
613
|
+
# def execute_delete_authenticated(endpoint, auth_token)
|
614
|
+
# token = get_auth_token(auth_token)
|
615
|
+
#
|
616
|
+
# begin
|
617
|
+
# if @environment.scheme == 'https'
|
618
|
+
# response = RestClient::Resource.new(endpoint, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
|
619
|
+
# .delete({
|
620
|
+
# :authorization => 'Bearer ' + token,
|
621
|
+
# :content_type => :json,
|
622
|
+
# :accept => :json
|
623
|
+
# })
|
624
|
+
# else
|
625
|
+
# response = RestClient.delete(endpoint,
|
626
|
+
# {
|
627
|
+
# :authorization => 'Bearer ' + token,
|
628
|
+
# :content_type => :json
|
629
|
+
# })
|
630
|
+
# end
|
631
|
+
# rescue Errno::ECONNREFUSED
|
632
|
+
# return {:error => 'Unable to connect to host ' + @environment.host.to_s + ':' + @environment.port.to_s}.to_json
|
633
|
+
# end
|
634
|
+
#
|
635
|
+
# response
|
636
|
+
# end
|
637
|
+
#
|
638
|
+
# def get_environment(environment)
|
639
|
+
# if !environment || environment == 'production'
|
640
|
+
# return PRODUCTION_ENVIRONMENT
|
641
|
+
# elsif environment == 'staging'
|
642
|
+
# return STAGING_ENVIRONMENT
|
643
|
+
# end
|
644
|
+
#
|
645
|
+
#
|
646
|
+
# LOCAL_ENVIRONMENT
|
647
|
+
# end
|
648
|
+
# end # Class Resources
|
649
|
+
# end # Module SinkingMoonRestClient
|
650
|
+
|