biblesearch-api 1.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.
@@ -0,0 +1,24 @@
1
+ class BibleSearch
2
+ module Search
3
+ def search(query, options = {})
4
+ options = options.merge({:query => query})
5
+
6
+ result = Hashie::Mash.new
7
+ api_result = get_mash("/search.js", :query => options)
8
+
9
+ if api_result.meta.http_code == 200
10
+ api_result.response.search.tap do |search_response|
11
+ result.type = search_response.result.type
12
+ result.summary = search_response.result.summary
13
+ if result.type == 'passages'
14
+ result.passages = pluralize_result(search_response.result.passages)
15
+ elsif result.type == 'verses'
16
+ result.verses = pluralize_result(search_response.result.verses)
17
+ end
18
+ end
19
+ end
20
+
21
+ fumsify(api_result, result)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ class BibleSearch
2
+ module Verses
3
+ def verses(chapter_id, start_verse="", end_verse="")
4
+
5
+ verses = []
6
+ api_result = get_mash("/chapters/#{chapter_id}/verses.js", :query => {:start => start_verse, :end => end_verse})
7
+ if api_result.meta.http_code == 200
8
+ verses = pluralize_result(api_result.response.verses)
9
+ end
10
+
11
+ fumsify(api_result, verses)
12
+ end
13
+
14
+ def verse(verse_sig)
15
+
16
+ # Validate a signature hash, calling its string sig equivalent if valid
17
+ if verse_sig.is_a?(Hash)
18
+ unless required_keys_present?(verse_sig, [:version_id, :book_id, :chapter, :verse])
19
+ raise ArgumentError.new('Verse signature hash must include :version_id, :book_id, :chapter, and :verse')
20
+ end
21
+
22
+ return verse("#{verse_sig[:version_id]}:#{verse_sig[:book_id]}.#{verse_sig[:chapter]}.#{verse_sig[:verse]}")
23
+ end
24
+
25
+ # Validate a signature string, returning a verse Mash if valid and the
26
+ # remote request succeeds
27
+ if verse_sig.is_a?(String)
28
+ unless verse_sig.match(/([A-Za-z0-9]+-)?[A-Za-z0-9]+:[A-Za-z0-9]+\.[0-9]+\.[0-9]+/)
29
+ raise ArgumentError.new('Verse signature must be in the form "VERSION_ID:BOOK_ID.CHAPTER_NUMBER.VERSE_NUMBER"')
30
+ end
31
+
32
+ verse = nil
33
+ api_result = get_mash("/verses/#{verse_sig}.js")
34
+ if api_result.meta.http_code == 200
35
+ verse = api_result.response.verses.first
36
+ end
37
+
38
+ return fumsify(api_result, verse)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,28 @@
1
+ class BibleSearch
2
+ module Versions
3
+ def versions(options = {})
4
+ api_endpoint = '/versions.js'
5
+ unless options[:language].nil?
6
+ api_endpoint += %{?language=#{options[:language]}}
7
+ end
8
+
9
+ versions = []
10
+ api_result = get_mash(api_endpoint)
11
+ if api_result.meta.http_code == 200
12
+ versions = pluralize_result(api_result.response.versions)
13
+ end
14
+
15
+ fumsify(api_result, versions)
16
+ end
17
+
18
+ def version(version_id)
19
+ version = nil
20
+ api_result = get_mash("/versions/#{version_id.upcase}.js")
21
+ if api_result.meta.http_code == 200
22
+ version = api_result.response.versions.first
23
+ end
24
+
25
+ fumsify(api_result, version)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+ require 'biblesearch-api'
3
+
4
+ describe BibleSearch do
5
+ before do
6
+ VCR.insert_cassette %{endpoint-#{File.basename(__FILE__, '.rb')}-#{__name__}}
7
+ @biblesearch = BibleSearch.new('DUMMY_API_KEY')
8
+ end
9
+
10
+ after do
11
+ VCR.eject_cassette
12
+ end
13
+
14
+ describe %{books} do
15
+ it %{requires a version id} do
16
+ lambda { @biblesearch.books }.must_raise ArgumentError
17
+ (lambda { @biblesearch.books('GNT') }.call rescue $!).wont_be_kind_of ArgumentError
18
+ end
19
+
20
+ it %{accepts an optional testament id} do
21
+ all_cool = lambda { @biblesearch.books('GNT', 'OT') }
22
+ (all_cool.call rescue $!).wont_be_kind_of ArgumentError
23
+ end
24
+
25
+ describe %{when I make a valid request} do
26
+ before do
27
+ @books = @biblesearch.books('GNT')
28
+ end
29
+
30
+ it %{has a collection} do
31
+ @books.collection.must_be_kind_of(Array)
32
+ end
33
+
34
+ it %{contains books} do
35
+ @books.collection.length.must_be :>, 0
36
+ @books.collection.each do |book|
37
+ book.must_respond_to(:version_id)
38
+ book.must_respond_to(:name)
39
+ book.must_respond_to(:abbr)
40
+ book.must_respond_to(:ord)
41
+ book.must_respond_to(:book_group_id)
42
+ book.must_respond_to(:testament)
43
+ book.must_respond_to(:id)
44
+ book.must_respond_to(:osis_end)
45
+ book.must_respond_to(:parent)
46
+ book.must_respond_to(:copyright)
47
+ end
48
+ end
49
+ end
50
+
51
+ describe %{when I make a bad request} do
52
+ before do
53
+ @books = @biblesearch.books('SupDawg')
54
+ end
55
+
56
+ it %{has a collection} do
57
+ @books.collection.must_be_instance_of Array
58
+ end
59
+
60
+ it %{contains no items} do
61
+ @books.collection.length.must_equal 0
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ describe %{book} do
68
+ describe %{given a book signature} do
69
+ describe %{as a string} do
70
+ it %{raises an argument error for bad input} do
71
+ bad_book_string = lambda { @biblesearch.book('SupDawg') }
72
+ bad_book_string.must_raise ArgumentError
73
+ (bad_book_string.call rescue $!).message.must_equal 'Book signature must be in the form "VERSION_ID:BOOK_ID"'
74
+ end
75
+ end
76
+
77
+ describe %{as a hash} do
78
+ before do
79
+ @options = {
80
+ :version_id => 'GNT',
81
+ :book_id => '2Tim'
82
+ }
83
+ end
84
+
85
+ describe %{if any pieces are missing} do
86
+ it %{raises an argument error} do
87
+ @options.keys.each do |key|
88
+ options = @options
89
+ options.delete(key)
90
+ bad_book_hash = lambda { @biblesearch.book(options) }
91
+ bad_book_hash.must_raise ArgumentError
92
+ (bad_book_hash.call rescue $!).message.must_equal 'Book signature hash must include :version_id and :book_id'
93
+ end
94
+ end
95
+ end
96
+
97
+ describe %{with a complete hash} do
98
+ it %{returns the same thing as the equivalent string sig} do
99
+ @biblesearch.book(@options).value.must_equal @biblesearch.book('GNT:2Tim').value
100
+ end
101
+ end
102
+ end
103
+
104
+ describe %{when I make a valid request} do
105
+ it %{has a book value} do
106
+ @biblesearch.book('GNT:2Tim').value.tap do |book|
107
+ book.must_respond_to(:version_id)
108
+ book.must_respond_to(:name)
109
+ book.must_respond_to(:abbr)
110
+ book.must_respond_to(:ord)
111
+ book.must_respond_to(:book_group_id)
112
+ book.must_respond_to(:testament)
113
+ book.must_respond_to(:id)
114
+ book.must_respond_to(:osis_end)
115
+ book.must_respond_to(:parent)
116
+ book.must_respond_to(:copyright)
117
+ end
118
+ end
119
+ end
120
+
121
+ describe %{when I request an invalid book} do
122
+ it %{returns nil} do
123
+ @biblesearch.book('GNT:Batman').value.must_be_nil
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,154 @@
1
+ require 'spec_helper'
2
+ require 'biblesearch-api'
3
+
4
+ describe BibleSearch do
5
+ before do
6
+ VCR.insert_cassette %{endpoint-#{File.basename(__FILE__, '.rb')}-#{__name__}}
7
+ @biblesearch = BibleSearch.new('DUMMY_API_KEY')
8
+ end
9
+
10
+ after do
11
+ VCR.eject_cassette
12
+ end
13
+
14
+ describe %{#chapters} do
15
+
16
+ describe %{given a book signature} do
17
+ describe %{as a string} do
18
+ it %{raises an argument error for bad input} do
19
+ bad_book_string = lambda { @biblesearch.chapters('SupDawg') }
20
+ bad_book_string.must_raise ArgumentError
21
+ (bad_book_string.call rescue $!).message.must_equal 'Book signature must be in the form "VERSION_ID:BOOK_ID"'
22
+ end
23
+ end
24
+
25
+ describe %{as a hash} do
26
+ before do
27
+ @options = {
28
+ :version_id => 'GNT',
29
+ :book_id => '2Tim'
30
+ }
31
+ end
32
+
33
+ describe %{if any pieces are missing} do
34
+ it %{raises an argument error} do
35
+ @options.keys.each do |key|
36
+ options = @options
37
+ options.delete(key)
38
+ bad_book_hash = lambda { @biblesearch.chapters(options) }
39
+ bad_book_hash.must_raise ArgumentError
40
+ (bad_book_hash.call rescue $!).message.must_equal "Book signature hash must include :version_id and :book_id"
41
+ end
42
+ end
43
+ end
44
+
45
+ describe %{with a complete hash} do
46
+ it %{returns the same thing as the equivalent string sig} do
47
+ @biblesearch.chapters(@options).collection.must_equal @biblesearch.chapters('GNT:2Tim').collection
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ describe %{when I make a valid request} do
54
+ before do
55
+ @chapters = @biblesearch.chapters('GNT:2Tim')
56
+ end
57
+
58
+ it %{has a collection} do
59
+ @chapters.collection.must_be_instance_of Array
60
+ end
61
+
62
+ it %{should contain chapters} do
63
+ @chapters.collection.length.must_be :>, 0
64
+ @chapters.collection.each do |chapter|
65
+ chapter.must_respond_to(:auditid)
66
+ chapter.must_respond_to(:label)
67
+ chapter.must_respond_to(:chapter)
68
+ chapter.must_respond_to(:id)
69
+ chapter.must_respond_to(:osis_end)
70
+ chapter.must_respond_to(:parent)
71
+ chapter.must_respond_to(:next)
72
+ chapter.must_respond_to(:previous)
73
+ chapter.must_respond_to(:copyright)
74
+ end
75
+ end
76
+ end
77
+
78
+ describe %{when I make a bad request} do
79
+ before do
80
+ @chapters = @biblesearch.chapters('GNT:Batman')
81
+ end
82
+
83
+ it %{has a collection} do
84
+ @chapters.collection.must_be_instance_of Array
85
+ end
86
+
87
+ it %{contains no items} do
88
+ @chapters.collection.length.must_equal 0
89
+ end
90
+ end
91
+ end
92
+
93
+ describe %{#chapter} do
94
+ describe %{given a chapter signature} do
95
+ describe %{as a string} do
96
+ it %{raises an argument error for bad input} do
97
+ bad_chapter_string = lambda { @biblesearch.chapter('SupDawg') }
98
+ bad_chapter_string.must_raise ArgumentError
99
+ (bad_chapter_string.call rescue $!).message.must_equal 'Chapter signature must be in the form "VERSION_ID:BOOK_ID.CHAPTER_NUMBER"'
100
+ end
101
+ end
102
+
103
+ describe %{as a hash} do
104
+ before do
105
+ @options = {
106
+ :version_id => 'GNT',
107
+ :book_id => '2Tim',
108
+ :chapter => 1
109
+ }
110
+ end
111
+
112
+ describe %{if any pieces are missing} do
113
+ it %{raises an argument error} do
114
+ @options.keys.each do |key|
115
+ options = @options
116
+ options.delete(key)
117
+ bad_chapter_hash = lambda { @biblesearch.chapter(options) }
118
+ bad_chapter_hash.must_raise ArgumentError
119
+ (bad_chapter_hash.call rescue $!).message.must_equal "Chapter signature hash must include :version_id, :book_id, and :chapter"
120
+ end
121
+ end
122
+ end
123
+
124
+ describe %{with a complete hash} do
125
+ it %{returns the same thing as the equivalent string sig} do
126
+ @biblesearch.chapter(@options).value.must_equal @biblesearch.chapter('GNT:2Tim.1').value
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ describe %{when I make a valid request} do
133
+ it %{has a chapter value} do
134
+ @biblesearch.chapter('GNT:2Tim.1').value.tap do |chapter|
135
+ chapter.must_respond_to(:auditid)
136
+ chapter.must_respond_to(:label)
137
+ chapter.must_respond_to(:chapter)
138
+ chapter.must_respond_to(:id)
139
+ chapter.must_respond_to(:osis_end)
140
+ chapter.must_respond_to(:parent)
141
+ chapter.must_respond_to(:next)
142
+ chapter.must_respond_to(:previous)
143
+ chapter.must_respond_to(:copyright)
144
+ end
145
+ end
146
+ end
147
+
148
+ describe %{when I request an invalid chapter} do
149
+ it %{has a nil value} do
150
+ @biblesearch.chapter('GNT:Batman.1').value.must_be_nil
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+ require 'biblesearch-api'
3
+
4
+ describe BibleSearch do
5
+ before do
6
+ VCR.insert_cassette %{#{File.basename(__FILE__, '.rb')}-#{__name__}}
7
+ @biblesearch = BibleSearch.new('DUMMY_API_KEY')
8
+ end
9
+
10
+ after do
11
+ VCR.eject_cassette
12
+ end
13
+
14
+ describe %{the Books endpoint} do
15
+ it %{has real fums for all valid requests} do
16
+ @biblesearch.books('GNT').fums.wont_be_nil
17
+ @biblesearch.book('GNT:2Tim').fums.wont_be_nil
18
+ end
19
+
20
+ it %{has nil fums for bad requests} do
21
+ @biblesearch.books('Gotham').fums.must_be_nil
22
+ @biblesearch.book('Gotham:Batman').fums.must_be_nil
23
+ end
24
+ end
25
+
26
+ describe %{the Chapters endpoint} do
27
+ it %{has real fums for all valid requests} do
28
+ @biblesearch.chapters('GNT:2Tim').fums.wont_be_nil
29
+ @biblesearch.chapter('GNT:2Tim.1').fums.wont_be_nil
30
+ end
31
+
32
+ it %{has nil fums for bad requests} do
33
+ @biblesearch.chapters('Gotham:Batman').fums.must_be_nil
34
+ @biblesearch.chapter('Gotham:Bataman.1').fums.must_be_nil
35
+ end
36
+ end
37
+
38
+ describe %{the Passages endpoint} do
39
+ it %{has real fums for all requests} do
40
+ @biblesearch.passages('John 3:16', :version => 'KJV').fums.wont_be_nil
41
+ @biblesearch.passages('Batman 13:17', :version => 'DCU').fums.wont_be_nil
42
+ end
43
+ end
44
+
45
+ describe %{the Search endpoint} do
46
+ it %{has real fums for all requests} do
47
+ @biblesearch.search('john 3:16').fums.wont_be_nil
48
+ @biblesearch.search('batman 13:17').fums.wont_be_nil
49
+ end
50
+ end
51
+
52
+ describe %{the Verses endpoint} do
53
+ it %{has real fums for all valid requests} do
54
+ @biblesearch.verses('KJV:John.3', '16', '16').fums.wont_be_nil
55
+ @biblesearch.verse('GNT:Acts.8.34').fums.wont_be_nil
56
+ end
57
+
58
+ it %{has nil fums for bad requests} do
59
+ @biblesearch.verses('Gotham:Batman', '13', '17').fums.must_be_nil
60
+ @biblesearch.verses('Metropolis:Superman.1.2').fums.must_be_nil
61
+ end
62
+ end
63
+
64
+ describe %{the Versions endpoint} do
65
+ it %{has real fums for all valid requests} do
66
+ @biblesearch.versions.fums.wont_be_nil
67
+ @biblesearch.version('GNT').fums.wont_be_nil
68
+ end
69
+
70
+ it %{has nil fums for bad requests} do
71
+ @biblesearch.version('Gotham').fums.must_be_nil
72
+ end
73
+ end
74
+
75
+ end