Class: BomDB::Import::Verses

Inherits:
Base
  • Object
show all
Defined in:
lib/bomdb/import/verses.rb

Instance Attribute Summary

Attributes inherited from Base

#db, #opts

Instance Method Summary collapse

Methods inherited from Base

#ensure_parsed_json, #import, #initialize, #schema, tables, #tables

Constructor Details

This class inherits a constructor from BomDB::Import::Base

Instance Method Details

#import_json(data, **args) ⇒ Object

Expected data format is: [

{
  "range_id": Int,  # the mericope range ID
  "book": String,   # the name of the book, e.g. 1 Nephi
  "chapter": Int,   # the chapter number
  "verse": Int      # the verse number
},
...

]



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bomdb/import/verses.rb', line 19

def import_json(data, **args)
  book, chapter = nil, nil
  verse_model = Models::Verse.new(@db)
  data.each do |r|
    if r['chapter'] != chapter || r['book'] != book
      # Create a heading per chapter
      Models::Verse.new(@db).find_or_create(
        chapter:   r['chapter'],
        verse:     nil,
        book_name: r['book'],
        heading:   true
      )
      chapter = r['chapter']
    end

    if r['book'] != book
      puts "Importing chapters & verses for '#{r['book']}'"
      book = r['book']
    end
    verse_model.find_or_create(
      chapter:   r['chapter'],
      verse:     r['verse'],
      book_name: r['book'],
      range_id:  r['range_id']
    )
  end
  Import::Result.new(success: true)
rescue Sequel::UniqueConstraintViolation => e
  Import::Result.new(success: false, error: e)
end