From 30553caf6208f22513c3c9fceb17cecfeed895e7 Mon Sep 17 00:00:00 2001 From: nilsding Date: Tue, 9 Dec 2014 00:59:33 +0100 Subject: [PATCH] initial commit --- .gitignore | 23 +++++++++++++ Gemfile | 3 ++ LICENSE.txt | 23 +++++++++++++ README.md | 39 ++++++++++++++++++++++ Rakefile | 2 ++ lib/questiongenerator.rb | 38 ++++++++++++++++++++++ lib/questions/en.yml | 68 +++++++++++++++++++++++++++++++++++++++ questiongenerator.gemspec | 23 +++++++++++++ 8 files changed, 219 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/questiongenerator.rb create mode 100644 lib/questions/en.yml create mode 100644 questiongenerator.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ffb20e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp +*.bundle +*.so +*.o +*.a +mkmf.log +.idea/ diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fa75df1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..32b5fd4 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,23 @@ +questiongenerator +Copyright (c) 2014 nilsding + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1d706f6 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# questiongenerator + +A simple question generator, used by justask. + +## Installation + +Add this line to your application's Gemfile: + + gem 'questiongenerator' + +If you're feeling _edgy_, you can add this line instead: + + gem 'questiongenerator', git: 'https://github.com/justask/questiongenerator.git' + +## Usage + +``` ruby +require 'questiongenerator' + +# Configure it +QuestionGenerator.question_base_path = '/home/nilsding/questions' +QuestionGenerator.default_locale = :en + +# Get some questions +puts QuestionGenerator.generate +# => "What is the best thing about the internet?" + +# You can also specify the locale, if you want to +puts QuestionGenerator.generate :de +# => "Was war das letzte, das du gegessen hast?" +``` + +## Contributing + +1. Fork it ( https://github.com/justask/questiongenerator/fork ) +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..809eb56 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require "bundler/gem_tasks" + diff --git a/lib/questiongenerator.rb b/lib/questiongenerator.rb new file mode 100644 index 0000000..74290cc --- /dev/null +++ b/lib/questiongenerator.rb @@ -0,0 +1,38 @@ +require 'yaml' + +module QuestionGenerator + VERSION = "0.0.1" + + class << self + attr_accessor :question_base_path + attr_accessor :default_locale + end + + def self.setup + yield self + end + + def self.generate(locale = @default_locale) + questions = YAML.load_file(File.expand_path("#{locale.to_s}.yml", @question_base_path)) + "#{get_question(questions)}?" + end + + private + + def self.get_question questions + question = "" + if questions.is_a? Hash + key = questions.keys.sample + value = questions[key] + question = "#{key} #{get_question(value)}" + elsif questions.is_a? Array + question = get_question questions.sample + elsif questions.is_a? String + question = questions + end + question + end + + @question_base_path = File.expand_path("../questions/", __FILE__) + @default_locale = :en +end diff --git a/lib/questions/en.yml b/lib/questions/en.yml new file mode 100644 index 0000000..fca59ed --- /dev/null +++ b/lib/questions/en.yml @@ -0,0 +1,68 @@ +Do: + you: + like: + - social networks + - muffins + - video games + your: + friends: + like: + - you: + - the way you are + - social networks + - muffins + - video games + know: + much: + about: + - your: + - life + - hobbies + - family + - you +What: + was: + the: + last: + thing: + you: + have: + - eaten + - done + your: + favourite: + song: + - as a 5 year old + - from a few weeks ago + would: + you: + do: + if: + you: + had: + - one million dollars + - the ability to fly + were: + - a dragon + do: + you: + think: + about: + - the: + - internet + - dragons + is: + the: + best: &is_the_best + thing: + about: + the: + - internet + your: + - favourite: + - series + - movie + - book + - country + - hometown + worst: *is_the_best \ No newline at end of file diff --git a/questiongenerator.gemspec b/questiongenerator.gemspec new file mode 100644 index 0000000..4c016c0 --- /dev/null +++ b/questiongenerator.gemspec @@ -0,0 +1,23 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'questiongenerator' + +Gem::Specification.new do |spec| + spec.name = "questiongenerator" + spec.version = QuestionGenerator::VERSION + spec.authors = ["nilsding"] + spec.email = ["nilsding@nilsding.org"] + spec.summary = %q{A simple question generator.} + spec.description = %q{A simple question generator.} + spec.homepage = "https://github.com/justask/questiongenerator" + spec.license = "MIT" + + spec.files = `git ls-files -z`.split("\x0") + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.6" + spec.add_development_dependency "rake" +end