-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathresponse.rb
More file actions
54 lines (48 loc) · 1.97 KB
/
response.rb
File metadata and controls
54 lines (48 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module ScriptureLookup
# Response encapsulates the response from any of the various
# scripture providers.
#
# Response.response_data is expected to be a well formatted hash of
# scripture. Current hash structure contains the following.
# * [:translation] - The name of the translation coming back from the
# provider, such as "English Standard Version (ESV)".
# * [:content] - A hash of scripture, where each key is reference, such
# as "Col-1-9" or "Ps-23-2".
# * [:content][<ref>][:title] - Either nil or a string containing a
# title for that verse.
# * [:content][<ref>][:subtitle] - Either nil or a string containing
# a subtitle for that verse.
# * [:content][<ref>][:verse] - An array of strings containing the scripture
# for that verse. This allows us to maintain
# linebreaks between each portion of the verse
# if desired (often Psalms and other poetic
# sections of Scripture place linebreaks in the midst
# of each verse).
class Response
attr_reader :response_data
def initialize(response_data)
@response_data = response_data
end
def verses
response_data[:content].values.inject([]) do |res, verse|
res + verse[:verse]
end
rescue ScriptureLookup::Error
raise
rescue => error
raise ScriptureLookup::Error
end
# Default implementation of to_s simply returns the text for each
# verse as a single paragraph (no line breaks).
def to_s
response_data[:content].values.inject("") do |res, verse|
res += " " unless res.empty?
res + verse[:verse].join(" ")
end
rescue ScriptureLookup::Error
raise
rescue => error
raise ScriptureLookup::Error
end
end
end