Class: TinCanApi::RemoteLRS
- Inherits:
-
Object
- Object
- TinCanApi::RemoteLRS
show all
- Includes:
- TCAPIVersion
- Defined in:
- lib/tin_can_api/remote_lrs.rb
Overview
Class used to communicate with a TCAPI endpoint synchronously
Constant Summary
collapse
- VALID_PARAMS =
%w(end_point user_name password version)
TCAPIVersion::V095, TCAPIVersion::V100, TCAPIVersion::V101
Constants included
from Enum
Enum::CONVERT_PROC
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#about ⇒ Object
-
#clear_state(activity, agent, registration) ⇒ Object
-
#delete_activity_profile(profile) ⇒ Object
-
#delete_agent_profile(profile) ⇒ Object
-
#delete_state(state) ⇒ Object
-
#initialize(options = {}, &block) ⇒ RemoteLRS
constructor
A new instance of RemoteLRS.
-
#more_statements(more_url) ⇒ Object
-
#query_statements(statement_query) ⇒ Object
-
#retrieve_activity_profile(id, activity) ⇒ Object
-
#retrieve_activity_profile_ids(activity) ⇒ Object
-
#retrieve_agent_profile(id, agent) ⇒ Object
-
#retrieve_agent_profile_ids(agent) ⇒ Object
-
#retrieve_state(id, activity, agent, registration) ⇒ Object
-
#retrieve_state_ids(activity, agent, registration) ⇒ Object
-
#retrieve_statement(id) ⇒ Object
-
#retrieve_voided_statement(id) ⇒ Object
-
#save_activity_profile(profile) ⇒ Object
-
#save_agent_profile(profile) ⇒ Object
-
#save_state(state) ⇒ Object
-
#save_statement(statement) ⇒ Object
-
#save_statements(statements) ⇒ Object
#latest_version
Methods included from Enum
extended
Constructor Details
#initialize(options = {}, &block) ⇒ RemoteLRS
Returns a new instance of RemoteLRS.
14
15
16
17
18
|
# File 'lib/tin_can_api/remote_lrs.rb', line 14
def initialize(options={}, &block)
setup_options(options)
yield_or_eval(&block) if block_given?
@version ||= latest_version
end
|
Instance Attribute Details
#end_point ⇒ Object
Returns the value of attribute end_point.
12
13
14
|
# File 'lib/tin_can_api/remote_lrs.rb', line 12
def end_point
@end_point
end
|
#password ⇒ Object
Returns the value of attribute password.
12
13
14
|
# File 'lib/tin_can_api/remote_lrs.rb', line 12
def password
@password
end
|
#user_name ⇒ Object
Returns the value of attribute user_name.
12
13
14
|
# File 'lib/tin_can_api/remote_lrs.rb', line 12
def user_name
@user_name
end
|
#version ⇒ Object
Returns the value of attribute version.
12
13
14
|
# File 'lib/tin_can_api/remote_lrs.rb', line 12
def version
@version
end
|
Instance Method Details
#about ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/tin_can_api/remote_lrs.rb', line 20
def about
response = connection.get("#{path}about")
LrsResponse.new do |lrs|
lrs.status = response.status
if response.status== 200
lrs.content = About.new(json: response.body)
lrs.success = true
else
lrs.success = false
end
end
end
|
#clear_state(activity, agent, registration) ⇒ Object
192
193
194
195
196
197
198
199
200
|
# File 'lib/tin_can_api/remote_lrs.rb', line 192
def clear_state(activity, agent, registration)
query_params = {
'activityId' => activity.id,
'agent' => agent.serialize(version).to_json
}
query_params['registration'] = registration if registration
delete_document('activities/state', query_params)
end
|
#delete_activity_profile(profile) ⇒ Object
236
237
238
239
240
241
242
243
|
# File 'lib/tin_can_api/remote_lrs.rb', line 236
def delete_activity_profile(profile)
query_params = {
'profileId' => profile.id,
'activityId' => profile.activity.id,
}
delete_document('activities/profile', query_params)
end
|
#delete_agent_profile(profile) ⇒ Object
279
280
281
282
283
284
285
286
|
# File 'lib/tin_can_api/remote_lrs.rb', line 279
def delete_agent_profile(profile)
query_params = {
'profileId' => profile.id,
'agent' => profile.agent.serialize(version).to_json
}
delete_document('agents/profile', query_params)
end
|
#delete_state(state) ⇒ Object
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/tin_can_api/remote_lrs.rb', line 181
def delete_state(state)
query_params = {
'stateId' => state.id,
'activityId' => state.activity.id,
'agent' => state.agent.serialize(version).to_json
}
query_params['registration'] = state.registration if state.registration
delete_document('activities/state', query_params)
end
|
#more_statements(more_url) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/tin_can_api/remote_lrs.rb', line 124
def more_statements(more_url)
response = connection.get do |req|
req.url("#{path}#{more_url}")
end
LrsResponse.new do |lrs|
lrs.status = response.status
if response.status == 200
lrs.success = true
lrs.content = StatementsResult.new(json: response.body)
else
lrs.success = false
end
end
end
|
#query_statements(statement_query) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/tin_can_api/remote_lrs.rb', line 99
def query_statements(statement_query)
query = statement_query
unless query
query = version == TCAPIVersion::V095 ? StatementsQueryV095.new : StatementsQuery.new
end
raise Errors::IncompatibleTCAPIVersion, "Attempted to issue #{version} query using a #{query.version} set of query parameters." unless version == query.version
response = connection.get do |req|
req.url("#{path}statements")
req.params.merge!(query.parameter_map)
end
LrsResponse.new do |lrs|
lrs.status = response.status
if response.status == 200
lrs.success = true
lrs.content = StatementsResult.new(json: response.body)
else
lrs.success = false
end
end
end
|
#retrieve_activity_profile(id, activity) ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/tin_can_api/remote_lrs.rb', line 210
def retrieve_activity_profile(id, activity)
query_params = {
'profileId' => id,
'activityId' => activity.id,
}
document = ActivityProfileDocument.new do |apd|
apd.id = id
apd.activity = activity
end
lrs_response = get_document('activities/profile', query_params, document)
if lrs_response.status == 200
lrs_response.content = document
end
lrs_response
end
|
#retrieve_activity_profile_ids(activity) ⇒ Object
202
203
204
205
206
207
208
|
# File 'lib/tin_can_api/remote_lrs.rb', line 202
def retrieve_activity_profile_ids(activity)
query_params = {
'activityId' => activity.id
}
get_profile_keys('activities/profile', query_params)
end
|
#retrieve_agent_profile(id, agent) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
# File 'lib/tin_can_api/remote_lrs.rb', line 253
def retrieve_agent_profile(id, agent)
query_params = {
'profileId' => id,
'agent' => agent.serialize(version).to_json
}
document = AgentProfileDocument.new do |apd|
apd.id = id
apd.agent = agent
end
lrs_response = get_document('agents/profile', query_params, document)
if lrs_response.status == 200
lrs_response.content = document
end
lrs_response
end
|
#retrieve_agent_profile_ids(agent) ⇒ Object
245
246
247
248
249
250
251
|
# File 'lib/tin_can_api/remote_lrs.rb', line 245
def retrieve_agent_profile_ids(agent)
query_params = {
'agent' => agent.serialize(version).to_json
}
get_profile_keys('agents/profile', query_params)
end
|
#retrieve_state(id, activity, agent, registration) ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/tin_can_api/remote_lrs.rb', line 152
def retrieve_state(id, activity, agent, registration)
query_params = {
'stateId' => id,
'activityId' => activity.id,
'agent' => agent.serialize(version).to_json
}
document = StateDocument.new do |sd|
sd.id = id
sd.activity = activity
sd.agent = agent
end
lrs_response = get_document('activities/state', query_params, document)
if lrs_response.status == 200
lrs_response.content = document
end
lrs_response
end
|
#retrieve_state_ids(activity, agent, registration) ⇒ Object
142
143
144
145
146
147
148
149
150
|
# File 'lib/tin_can_api/remote_lrs.rb', line 142
def retrieve_state_ids(activity, agent, registration)
query_params = {
'activityId' => activity.id,
'agent' => agent.serialize(version).to_json
}
query_params['registration'] = registration if registration
get_profile_keys('activities/state', query_params)
end
|
#retrieve_statement(id) ⇒ Object
90
91
92
|
# File 'lib/tin_can_api/remote_lrs.rb', line 90
def retrieve_statement(id)
get_statement(id, 'statementId')
end
|
#retrieve_voided_statement(id) ⇒ Object
94
95
96
97
|
# File 'lib/tin_can_api/remote_lrs.rb', line 94
def retrieve_voided_statement(id)
param_name = version == TCAPIVersion.V095 ? 'statementId' : 'voidedStatementId'
get_statement(id, param_name)
end
|
#save_activity_profile(profile) ⇒ Object
227
228
229
230
231
232
233
234
|
# File 'lib/tin_can_api/remote_lrs.rb', line 227
def save_activity_profile(profile)
query_params = {
'profileId' => profile.id,
'activityId' => profile.activity.id,
}
save_document('activities/profile', query_params, profile)
end
|
#save_agent_profile(profile) ⇒ Object
270
271
272
273
274
275
276
277
|
# File 'lib/tin_can_api/remote_lrs.rb', line 270
def save_agent_profile(profile)
query_params = {
'profileId' => profile.id,
'agent' => profile.agent.serialize(version).to_json
}
save_document('agents/profile', query_params, profile)
end
|
#save_state(state) ⇒ Object
171
172
173
174
175
176
177
178
179
|
# File 'lib/tin_can_api/remote_lrs.rb', line 171
def save_state(state)
query_params = {
'stateId' => state.id,
'activityId' => state.activity.id,
'agent' => state.agent.serialize(version).to_json
}
save_document('activities/state', query_params, state)
end
|
#save_statement(statement) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/tin_can_api/remote_lrs.rb', line 33
def save_statement(statement)
if statement.id
response = connection.put do |req|
req.url("#{path}statements")
req.['Content-Type'] = 'application/json'
req.params.merge!({'statementId' => statement.id})
req.body = statement.serialize(latest_version).to_json
end
else
response = connection.post do |req|
req.url("#{path}statements")
req.['Content-Type'] = 'application/json'
req.body = statement.serialize(latest_version).to_json
end
end
LrsResponse.new do |lrs|
lrs.status = response.status
lrs.content = statement
if response.status == 200
statement.id = JSON.parse(response.body).first
lrs.success = true
elsif response.status == 204
lrs.success = true
else
lrs.success = false
end
end
end
|
#save_statements(statements) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/tin_can_api/remote_lrs.rb', line 63
def save_statements(statements)
if statements.empty?
return LrsResponse.new do |lrs|
lrs.success = true
end
end
response = connection.post do |req|
req.url("#{path}statements")
req.['Content-Type'] = 'application/json'
req.body = statements.map {|s| s.serialize(latest_version)}.to_json
end
LrsResponse.new do |lrs|
lrs.status = response.status
if response.status == 200
lrs.content = statements
ids = JSON.parse(response.body)
statements.each_with_index do |statement, index|
statement.id = ids[index]
end
lrs.success = true
else
lrs.success = false
end
end
end
|