Class: BomDB::Import::Contents

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

Constant Summary collapse

DEFAULT_VERSE_CONTENT_RE =
/^\s*(.+)\s+(\d+):(\d+)\s+(.*)$/
DEFAULT_VERSE_REF_RE =
/^\s*(.+)\s+(\d+):(\d+)$/
MAX_DUPS =
5

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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bomdb/import/contents.rb', line 68

def import_json(data)
  # this cross-ref is for looking up file-edition-id => database-edition-id
  editions_xref = {}

  ed_model = Models::Edition.new(@db)
  verse_model = Models::Verse.new(@db)

  data['editions'].each_pair do |id, e|
    editions_xref[ id ] = ed_model.find_or_create(e["year"].to_i, e["name"])
  end

  data['contents'].each_pair do |book_name, chapters|
    chapters.each_pair do |chapter, verses|
      verses.each_pair do |verse_full_ref, editions|
        match = DEFAULT_VERSE_REF_RE.match(verse_full_ref)
        if match
          verse_number = match[3].to_i
          verse = verse_model.find(
            book_name: book_name,
            chapter: chapter,
            verse: verse_number
          )
          if verse.nil?
            return Import::Result.new(success: false,
              error: "Unable to find verse: book: " +
                     "'#{book_name}', chapter: '#{chapter}', " +
                     "verse: '#{verse_number}'")
          end
          editions.each_pair do |file_edition_id, content_body|
            @db[:contents].insert(
              edition_id: editions_xref[ file_edition_id ],
              verse_id: verse[:verse_id],
              content_body: content_body
            )
          end
        else
          $stderr.puts "Unable to parse verse ref from '#{verse_full_ref}', skipping"
        end
      end
    end
  end
  Import::Result.new(success: true)
end

#import_json_old(data) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bomdb/import/contents.rb', line 112

def import_json_old(data)
  data.each_pair do |book_name, year_editions|
    year_editions.each do |year_edition|
      year_edition.each_pair do |year, d|
        m = d["meta"]

        book = find_book(book_name)
        return book if book.is_a?(Import::Result)

        verse_id = Models::Verse.new(@db).find_or_create(
          chapter: m['chapter'],
          verse: m['verse'],
          book_id: book[:book_id],
          heading: m['heading']
        )

        ed_id = opts[:edition_id] || find_or_create_edition(year)

        @db[:contents].insert(
          edition_id:   ed_id,
          verse_id:     verse_id,
          content_body: d["content"]
        )
      end
    end
  end
  Import::Result.new(success: true)
rescue Sequel::UniqueConstraintViolation => e
  Import::Result.new(success: false, error: e)
end

#import_text(data) ⇒ Object



12
13
14
15
16
17
18
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bomdb/import/contents.rb', line 12

def import_text(data)
  if opts[:edition_prefix].nil?
    return Import::Result.new(success: false,
      error: "'--edition' is required for text import of contents"
    )
  end

  edition_model = Models::Edition.new(@db)

  edition = edition_model.find(opts[:edition_prefix])
  if edition.nil?
    return Import::Result.new(success: false,
      error: "Edition matching prefix '#{opts[:edition_prefix]}' not found"
    )
  end
  edition_id = edition[:edition_id]

  verse_re = opts[:verse_re] || DEFAULT_VERSE_CONTENT_RE

  times_tried = 0
  data.each_line do |line|
    if line =~ verse_re
      book_name, chapter, verse, content = $1, $2, $3, $4

      book = find_book(book_name)
      return book if book.is_a?(Import::Result)

      verse_id = Models::Verse.new(@db).find_or_create(
        chapter: chapter,
        verse: verse,
        book_id: book[:book_id]
      )

      begin
        @db[:contents].insert(
          edition_id:   edition_id,
          verse_id:     verse_id,
          content_body: content
        )
      rescue Sequel::UniqueConstraintViolation => e
        msg = "edition_id: #{edition_id}, verse: '#{book_name} #{chapter}:#{verse}', content: #{content.inspect}"
        $stderr.puts "Warning: duplicate #{msg}"
        times_tried += 1
        if times_tried > MAX_DUPS
          return Import::Result.new(success: false,
            error: "Too many duplicate rows. Stopped at #{msg}"
          )
        else
          next
        end
      end
    end
  end
  Import::Result.new(success: true)
end