class BibleGateway

Constants

CLASSIC_GATEWAY_URL
GATEWAY_URL
VERSION
VERSIONS

Attributes

version[RW]

Public Class Methods

new(version = :king_james_version) click to toggle source
# File lib/bible_gateway.rb, line 54
def initialize(version = :king_james_version)
  self.version = version
end
versions() click to toggle source
# File lib/bible_gateway.rb, line 48
def self.versions
  VERSIONS.keys
end

Public Instance Methods

lookup(passage) click to toggle source
# File lib/bible_gateway.rb, line 63
def lookup(passage)
  response = Typhoeus.get(passage_url(passage), followlocation: true)
  doc = Nokogiri::HTML(response.body)
  scrape_passage(doc, @version)
end
old_lookup(passage) click to toggle source
# File lib/bible_gateway.rb, line 69
def old_lookup(passage)
  response = Typhoeus.get(old_passage_url(passage), followlocation: true)
  doc = Nokogiri::HTML(response.body)
  old_way_scrape_passage(doc)
end
version=(version) click to toggle source
# File lib/bible_gateway.rb, line 58
def version=(version)
  raise BibleGatewayError, 'Unsupported version' unless VERSIONS.keys.include? version
  @version = version
end

Private Instance Methods

old_passage_url(passage) click to toggle source
# File lib/bible_gateway.rb, line 80
def old_passage_url(passage)
  "#{CLASSIC_GATEWAY_URL}/passage/?search=#{URI.encode_www_form_component(passage)}&version=#{URI.encode_www_form_component(VERSIONS[version])}"
end
old_way_scrape_passage(doc) click to toggle source
# File lib/bible_gateway.rb, line 113
def old_way_scrape_passage(doc)
  container = doc.css('div.container')
  title = container.css('div.passage-details h1')[0].content.strip
  segment = doc.at('div.passage-wrap')
  segment.search('sup.crossreference').remove # remove cross reference links
  segment.search('sup.footnote').remove # remove footnote links
  segment.search("div.crossrefs").remove # remove cross references
  segment.search("div.footnotes").remove # remove footnotes

  # extract text only from scripture
  text = ""
  segment.search("span.text").each do |span|
    text += span.inner_text
  end

  segment.search("span.text").each do |span|
    html_content = span.inner_html
    span.swap html_content
  end
  
  segment.search('sup.versenum').each do |sup|
    html_content = sup.content
    sup.swap "<sup>#{html_content}</sup>"
  end

  content = segment.inner_html.gsub('<p></p>', '').gsub(/<!--.*?-->/, '').strip
  {:title => title, :content => content, :text => text }
end
passage_url(passage) click to toggle source
# File lib/bible_gateway.rb, line 76
def passage_url(passage)
  "#{GATEWAY_URL}/passage/?search=#{URI.encode_www_form_component(passage)}&version=#{URI.encode_www_form_component(VERSIONS[version])}"
end
scrape_passage(doc, version) click to toggle source
# File lib/bible_gateway.rb, line 84
def scrape_passage(doc, version)
  container = doc.css('div.passage-text')
  title = container.css("div.version-#{VERSIONS[version]}.result-text-style-normal.text-html h1 span")[0].content.strip if container.css("div.version-#{VERSIONS[version]}.result-text-style-normal.text-html h1")[0] != nil
  segment = doc.at('div.passage-text')

  segment.search('sup.crossreference').remove # remove cross reference links
  segment.search('sup.footnote').remove # remove footnote links
  segment.search("div.crossrefs").remove # remove cross references
  segment.search("div.footnotes").remove # remove footnotes

  text = ""
  segment.search("span.text").each do |span|
    text += span.inner_text
  end

  segment.search("span.text").each do |span|
    html_content = span.inner_html
    span.swap html_content
  end
  
  segment.search('sup.versenum').each do |sup|
    html_content = sup.content
    sup.swap "<sup>#{html_content}</sup>"
  end

  content = segment.inner_html.gsub('<p></p>', '').gsub(/<!--.*?-->/, '').strip
  {:title => title, :content => content, :text => text }
end