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.



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
166
167
168
169
170
171
# File 'lib/kjlite.rb', line 136

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
  
  #puts '@h: ' + @h.keys.inspect
=begin
  @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
=end
  @to_h, @to_s, @booklist = @h, s, @h.keys

end

Instance Attribute Details

#booklistObject (readonly)

Returns the value of attribute booklist.



134
135
136
# File 'lib/kjlite.rb', line 134

def booklist
  @booklist
end

#to_hObject (readonly)

Returns the value of attribute to_h.



134
135
136
# File 'lib/kjlite.rb', line 134

def to_h
  @to_h
end

#to_sObject (readonly)

Returns the value of attribute to_s.



134
135
136
# File 'lib/kjlite.rb', line 134

def to_s
  @to_s
end

Instance Method Details

#books(ref = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/kjlite.rb', line 173

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+/,'')]
  r = @h[title]

  puts 'r: '  + r.class.inspect if @debug

  Book.new index+1, title, r, debug: @debug

end

#inspectObject



189
190
191
# File 'lib/kjlite.rb', line 189

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

#random_bookObject



193
194
195
# File 'lib/kjlite.rb', line 193

def random_book()
  books booklist.sample
end

#random_chapterObject



197
198
199
# File 'lib/kjlite.rb', line 197

def random_chapter()
  random_book.chapters.sample
end

#random_verseObject



201
202
203
# File 'lib/kjlite.rb', line 201

def random_verse()
  random_chapter.verses.sample
end