Skip to content

Commit 2dc6c4b

Browse files
Merge pull request #20 from Learnosity/LRN-39609/ruby-signature-api-demos
poc hash hmac signature
2 parents 27201cd + fd50c91 commit 2dc6c4b

File tree

16 files changed

+434
-81
lines changed

16 files changed

+434
-81
lines changed

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
10+
## [v0.2.2] - 2023-06-29
11+
### Security
12+
- Upgraded signature to match the security standard.
13+
914
### Added
1015

1116
- Improved Makefile and tests

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Using RubyGems is the recommended way to install the Learnosity SDK for Ruby in
7070
### **Alternative method 1: download the zip file**
7171
Download the latest version of the SDK as a self-contained ZIP file from the [GitHub Releases](https://github.com/Learnosity/learnosity-sdk-ruby/releases) page. The distribution ZIP file contains all the necessary dependencies.
7272

73-
Note: after installation, run this command in the SDK root folder:
73+
Note: after installation, run this command in (docs/quickstart/lrn-sdk-rails/):
7474

7575
``` bash
7676
bundle install
@@ -83,7 +83,7 @@ To install from the terminal, run this command:
8383
git clone [email protected]:Learnosity/learnosity-sdk-Ruby.git
8484
```
8585

86-
Note: after installation, run this command in the SDK root folder:
86+
Note: after installation, run this command in (docs/quickstart/lrn-sdk-rails/):
8787

8888
``` bash
8989
bundle install
@@ -114,7 +114,18 @@ From this point on, we'll assume that your web server is available at this local
114114

115115
http://localhost:3000
116116

117-
The page will load. This is a basic example of an assessment loaded into a web page with Learnosity's assessment player. You can interact with this demo assessment to try out the various Question types.
117+
You can now access the APIs using the following URL [click here](http://localhost:3000)
118+
119+
<img width="50%" height="50%" src="docs/images/image-quickstart-index.png">
120+
121+
Following are the routes to access our APIs.
122+
123+
* Author API : http://localhost:3000/author/index
124+
* Questions API : http://localhost:3000/questions/index
125+
* Items API : http://localhost:3000/items/index
126+
* Reports API : http://localhost:3000/reports/index
127+
128+
Open these pages with your web browser. These are all basic examples of Learnosity's integration. You can interact with these demo pages to try out the various APIs. The Items API example is a basic example of an assessment loaded into a web page with Learnosity's assessment player. You can interact with this demo assessment to try out the various Question types.
118129

119130
<img width="50%" height="50%" src="docs/images/image-quickstart-examples-assessment.png">
120131

@@ -123,12 +134,12 @@ The page will load. This is a basic example of an assessment loaded into a web p
123134
### **How it works**
124135
Let's walk through the code for this standalone assessment example. The source files are included under the `docs/quickstart/lrn-sdk-rails/` folder.
125136

126-
The first section of code is a controller file in Ruby, [index_controller.rb](docs/quickstart/lrn-sdk-rails/app/controllers/index_controller.rb) from `docs/quickstart/lrn-sdk-rails/app/controllers/` and it is executed server-side. It constructs a set of configuration options for Items API, and securely signs them using the consumer key. We also add a few lines to [application.rb](docs/quickstart/lrn-sdk-rails/config/application.rb) for our Learnosity credentials. The second section is HTML and JavaScript in an [ERB](https://docs.ruby-lang.org/en/2.3.0/ERB.html) template [index.html.erb](docs/quickstart/lrn-sdk-rails/app/views/index/index.html.erb) and is executed client-side, once the page is loaded in the browser. It renders and runs the assessment functionality.
137+
Let's consider the Items API code. The first section is a controller file in Ruby, [items_controller.rb](docs/quickstart/lrn-sdk-rails/app/controllers/items_controller.rb) from `docs/quickstart/lrn-sdk-rails/app/controllers/` and it is executed server-side. It constructs a set of configuration options for Items API, and securely signs them using the consumer key. We also add a few lines to [application.rb](docs/quickstart/lrn-sdk-rails/config/application.rb) for our Learnosity credentials. The second section is HTML and JavaScript in an [ERB](https://docs.ruby-lang.org/en/2.3.0/ERB.html) template [index.html.erb](docs/quickstart/lrn-sdk-rails/app/views/items/index.html.erb) and is executed client-side, once the page is loaded in the browser. It renders and runs the assessment functionality.
127138

128139
[(Back to top)](#table-of-contents)
129140

130141
### **Server-side code**
131-
We start by including some LearnositySDK helpers in [index_controller.rb](docs/quickstart/lrn-sdk-rails/app/controllers/index_controller.rb) - they'll make it easy to generate and sign the config options, and unique user and session IDs.
142+
We start by including some LearnositySDK helpers in [items_controller.rb](docs/quickstart/lrn-sdk-rails/app/controllers/items_controller.rb) - they'll make it easy to generate and sign the config options, and unique user and session IDs.
132143

133144
``` ruby
134145
require 'learnosity/sdk/request/init' # Learnosity helper.
@@ -228,7 +239,7 @@ This example uses plain HTML in an ERB template, served by Rails.
228239
``` html
229240
<h1>Standalone Assessment Example</h1>
230241
<div id="learnosity_assess"></div>
231-
<script src="//items.learnosity.com?v2021.2.LTS"></script>
242+
<script src="https://items.learnosity.com/?latest-lts"></script>
232243
<script>
233244
var eventOptions = {
234245
readyListener: init
@@ -249,7 +260,7 @@ This example uses plain HTML in an ERB template, served by Rails.
249260
The important parts to be aware of in this HTML are:
250261

251262
* A div with `id="learnosity_assess"`. This is where the Learnosity assessment player will be rendered to deliver the assessment.
252-
* The `<script src="https://items.learnosity.com/?v2021.2.LTS"></script>` tag, which includes Learnosity's Items API on the page and makes the global `LearnosityItems` object available. The version specified as `v2021.2.LTS` will retrieve that specific [Long Term Support (LTS) version](https://help.learnosity.com/hc/en-us/articles/360001268538-Release-Cadence-and-Version-Lifecycle). In production, you should always pin to a specific LTS version to ensure version compatibility.
263+
* The `<script src="https://items.learnosity.com/?latest-lts"></script>` tag, which includes Learnosity's Items API on the page and makes the global `LearnosityItems` object available. The version specified as `latest-lts` will retrieve the latest version supported. To know more about switching to a specific LTS version, visit our [Long Term Support (LTS) page](https://help.learnosity.com/hc/en-us/articles/360001268538-Release-Cadence-and-Version-Lifecycle). In production, you should always pin to a specific LTS version to ensure version compatibility.
253264
* The call to `LearnosityItems.init()`, which initiates Items API to inject the assessment player into the page.
254265
* The variable `<%= raw @init.generate %>` dynamically sends the contents of our init options to JavaScript, so it can be passed to `init()`.
255266

198 KB
Loading
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
require 'learnosity/sdk/request/init' # Learnosity helper.
2+
require 'securerandom'
3+
4+
class AuthorController < ApplicationController
5+
6+
@@security_packet = {
7+
# XXX: This is a Learnosity Demos consumer; replace it with your own consumer key. Set values in application.rb.
8+
'consumer_key' => Rails.configuration.consumer_key,
9+
'domain' => 'localhost',
10+
'user_id' => SecureRandom.uuid
11+
}
12+
13+
# XXX: The consumer secret should be in a properly secured credential store, and *NEVER* checked into version control
14+
@@consumer_secret = Rails.configuration.consumer_secret
15+
16+
@@author_request = {
17+
"mode"=> "item_edit",
18+
"reference"=> "a15ac409-f6d5-42de-a491-a1e4ab03c826",
19+
"user"=> {
20+
"id" => "brianmoser",
21+
"firstname" => "Test",
22+
"lastname" => "Test",
23+
"email" => "[email protected]"
24+
},
25+
"config"=> {
26+
"global"=> {
27+
"disable_onbeforeunload"=> true,
28+
"hide_tags"=>
29+
[
30+
{
31+
"type"=> "internal_category_uuid"
32+
}
33+
]
34+
},
35+
"item_edit"=> {
36+
"item"=> {
37+
"back"=> true,
38+
"columns"=> true,
39+
"answers"=> true,
40+
"scoring"=> true,
41+
"reference"=> {
42+
"edit"=> false,
43+
"show"=> false,
44+
"prefix"=> "LEAR_"
45+
},
46+
"save"=> true,
47+
"status"=> false,
48+
"dynamic_content"=> true,
49+
"shared_passage"=> true
50+
},
51+
"widget"=> {
52+
"delete"=> false,
53+
"edit"=> true
54+
}
55+
},
56+
"item_list"=> {
57+
"item"=> {
58+
"status"=> true,
59+
"url"=> "http://myApp.com/items/:reference/edit"
60+
},
61+
"toolbar"=> {
62+
"add"=> true,
63+
"browse"=> {
64+
"controls"=> [
65+
{
66+
"type"=> "hierarchy",
67+
"hierarchies"=> [
68+
{
69+
"reference"=> "CCSS_Math_Hierarchy",
70+
"label"=> "CCSS Math"
71+
},
72+
{
73+
"reference"=> "CCSS_ELA_Hierarchy",
74+
"label"=> "CCSS ELA"
75+
},
76+
{
77+
"reference"=> "Demo_Items_Hierarchy",
78+
"label"=> "Demo Items"
79+
}
80+
]
81+
},
82+
{
83+
"type"=> "tag",
84+
"tag"=> {
85+
"type"=> "Alignment",
86+
"label"=> "def456"
87+
}
88+
},
89+
{
90+
"type"=> "tag",
91+
"tag"=> {
92+
"type"=> "Course",
93+
"label"=> "commoncore"
94+
}
95+
}
96+
]
97+
}
98+
},
99+
"filter"=> {
100+
"restricted"=> {
101+
"current_user"=> true,
102+
"tags"=> {
103+
"all"=> [
104+
{
105+
"type"=> "Alignment",
106+
"name"=> ["def456", "abc123"]
107+
},
108+
{
109+
"type"=> "Course"
110+
}
111+
],
112+
"either"=> [
113+
{
114+
"type"=> "Grade",
115+
"name"=> "4"
116+
},
117+
{
118+
"type"=> "Grade",
119+
"name"=> "5"
120+
},
121+
{
122+
"type"=> "Subject",
123+
"name"=> ["Math", "Science"]
124+
}
125+
],
126+
"none"=> [
127+
{
128+
"type"=> "Grade",
129+
"name"=> "6"
130+
}
131+
]
132+
}
133+
}
134+
}
135+
},
136+
"dependencies"=> {
137+
"question_editor_api"=> {
138+
"init_options"=> {}
139+
},
140+
"questions_api"=> {
141+
"init_options"=> {}
142+
}
143+
},
144+
"widget_templates"=> {
145+
"back"=> true,
146+
"save"=> true,
147+
"widget_types"=> {
148+
"default"=> "questions",
149+
"show"=> true
150+
}
151+
},
152+
"container"=> {
153+
"height"=> "auto",
154+
"fixed_footer_height"=> 0,
155+
"scroll_into_view_selector"=> "body"
156+
},
157+
"label_bundle"=> {
158+
"backButton"=> "Zurück",
159+
"loadingText"=> "Wird geladen",
160+
"modalClose"=> "Schließen",
161+
"saveButton"=> "Speichern",
162+
"duplicateButton"=> "Duplikat",
163+
"dateTimeLocale"=> "en-us",
164+
"toolTipDateTimeSeparator"=> "um",
165+
"toolTipDateFormat"=> "DD-MM-YYYY",
166+
"toolTipTimeFormat"=> "HH:MM:SS",
167+
}
168+
},
169+
}
170+
171+
def index
172+
@init = Learnosity::Sdk::Request::Init.new(
173+
'author',
174+
@@security_packet,
175+
@@consumer_secret,
176+
@@author_request
177+
)
178+
end
179+
end

docs/quickstart/lrn-sdk-rails/app/controllers/index_controller.rb

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,8 @@
22
require 'securerandom' # Library for generating UUIDs.
33

44
class IndexController < ApplicationController
5-
@@security_packet = {
6-
# XXX: This is a Learnosity Demos consumer; replace it with your own consumer key. Set values in application.rb.
7-
'consumer_key' => Rails.configuration.consumer_key,
8-
'domain' => 'localhost'
9-
}
10-
11-
# XXX: The consumer secret should be in a properly secured credential store, and *NEVER* checked into version control
12-
@@consumer_secret = Rails.configuration.consumer_secret
13-
14-
@@items_request = {
15-
"user_id" => SecureRandom.uuid,
16-
"activity_template_id" => "quickstart_examples_activity_template_001",
17-
"session_id" => SecureRandom.uuid,
18-
"activity_id" => "quickstart_examples_activity_001",
19-
"rendering_type" => "assess",
20-
"type" => "submit_practice",
21-
"name" => "Items API Quickstart",
22-
"state" => "initial"
23-
}
245

256
def index
26-
@init = Learnosity::Sdk::Request::Init.new(
27-
'items',
28-
@@security_packet,
29-
@@consumer_secret,
30-
@@items_request
31-
)
7+
328
end
339
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'learnosity/sdk/request/init' # Learnosity helper.
2+
require 'securerandom' # Library for generating UUIDs.
3+
4+
class ItemsController < ApplicationController
5+
@@security_packet = {
6+
# XXX: This is a Learnosity Demos consumer; replace it with your own consumer key. Set values in application.rb.
7+
'consumer_key' => Rails.configuration.consumer_key,
8+
'domain' => 'localhost'
9+
}
10+
11+
# XXX: The consumer secret should be in a properly secured credential store, and *NEVER* checked into version control
12+
@@consumer_secret = Rails.configuration.consumer_secret
13+
14+
@@items_request = {
15+
"user_id" => SecureRandom.uuid,
16+
"activity_template_id" => "quickstart_examples_activity_template_001",
17+
"session_id" => SecureRandom.uuid,
18+
"activity_id" => "quickstart_examples_activity_001",
19+
"rendering_type" => "assess",
20+
"type" => "submit_practice",
21+
"name" => "Items API Quickstart",
22+
"state" => "initial"
23+
}
24+
25+
def index
26+
@init = Learnosity::Sdk::Request::Init.new(
27+
'items',
28+
@@security_packet,
29+
@@consumer_secret,
30+
@@items_request
31+
)
32+
end
33+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'learnosity/sdk/request/init' # Learnosity helper.
2+
require 'securerandom'
3+
4+
class QuestionsController < ApplicationController
5+
6+
@@security_packet = {
7+
# XXX: This is a Learnosity Demos consumer; replace it with your own consumer key. Set values in application.rb.
8+
'consumer_key' => Rails.configuration.consumer_key,
9+
'domain' => 'localhost',
10+
'user_id' => SecureRandom.uuid
11+
}
12+
13+
# XXX: The consumer secret should be in a properly secured credential store, and *NEVER* checked into version control
14+
@@consumer_secret = Rails.configuration.consumer_secret
15+
16+
@@questions_request = {
17+
"id"=> "f0001",
18+
"name"=> "Intro Activity - French 101",
19+
"questions"=>[
20+
{
21+
"response_id"=> "60005",
22+
"type"=> "association",
23+
"stimulus"=> "Match the cities to the parent nation.",
24+
"stimulus_list"=>["London", "Dublin", "Paris", "Sydney"],
25+
"possible_responses"=>["Australia", "France", "Ireland", "England"
26+
],
27+
"validation"=> {
28+
"valid_responses"=> [
29+
["England"],["Ireland"],["France"],["Australia"]
30+
]
31+
},
32+
"instant_feedback" => true
33+
}
34+
],
35+
}
36+
def index
37+
@init = Learnosity::Sdk::Request::Init.new(
38+
'questions',
39+
@@security_packet,
40+
@@consumer_secret,
41+
@@questions_request
42+
)
43+
end
44+
end

0 commit comments

Comments
 (0)