tapirgo 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.travis.yml +10 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +15 -0
- data/README.md +58 -0
- data/Rakefile +25 -0
- data/lib/tapirgo/errors.rb +33 -0
- data/lib/tapirgo/search/result.rb +70 -0
- data/lib/tapirgo/search.rb +68 -0
- data/lib/tapirgo/version.rb +27 -0
- data/lib/tapirgo.rb +25 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/tapirgo/errors_spec.rb +34 -0
- data/spec/tapirgo/search/result_spec.rb +79 -0
- data/spec/tapirgo/search_spec.rb +107 -0
- data/tapirgo.gemspec +39 -0
- metadata +205 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 130fa081e37fb0cb1ea8a735e2e97564a5dec0a9
|
4
|
+
data.tar.gz: 565f8203f8c2fee22ba0852b23d2fabc870c24b8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 608c5b630cb02eea001e7a7e8f88cfd02b403c0596e9f87e811e519b8e341c9efa8ffe7126443cd9fca4464b7c60a1ab7f151097d22986b4f1baff2ccf1db498
|
7
|
+
data.tar.gz: aa0ab43d95ddd483d38b6a9266cdd8aa63e95e783c4b2b4e22246e2696093cfad3d2831c1a24a024de772ced6cb3c8c0c577d597acb8f795d95d471813f6036b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Author:: Jonathan Hartman (<j@p4nt5.com>)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Jonathan Hartman
|
4
|
+
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
you may not use this file except in compliance with the License.
|
7
|
+
You may obtain a copy of the License at
|
8
|
+
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
See the License for the specific language governing permissions and
|
15
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
[](http://badge.fury.io/rb/tapirgo)
|
2
|
+
[](https://travis-ci.org/RoboticCheese/tapirgo-ruby)
|
3
|
+
[](https://codeclimate.com/github/RoboticCheese/tapirgo-ruby)
|
4
|
+
[](https://coveralls.io/r/RoboticCheese/tapirgo-ruby)
|
5
|
+
[](https://gemnasium.com/RoboticCheese/tapirgo-ruby)
|
6
|
+
|
7
|
+
# Tapirgo
|
8
|
+
|
9
|
+
A simple Ruby library for the [Tapir](http://tapirgo.com/) API.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'tapirgo'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install tapirgo
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Searches
|
28
|
+
|
29
|
+
Create a `Tapirgo::Search` instance with your Tapir API token and desired
|
30
|
+
search string:
|
31
|
+
|
32
|
+
search = Tapirgo::Search.new('XXXXXXXXXXXXXX', 'example')
|
33
|
+
|
34
|
+
Search results can be accessed individually or iterated over:
|
35
|
+
|
36
|
+
first_result = search[0]
|
37
|
+
search.each { |result| puts result }
|
38
|
+
|
39
|
+
Each result has methods for all the data returned by the Tapir API:
|
40
|
+
|
41
|
+
result.title
|
42
|
+
result.score
|
43
|
+
result.link
|
44
|
+
result.content
|
45
|
+
result.summary
|
46
|
+
result.published
|
47
|
+
|
48
|
+
For additional information, see the
|
49
|
+
[Tapir documentation](http://tapirgo.com/#docs).
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it ( http://github.com/RoboticCheese/tapirgo/fork )
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Ensure all tests still pass and that your change is tested (`rake`)
|
56
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
6. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Encoding: UTF-8
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
require 'rubocop/rake_task'
|
6
|
+
require 'cane/rake_task'
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
|
9
|
+
Cane::RakeTask.new
|
10
|
+
|
11
|
+
Rubocop::RakeTask.new do |task|
|
12
|
+
task.patterns = %w{**/*.rb}
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Display LOC stats'
|
16
|
+
task :loc do
|
17
|
+
puts "\n## LOC Stats"
|
18
|
+
sh 'countloc -r lib'
|
19
|
+
end
|
20
|
+
|
21
|
+
RSpec::Core::RakeTask.new(:spec)
|
22
|
+
|
23
|
+
task :default => [ :cane, :rubocop, :loc, :spec ]
|
24
|
+
|
25
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author:: Jonathan Hartman (<j@p4nt5.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014, Jonathan Hartman
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Tapirgo
|
21
|
+
module Errors
|
22
|
+
# Exception class for HTTP Tapir errors
|
23
|
+
#
|
24
|
+
# @author Jonathan Hartman <j@p4nt5.com>
|
25
|
+
class HTTPError < StandardError
|
26
|
+
def initialize(response)
|
27
|
+
super("Request failed with code #{response.code}: #{response.to_s}")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# Encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author:: Jonathan Hartman (<j@p4nt5.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014, Jonathan Hartman
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
require 'json'
|
21
|
+
|
22
|
+
module Tapirgo
|
23
|
+
class Search
|
24
|
+
# Tapir search result class
|
25
|
+
#
|
26
|
+
# @author Jonathan Hartman <j@p4nt5.com>
|
27
|
+
class Result
|
28
|
+
include Errors
|
29
|
+
|
30
|
+
def initialize(result)
|
31
|
+
@json = case result
|
32
|
+
when Hash
|
33
|
+
result
|
34
|
+
when String
|
35
|
+
JSON.parse(result)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def title
|
40
|
+
@title ||= json['title']
|
41
|
+
end
|
42
|
+
|
43
|
+
def score
|
44
|
+
@score ||= json['_score']
|
45
|
+
end
|
46
|
+
|
47
|
+
def link
|
48
|
+
@link ||= json['link']
|
49
|
+
end
|
50
|
+
|
51
|
+
def content
|
52
|
+
@content ||= json['content']
|
53
|
+
end
|
54
|
+
|
55
|
+
def summary
|
56
|
+
@summary ||= json['summary']
|
57
|
+
end
|
58
|
+
|
59
|
+
def published
|
60
|
+
@published ||= json['published_on']
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
attr_accessor :json
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# Encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author:: Jonathan Hartman (<j@p4nt5.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014, Jonathan Hartman
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
require 'rest-client'
|
21
|
+
require 'uri'
|
22
|
+
require 'tapirgo/search/result'
|
23
|
+
|
24
|
+
module Tapirgo
|
25
|
+
# Tapir search class
|
26
|
+
#
|
27
|
+
# @author Jonathan Hartman <j@p4nt5.com>
|
28
|
+
class Search
|
29
|
+
include Enumerable
|
30
|
+
include Tapirgo::Errors
|
31
|
+
|
32
|
+
def initialize(token, query_str)
|
33
|
+
@token = token
|
34
|
+
@query = query_str
|
35
|
+
@results = JSON.parse(get).map { |r| Search::Result.new(r) }
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def [](index)
|
40
|
+
results[index]
|
41
|
+
end
|
42
|
+
|
43
|
+
def each
|
44
|
+
results.each { |r| yield(r) }
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
attr_reader :token
|
50
|
+
attr_reader :results
|
51
|
+
|
52
|
+
def get
|
53
|
+
response = RestClient.get(uri, accept: :json)
|
54
|
+
response.code == 200 || fail(HTTPError, response)
|
55
|
+
response.to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
def uri
|
59
|
+
"http://www.tapirgo.com/api/1/search.json?token=#{token}&query=#{query}"
|
60
|
+
end
|
61
|
+
|
62
|
+
def query
|
63
|
+
URI.escape(@query)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# Encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author:: Jonathan Hartman (<j@p4nt5.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014, Jonathan Hartman
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
# Tapirgo Gem version string
|
21
|
+
#
|
22
|
+
# @author Jonathan Hartman <j@p4nt5.com>
|
23
|
+
module Tapirgo
|
24
|
+
VERSION = '0.1.0'
|
25
|
+
end
|
26
|
+
|
27
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
data/lib/tapirgo.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author:: Jonathan Hartman (<j@p4nt5.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014, Jonathan Hartman
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
require 'tapirgo/errors'
|
21
|
+
require 'tapirgo/search'
|
22
|
+
require 'tapirgo/search/result'
|
23
|
+
require 'tapirgo/version'
|
24
|
+
|
25
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author:: Jonathan Hartman (<j@p4nt5.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014, Jonathan Hartman
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
require 'rspec'
|
21
|
+
require 'simplecov'
|
22
|
+
require 'simplecov-console'
|
23
|
+
require 'coveralls'
|
24
|
+
|
25
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
26
|
+
Coveralls::SimpleCov::Formatter,
|
27
|
+
SimpleCov::Formatter::HTMLFormatter,
|
28
|
+
SimpleCov::Formatter::Console
|
29
|
+
]
|
30
|
+
SimpleCov.minimum_coverage 90
|
31
|
+
SimpleCov.start
|
32
|
+
|
33
|
+
require_relative '../lib/tapirgo'
|
34
|
+
|
35
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Author:: Jonathan Hartman (<j@p4nt5.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014, Jonathan Hartman
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
|
19
|
+
require_relative '../spec_helper'
|
20
|
+
|
21
|
+
describe Tapirgo::Errors::HTTPError do
|
22
|
+
describe '#initialize' do
|
23
|
+
let(:code) { 500 }
|
24
|
+
let(:to_s) { 'OH NOES' }
|
25
|
+
let(:response) { double(code: code, to_s: to_s) }
|
26
|
+
let(:http_error) { Tapirgo::Errors::HTTPError.new(response) }
|
27
|
+
|
28
|
+
it 'generates a correct error string' do
|
29
|
+
expect(http_error.to_s).to eq('Request failed with code 500: OH NOES')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Author:: Jonathan Hartman (<j@p4nt5.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014, Jonathan Hartman
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
|
19
|
+
require_relative '../../spec_helper'
|
20
|
+
require 'json'
|
21
|
+
|
22
|
+
describe Tapirgo::Search::Result do
|
23
|
+
let(:title) { 'Star Wars Ipsum' }
|
24
|
+
let(:score) { nil }
|
25
|
+
let(:link) { '2014-02-22-starwars-test-ipsum.html' }
|
26
|
+
let(:content) { "Lucas ipsum dolor sit amet organa lars kashyyk moff.\n" }
|
27
|
+
let(:summary) { nil }
|
28
|
+
let(:published) { '2014-02-22T02:34:00Z' }
|
29
|
+
let(:input) do
|
30
|
+
"{\"title\":\"#{title}\"," \
|
31
|
+
"\"_score\":#{score || 'null'}," \
|
32
|
+
"\"link\":\"#{link}\"," \
|
33
|
+
"\"content\":\"#{content.gsub("\n", '\\n')}\"," \
|
34
|
+
"\"summary\":#{summary || 'null'}," \
|
35
|
+
"\"published_on\":\"#{published}\"}"
|
36
|
+
end
|
37
|
+
let(:json) { input.class == String ? JSON.parse(input) : input }
|
38
|
+
let(:result) { Tapirgo::Search::Result.new(input) }
|
39
|
+
|
40
|
+
describe '#initialize' do
|
41
|
+
context 'JSON string input' do
|
42
|
+
it 'parses and stores the JSON input' do
|
43
|
+
expect(result.instance_variable_get(:@json)).to eq(json)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'Hash input' do
|
48
|
+
let(:input) do
|
49
|
+
{
|
50
|
+
'title' => title,
|
51
|
+
'_score' => score,
|
52
|
+
'link' => link,
|
53
|
+
'content' => content,
|
54
|
+
'summary' => summary,
|
55
|
+
'published_on' => published
|
56
|
+
}
|
57
|
+
end
|
58
|
+
it 'stores the Hash input' do
|
59
|
+
expect(result.instance_variable_get(:@json)).to eq(input)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
%w{title score link content summary published}.each do |i|
|
65
|
+
describe "##{i}" do
|
66
|
+
it "returns the result #{i}" do
|
67
|
+
expect(result.instance_eval(i)).to eq(instance_eval(i))
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#json' do
|
73
|
+
it 'returns the parsed JSON data' do
|
74
|
+
expect(result.send(:json)).to eq(json)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Author:: Jonathan Hartman (<j@p4nt5.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014, Jonathan Hartman
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
|
19
|
+
require_relative '../spec_helper'
|
20
|
+
require 'json'
|
21
|
+
|
22
|
+
describe Tapirgo::Search do
|
23
|
+
let(:token) { 'xyz123' }
|
24
|
+
let(:query) { 'successful test' }
|
25
|
+
let(:search) { Tapirgo::Search.new(token, query) }
|
26
|
+
let(:code) { 200 }
|
27
|
+
let(:to_s) { '[{"things": [1, 2, 3]}]' }
|
28
|
+
let(:api_get) { double(code: code, to_s: to_s) }
|
29
|
+
let(:json) { JSON.parse(to_s) }
|
30
|
+
|
31
|
+
before(:each) do
|
32
|
+
RestClient.stub(:get).and_return(api_get)
|
33
|
+
Tapirgo::Search::Result.stub(:new) { |r| r }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#initialize' do
|
37
|
+
before(:each) { Tapirgo::Search.stub(:get).and_return(to_s) }
|
38
|
+
|
39
|
+
it 'sets up a token instance variable' do
|
40
|
+
expect(search.instance_variable_get(:@token)).to eq(token)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'sets up a query instance variable' do
|
44
|
+
expect(search.instance_variable_get(:@query)).to eq(query)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'saves the set of search results' do
|
48
|
+
expect(search.instance_variable_get(:@results)).to eq(json)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#[]' do
|
53
|
+
it 'returns the result at the given index' do
|
54
|
+
expect(search[0]).to eq(json[0])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#each' do
|
59
|
+
it 'iterates over the array of search results' do
|
60
|
+
expect(search.each { |r| r }).to eq(json)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#get' do
|
65
|
+
context 'a successful HTTP get' do
|
66
|
+
it 'returns the appropriate JSON result' do
|
67
|
+
expect(search.send(:get)).to eq(to_s)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'a failed HTTP get' do
|
72
|
+
let(:code) { 500 }
|
73
|
+
let(:to_s) { 'Well that did not work' }
|
74
|
+
|
75
|
+
it 'raises an exception' do
|
76
|
+
expect { search.send(:get) }.to raise_error(Tapirgo::Errors::HTTPError)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#uri' do
|
82
|
+
before(:each) do
|
83
|
+
Tapirgo::Search.stub(:query).with(query).and_return('successful%20test')
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'generates the correct URI string' do
|
87
|
+
res = search.send(:uri)
|
88
|
+
expect(res).to eq('http://www.tapirgo.com/api/1/search.json?' \
|
89
|
+
"token=xyz123&query=successful%20test")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '#token' do
|
94
|
+
it 'returns the Search token' do
|
95
|
+
expect(search.send(:token)).to eq(token)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#query' do
|
100
|
+
it 'returns a URI-ized copy of the original query string' do
|
101
|
+
res = search.send(:query)
|
102
|
+
expect(res).to eq('successful%20test')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
data/tapirgo.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Encoding: UTF-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'tapirgo/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'tapirgo'
|
9
|
+
spec.version = Tapirgo::VERSION
|
10
|
+
spec.authors = ['Jonathan Hartman']
|
11
|
+
spec.email = ['j@p4nt5.com']
|
12
|
+
spec.summary = %q{A simple Ruby library for the Tapir API}
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = 'https://github.com/RoboticCheese/tapirgo'
|
15
|
+
spec.license = 'Apache'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'rest-client'
|
23
|
+
|
24
|
+
%w{
|
25
|
+
bundler
|
26
|
+
rake
|
27
|
+
cane
|
28
|
+
countloc
|
29
|
+
rubocop
|
30
|
+
rspec
|
31
|
+
simplecov
|
32
|
+
simplecov-console
|
33
|
+
coveralls
|
34
|
+
}.each do |d|
|
35
|
+
spec.add_development_dependency d
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# vim: ai et ts=2 sts=2 sw=2 ft=ruby
|
metadata
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tapirgo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Hartman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: cane
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: countloc
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov-console
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: coveralls
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: A simple Ruby library for the Tapir API
|
154
|
+
email:
|
155
|
+
- j@p4nt5.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".gitignore"
|
161
|
+
- ".travis.yml"
|
162
|
+
- CHANGELOG.md
|
163
|
+
- Gemfile
|
164
|
+
- LICENSE.txt
|
165
|
+
- README.md
|
166
|
+
- Rakefile
|
167
|
+
- lib/tapirgo.rb
|
168
|
+
- lib/tapirgo/errors.rb
|
169
|
+
- lib/tapirgo/search.rb
|
170
|
+
- lib/tapirgo/search/result.rb
|
171
|
+
- lib/tapirgo/version.rb
|
172
|
+
- spec/spec_helper.rb
|
173
|
+
- spec/tapirgo/errors_spec.rb
|
174
|
+
- spec/tapirgo/search/result_spec.rb
|
175
|
+
- spec/tapirgo/search_spec.rb
|
176
|
+
- tapirgo.gemspec
|
177
|
+
homepage: https://github.com/RoboticCheese/tapirgo
|
178
|
+
licenses:
|
179
|
+
- Apache
|
180
|
+
metadata: {}
|
181
|
+
post_install_message:
|
182
|
+
rdoc_options: []
|
183
|
+
require_paths:
|
184
|
+
- lib
|
185
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
186
|
+
requirements:
|
187
|
+
- - ">="
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: '0'
|
190
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
195
|
+
requirements: []
|
196
|
+
rubyforge_project:
|
197
|
+
rubygems_version: 2.2.2
|
198
|
+
signing_key:
|
199
|
+
specification_version: 4
|
200
|
+
summary: A simple Ruby library for the Tapir API
|
201
|
+
test_files:
|
202
|
+
- spec/spec_helper.rb
|
203
|
+
- spec/tapirgo/errors_spec.rb
|
204
|
+
- spec/tapirgo/search/result_spec.rb
|
205
|
+
- spec/tapirgo/search_spec.rb
|