Class: KjLite::Bible
- Inherits:
-
Object
- Object
- KjLite::Bible
- Defined in:
- lib/kjlite.rb
Instance Attribute Summary collapse
-
#booklist ⇒ Object
readonly
Returns the value of attribute booklist.
-
#to_h ⇒ Object
readonly
Returns the value of attribute to_h.
-
#to_s ⇒ Object
readonly
Returns the value of attribute to_s.
Instance Method Summary collapse
- #books(ref = nil) ⇒ Object
-
#initialize(url = 'https://gutenberg.org/cache/epub/30/pg30.txt', debug: false) ⇒ Bible
constructor
A new instance of Bible.
- #inspect ⇒ Object
- #random_book ⇒ Object
- #random_chapter ⇒ Object
- #random_verse ⇒ Object
Constructor Details
#initialize(url = 'https://gutenberg.org/cache/epub/30/pg30.txt', debug: false) ⇒ Bible
Returns a new instance of Bible.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/kjlite.rb', line 132 def initialize(url='https://gutenberg.org/cache/epub/30/pg30.txt', debug: false) filename, @debug = 'kjbible.txt', debug if File.exists?(filename) then s = File.read(filename) else s = URI.open(url).read File.write filename, s end s2 = s.split(/.*(?=^Book 01)/,3).last; 0 a = s2.split(/.*(?=^Book \d+)/); 0 h = a.inject({}) do |r,x| title, body = x.match(/^Book \d+\s+([^\r]+)\s+(.*)/m).captures a2 = body.split(/.*(?=\d+\:\d+\:\d+)/) a3 = a2.group_by {|x| x[/^\d+:\d+/]}.to_a.map(&:last) r.merge(title => a3[1..-1]) end @h = h.group_by {|key, _| key[/\d*\s*(.*)/,1]}; 0 @h.each do |key, value| @h[key] = value.length < 2 ? value.last.last : value.map(&:last) end @to_h, @to_s, @booklist = @h, s, h.keys end |
Instance Attribute Details
#booklist ⇒ Object (readonly)
Returns the value of attribute booklist.
130 131 132 |
# File 'lib/kjlite.rb', line 130 def booklist @booklist end |
#to_h ⇒ Object (readonly)
Returns the value of attribute to_h.
130 131 132 |
# File 'lib/kjlite.rb', line 130 def to_h @to_h end |
#to_s ⇒ Object (readonly)
Returns the value of attribute to_s.
130 131 132 |
# File 'lib/kjlite.rb', line 130 def to_s @to_s end |
Instance Method Details
#books(ref = nil) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/kjlite.rb', line 167 def books(ref=nil) return @booklist.map {|x| books(x) } unless ref index = ref.to_s[/^\d+$/] ? (ref.to_i - 1) : find_book(ref.downcase) puts 'index: ' + index.inspect if @debug title = @booklist[index] r = @h[title.sub(/^\d+\s+/,'')] puts 'r: ' + r.class.inspect if @debug if r.length > 3 then Book.new index+1, title, r, debug: @debug else i = ref[/\d+/].to_i - 1 a = r.map.with_index {|x,i| Book.new index+1, title, r[i], debug: @debug} a[i] end end |
#inspect ⇒ Object
187 188 189 |
# File 'lib/kjlite.rb', line 187 def inspect() "#<KjLite::Bible @booklist=#{@booklist}>" end |
#random_book ⇒ Object
191 192 193 |
# File 'lib/kjlite.rb', line 191 def random_book() books booklist.sample end |
#random_chapter ⇒ Object
195 196 197 |
# File 'lib/kjlite.rb', line 195 def random_chapter() random_book.chapters.sample end |
#random_verse ⇒ Object
199 200 201 |
# File 'lib/kjlite.rb', line 199 def random_verse() random_chapter.verses.sample end |