trivia_factory 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +86 -26
- data/lib/data/{country_capitals.csv → capital_cities.csv} +0 -0
- data/lib/trivia_factory/capital_cities_question.rb +22 -0
- data/lib/trivia_factory/version.rb +1 -1
- data/lib/trivia_factory.rb +7 -2
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4524191157360deda5cebba57fdf401f4ff03140
|
|
4
|
+
data.tar.gz: 588645eae9360aa04178b207850b9aaa16dcd1e8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 354348a7f0a91e1804f94f5efa0b7243e5c42f4375e61885cf6a31f2428c827f7246f203585fe8b16c57a42b5eb000cc3a399b7044446fc1d97fa799b08ce1bb
|
|
7
|
+
data.tar.gz: b56f43c564a52618d6de63c732f875af86f9c45f9d8a35eb764e489ccc1fb7f0023b3db7dac5bbde1afe0eb70edf4e4e08772614ca8bd9459e1d35db48e380f3
|
data/README.md
CHANGED
|
@@ -1,43 +1,103 @@
|
|
|
1
1
|
# Trivia Factory
|
|
2
2
|
|
|
3
|
-
Ruby gem for creating sample trivia/test questions. Intended for use in specs
|
|
3
|
+
Ruby gem for creating tons of sample trivia/test questions. Intended for use in specs or apps that need questions.
|
|
4
|
+
Think of it as [Faker](https://github.com/stympy/faker) for trivia questions.
|
|
5
|
+
|
|
6
|
+
Originally part of a sports trivia app I had that was somewhat popular (Hat Trick), this gem uses tables of data (in CSV form) to
|
|
7
|
+
create random questions.
|
|
4
8
|
|
|
5
9
|
## Installation
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
`gem install trivia_factory`
|
|
12
|
+
|
|
13
|
+
If you're using Bundler and/or Rails, add the following to your Gemfile:
|
|
14
|
+
|
|
15
|
+
`gem 'trivia_factory'`
|
|
8
16
|
|
|
9
17
|
## Usage
|
|
10
18
|
|
|
11
|
-
|
|
19
|
+
### Generate a random question (from all available types)
|
|
12
20
|
|
|
13
|
-
|
|
21
|
+
`question = TriviaFactory::Question.random`
|
|
14
22
|
|
|
15
|
-
|
|
16
|
-
>
|
|
17
|
-
> question.to_h
|
|
18
|
-
> {
|
|
19
|
-
> label: "Who won the MLB World Series in 2016?",
|
|
20
|
-
> question_type: :multiple_choice,
|
|
21
|
-
> choices: ["San Francisco Giants", "Chicago Cubs", "Cleveland Indians", "Golden State Warriors"],
|
|
22
|
-
> answer_type: :choice_index,
|
|
23
|
-
> answer: 1
|
|
24
|
-
> }
|
|
23
|
+
This will return a `TriviaFactory::Question` object. Use the `to_h` helper to get it as a hash.
|
|
25
24
|
|
|
26
25
|
```
|
|
26
|
+
question.to_h
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
{
|
|
29
|
+
:label => "Sacramento is the capital of what US state?",
|
|
30
|
+
:question_type => :multiple_choice,
|
|
31
|
+
:choices => [
|
|
32
|
+
"Oklahoma",
|
|
33
|
+
"Delaware",
|
|
34
|
+
"California",
|
|
35
|
+
"Oregon"
|
|
36
|
+
],
|
|
37
|
+
:answer => 2,
|
|
38
|
+
:answer_type => :choice_index
|
|
39
|
+
}
|
|
30
40
|
```
|
|
31
41
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
### Question object structure
|
|
43
|
+
|
|
44
|
+
Question objects have the following attributes (which are also available as keys in the hash):
|
|
45
|
+
|
|
46
|
+
**label**: this is the actual question being asked
|
|
47
|
+
|
|
48
|
+
**question_type**: a symbol denoting the type of question. Will be one of the following:
|
|
49
|
+
|
|
50
|
+
* :true_false
|
|
51
|
+
* :multiple_choice
|
|
52
|
+
* :fill_in_the_blank
|
|
53
|
+
|
|
54
|
+
**choices**: an array of choices for multiple choice questions. Will be an empty array (not `nil`) for other question types.
|
|
55
|
+
|
|
56
|
+
**answer**: the correct answer for the question. Its type/class may vary (see below).
|
|
57
|
+
|
|
58
|
+
**answer_type**: specifies the type of answer. Will be one of the following: `[:choice_index, :boolean, :string, :integer]`. *Note*: when `:choice_index`, the correct answer corresponds to the correct choice in the `choices` array.
|
|
59
|
+
|
|
60
|
+
### Question categories
|
|
61
|
+
|
|
62
|
+
Currently the following question categories exist as subclasses of `TriviaFactory::Question`:
|
|
63
|
+
|
|
64
|
+
**TriviaFactory::MathQuestion**: generates a basic integer addition problem (fill in the blank).
|
|
65
|
+
|
|
66
|
+
**TriviaFactory::SportsQuestion**: generates a sports trivia question (multiple choice).
|
|
67
|
+
|
|
68
|
+
**TriviaFactory::UsStateCapitalsQuestion**: generates a question about US state capitals.
|
|
69
|
+
|
|
70
|
+
**TriviaFactory::VocabularyQuestion**: generates a multiple choice vocabulary *word* <-> *definition* question. From a list of 1,000 vocabulary study words.
|
|
71
|
+
|
|
72
|
+
To create a question from any of these types, simply call the `generate()` method on any of the classes.
|
|
73
|
+
|
|
74
|
+
### More examples
|
|
42
75
|
|
|
43
76
|
```
|
|
77
|
+
# Math question can accept a max param that can limit the size of operands
|
|
78
|
+
q = TriviaFactory::MathQuestion.generate(20)
|
|
79
|
+
q.to_h
|
|
80
|
+
=> {
|
|
81
|
+
:label => "5 + 5 = _____?",
|
|
82
|
+
:question_type => :fill_in_the_blank,
|
|
83
|
+
:choices => [],
|
|
84
|
+
:answer => 10,
|
|
85
|
+
:answer_type => :integer
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
q = TriviaFactory::SportsQuestion.generate
|
|
91
|
+
q.to_h
|
|
92
|
+
=> {
|
|
93
|
+
:label => "In 1987 who defeated the St. Louis Cardinals to win the World Series?",
|
|
94
|
+
:question_type => :multiple_choice,
|
|
95
|
+
:choices => ["Detroit Tigers", "Minnesota Twins", "Anaheim Angels", "New York Yankees"],
|
|
96
|
+
:answer => 1,
|
|
97
|
+
:answer_type => :choice_index
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Contributing
|
|
102
|
+
|
|
103
|
+
Contributions welcome. Add a CSV to the data/ folder, create a subclass, write a test, and submit a PR.
|
|
File without changes
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module TriviaFactory
|
|
2
|
+
class CapitalCitiesQuestion < Question
|
|
3
|
+
|
|
4
|
+
class << self
|
|
5
|
+
def generate
|
|
6
|
+
# File: capital_cities.csv
|
|
7
|
+
# Column 1: city
|
|
8
|
+
# Column 2: country
|
|
9
|
+
data = fetch_csv('capital_cities')
|
|
10
|
+
answer_row = data.sample
|
|
11
|
+
question = TriviaFactory::Question.new
|
|
12
|
+
question.label = "#{answer_row[0]} is the capital city of what country?"
|
|
13
|
+
question.question_type = :fill_in_the_blank
|
|
14
|
+
question.answer_type = :string
|
|
15
|
+
question.answer = answer_row[1]
|
|
16
|
+
question.choices = []
|
|
17
|
+
question
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/trivia_factory.rb
CHANGED
|
@@ -43,7 +43,7 @@ module TriviaFactory
|
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
def generate
|
|
46
|
-
|
|
46
|
+
TriviaFactory::Question.new
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
def math(max = 100)
|
|
@@ -54,6 +54,10 @@ module TriviaFactory
|
|
|
54
54
|
TriviaFactory::UsStateCapitalsQuestion.generate
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
+
def capital_cities
|
|
58
|
+
TriviaFactory::CapitalCitiesQuestion.generate
|
|
59
|
+
end
|
|
60
|
+
|
|
57
61
|
def sports
|
|
58
62
|
TriviaFactory::SportsQuestion.generate
|
|
59
63
|
end
|
|
@@ -64,7 +68,7 @@ module TriviaFactory
|
|
|
64
68
|
|
|
65
69
|
def fetch_csv(name)
|
|
66
70
|
file = File.join(File.dirname(__FILE__), "data", "#{name}.csv")
|
|
67
|
-
|
|
71
|
+
CSV.read(file)
|
|
68
72
|
end
|
|
69
73
|
end
|
|
70
74
|
end
|
|
@@ -74,3 +78,4 @@ require "trivia_factory/vocabulary_question"
|
|
|
74
78
|
require "trivia_factory/math_question"
|
|
75
79
|
require "trivia_factory/sports_question"
|
|
76
80
|
require "trivia_factory/us_state_capitals_question"
|
|
81
|
+
require "trivia_factory/capital_cities_question"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: trivia_factory
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Callmeed
|
|
@@ -71,11 +71,12 @@ files:
|
|
|
71
71
|
- Rakefile
|
|
72
72
|
- bin/console
|
|
73
73
|
- bin/setup
|
|
74
|
-
- lib/data/
|
|
74
|
+
- lib/data/capital_cities.csv
|
|
75
75
|
- lib/data/sports_champions.csv
|
|
76
76
|
- lib/data/us_state_capitals.csv
|
|
77
77
|
- lib/data/vocabulary.csv
|
|
78
78
|
- lib/trivia_factory.rb
|
|
79
|
+
- lib/trivia_factory/capital_cities_question.rb
|
|
79
80
|
- lib/trivia_factory/math_question.rb
|
|
80
81
|
- lib/trivia_factory/sports_question.rb
|
|
81
82
|
- lib/trivia_factory/us_state_capitals_question.rb
|