open_civic_data 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/README.md +9 -0
- data/Rakefile +1 -1
- data/lib/open_civic_data.rb +13 -3
- data/lib/open_civic_data/client.rb +34 -1
- data/lib/open_civic_data/connection.rb +2 -2
- data/lib/open_civic_data/request.rb +1 -1
- data/lib/open_civic_data/version.rb +1 -1
- data/spec/helper.rb +3 -3
- data/spec/open_civic_data/client_spec.rb +2 -3
- data/spec/open_civic_data_spec.rb +40 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8bb95989575453bc018f0019271ad66bad07f70f
|
4
|
+
data.tar.gz: 359eeabd885b3f57e9938bd80ad1b4792b0a9e63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b29a0eff891f4ee80172e8e8479ab6bd42ca49f554903abf85f4910c0b415ba6b6f968163cc2315a1fba323754959fc6f7b168387dd43aab7f20c0db85c5987e
|
7
|
+
data.tar.gz: cbf522dbd5e03d454b3c15c1c721f6887fe2cfaa043b9799fab75cadb45b033597a631a78699a0dcfd185f1525de9c12d4ee7bdc76eda9b9239a13ba2b2f1937
|
data/.travis.yml
CHANGED
@@ -12,6 +12,12 @@ rvm:
|
|
12
12
|
- 2.1.0
|
13
13
|
- ruby-head
|
14
14
|
matrix:
|
15
|
+
include:
|
16
|
+
- rvm: jruby-19mode
|
17
|
+
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
|
18
|
+
- rvm: jruby-head
|
19
|
+
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
|
15
20
|
allow_failures:
|
21
|
+
- rvm: jruby-head
|
16
22
|
- rvm: ruby-head
|
17
23
|
fast_finish: true
|
data/README.md
CHANGED
@@ -73,6 +73,13 @@ OpenCivicData.people({'name' => 'Obama'})
|
|
73
73
|
OpenCivicData.votes({'page' => 1 })
|
74
74
|
```
|
75
75
|
|
76
|
+
###### Use with Other APIs
|
77
|
+
```ruby
|
78
|
+
# You can change the end point and disable key if necessary.
|
79
|
+
OpenCivicData.key_required = false
|
80
|
+
OpenCivicData.endpoint = 'http://imago-ca.herokuapp.com/'
|
81
|
+
```
|
82
|
+
|
76
83
|
## Supported Ruby Versions
|
77
84
|
This library aims to support and is [tested against][travis] the following Ruby
|
78
85
|
implementations:
|
@@ -81,6 +88,8 @@ implementations:
|
|
81
88
|
* Ruby 1.9.3
|
82
89
|
* Ruby 2.0.0
|
83
90
|
* Ruby 2.1.0
|
91
|
+
* [JRuby][]
|
92
|
+
[jruby]: http://www.jruby.org/
|
84
93
|
|
85
94
|
If something doesn't work on one of these interpreters, it should be considered
|
86
95
|
a bug.
|
data/Rakefile
CHANGED
data/lib/open_civic_data.rb
CHANGED
@@ -5,7 +5,9 @@ module OpenCivicData
|
|
5
5
|
attr_accessor :key
|
6
6
|
|
7
7
|
# Alias for OpenCivicData::Client.new
|
8
|
-
#
|
8
|
+
# @api public
|
9
|
+
# @example
|
10
|
+
# OpenCivicData.new(SUNLIGHT_KEY)
|
9
11
|
# @return [OpenCivicData::Client]
|
10
12
|
def new(key = key)
|
11
13
|
yield self if block_given?
|
@@ -14,9 +16,17 @@ module OpenCivicData
|
|
14
16
|
end
|
15
17
|
|
16
18
|
# Delegate to OpenCivicData::Client
|
19
|
+
# @api public
|
20
|
+
# @example
|
21
|
+
# OpenCivicData.jurisdictions
|
17
22
|
def method_missing(method, *args, &block)
|
18
|
-
|
19
|
-
|
23
|
+
if OpenCivicData::Client.respond_to?(method)
|
24
|
+
OpenCivicData::Client.send(method, *args, &block)
|
25
|
+
elsif new.respond_to?(method)
|
26
|
+
new.send(method, *args, &block)
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
20
30
|
end
|
21
31
|
|
22
32
|
# return [Boolean]
|
@@ -8,8 +8,11 @@ module OpenCivicData
|
|
8
8
|
|
9
9
|
attr_reader :key
|
10
10
|
|
11
|
+
@@key_required = true # rubocop:disable ClassVars
|
12
|
+
@@endpoint = 'http://api.opencivicdata.org' # rubocop:disable ClassVars
|
13
|
+
|
11
14
|
def initialize(key)
|
12
|
-
fail ArgumentError, 'API key required' if key.nil?
|
15
|
+
fail ArgumentError, 'API key required' if key.nil? && @@key_required
|
13
16
|
@key = key
|
14
17
|
end
|
15
18
|
|
@@ -82,5 +85,35 @@ module OpenCivicData
|
|
82
85
|
def votes(options = {})
|
83
86
|
get('/votes/', options)
|
84
87
|
end
|
88
|
+
|
89
|
+
class << self
|
90
|
+
# Disable ArgumentError if no api key is provided. Useful for using different endpoints.
|
91
|
+
# @param [Boolean]
|
92
|
+
# @api public
|
93
|
+
# @example
|
94
|
+
# OpenCivicData.key_required(false)
|
95
|
+
# OpenCivicData.jurisdictions
|
96
|
+
def key_required=(bool)
|
97
|
+
@@key_required = bool # rubocop:disable ClassVars
|
98
|
+
end
|
99
|
+
|
100
|
+
# Check if key is required.
|
101
|
+
# @return [Boolean]
|
102
|
+
# @api public
|
103
|
+
# @example
|
104
|
+
# if OpenCivicData.key_required
|
105
|
+
# OpenCivicData.key = SUNLIGHT_KEY
|
106
|
+
def key_required
|
107
|
+
@@key_required
|
108
|
+
end
|
109
|
+
|
110
|
+
def endpoint=(endpoint)
|
111
|
+
@@endpoint = endpoint
|
112
|
+
end
|
113
|
+
|
114
|
+
def endpoint
|
115
|
+
@@endpoint
|
116
|
+
end
|
117
|
+
end
|
85
118
|
end
|
86
119
|
end
|
@@ -2,7 +2,7 @@ require 'faraday_middleware'
|
|
2
2
|
|
3
3
|
module OpenCivicData
|
4
4
|
module Connection
|
5
|
-
|
5
|
+
# rubocop:disable EmptyLinesAroundBody
|
6
6
|
|
7
7
|
private
|
8
8
|
|
@@ -11,7 +11,7 @@ module OpenCivicData
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def create_connection
|
14
|
-
Faraday.new(url:
|
14
|
+
Faraday.new(url: OpenCivicData.endpoint) do |connection|
|
15
15
|
middlewares.each { |middleware| connection.use(middleware) }
|
16
16
|
connection.adapter(Faraday.default_adapter)
|
17
17
|
end
|
data/spec/helper.rb
CHANGED
@@ -6,7 +6,7 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
|
6
6
|
|
7
7
|
SimpleCov.start do
|
8
8
|
add_filter '/spec/'
|
9
|
-
minimum_coverage(
|
9
|
+
minimum_coverage(98)
|
10
10
|
end
|
11
11
|
|
12
12
|
require 'open_civic_data'
|
@@ -20,11 +20,11 @@ RSpec.configure do |config|
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def a_get(path)
|
23
|
-
a_request(:get, OpenCivicData
|
23
|
+
a_request(:get, OpenCivicData.endpoint + path)
|
24
24
|
end
|
25
25
|
|
26
26
|
def stub_get(path, options)
|
27
|
-
stub_request(:get, OpenCivicData
|
27
|
+
stub_request(:get, OpenCivicData.endpoint + path).with(query: options)
|
28
28
|
end
|
29
29
|
|
30
30
|
def fixture_path
|
@@ -2,9 +2,8 @@ require 'helper'
|
|
2
2
|
|
3
3
|
describe OpenCivicData::Client do
|
4
4
|
before do
|
5
|
-
|
6
|
-
@
|
7
|
-
@options = {'apikey' => apikey}
|
5
|
+
@client = OpenCivicData::Client.new('abcd1234')
|
6
|
+
@options = {'apikey' => 'abcd1234'}
|
8
7
|
end
|
9
8
|
|
10
9
|
describe '#jurisdictions' do
|
@@ -1,14 +1,12 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
describe OpenCivicData do
|
4
|
-
before do
|
5
|
-
OpenCivicData.key = 'abcd1234'
|
6
|
-
end
|
7
|
-
|
8
4
|
describe '.new' do
|
9
5
|
it 'returns a OpenCivicData::Client' do
|
6
|
+
OpenCivicData.key = 'abcd1234'
|
10
7
|
expect(OpenCivicData.new).to be_a OpenCivicData::Client
|
11
8
|
end
|
9
|
+
# This test should fail even though it doesn't always
|
12
10
|
it 'sets key with a block' do
|
13
11
|
OpenCivicData.new do |ocd|
|
14
12
|
ocd.key = '1234abcd'
|
@@ -17,12 +15,49 @@ describe OpenCivicData do
|
|
17
15
|
end
|
18
16
|
context 'with no api key' do
|
19
17
|
it 'raises an arguement error' do
|
20
|
-
expect
|
18
|
+
expect(OpenCivicData.key_required).to eql(true)
|
19
|
+
expect { OpenCivicData.new(nil) }.to raise_error
|
20
|
+
end
|
21
|
+
describe 'api key is disabled' do
|
22
|
+
before do
|
23
|
+
OpenCivicData.key_required = false
|
24
|
+
end
|
25
|
+
after do
|
26
|
+
OpenCivicData.key_required = true
|
27
|
+
end
|
28
|
+
it 'does not raise an arguement error' do
|
29
|
+
expect(OpenCivicData.key_required).to eql(false)
|
30
|
+
expect { OpenCivicData.new(nil) }.not_to raise_error
|
31
|
+
end
|
21
32
|
end
|
22
33
|
end
|
34
|
+
describe '.endpoint' do
|
35
|
+
it 'has a default endpoint' do
|
36
|
+
expect(OpenCivicData.endpoint).to eq('http://api.opencivicdata.org')
|
37
|
+
end
|
23
38
|
|
39
|
+
describe 'setting a new endpoint' do
|
40
|
+
before do
|
41
|
+
OpenCivicData.key = 'ghapi'
|
42
|
+
OpenCivicData.endpoint = 'http://github.com'
|
43
|
+
stub_request(:get, 'http://github.com/jurisdictions/').
|
44
|
+
with(query: {'apikey' => 'ghapi'}).
|
45
|
+
to_return(status: 200, body: fixture('jurisdictions.json'))
|
46
|
+
end
|
47
|
+
after do
|
48
|
+
OpenCivicData.endpoint = 'http://api.opencivicdata.org'
|
49
|
+
OpenCivicData.key = 'abcd1234'
|
50
|
+
end
|
51
|
+
it 'changes the url queried' do
|
52
|
+
OpenCivicData.jurisdictions
|
53
|
+
expect(a_request(:get, 'http://github.com/jurisdictions/')
|
54
|
+
.with(query: {'apikey' => 'ghapi'})).to have_been_made
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
24
58
|
describe '.method_missing' do
|
25
59
|
before do
|
60
|
+
OpenCivicData.key = 'abcd1234'
|
26
61
|
stub_get('/jurisdictions/', 'apikey' => 'abcd1234').
|
27
62
|
to_return(status: 200, body: fixture('jurisdictions.json'))
|
28
63
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_civic_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sunny Juneja
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|