Module: Wahy::Parser

Included in:
Wahy
Defined in:
lib/wahy/parser.rb

Instance Method Summary collapse

Instance Method Details

#chapters_data(doc) ⇒ Object

Returns the list of Chapter nodes

Parameters:

  • doc (Nokogiri::XML::Document)


18
19
20
# File 'lib/wahy/parser.rb', line 18

def chapters_data(doc)
  doc.xpath("//Chapter")
end

#new_data(lang = "eng") ⇒ Object

Returns the parsed Nokogiri document for the specified language

Parameters:

  • lang (String) (defaults to: "eng")

    ‘eng’ or ‘tur’



7
8
9
10
11
12
13
14
# File 'lib/wahy/parser.rb', line 7

def new_data(lang = "eng")
  filename = lang.to_s.downcase == "tur" ? "config_tr.xml" : "config_en.xml"
  filepath = File.join(__dir__, "data", filename)

  raise "Data file not found: #{filepath}" unless File.exist?(filepath)

  Nokogiri::XML(File.read(filepath))
end

#scripture_data(chapters, identifier) ⇒ Object

Finds a specific chapter by ID or Name (case-insensitive)

Parameters:

  • chapters (Nokogiri::XML::NodeSet)
  • identifier (String, Integer)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wahy/parser.rb', line 25

def scripture_data(chapters, identifier)
  # Eğer identifier bir sayı ise direkt ID ile eşleştir
  return chapters.find { |c| c["ChapterID"] == identifier.to_s } if identifier.to_s =~ /^\d+$/

  # Sayı değilse hem Türkçe hem İngilizce isimle eşleştirmeye çalış
  # Bunun için hem TR hem EN datasına ihtiyacımız olacak
  tr_doc = new_data("tur")
  en_doc = new_data("eng")

  tr_chapter = tr_doc.xpath("//Chapter").find { |c| c["ChapterName"].downcase == identifier.downcase }
  return chapters_data(tr_doc).find { |c| c["ChapterID"] == tr_chapter["ChapterID"] } if tr_chapter

  en_chapter = en_doc.xpath("//Chapter").find { |c| c["ChapterName"].downcase == identifier.downcase }
  return chapters_data(en_doc).find { |c| c["ChapterID"] == en_chapter["ChapterID"] } if en_chapter

  nil
end

#sign_data(chapter) ⇒ Object

Returns an array of Verse nodes for a given chapter

Parameters:

  • chapter (Nokogiri::XML::Node)


45
46
47
# File 'lib/wahy/parser.rb', line 45

def sign_data(chapter)
  chapter.xpath("Verse").to_a
end

#take_specific_sign(signs, verse_number) ⇒ Object

Returns a specific verse from the signs array

Parameters:

  • signs (Array<Nokogiri::XML::Node>)
  • verse_number (String, Integer)


52
53
54
# File 'lib/wahy/parser.rb', line 52

def take_specific_sign(signs, verse_number)
  signs.find { |verse| verse["VerseID"] == verse_number.to_s }
end