Class: KjLite::Bible

Inherits:
Object
  • Object
show all
Defined in:
lib/kjlite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#booklistObject (readonly)

Returns the value of attribute booklist.



130
131
132
# File 'lib/kjlite.rb', line 130

def booklist
  @booklist
end

#to_hObject (readonly)

Returns the value of attribute to_h.



130
131
132
# File 'lib/kjlite.rb', line 130

def to_h
  @to_h
end

#to_sObject (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

#inspectObject



187
188
189
# File 'lib/kjlite.rb', line 187

def inspect()
  "#<KjLite::Bible @booklist=#{@booklist}>"
end

#random_bookObject



191
192
193
# File 'lib/kjlite.rb', line 191

def random_book()
  books booklist.sample
end

#random_chapterObject



195
196
197
# File 'lib/kjlite.rb', line 195

def random_chapter()
  random_book.chapters.sample
end

#random_verseObject



199
200
201
# File 'lib/kjlite.rb', line 199

def random_verse()
  random_chapter.verses.sample
end