Module: Quote

Defined in:
lib/quote.rb

Constant Summary collapse

CRITERIA =
{
  "context" => ["from", "in"],
  "source"  => ["by"],
  "quote"   => ["with", "containing", "matching"],
  "theme"   => ["category"]
}

Class Method Summary collapse

Class Method Details

.add(json) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/quote.rb', line 54

def add(json)
  quote = Yajl::Parser.parse(json)
  missing_criteria = []
  CRITERIA.keys.each do |criterion|
    missing_criteria << criterion unless quote[criterion]
  end
  missing_criteria.empty? ? add_quote(quote) : "Can't add quote: missing #{missing_criteria.sort.join(', ')}"
rescue Yajl::ParseError
  return "Please supply a valid json"
end

.load_quotesObject



65
66
67
# File 'lib/quote.rb', line 65

def load_quotes
  Yajl::Parser.parse(File.new(source_file_path))
end

.say(criteria = {}) ⇒ Object



42
43
44
45
46
# File 'lib/quote.rb', line 42

def say(criteria = {})
  random_quote = load_quotes.where(criteria).random
  return "No quote found" unless random_quote
  ENV['QUOTE_COLORIZE'] == "true" ? random_quote.format_with_color : random_quote.format
end

.say_all(criteria = {}) ⇒ Object



48
49
50
51
52
# File 'lib/quote.rb', line 48

def say_all(criteria = {})
  quotes = load_quotes.where(criteria)
  return "No quotes found" if quotes.empty?
  ENV['QUOTE_COLORIZE'] == "true" ? quotes.map(&:format_with_color) : quotes.map(&:format)
end