litmos-client 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.rdoc +1 -2
- data/Rakefile +1 -1
- data/lib/litmos_client/teams.rb +4 -4
- data/lib/litmos_client.rb +58 -11
- data/litmos-client.gemspec +1 -1
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -44,8 +44,7 @@ For methods that aren't yet implemented in the gem, you can simply use the *get*
|
|
44
44
|
Litmos-client depends on the Rest-client[https://github.com/archiloque/rest-client], and JSON gems (should be automatically loaded via the gemspec).
|
45
45
|
|
46
46
|
== TODO
|
47
|
-
*
|
48
|
-
* Support for creating objects and other http verbs (PUT, POST)
|
47
|
+
* Figure out how to write a test suite for a live API!
|
49
48
|
|
50
49
|
== Contributing to litmos-client
|
51
50
|
|
data/Rakefile
CHANGED
@@ -15,7 +15,7 @@ Jeweler::Tasks.new do |gem|
|
|
15
15
|
gem.name = "litmos-client"
|
16
16
|
gem.homepage = "http://github.com/kennon/litmos-client"
|
17
17
|
gem.license = "MIT"
|
18
|
-
gem.version = "0.0.
|
18
|
+
gem.version = "0.0.5"
|
19
19
|
gem.summary = %Q{Litmos-client is a Ruby wrapper for the Litmos API}
|
20
20
|
gem.description = %Q{Litmos-Client is a Ruby gem that provides a wrapper for interacting with the Litmos Learning Management System API.}
|
21
21
|
gem.email = "kennon@angryturnip.com"
|
data/lib/litmos_client/teams.rb
CHANGED
@@ -17,8 +17,8 @@ module LitmosClient
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def add_user_to_team(options={})
|
20
|
-
raise ArgumentError.new("team_id is required") if options[:team_id].blank?
|
21
|
-
raise ArgumentError.new("user_id is required") if options[:user_id].blank?
|
20
|
+
raise ArgumentError.new(":team_id is required") if options[:team_id].blank?
|
21
|
+
raise ArgumentError.new(":user_id is required") if options[:user_id].blank?
|
22
22
|
|
23
23
|
params = {
|
24
24
|
'Id' => options[:user_id]
|
@@ -28,8 +28,8 @@ module LitmosClient
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def remove_user_from_team(options={})
|
31
|
-
raise ArgumentError.new("team_id is required") if options[:team_id].blank?
|
32
|
-
raise ArgumentError.new("user_id is required") if options[:user_id].blank?
|
31
|
+
raise ArgumentError.new(":team_id is required") if options[:team_id].blank?
|
32
|
+
raise ArgumentError.new(":user_id is required") if options[:user_id].blank?
|
33
33
|
|
34
34
|
delete("teams/#{options[:team_id]}/users/#{options[:user_id]}")
|
35
35
|
end
|
data/lib/litmos_client.rb
CHANGED
@@ -34,18 +34,28 @@ module LitmosClient
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def get(path, params={})
|
37
|
+
dont_parse_response = params.delete(:dont_parse_response)
|
38
|
+
|
37
39
|
options = {
|
38
40
|
:content_type => :json,
|
39
41
|
:accept => :json,
|
40
42
|
:params => params.merge(:apikey => @api_key, :source => @source)
|
41
43
|
}
|
44
|
+
|
42
45
|
RestClient.get("#{@litmosURL}/#{path}", options) do |response, request, result|
|
43
46
|
case response.code
|
44
47
|
when 200, 201
|
45
48
|
# 200 Success. User/Course etc updated, deleted or retrieved
|
46
49
|
# 201 Success. User/Course etc created
|
47
|
-
|
48
|
-
|
50
|
+
if response.blank?
|
51
|
+
true
|
52
|
+
else
|
53
|
+
if dont_parse_response
|
54
|
+
response
|
55
|
+
else
|
56
|
+
parse_response(response)
|
57
|
+
end
|
58
|
+
end
|
49
59
|
when 404 # 404 Not Found. The User/Course etc that you requested does not exist
|
50
60
|
raise NotFound.new(response)
|
51
61
|
|
@@ -63,6 +73,8 @@ module LitmosClient
|
|
63
73
|
query_params = query_params.merge(:apikey => @api_key, :source => @source)
|
64
74
|
query_string = query_params.collect { |k,v| "#{k}=#{CGI::escape(v)}" }.join('&')
|
65
75
|
query_string = "?#{query_string}" unless query_string.blank?
|
76
|
+
|
77
|
+
dont_parse_response = params.delete(:dont_parse_response)
|
66
78
|
|
67
79
|
options = {
|
68
80
|
:content_type => :json,
|
@@ -74,7 +86,16 @@ module LitmosClient
|
|
74
86
|
when 200, 201
|
75
87
|
# 200 Success. User/Course etc updated, deleted or retrieved
|
76
88
|
# 201 Success. User/Course etc created
|
77
|
-
|
89
|
+
|
90
|
+
if response.blank?
|
91
|
+
true
|
92
|
+
else
|
93
|
+
if dont_parse_response
|
94
|
+
response
|
95
|
+
else
|
96
|
+
parse_response(response)
|
97
|
+
end
|
98
|
+
end
|
78
99
|
|
79
100
|
when 404 # 404 Not Found. The User/Course etc that you requested does not exist
|
80
101
|
raise NotFound.new(response)
|
@@ -90,17 +111,29 @@ module LitmosClient
|
|
90
111
|
end
|
91
112
|
|
92
113
|
def delete(path, params={})
|
114
|
+
dont_parse_response = params.delete(:dont_parse_response)
|
115
|
+
|
93
116
|
options = {
|
94
117
|
:content_type => :json,
|
95
118
|
:accept => :json,
|
96
119
|
:params => params.merge(:apikey => @api_key, :source => @source)
|
97
120
|
}
|
121
|
+
|
98
122
|
RestClient.delete("#{@litmosURL}/#{path}", options) do |response, request, result|
|
99
123
|
case response.code
|
100
124
|
when 200, 201
|
101
125
|
# 200 Success. User/Course etc updated, deleted or retrieved
|
102
126
|
# 201 Success. User/Course etc created
|
103
|
-
|
127
|
+
|
128
|
+
if response.blank?
|
129
|
+
true
|
130
|
+
else
|
131
|
+
if dont_parse_response
|
132
|
+
response
|
133
|
+
else
|
134
|
+
parse_response(response)
|
135
|
+
end
|
136
|
+
end
|
104
137
|
|
105
138
|
when 404 # 404 Not Found. The User/Course etc that you requested does not exist
|
106
139
|
raise NotFound.new(response)
|
@@ -113,15 +146,20 @@ module LitmosClient
|
|
113
146
|
|
114
147
|
end
|
115
148
|
end
|
116
|
-
end
|
149
|
+
end
|
150
|
+
|
151
|
+
protected
|
152
|
+
|
153
|
+
ASP_DATE_REGEXP=/\/Date\(([0-9]+)\+[0-9]+\)\//
|
154
|
+
|
155
|
+
def parse_asp_date(asp_date)
|
156
|
+
DateTime.strptime(asp_date.gsub(ASP_DATE_REGEXP, '\1'), '%Q')
|
157
|
+
end
|
117
158
|
|
118
|
-
end
|
119
|
-
|
120
|
-
module StringHelpers
|
121
159
|
# for de-camelCasing the result keys
|
122
160
|
# from: http://stackoverflow.com/questions/8706930/converting-nested-hash-keys-from-camelcase-to-snake-case-in-ruby
|
123
161
|
|
124
|
-
def
|
162
|
+
def underscore(string)
|
125
163
|
string.gsub(/::/, '/').
|
126
164
|
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
127
165
|
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
@@ -129,11 +167,19 @@ module LitmosClient
|
|
129
167
|
downcase
|
130
168
|
end
|
131
169
|
|
132
|
-
def
|
170
|
+
def underscore_key(k)
|
133
171
|
underscore(k.to_s).to_sym
|
134
172
|
end
|
135
173
|
|
136
|
-
def
|
174
|
+
def parse_response(response)
|
175
|
+
convert_hash_keys(JSON.parse(response))
|
176
|
+
end
|
177
|
+
|
178
|
+
def convert_hash_keys(value)
|
179
|
+
if value.is_a?(String) and value =~ ASP_DATE_REGEXP
|
180
|
+
return parse_asp_date(value)
|
181
|
+
end
|
182
|
+
|
137
183
|
case value
|
138
184
|
when Array
|
139
185
|
value.map { |v| convert_hash_keys(v) }
|
@@ -144,6 +190,7 @@ module LitmosClient
|
|
144
190
|
value
|
145
191
|
end
|
146
192
|
end
|
193
|
+
|
147
194
|
end
|
148
195
|
|
149
196
|
end
|
data/litmos-client.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: litmos-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kennon Ballou
|