scripture_lookup 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/scripture +10 -0
- data/lib/scripture_lookup/bible_gateway_scraper.rb +48 -0
- data/lib/scripture_lookup/errors/error.rb +10 -0
- data/lib/scripture_lookup/parsers/bible_gateway_scrape_parser.rb +46 -0
- data/lib/scripture_lookup/response.rb +54 -0
- data/lib/scripture_lookup.rb +14 -0
- metadata +99 -0
data/bin/scripture
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'metainspector'
|
2
|
+
require_relative 'response'
|
3
|
+
require_relative 'parsers/bible_gateway_scrape_parser'
|
4
|
+
|
5
|
+
module ScriptureLookup
|
6
|
+
class BibleGatewayScraper
|
7
|
+
attr_accessor :options
|
8
|
+
|
9
|
+
def initialize(opts = {})
|
10
|
+
@options = default_options.merge(opts)
|
11
|
+
end
|
12
|
+
|
13
|
+
def default_options
|
14
|
+
{response_class: Response,
|
15
|
+
parser: BibleGatewayScrapeParser}
|
16
|
+
end
|
17
|
+
|
18
|
+
def lookup reference, version
|
19
|
+
url = "http://www.biblegateway.com/passage/?search=#{reference}&version=#{version.to_s}"
|
20
|
+
doc = get_doc(url)
|
21
|
+
|
22
|
+
generate_response doc
|
23
|
+
|
24
|
+
rescue ScriptureLookup::Error
|
25
|
+
raise
|
26
|
+
rescue => error
|
27
|
+
raise ScriptureLookup::Error
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
##
|
33
|
+
# BibleGatewayScraper#get_doc deals with the fetching of HTML content from Bible Gateway.
|
34
|
+
def get_doc(url)
|
35
|
+
page = MetaInspector.new(url)
|
36
|
+
doc = page.document
|
37
|
+
|
38
|
+
raise page.errors[0] if !page.ok?
|
39
|
+
|
40
|
+
doc
|
41
|
+
end
|
42
|
+
|
43
|
+
def generate_response doc
|
44
|
+
parser = options[:parser].new
|
45
|
+
options[:response_class].new(parser.parse(doc))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'loofah'
|
2
|
+
|
3
|
+
module ScriptureLookup
|
4
|
+
# Encapsulate the messy parsing of raw BibleGateway.com HTML into
|
5
|
+
# a structure of hashes and arrays expected by Response.
|
6
|
+
class BibleGatewayScrapeParser
|
7
|
+
def parse(doc)
|
8
|
+
# scrub out footnotes and cross references.
|
9
|
+
frag = Loofah.fragment(doc)
|
10
|
+
kill_sup = Loofah::Scrubber.new { |node| node.remove if (node.name == "sup") or node["class"] == "chapternum" }
|
11
|
+
frag.scrub!(kill_sup)
|
12
|
+
|
13
|
+
{translation: translation(frag),
|
14
|
+
content: content(frag)}
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def translation(doc)
|
20
|
+
path = './/div[contains(@class, "heading")]/p'
|
21
|
+
doc.xpath(path).text
|
22
|
+
end
|
23
|
+
|
24
|
+
def content(doc)
|
25
|
+
path = './/div[contains(@class, "passage")]//span[contains(@class, "text")]'
|
26
|
+
passages = doc.xpath(path)
|
27
|
+
|
28
|
+
passages.each_with_object({}) do |passage, hsh|
|
29
|
+
reference = passage["class"].split.last #ex. "Col-1-9"
|
30
|
+
hsh[reference] = {} unless hsh.has_key? reference
|
31
|
+
add_passage_text(hsh[reference], passage)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def add_passage_text(hsh, passage)
|
36
|
+
if passage.parent.name == "h3"
|
37
|
+
hsh[:title] = passage.text
|
38
|
+
elsif passage.parent.name == "h4"
|
39
|
+
hsh[:subtitle] = passage.text
|
40
|
+
else
|
41
|
+
hsh[:verse] = [] unless hsh.has_key? :verse
|
42
|
+
hsh[:verse] << passage.text
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ScriptureLookup
|
2
|
+
|
3
|
+
# Response encapsulates the response from any of the various
|
4
|
+
# scripture providers.
|
5
|
+
#
|
6
|
+
# Response.response_data is expected to be a well formatted hash of
|
7
|
+
# scripture. Current hash structure contains the following.
|
8
|
+
# * [:translation] - The name of the translation coming back from the
|
9
|
+
# provider, such as "English Standard Version (ESV)".
|
10
|
+
# * [:content] - A hash of scripture, where each key is reference, such
|
11
|
+
# as "Col-1-9" or "Ps-23-2".
|
12
|
+
# * [:content][<ref>][:title] - Either nil or a string containing a
|
13
|
+
# title for that verse.
|
14
|
+
# * [:content][<ref>][:subtitle] - Either nil or a string containing
|
15
|
+
# a subtitle for that verse.
|
16
|
+
# * [:content][<ref>][:verse] - An array of strings containing the scripture
|
17
|
+
# for that verse. This allows us to maintain
|
18
|
+
# linebreaks between each portion of the verse
|
19
|
+
# if desired (often Psalms and other poetic
|
20
|
+
# sections of Scripture place linebreaks in the midst
|
21
|
+
# of each verse).
|
22
|
+
class Response
|
23
|
+
attr_reader :response_data
|
24
|
+
|
25
|
+
def initialize(response_data)
|
26
|
+
@response_data = response_data
|
27
|
+
end
|
28
|
+
|
29
|
+
def verses
|
30
|
+
response_data[:content].values.inject([]) do |res, verse|
|
31
|
+
res + verse[:verse]
|
32
|
+
end
|
33
|
+
|
34
|
+
rescue ScriptureLookup::Error
|
35
|
+
raise
|
36
|
+
rescue => error
|
37
|
+
raise ScriptureLookup::Error
|
38
|
+
end
|
39
|
+
|
40
|
+
# Default implementation of to_s simply returns the text for each
|
41
|
+
# verse as a single paragraph (no line breaks).
|
42
|
+
def to_s
|
43
|
+
response_data[:content].values.inject("") do |res, verse|
|
44
|
+
res += " " unless res.empty?
|
45
|
+
res + verse[:verse].join(" ")
|
46
|
+
end
|
47
|
+
|
48
|
+
rescue ScriptureLookup::Error
|
49
|
+
raise
|
50
|
+
rescue => error
|
51
|
+
raise ScriptureLookup::Error
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ScriptureLookup
|
2
|
+
extend self
|
3
|
+
|
4
|
+
require_relative 'scripture_lookup/errors/error'
|
5
|
+
require_relative 'scripture_lookup/bible_gateway_scraper'
|
6
|
+
|
7
|
+
# Sugar method to be able to quickly create a provider.
|
8
|
+
#
|
9
|
+
# BibleGatewayScraper is the default provider as it can return scripture
|
10
|
+
# from the most translations.
|
11
|
+
def new
|
12
|
+
BibleGatewayScraper.new
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scripture_lookup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Warren Wright
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: metainspector
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: loofah
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A gem to assist in pulling back and querying scripture.
|
63
|
+
email: warren@thewrightangle.com
|
64
|
+
executables:
|
65
|
+
- scripture
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- lib/scripture_lookup.rb
|
70
|
+
- lib/scripture_lookup/bible_gateway_scraper.rb
|
71
|
+
- lib/scripture_lookup/response.rb
|
72
|
+
- lib/scripture_lookup/parsers/bible_gateway_scrape_parser.rb
|
73
|
+
- lib/scripture_lookup/errors/error.rb
|
74
|
+
- bin/scripture
|
75
|
+
homepage: http://thewrightangle.com
|
76
|
+
licenses: []
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.24
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Pull back verses from popular Bible sites easily.
|
99
|
+
test_files: []
|