lib/bible_gateway.rb in bible_gateway-0.0.2 vs lib/bible_gateway.rb in bible_gateway-0.0.3
- old
+ new
@@ -1,14 +1,16 @@
+# coding: utf-8
+require 'bible_gateway/version'
require 'open-uri'
require 'nokogiri'
class BibleGatewayError < StandardError; end
-
+
class BibleGateway
GATEWAY_URL = "http://www.biblegateway.com"
-
- VERSIONS = {
+
+ VERSIONS = {
:new_international_version => "NIV",
:new_american_standard_bible => "NASB",
:the_message => "MSG",
:amplified_bible => "AMP",
:new_living_translation => "NLT",
@@ -26,46 +28,52 @@
:wycliffe_new_testament => "WYC",
:worldwide_english_new_testament => "WE",
:new_international_version_uk => "NIVUK",
:todays_new_international_version => "TNIV",
}
-
+
def self.versions
VERSIONS.keys
end
-
+
attr_accessor :version
-
+
def initialize(version = :king_james_version)
self.version = version
end
-
+
def version=(version)
raise BibleGatewayError, 'Unsupported version' unless VERSIONS.keys.include? version
@version = version
end
def lookup(passage)
doc = Nokogiri::HTML(open(passage_url(passage)))
scrape_passage(doc)
- end
+ end
private
def passage_url(passage)
URI.escape "#{GATEWAY_URL}/passage/?search=#{passage}&version=#{VERSIONS[version]}"
end
def scrape_passage(doc)
- title = doc.css('h2')[0].content
+ container = doc.css('div#content')
+ title = container.css('h1')[0].content
segment = doc.at('div.result-text-style-normal')
- segment.search('sup.xref').remove # remove cross reference links
+ 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
- segment.search('sup.versenum').each do |span|
+ segment.search("span.text").each do |span|
text = span.content
- span.swap "<sup>#{text}</sup>"
+ span.swap text
end
+
+ segment.search('sup.versenum').each do |sup|
+ text = sup.content
+ sup.swap "<sup>#{text}</sup>"
+ end
content = segment.inner_html.gsub('<p></p>', '').gsub(/<!--.*?-->/, '').strip
{:title => title, :content => content }
end
-end
\ No newline at end of file
+end