Module: Quotes
- Defined in:
- lib/cryptoquotes.rb,
lib/cryptoquotes/version.rb
Constant Summary collapse
Class Method Summary collapse
- .all ⇒ Object
- .banner ⇒ Object
- .lottery ⇒ Object
-
.random ⇒ Object
get a random quote.
-
.read(path) ⇒ Object
read-in a quote dataset - returns an array of hash records.
- .read_builtin(name) ⇒ Object
- .root ⇒ Object
-
.sources ⇒ Object
rename to authors or datafiles or such - why? why not?.
- .version ⇒ Object
Class Method Details
.all ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/cryptoquotes.rb', line 50 def self.all @all ||= begin all = [] sources.each { |source| all += read_builtin( source ) } all end @all end |
.banner ⇒ Object
14 15 16 |
# File 'lib/cryptoquotes/version.rb', line 14 def self. "cryptoquotes/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}] (in #{root})" end |
.lottery ⇒ Object
62 63 64 65 |
# File 'lib/cryptoquotes.rb', line 62 def self.lottery ## get random shuffle (shuffle three times - why? why not?) @lottery ||= (0..(all.size-1)).to_a.shuffle.shuffle.shuffle end |
.random ⇒ Object
get a random quote
67 68 69 70 71 |
# File 'lib/cryptoquotes.rb', line 67 def self.random ## get a random quote idx = lottery.shift ## remove (shift) first index (ticket) in queue / array ## puts "[debug] rand => #{idx} of #{all.size} (remaining #{lottery.size})" all[ idx ] end |
.read(path) ⇒ Object
read-in a quote dataset - returns an array of hash records
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cryptoquotes.rb', line 22 def self.read( path ) ## read-in a quote dataset - returns an array of hash records ## puts "[debug] reading #{path}..." txt = File.open( path, 'r:utf-8' ) { |f| f.read } data = YAML.load( txt ) ## auto-fill by-line using filename (convention) ## e.g. David_Gerard.yml => David Gerard by_line = File.basename( path, File.extname( path ) ) by_line = by_line.gsub( '_', ' ' ) ## auto-convert underscore (_) back to space data.each do |rec| rec['by'] = by_line end data end |
.read_builtin(name) ⇒ Object
17 18 19 20 |
# File 'lib/cryptoquotes.rb', line 17 def self.read_builtin( name ) name = name.gsub( ' ', '_') ## auto-convert spaces to underscore (_) read( "#{root}/data/#{name}.yml" ) end |
.root ⇒ Object
18 19 20 |
# File 'lib/cryptoquotes/version.rb', line 18 def self.root File.( File.dirname(File.dirname(File.dirname(__FILE__))) ) end |
.sources ⇒ Object
rename to authors or datafiles or such - why? why not?
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cryptoquotes.rb', line 38 def self.sources ## rename to authors or datafiles or such - why? why not? ['Amy_Castor', 'Bitcoiner', 'Crypto_US$_Stablecoin_Printer', 'David_Gerard', 'Frances_Coppola', 'Nouriel_Roubini', 'Patrick_McKenzie', 'Preston_Byrne', 'Trolly_McTrollface'] end |
.version ⇒ Object
10 11 12 |
# File 'lib/cryptoquotes/version.rb', line 10 def self.version VERSION end |