class BibleRef::Reference

Attributes

book[R]
canon[R]
language[R]
reference[R]

Public Class Methods

new(reference, language: 'eng', canon: 'all') click to toggle source

Create a new Reference instance by passing in the user-supplied bible reference as a string.

# File lib/bible_ref/reference.rb, line 10
def initialize(reference, language: 'eng', canon: 'all')
  @language = language.respond_to?(:book_id) ? language : LANGUAGES.fetch(language.to_s).new
  @canon = canon.respond_to?(:books) ? canon : CANONS.fetch(canon.to_s).new
  @reference = reference
  standardize_reference()
  @details = parse
end

Public Instance Methods

book_id() click to toggle source

Returns a USFX-compatible book id, or nil if book is unknown.

# File lib/bible_ref/reference.rb, line 32
def book_id
  return nil unless @details
  @book_id ||= @language.book_id(@details[:book], @canon)
end
book_name() click to toggle source

Returns the formatted book name

# File lib/bible_ref/reference.rb, line 38
def book_name
  return nil unless @details
  @book_name ||= @language.book_name(@details[:book], @canon)
end
normalize() click to toggle source

Returns a normalized passage reference. e.g.

‘JOHN 3:16&17’ => ‘John 3:16,17’

# File lib/bible_ref/reference.rb, line 51
def normalize
  return unless book_id and ranges
  book_name + ' ' +
  ranges.map do |(ref_from, ref_to)|
    if ref_from != ref_to
      ref_part(ref_from) + '-' + ref_part(ref_to)
    else
      ref_part(ref_from)
    end
  end.join(',')
end
ranges() click to toggle source

Returns an array of pairs, each one being the from and to for a range. For single verses, the same ref is repeated twice. This is most helpful for converting the entire passage into a SQL query, and in fact is exactly why this library was built. See github.com/seven1m/bible_api/blob/master/app.rb for an example.

# File lib/bible_ref/reference.rb, line 23
def ranges
  return nil unless valid?
  @chapter = nil
  [@details[:refs]].flatten.map do |ref|
    normalize_range(ref) || normalize_ref(ref)
  end
end
valid?() click to toggle source

Returns true if the reference is a known bible passage.

# File lib/bible_ref/reference.rb, line 44
def valid?
  @details && !book_id.nil?
end

Private Instance Methods

normalize_range(ref) click to toggle source
# File lib/bible_ref/reference.rb, line 101
def normalize_range(ref)
  return unless range = ref[:range]
  ch = range.detect { |_, r| r[:chapter] }
  @chapter = ch.last[:chapter] if ch
  [
    { book: book_id, chapter: @chapter }.merge(range[:from]),
    { book: book_id, chapter: @chapter }.merge(range[:to])
  ]
end
normalize_ref(ref) click to toggle source
# File lib/bible_ref/reference.rb, line 111
def normalize_ref(ref)
  @chapter = ref[:chapter] if ref[:chapter]
  (1..2).map do
    { book: book_id, chapter: @chapter }.merge(ref)
  end
end
parse() click to toggle source
# File lib/bible_ref/reference.rb, line 91
def parse
  begin
    parsed = Parser.new.parse(@reference)
  rescue Parslet::ParseFailed
    nil
  else
    ParserTransform.new.apply(parsed)
  end
end
ref_part(ref) click to toggle source
# File lib/bible_ref/reference.rb, line 65
def ref_part(ref)
  if @last_chapter != ref[:chapter] and ref[:chapter]
    @last_chapter = ref[:chapter]
    if ref[:verse]
      "#{ref[:chapter]}:#{ref[:verse]}"
    else
      ref[:chapter]
    end
  else
    ref[:verse].to_s
  end
end
standardize_reference() click to toggle source

standardize the reference so it can be handed to the parser.

# File lib/bible_ref/reference.rb, line 79
def standardize_reference
    return @reference unless @language.has_single_chapter?(@reference)
    return @reference if @reference.include? ':'

    matches = @reference.match(/^([\d]?[\D\s]*)/)
    return @reference if matches.length() == 0

    book = matches[0].strip
    requested = @reference.sub(book, '').strip
    @reference = "#{book} 1:#{requested}"
end