class KjLite::Bible

Attributes

booklist[R]
to_h[R]
to_s[R]

Public Class Methods

new(url='http://www.gutenberg.org/cache/epub/30/pg30.txt', debug: false) click to toggle source
# File lib/kjlite.rb, line 132
def initialize(url='http://www.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 = 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

Public Instance Methods

books(ref=nil) click to toggle source
# 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() click to toggle source
# File lib/kjlite.rb, line 187
def inspect()
  "#<KjLite::Bible @booklist=#{@booklist}>"
end
random_book() click to toggle source
# File lib/kjlite.rb, line 191
def random_book()
  books booklist.sample
end
random_chapter() click to toggle source
# File lib/kjlite.rb, line 195
def random_chapter()
  random_book.chapters.sample
end
random_verse() click to toggle source
# File lib/kjlite.rb, line 199
def random_verse()
  random_chapter.verses.sample
end

Private Instance Methods

find_book(ref) click to toggle source
# File lib/kjlite.rb, line 205
def find_book(ref)

  h = @booklist.inject({}) do |r,rawx|

    x = rawx.downcase
    a3 = [
      x,
      x.sub(/(\d)\s/,'\1'),
      x.sub(/(\d)\s/,'\1-'),
      NoVowels.compact(x),
      NoVowels.compact(x.sub(/(\d)\s/,'\1')),
      NoVowels.compact(x.sub(/(\d)\s/,'\1-')),
    ]
    puts 'a3: ' + a3.inspect if @debug
    a3b = a3.uniq.abbrev.keys.reject {|x| x[/^\s*\d+$/] or x.length < 2}
    r.merge(rawx => a3b)

  end
  puts 'h: '  + h.inspect if @debug
  r = h.find {|key, val| val.grep(/#{ref}/).any? }
  r = h.find {|key, vl| vl.grep(/#{ref.sub(/\d+\s*/,'')}/).any? } unless r
  puts 'r: ' + r.inspect if @debug
  @booklist.index r.first

end