trivia_factory 0.1.1

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.
@@ -0,0 +1,22 @@
1
+ module TriviaFactory
2
+ class MathQuestion < Question
3
+
4
+ QUESTION_SUB_TYPES = [:addition, :subtraction, :multiplication, :division].freeze
5
+
6
+ class << self
7
+ def generate(max = 100)
8
+ question = TriviaFactory::Question.new
9
+ rng = Random.new
10
+ first = rng.rand(max)
11
+ second = rng.rand(max)
12
+ question.label = "#{first} + #{second} = _____?"
13
+ question.question_type = :fill_in_the_blank
14
+ question.answer_type = :integer
15
+ question.answer = first + second
16
+ question.choices = []
17
+ question
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,40 @@
1
+ module TriviaFactory
2
+ class SportsQuestion < Question
3
+
4
+ QUESTION_SUB_TYPES = [:champions, :mvps].freeze
5
+
6
+ class << self
7
+ def generate
8
+ # File: vocabulary.csv
9
+ # 0: year
10
+ # 1: championship ("wold series")
11
+ # 2: counter/number ("IV" for super bowl)
12
+ # 3: winner
13
+ # 4: loser
14
+ # Format: "Who defeated the [LOSER] to win (the) [YEAR] [CHAMPIONSHIP]?"
15
+ question = TriviaFactory::Question.new
16
+ data = fetch_csv('sports_champions')
17
+ answer_row = data.sample
18
+ question = TriviaFactory::VocabularyQuestion.new
19
+ if answer_row[2].nil?
20
+ question.label = "In #{answer_row[0]} who defeated the #{answer_row[4]} to win the #{answer_row[1]}?"
21
+ else
22
+ question.label = "In #{answer_row[0]} who defeated the #{answer_row[4]} to win #{answer_row[1]} #{answer_row[2]}?"
23
+ end
24
+ question.choices = [answer_row[3]]
25
+ question.question_type = :multiple_choice
26
+ question.answer_type = :choice_index
27
+ context_data = data.select {|row| row[1] == answer_row[1] }
28
+ loop do
29
+ row = context_data.sample
30
+ question.choices << row[3] if question.choices.index(row[3]).nil?
31
+ break if question.choices.size == 4
32
+ end
33
+ question.choices.shuffle!
34
+ question.answer = question.choices.index(answer_row[3])
35
+ question
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ module TriviaFactory
2
+ class UsStateCapitalsQuestion < Question
3
+
4
+ class << self
5
+ def generate
6
+ # File: us_state_capitals.csv
7
+ # Column 1: state
8
+ # Column 2: city
9
+ # Format: "Which is the most appropriate definition of the word '[WORD]'?"
10
+ data = fetch_csv('us_state_capitals')
11
+ answer_row = data.sample
12
+ question = TriviaFactory::Question.new
13
+ question.label = "#{answer_row[1]} is the capital of what US state?"
14
+ question.choices = [answer_row[0]]
15
+ question.question_type = :multiple_choice
16
+ question.answer_type = :choice_index
17
+ 3.times do
18
+ question.choices << data.sample[0]
19
+ end
20
+ question.choices.shuffle!
21
+ question.answer = question.choices.index(answer_row[0])
22
+ question
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module TriviaFactory
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ module TriviaFactory
2
+ class VocabularyQuestion < Question
3
+
4
+ class << self
5
+ def generate
6
+ # File: vocabulary.csv
7
+ # Column 1: word
8
+ # Column 2: definition
9
+ # Format: "Which is the most appropriate definition of the word '[WORD]'?"
10
+ data = fetch_csv('vocabulary')
11
+ answer_row = data.sample
12
+ question = TriviaFactory::VocabularyQuestion.new
13
+ question.label = "Choose the correct definition of the word '#{answer_row[0]}'"
14
+ question.choices = [answer_row[1]]
15
+ question.question_type = :multiple_choice
16
+ question.answer_type = :choice_index
17
+ 3.times do
18
+ question.choices << data.sample[1]
19
+ end
20
+ question.choices.shuffle!
21
+ question.answer = question.choices.index(answer_row[1])
22
+ question
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,76 @@
1
+ require "trivia_factory/version"
2
+ require "csv"
3
+
4
+ module TriviaFactory
5
+ class Question
6
+
7
+ QUESTION_TYPES = [:true_false, :multiple_choice, :fill_in_the_blank].freeze
8
+ ANSWER_TYPES = [:choice_index, :boolean, :string, :integer].freeze
9
+
10
+ attr_accessor :label
11
+ attr_accessor :question_type
12
+ attr_accessor :choices
13
+ attr_accessor :answer_type
14
+ attr_accessor :answer
15
+
16
+ def initialize
17
+ @label = "Who won the MLB World Series in 2016?"
18
+ @question_type = :multiple_choice
19
+ @choices = ["San Francisco Giants", "Chicago Cubs", "Cleveland Indians", "Golden State Warriors"]
20
+ @answer_type = :choice_index
21
+ @answer = 1
22
+ end
23
+
24
+ def to_h
25
+ {
26
+ label: @label,
27
+ question_type: @question_type,
28
+ choices: @choices,
29
+ answer: @answer,
30
+ answer_type: @answer_type
31
+ }
32
+ end
33
+
34
+ class << self
35
+
36
+ def question_types
37
+ TriviaFactory.constants.select { |k| TriviaFactory.const_get(k).instance_of?(Class) && k != :Question }
38
+ end
39
+
40
+ def random
41
+ klass = question_types.sample
42
+ TriviaFactory.const_get(klass).generate
43
+ end
44
+
45
+ def generate
46
+ question = TriviaFactory::Question.new
47
+ end
48
+
49
+ def math(max = 100)
50
+ TriviaFactory::MathQuestion.generate(max)
51
+ end
52
+
53
+ def us_state_capitals
54
+ TriviaFactory::UsStateCapitalsQuestion.generate
55
+ end
56
+
57
+ def sports
58
+ TriviaFactory::SportsQuestion.generate
59
+ end
60
+
61
+ def vocabulary
62
+ TriviaFactory::VocabularyQuestion.generate
63
+ end
64
+
65
+ def fetch_csv(name)
66
+ file = File.join(File.dirname(__FILE__), "data", "#{name}.csv")
67
+ data = CSV.read(file)
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ require "trivia_factory/vocabulary_question"
74
+ require "trivia_factory/math_question"
75
+ require "trivia_factory/sports_question"
76
+ require "trivia_factory/us_state_capitals_question"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "trivia_factory/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trivia_factory"
8
+ spec.version = TriviaFactory::VERSION
9
+ spec.authors = ["Callmeed"]
10
+ spec.email = ["erik.dungan@gmail.com"]
11
+
12
+ spec.summary = %q{A trivia question generator for use in specs or sample applications.}
13
+ spec.description = %q{If you have an app that does testing or trivia, use this to create sample/dummy/fake questions. Its like Faker for questions.}
14
+ spec.homepage = "https://github.com/callmeed/trivia_factory"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trivia_factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Callmeed
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: If you have an app that does testing or trivia, use this to create sample/dummy/fake
56
+ questions. Its like Faker for questions.
57
+ email:
58
+ - erik.dungan@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/data/country_capitals.csv
75
+ - lib/data/sports_champions.csv
76
+ - lib/data/us_state_capitals.csv
77
+ - lib/data/vocabulary.csv
78
+ - lib/trivia_factory.rb
79
+ - lib/trivia_factory/math_question.rb
80
+ - lib/trivia_factory/sports_question.rb
81
+ - lib/trivia_factory/us_state_capitals_question.rb
82
+ - lib/trivia_factory/version.rb
83
+ - lib/trivia_factory/vocabulary_question.rb
84
+ - trivia_factory.gemspec
85
+ homepage: https://github.com/callmeed/trivia_factory
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.5.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: A trivia question generator for use in specs or sample applications.
109
+ test_files: []