litmos-api-client 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ecdf099ae1e471f13510b157b2a7e38c2fd0138e
4
+ data.tar.gz: eeaa1ee2fa0c714d02627c5533740e3d70d01c39
5
+ SHA512:
6
+ metadata.gz: ddd84109cb3688698653140fd22589f1e37e57023289e8aa18da368fa029b48f026062ed208934fc7b2ad980793c55b49d302defde6ea1427c7c416c89b3ae98
7
+ data.tar.gz: 4fadad3e458519f2da0280e73725c1af89ef12ff92e013e2caf1c3163ab150d4a775fbd632efe5d8107f4284546da98771167bfaf96152bf1eebb1e10679fa63
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/litmos_api_client'
@@ -0,0 +1,234 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+
4
+ require File.dirname(__FILE__) + '/litmos_api_client/users'
5
+ require File.dirname(__FILE__) + '/litmos_api_client/teams'
6
+ require File.dirname(__FILE__) + '/litmos_api_client/courses'
7
+
8
+ module LitmosApiClient
9
+ class NotFound < Exception; end
10
+ class ApiError < Exception; end
11
+ class ArgumentError < Exception; end
12
+
13
+ class API
14
+ include LitmosApiClient::Users
15
+ include LitmosApiClient::Teams
16
+ include LitmosApiClient::Courses
17
+
18
+ # Litmos Developer API Documentation: http://help.litmos.com/developer-api/
19
+ API_VERSION = "1"
20
+ def initialize(api_key, source, config = {})
21
+ raise ArgumentError.new('Your need to specify your api key') unless api_key
22
+ raise ArgumentError.new('You need to specify a source website') unless source
23
+
24
+ defaults = {
25
+ :api_version => API_VERSION
26
+ }
27
+
28
+ @config = defaults.merge(config).freeze
29
+ @api_key = api_key
30
+ @source = source
31
+ @litmosURL = "https://api.litmos.com/v#{@config[:api_version]}.svc"
32
+ end
33
+
34
+ def get(path, params={})
35
+ dont_parse_response = params.delete(:dont_parse_response)
36
+
37
+ options = {
38
+ :content_type => :json,
39
+ :accept => :json,
40
+ :params => params.merge(:apikey => @api_key, :source => @source)
41
+ }
42
+
43
+ RestClient.get("#{@litmosURL}/#{path}", options) do |response, request, result|
44
+ case response.code
45
+ when 200, 201
46
+ # 200 Success. User/Course etc updated, deleted or retrieved
47
+ # 201 Success. User/Course etc created
48
+ if response.blank?
49
+ true
50
+ else
51
+ if dont_parse_response
52
+ response
53
+ else
54
+ parse_response(response)
55
+ end
56
+ end
57
+ when 404 # 404 Not Found. The User/Course etc that you requested does not exist
58
+ raise NotFound.new(response)
59
+
60
+ else
61
+ # 400 Bad Request. Check that your Uri and request body is well formed
62
+ # 403 Forbidden. Check your API key, HTTPS setting, Source parameter etc
63
+ # 409 Conflict. Often occurs when trying to create an item that already exists
64
+ raise ApiError.new(response)
65
+
66
+ end
67
+ end
68
+ end
69
+
70
+ def post(path, params={}, query_params={})
71
+ query_params = query_params.merge(:apikey => @api_key, :source => @source)
72
+ query_string = query_params.collect { |k,v| "#{k}=#{CGI::escape(v)}" }.join('&')
73
+ query_string = "?#{query_string}" unless query_string.blank?
74
+
75
+ dont_parse_response = params.delete(:dont_parse_response)
76
+
77
+ options = {
78
+ :content_type => :json,
79
+ :accept => :json,
80
+ }
81
+
82
+ RestClient.post("#{@litmosURL}/#{path}#{query_string}", params.to_json, options) do |response, request, result|
83
+ case response.code
84
+ when 200, 201
85
+ # 200 Success. User/Course etc updated, deleted or retrieved
86
+ # 201 Success. User/Course etc created
87
+
88
+ if response.blank?
89
+ true
90
+ else
91
+ if dont_parse_response
92
+ response
93
+ else
94
+ parse_response(response)
95
+ end
96
+ end
97
+
98
+ when 404 # 404 Not Found. The User/Course etc that you requested does not exist
99
+ raise NotFound.new(response)
100
+
101
+ else
102
+ # 400 Bad Request. Check that your Uri and request body is well formed
103
+ # 403 Forbidden. Check your API key, HTTPS setting, Source parameter etc
104
+ # 409 Conflict. Often occurs when trying to create an item that already exists
105
+ raise ApiError.new(response)
106
+
107
+ end
108
+ end
109
+ end
110
+
111
+ def put(path, params={}, query_params={})
112
+ query_params = query_params.merge(:apikey => @api_key, :source => @source)
113
+ query_string = query_params.collect { |k,v| "#{k}=#{CGI::escape(v)}" }.join('&')
114
+ query_string = "?#{query_string}" unless query_string.blank?
115
+
116
+ dont_parse_response = params.delete(:dont_parse_response)
117
+
118
+ options = {
119
+ :content_type => :json,
120
+ :accept => :json,
121
+ }
122
+
123
+ RestClient.put("#{@litmosURL}/#{path}#{query_string}", params.to_json, options) do |response, request, result|
124
+ case response.code
125
+ when 200, 201
126
+ # 200 Success. User/Course etc updated, deleted or retrieved
127
+ # 201 Success. User/Course etc created
128
+
129
+ if response.blank?
130
+ true
131
+ else
132
+ if dont_parse_response
133
+ response
134
+ else
135
+ parse_response(response)
136
+ end
137
+ end
138
+
139
+ when 404 # 404 Not Found. The User/Course etc that you requested does not exist
140
+ raise NotFound.new(response)
141
+
142
+ else
143
+ puts response.code
144
+ # 400 Bad Request. Check that your Uri and request body is well formed
145
+ # 403 Forbidden. Check your API key, HTTPS setting, Source parameter etc
146
+ # 409 Conflict. Often occurs when trying to create an item that already exists
147
+ raise ApiError.new(response)
148
+
149
+ end
150
+ end
151
+ end
152
+
153
+ def delete(path, params={})
154
+ dont_parse_response = params.delete(:dont_parse_response)
155
+
156
+ options = {
157
+ :content_type => :json,
158
+ :accept => :json,
159
+ :params => params.merge(:apikey => @api_key, :source => @source)
160
+ }
161
+
162
+ RestClient.delete("#{@litmosURL}/#{path}", options) do |response, request, result|
163
+ case response.code
164
+ when 200, 201
165
+ # 200 Success. User/Course etc updated, deleted or retrieved
166
+ # 201 Success. User/Course etc created
167
+
168
+ if response.blank?
169
+ true
170
+ else
171
+ if dont_parse_response
172
+ response
173
+ else
174
+ parse_response(response)
175
+ end
176
+ end
177
+
178
+ when 404 # 404 Not Found. The User/Course etc that you requested does not exist
179
+ raise NotFound.new(response)
180
+
181
+ else
182
+ # 400 Bad Request. Check that your Uri and request body is well formed
183
+ # 403 Forbidden. Check your API key, HTTPS setting, Source parameter etc
184
+ # 409 Conflict. Often occurs when trying to create an item that already exists
185
+ raise ApiError.new(response)
186
+
187
+ end
188
+ end
189
+ end
190
+
191
+ protected
192
+
193
+ ASP_DATE_REGEXP=/\/Date\(([0-9]+)\+[0-9]+\)\//
194
+
195
+ def parse_asp_date(asp_date)
196
+ DateTime.strptime(asp_date.gsub(ASP_DATE_REGEXP, '\1'), '%Q')
197
+ end
198
+
199
+ # for de-camelCasing the result keys
200
+ # from: http://stackoverflow.com/questions/8706930/converting-nested-hash-keys-from-camelcase-to-snake-case-in-ruby
201
+
202
+ def underscore(string)
203
+ string.gsub(/::/, '/').
204
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
205
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
206
+ tr("-", "_").
207
+ downcase
208
+ end
209
+
210
+ def underscore_key(k)
211
+ underscore(k.to_s).to_sym
212
+ end
213
+
214
+ def parse_response(response)
215
+ convert_hash_keys(JSON.parse(response))
216
+ end
217
+
218
+ def convert_hash_keys(value)
219
+ if value.is_a?(String) and value =~ ASP_DATE_REGEXP
220
+ return parse_asp_date(value)
221
+ end
222
+
223
+ case value
224
+ when Array
225
+ value.map { |v| convert_hash_keys(v) }
226
+ # or `value.map(&method(:convert_hash_keys))`
227
+ when Hash
228
+ Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
229
+ else
230
+ value
231
+ end
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,25 @@
1
+ module LitmosApiClient
2
+ module Courses
3
+ def courses(params={})
4
+ get :courses, params
5
+ end
6
+
7
+ def find_course_by_id(id)
8
+ get("courses/#{id}")
9
+ rescue NotFound
10
+ nil
11
+ end
12
+
13
+ def reset_user_course(options={})
14
+ raise ArgumentError.new(":user_id is required") if options[:user_id].blank?
15
+ raise ArgumentError.new(":course_id is required") if options[:course_id].blank?
16
+
17
+ put("/users/#{options[:user_id]}/courses/#{options[:course_id]}/reset")
18
+ end
19
+ def find_courses_by_user_id(id)
20
+ get("users/#{id}/courses")
21
+ rescue NotFound
22
+ nil
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ module LitmosApiClient
2
+ module Teams
3
+ def teams(options={})
4
+ get :teams, options
5
+ end
6
+
7
+ def find_team_by_id(id)
8
+ get("teams/#{id}")
9
+ rescue NotFound
10
+ nil
11
+ end
12
+
13
+ def find_users_by_team_id(id)
14
+ get("teams/#{id}/users")
15
+ rescue NotFound
16
+ nil
17
+ end
18
+
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?
22
+
23
+ params = {
24
+ 'Id' => options[:user_id]
25
+ }
26
+
27
+ post("teams/#{options[:team_id]}/users", [params])
28
+ end
29
+
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?
33
+
34
+ delete("teams/#{options[:team_id]}/users/#{options[:user_id]}")
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ module LitmosApiClient
2
+ module Users
3
+ def users(params={})
4
+ get(:users, params)
5
+ end
6
+
7
+ def find_user_by_id(id)
8
+ get("users/#{id}")
9
+ rescue NotFound
10
+ nil
11
+ end
12
+
13
+ def create_user(options={})
14
+ raise ArgumentError.new(":username is required") if options[:username].blank?
15
+ raise ArgumentError.new(":first_name is required") if options[:first_name].blank?
16
+ raise ArgumentError.new(":last_name is required") if options[:last_name].blank?
17
+ raise ArgumentError.new(":email is required") if options[:email].blank?
18
+
19
+ params = {
20
+ 'UserName' => options[:username],
21
+ 'FirstName' => options[:first_name],
22
+ 'LastName' => options[:last_name],
23
+ 'Email' => options[:email],
24
+ 'DisableMessages' => true,
25
+ 'IsCustomUsername' => true,
26
+ 'SkipFirstLogin' => true
27
+ }
28
+
29
+ post "users", params
30
+ end
31
+
32
+ def delete_user(id)
33
+ delete "users/#{id}"
34
+ end
35
+
36
+ def update_user(id, options={})
37
+ put "users/#{id}", options
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'litmos-api-client'
3
+ s.version = '0.0.2'
4
+ s.date = '2014-05-09'
5
+ s.summary = "Simple lib to consume litmos api"
6
+ s.description = "Simple lib to consume litmos api from litmos platform"
7
+ s.authors = ["Fabien Garcia"]
8
+ s.email = 'fab0670312047@gmail.com'
9
+ s.files = ["lib/litmos-api-client.rb"]
10
+ s.files = [
11
+ "lib/litmos-api-client.rb",
12
+ "lib/litmos_api_client.rb",
13
+ "lib/litmos_api_client/courses.rb",
14
+ "lib/litmos_api_client/teams.rb",
15
+ "lib/litmos_api_client/users.rb",
16
+ "litmos-api-client.gemspec"
17
+ ]
18
+ s.homepage =
19
+ 'http://rubygems.org/gems/hola'
20
+ s.license = 'MIT'
21
+
22
+ s.add_runtime_dependency 'rest-client', '>= 0'
23
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: litmos-api-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Fabien Garcia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-09 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
+ description: Simple lib to consume litmos api from litmos platform
28
+ email: fab0670312047@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/litmos-api-client.rb
34
+ - lib/litmos_api_client.rb
35
+ - lib/litmos_api_client/courses.rb
36
+ - lib/litmos_api_client/teams.rb
37
+ - lib/litmos_api_client/users.rb
38
+ - litmos-api-client.gemspec
39
+ homepage: http://rubygems.org/gems/hola
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.2.2
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Simple lib to consume litmos api
63
+ test_files: []