cli_trivia 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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e217ed45452cf6756ceeb7777cddfed91314a876f89b1fa6c0d59e30ce2cc6f
4
+ data.tar.gz: 80d5781970eb65ce5986251cb2f8e529297711960064c127e60084e5506d7347
5
+ SHA512:
6
+ metadata.gz: 7aadd1748aa813bce6e8b7d0855361434a307e4e4796c7f35d83f33fd44b7109d0ee2ad37e5c340945553b9cdb82f420499ef88b528c73cc3f6e49781339dcf7
7
+ data.tar.gz: ee216e5423bd39981990a601389c90f8834f6d4676957dec51662ec360bc33e5352afaf7232fba5ffa38c528d3250f7b62699fc17e47cdd9f00398bfa5197879
Binary file
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,8 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Datasource local storage ignored files
5
+ /dataSources/
6
+ /dataSources.local.xml
7
+ # Editor-based HTTP Client requests
8
+ /httpRequests/
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build cli_trivia-0.1.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install cli_trivia-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install cli_trivia-0.1.0.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.1.0 and build and push cli_trivia-0.1.0.gem to https://rubygems.org" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
@@ -0,0 +1,33 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="jdk" jdkName="RVM: ruby-2.6.1" jdkType="RUBY_SDK" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.1.4, RVM: ruby-2.6.1) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="coderay (v1.1.3, RVM: ruby-2.6.1) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="equatable (v0.6.1, RVM: ruby-2.6.1) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="httparty (v0.18.1, RVM: ruby-2.6.1) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="method_source (v1.0.0, RVM: ruby-2.6.1) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="mime-types (v3.3.1, RVM: ruby-2.6.1) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="mime-types-data (v3.2020.0512, RVM: ruby-2.6.1) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="multi_xml (v0.6.0, RVM: ruby-2.6.1) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="necromancer (v0.5.1, RVM: ruby-2.6.1) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="pastel (v0.7.4, RVM: ruby-2.6.1) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="pry (v0.13.1, RVM: ruby-2.6.1) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="rake (v12.3.3, RVM: ruby-2.6.1) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="tty-color (v0.5.1, RVM: ruby-2.6.1) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="tty-cursor (v0.7.1, RVM: ruby-2.6.1) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="tty-prompt (v0.21.0, RVM: ruby-2.6.1) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="tty-reader (v0.7.0, RVM: ruby-2.6.1) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="tty-screen (v0.8.0, RVM: ruby-2.6.1) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="wisper (v2.0.1, RVM: ruby-2.6.1) [gem]" level="application" />
32
+ </component>
33
+ </module>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-2.7.0 [global]" project-jdk-type="RUBY_SDK" />
7
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/cli_trivia.iml" filepath="$PROJECT_DIR$/.idea/cli_trivia.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build cli_trivia-0.1.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install cli_trivia-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install cli_trivia-0.1.0.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.1.0 and build and push cli_trivia-0.1.0.gem to TODO: Set to 'http://mygemserver.com'" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at matt@farncombe.co. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cli_trivia.gemspec
4
+ gemspec
5
+
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cli_trivia (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.3)
10
+ equatable (0.6.1)
11
+ httparty (0.18.1)
12
+ mime-types (~> 3.0)
13
+ multi_xml (>= 0.5.2)
14
+ method_source (1.0.0)
15
+ mime-types (3.3.1)
16
+ mime-types-data (~> 3.2015)
17
+ mime-types-data (3.2020.0512)
18
+ multi_xml (0.6.0)
19
+ necromancer (0.5.1)
20
+ pastel (0.7.4)
21
+ equatable (~> 0.6)
22
+ tty-color (~> 0.5)
23
+ pry (0.13.1)
24
+ coderay (~> 1.1)
25
+ method_source (~> 1.0)
26
+ rake (12.3.3)
27
+ tty-color (0.5.1)
28
+ tty-cursor (0.7.1)
29
+ tty-prompt (0.21.0)
30
+ necromancer (~> 0.5.0)
31
+ pastel (~> 0.7.0)
32
+ tty-reader (~> 0.7.0)
33
+ tty-reader (0.7.0)
34
+ tty-cursor (~> 0.7)
35
+ tty-screen (~> 0.7)
36
+ wisper (~> 2.0.0)
37
+ tty-screen (0.8.0)
38
+ wisper (2.0.1)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler (~> 2.1.4)
45
+ cli_trivia!
46
+ httparty (~> 0.18.1)
47
+ pry
48
+ rake (~> 12.0)
49
+ tty-prompt (~> 0.21.0)
50
+
51
+ BUNDLED WITH
52
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 C0mbed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ # CliTrivia
2
+
3
+ The best Command Line Trivia app on the internet! A quick one player game of Trivia pitting each user against the best of the Open Trivia Database.
4
+
5
+ CLI Trivia makes use of 'HTTParty' to retrieve category and question JSON data from the [Open Trivia Database API](https://opentdb.com/api_config.php). Upon initialize, CLI Trivia generates a token to prevent duplicate questions from appearing, and creates a Category class Object for each trivia category. The user is then offered the choice between 10 questions: either randomly selected from a specific category. CLI Trivia then displays the questions text, answer choices, and requests user input as to which answer they wish to select. User score is calculated and displayed.
6
+
7
+ IF the user selects 'random', CLI Trivia randomly generates 10 questions and creates Question class Objects. If the user selects 'category' the user is presented with a list of possible trivia categories. Upon selected, CLI Trivia makes an API call to retrieve 10 questions based on the user selected Category ID.
8
+
9
+ Once all 10 questions are answered, the User is presented with a summary screen and the chance to exit or begin again.
10
+
11
+ ## Installation
12
+ Clone this repo to the directory of your choice!
13
+
14
+ ## Run this Gem
15
+ In your terminal, run `bin/cli_trivia` from within the `cli_trivia` directory to begin!
16
+
17
+ ## Development
18
+
19
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
20
+
21
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/<github username>/cli_trivia. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/<github username>/cli_trivia/blob/master/CODE_OF_CONDUCT.md).
26
+
27
+
28
+ ## License
29
+
30
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
31
+
32
+ ## Code of Conduct
33
+
34
+ Everyone interacting in the CliTrivia project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/<github username>/cli_trivia/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require_relative '../config/environment'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ require_relative 'lib/cli_trivia/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "cli_trivia"
5
+ spec.version = CliTrivia::VERSION
6
+ spec.authors = ["Matt Farncombe"]
7
+ spec.email = ["matt@farncombe.co"]
8
+ spec.summary = 'Command Line Trivia Game'
9
+ spec.description = 'Select multiple choice or boolean questions from a category, or get them randomly.'
10
+ spec.homepage = 'https://rubygems.org/gems/cli_trivia'
11
+ spec.license = "MIT"
12
+ #spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/C0mbed/cli_trivia"
18
+ spec.metadata["changelog_uri"] = "https://github.com/C0mbed/cli_trivia"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 2.1.4"
30
+ spec.add_development_dependency 'httparty', '~> 0.18.1'
31
+ spec.add_development_dependency "pry", ">= 0"
32
+ spec.add_development_dependency "rake", "~> 12.0"
33
+ spec.add_development_dependency 'tty-prompt', '~> 0.21.0'
34
+ end
@@ -0,0 +1,13 @@
1
+ require 'httparty'
2
+ require 'tty-prompt'
3
+
4
+ require_relative '../lib/concerns/filter'
5
+ require_relative '../lib/concerns/findable'
6
+ require_relative '../lib/cli_trivia/apimanager'
7
+ require_relative '../lib/cli_trivia/question'
8
+ require_relative '../lib/cli_trivia/cli'
9
+ require_relative '../lib/cli_trivia/category'
10
+ require_relative '../lib/cli_trivia/version'
11
+
12
+
13
+
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/cli_trivia'
4
+
5
+ Cli.new.call
@@ -0,0 +1,6 @@
1
+ module CliTrivia
2
+ class Error < StandardError; end
3
+ # Your code goes here...
4
+ end
5
+
6
+ require_relative '../config/environment'
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Handles the external API call to the "Open Trivia Database" to get trivia question hashes
4
+ class ApiManager
5
+ # base api url
6
+ BASE_URL = 'https://opentdb.com/api.php?amount=10&token='
7
+
8
+ def initialize
9
+ @categories = nil
10
+ @token = nil
11
+ end
12
+
13
+ def self.generate_token
14
+ @token = HTTParty.get('https://opentdb.com/api_token.php?command=request10&token=')
15
+ @token
16
+ end
17
+
18
+ def self.generate_categories
19
+ @categories = HTTParty.get('https://opentdb.com/api_category.php')
20
+ @categories['trivia_categories'].each do |category|
21
+ Category.new(category['name'], category['id'])
22
+ end
23
+ end
24
+
25
+ def self.generate_random_questions
26
+ response = HTTParty.get("#{BASE_URL}#{@token}")
27
+ create_questions(response)
28
+ end
29
+
30
+ def self.generate_questions_by_id(category_id)
31
+ category_url = "&category=#{category_id}"
32
+ response = HTTParty.get("#{BASE_URL}#{@token}#{category_url}")
33
+ create_questions(response)
34
+ end
35
+
36
+ def self.create_questions(response)
37
+ response['results'].each do |question|
38
+ Question.new(question)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ # Category Objects are created upon initializing the app using bin/cli_trivia. This allows the user to choose between
2
+ # random and category. Each Category has MANY questions, but there is only one of every Category.
3
+ class Category
4
+ attr_accessor :name, :questions, :id
5
+ extend CliTrivia::Findable # allows for the searching of @@all by name and creating the object if one isn't found
6
+
7
+ # Categories are stored in @@all and are accessed to print the list of user selectable categories.
8
+ @@all = []
9
+
10
+ def initialize(name, id)
11
+ @id = id
12
+ @name = name
13
+ @questions = []
14
+ @@all << self
15
+ end
16
+
17
+ def self.all
18
+ @@all
19
+ end
20
+
21
+ # A modification of the self.all method to return all Categories alphabetically. Used when categories are printed
22
+ # for user choice.
23
+ def self.all_by_name
24
+ sorted = @@all.sort! {|a, b| a.name <=> b.name}
25
+ sorted_names = []
26
+ sorted.each do |category|
27
+ sorted_names << category.name
28
+ end
29
+ sorted_names
30
+ end
31
+
32
+ # This stores questions that belong to this Category. @questions is an instance variable available to this Category
33
+ # Object only
34
+ def add_question(question)
35
+ @questions << question unless question.nil? || @questions.include?(question)
36
+ end
37
+
38
+ def self.clear_all
39
+ @@all.clear
40
+ end
41
+ end
@@ -0,0 +1,142 @@
1
+ # Cli class is a controller for the app and handles the display of all information and interactions with the user.
2
+ class Cli
3
+
4
+ # Generates the categories, but not the questions on initialization and resets the @right and @wrong counters. Finally
5
+ # invokes call to begin the app.
6
+ def initialize
7
+ ApiManager.generate_categories
8
+ @right = 0
9
+ @wrong = 0
10
+ @prompt = TTY::Prompt.new
11
+ end
12
+
13
+ # Begins the trivia game by printing the welcome, title, and asks user to make the first choice between random and
14
+ # category. Uses case to determine how to proceed from call.
15
+ def call
16
+ system('clear')
17
+ puts('')
18
+ puts('CLI Trivia')
19
+ puts('')
20
+ user_input = @prompt.select('Choose either random, or select a category?', %w[Random Category Exit])
21
+ case user_input
22
+ when 'Random'
23
+ generate_random
24
+ when 'Category'
25
+ display_categories
26
+ when 'Exit'
27
+ system('clear')
28
+ system(exit)
29
+ end
30
+ end
31
+
32
+ # Run when user selects 'Random' from the main menu, calls on ApiManager class to query the api and create
33
+ # Question Objects. Finally, new question objects are displayed using display_question.
34
+ def generate_random
35
+ ApiManager.generate_random_questions
36
+ Question.all.each do |question|
37
+ display_question(question)
38
+ end
39
+ display_result
40
+ end
41
+
42
+ def display_categories
43
+ system('clear')
44
+ categories = Category.all_by_name
45
+ puts('')
46
+ puts('CLI Trivia')
47
+ puts('')
48
+ puts('')
49
+ user_input = @prompt.select('Please Select a Category (scroll for more)', categories, per_page: 15)
50
+ display_questions_by_category(user_input)
51
+ end
52
+
53
+ def display_questions_by_category(user_input)
54
+ category = Category.find_by_name(user_input)
55
+ question_id = category.id
56
+ ApiManager.generate_questions_by_id(question_id)
57
+ category.questions.each do |question|
58
+ display_question(question)
59
+ end
60
+ display_result
61
+ end
62
+
63
+ def generate_answers(correct, incorrect, type)
64
+ if type == 'boolean'
65
+ answers = {
66
+ correct: Question.format_string(correct),
67
+ incorrect: incorrect,
68
+ choices: %w[True False]
69
+ }
70
+ answers
71
+ elsif type == 'multiple'
72
+ random_answers = []
73
+ incorrect.each do |wrong|
74
+ formatted_answer = Question.format_string(wrong)
75
+ random_answers << formatted_answer
76
+ end
77
+ answers = {
78
+ correct: Question.format_string(correct),
79
+ choices: [correct] + random_answers
80
+ }
81
+ answers
82
+ end
83
+ end
84
+
85
+ def display_answer_choices(question, answer_choices, random_answers)
86
+ if answer_choices[:choices].length > 2
87
+ @prompt.select("Q#{@right+@wrong+1}/10> #{question.question}", random_answers)
88
+ else
89
+ @prompt.select("Q#{@right+@wrong+1}/10> #{question.question}", answer_choices[:choices])
90
+ end
91
+ end
92
+
93
+ def question_type(answer_choices)
94
+ if answer_choices[:choices].length > 2
95
+ random_answers = answer_choices[:choices].shuffle
96
+ else
97
+ random_answers = answer_choices[:choices]
98
+ end
99
+ random_answers
100
+ end
101
+
102
+ def correct?(user_input, correct_answer)
103
+ user_input == correct_answer
104
+ end
105
+
106
+ def display_question(question)
107
+ system('clear')
108
+ answer_choices = generate_answers(question.correct_answer, question.incorrect_answers, question.type)
109
+ random_answers = answer_choices[:choices]
110
+ puts('')
111
+ puts("CLI trivia :: #{@right} correct")
112
+ puts('')
113
+ user_input = display_answer_choices(question, answer_choices, random_answers)
114
+ puts('')
115
+ if correct?(user_input, answer_choices[:correct])
116
+ @right += 1
117
+ puts "That's correct. Press Any Enter to Continue."
118
+ else
119
+ @wrong += 1
120
+ puts "Wrong. #{answer_choices[:correct]} was right. Enter to continue."
121
+ end
122
+ gets
123
+ end
124
+
125
+ def display_result
126
+ system('clear')
127
+ puts('')
128
+ puts("CLI trivia :: Final score: #{@right}/10")
129
+ puts('')
130
+ user_input = @prompt.select('Exit or restart?', %w[Restart Exit])
131
+ case user_input
132
+ when 'Restart'
133
+ Question.clear_all
134
+ @right = 0
135
+ @wrong = 0
136
+ call
137
+ when 'Exit'
138
+ system('clear')
139
+ system(exit)
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,35 @@
1
+ # This is a question object. Question objects are created based on the user choice (either random or category).
2
+ # A JSON hash is returned from the openTDB GET request and stored in each question object.
3
+ class Question
4
+ attr_accessor :category, :type, :difficulty, :question, :correct_answer, :incorrect_answers
5
+ extend CliTrivia::Findable # allows for the searching of @@all by name and creating the object if one isn't found
6
+ extend CliTrivia::Filter # filter to remove non-ASCII characters from strings.
7
+
8
+ @@all = []
9
+
10
+ # Because all metadata regarding a question is available on creation of each Question Object, it is populated upon
11
+ # initialization and not after.
12
+ def initialize(question_data)
13
+ @category = add_category(question_data['category'])
14
+ @type = question_data['type']
15
+ @difficulty = question_data['difficulty']
16
+ @question = Question.format_string(question_data['question'])
17
+ @correct_answer = question_data['correct_answer']
18
+ @incorrect_answers = question_data['incorrect_answers']
19
+ @@all << self
20
+ end
21
+
22
+ # Allows for the adding of a Category Object to self. A question BELONGS to a category.
23
+ def add_category(name)
24
+ category = Category.find_or_create_by_name(name)
25
+ category.add_question(self)
26
+ end
27
+
28
+ def self.all
29
+ @@all
30
+ end
31
+
32
+ def self.clear_all
33
+ @@all.clear
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module CliTrivia
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ module CliTrivia::Filter
2
+
3
+ # A method available to all classes that passes in a string and replaces unreadable characters with escaped
4
+ # punctuation. Used by question text and answers.
5
+ def format_string(fsg)
6
+ fsg = fsg.gsub(/&quot;/, "'")
7
+ fsg = fsg.gsub(/&amp;/, '&')
8
+ fsg = fsg.gsub(/&ograve;/, 'o')
9
+ fsg = fsg.gsub(/&ouml;/, 'o')
10
+ fsg = fsg.gsub(/&oacute;/, 'o')
11
+ fsg = fsg.gsub(/&atilde;/, 'a')
12
+ fsg = fsg.gsub(/&uuml;/, 'u')
13
+ fsg = fsg.gsub(/&Ouml;/, 'O')
14
+ fsg = fsg.gsub(/&prime;/, "'")
15
+ fsg = fsg.gsub(/&Prime;/, '"')
16
+ fsg = fsg.gsub(/&iacute;/, 'i')
17
+ fsg = fsg.gsub(/&aacute;/, 'a')
18
+ fsg = fsg.gsub(/&eacute;/, 'e')
19
+ fsg = fsg.gsub(/&#039;/, "'")
20
+ fsg = fsg.gsub(/&ldquo;/, '"')
21
+ fsg = fsg.gsub(/&rdquo;/, '"')
22
+ fsg = fsg.gsub(/&rsquo;/, "'")
23
+ fsg = fsg.gsub(/&lsquo;/, "'")
24
+ fsg = fsg.gsub(/&ntilde;/, 'n')
25
+ fsg = fsg.gsub(/&lrm;/, '')
26
+ fsg
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # Allows each class to search its @@all variable, and if necessary create a new version of the class by name.
2
+ module CliTrivia::Findable
3
+
4
+ def find_by_name(name)
5
+ all.detect { |a| a.name == name }
6
+ end
7
+
8
+ def find_or_create_by_name(name)
9
+ found = find_by_name(name)
10
+ if found.nil?
11
+ create(name)
12
+ else
13
+ found
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cli_trivia
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Farncombe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-20 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: 2.1.4
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.18.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.18.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: tty-prompt
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.21.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.21.0
83
+ description: Select multiple choice or boolean questions from a category, or get them
84
+ randomly.
85
+ email:
86
+ - matt@farncombe.co
87
+ executables:
88
+ - cli_trivia
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".DS_Store"
93
+ - ".gitignore"
94
+ - ".idea/.gitignore"
95
+ - ".idea/.rakeTasks"
96
+ - ".idea/cli_trivia.iml"
97
+ - ".idea/misc.xml"
98
+ - ".idea/modules.xml"
99
+ - ".idea/vcs.xml"
100
+ - ".rakeTasks"
101
+ - CODE_OF_CONDUCT.md
102
+ - Gemfile
103
+ - Gemfile.lock
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - bin/console
108
+ - bin/setup
109
+ - cli_trivia.gemspec
110
+ - config/environment.rb
111
+ - exe/cli_trivia
112
+ - lib/cli_trivia.rb
113
+ - lib/cli_trivia/apimanager.rb
114
+ - lib/cli_trivia/category.rb
115
+ - lib/cli_trivia/cli.rb
116
+ - lib/cli_trivia/question.rb
117
+ - lib/cli_trivia/version.rb
118
+ - lib/concerns/filter.rb
119
+ - lib/concerns/findable.rb
120
+ homepage: https://rubygems.org/gems/cli_trivia
121
+ licenses:
122
+ - MIT
123
+ metadata:
124
+ allowed_push_host: https://rubygems.org
125
+ homepage_uri: https://rubygems.org/gems/cli_trivia
126
+ source_code_uri: https://github.com/C0mbed/cli_trivia
127
+ changelog_uri: https://github.com/C0mbed/cli_trivia
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubygems_version: 3.0.8
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Command Line Trivia Game
147
+ test_files: []