Class: BibleRef::Reference

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reference, language: 'eng', canon: 'all') ⇒ Reference

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



10
11
12
13
14
15
16
# 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

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



7
8
9
# File 'lib/bible_ref/reference.rb', line 7

def book
  @book
end

#canonObject (readonly)

Returns the value of attribute canon.



7
8
9
# File 'lib/bible_ref/reference.rb', line 7

def canon
  @canon
end

#languageObject (readonly)

Returns the value of attribute language.



7
8
9
# File 'lib/bible_ref/reference.rb', line 7

def language
  @language
end

#referenceObject (readonly)

Returns the value of attribute reference.



7
8
9
# File 'lib/bible_ref/reference.rb', line 7

def reference
  @reference
end

Instance Method Details

#book_idObject

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



32
33
34
35
# 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_nameObject

Returns the formatted book name



38
39
40
41
# File 'lib/bible_ref/reference.rb', line 38

def book_name
  return nil unless @details
  @book_name ||= @language.book_name(@details[:book], @canon)
end

#normalizeObject

Returns a normalized passage reference. e.g.

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



51
52
53
54
55
56
57
58
59
60
61
# 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

#rangesObject

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.



23
24
25
26
27
28
29
# 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?Boolean

Returns true if the reference is a known bible passage.

Returns:

  • (Boolean)


44
45
46
# File 'lib/bible_ref/reference.rb', line 44

def valid?
  @details && !book_id.nil?
end