congress 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/congress.rb CHANGED
@@ -1,27 +1,27 @@
1
1
  require 'congress/client'
2
2
 
3
3
  module Congress
4
- class << self
5
- attr_accessor :key
6
- def configure
7
- yield self
8
- end
4
+ extend self # rubocop:disable ModuleFunction
5
+ attr_accessor :key
9
6
 
10
- # Alias for Congress::Client.new
11
- #
12
- # @return [Congress::Client]
13
- def new
14
- Congress::Client.new
15
- end
16
-
17
- # Delegate to Congress::Client
18
- def method_missing(method, *args, &block)
19
- return super unless new.respond_to?(method)
20
- new.send(method, *args, &block)
21
- end
7
+ # Alias for Congress::Client.new
8
+ #
9
+ # @return [Congress::Client]
10
+ def new(key = key)
11
+ yield self if block_given?
12
+ return @client if instance_variable_defined?(:@client) && @client.key == key
13
+ @client = Congress::Client.new(key)
14
+ end
22
15
 
23
- def respond_to?(method, include_private=false)
24
- new.respond_to?(method, include_private) || super(method, include_private)
25
- end
16
+ # Delegate to Congress::Client
17
+ def method_missing(method, *args, &block)
18
+ return super unless new.respond_to?(method)
19
+ new.send(method, *args, &block)
26
20
  end
21
+
22
+ # @return [Boolean]
23
+ def respond_to?(method, include_private = false) new.respond_to?(method, include_private) end if RUBY_VERSION < '1.9' # rubocop:disable SingleLineMethods
24
+
25
+ # @return [Boolean]
26
+ def respond_to_missing?(method_name, include_private = false) new.respond_to?(method_name, include_private) end if RUBY_VERSION >= '1.9' # rubocop:disable SingleLineMethods
27
27
  end
@@ -1,194 +1,205 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Congress::Client do
4
+ let(:gmaps_api) { 'http://maps.googleapis.com/maps/api/geocode/json?address=2169%20Mission%20Street,%20San%20Francisco,%20CA%2094110&language=en&sensor=false' }
5
+
4
6
  before do
5
- @client = Congress::Client.new
6
- Congress.configure do |config|
7
- config.key = "abc123"
8
- end
7
+ @client = Congress::Client.new('abc123')
9
8
  end
10
9
 
11
10
  describe '#legislators' do
12
11
  before do
13
- stub_get('/legislators?apikey=abc123').
12
+ stub_get('/legislators').
14
13
  to_return(:status => 200, :body => fixture('legislators.json'))
15
14
  end
16
15
  it "fetches current legislators' names, IDs, biography, and social media" do
17
16
  legislators = @client.legislators
18
- a_get('/legislators?apikey=abc123').
19
- should have_been_made
20
- legislators['count'].should == 539
21
- legislators['results'].first.bioguide_id.should == "K000385"
17
+ expect(a_get('/legislators').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
18
+ expect(legislators['count']).to eq(539)
19
+ expect(legislators['results'].first.bioguide_id).to eq('K000385')
22
20
  end
23
21
  end
24
22
 
25
23
  describe '#legislators_locate' do
26
- context "with a zip code passed" do
24
+ context 'with a zip code passed' do
27
25
  before do
28
- stub_get('/legislators/locate?apikey=abc123&zip=94107').
26
+ stub_get('/legislators/locate?zip=94107').
29
27
  to_return(:status => 200, :body => fixture('legislators_locate.json'))
30
28
  end
31
- it "fetches representatives and senators for a zip code" do
29
+ it 'fetches representatives and senators for a zip code' do
32
30
  legislators_locate = @client.legislators_locate(94107)
33
- a_get('/legislators/locate?apikey=abc123&zip=94107').
34
- should have_been_made
35
- legislators_locate['count'].should == 3
36
- legislators_locate['results'].first.bioguide_id.should == "P000197"
31
+ expect(a_get('/legislators/locate?zip=94107').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
32
+ expect(legislators_locate['count']).to eq(3)
33
+ expect(legislators_locate['results'].first.bioguide_id).to eq('P000197')
37
34
  end
38
35
  end
39
- context "with a latitude and longitude passed" do
36
+ context 'with a latitude and longitude passed' do
40
37
  before do
41
- stub_get('/legislators/locate?apikey=abc123&latitude=37.775&longitude=-122.418').
38
+ stub_get('/legislators/locate?latitude=37.775&longitude=-122.418').
42
39
  to_return(:status => 200, :body => fixture('legislators_locate.json'))
43
40
  end
44
- it "fetches representatives and senators for a latitude/longitude pir" do
41
+ it 'fetches representatives and senators for a latitude/longitude pair' do
45
42
  legislators_locate = @client.legislators_locate(37.775, -122.418)
46
- a_get('/legislators/locate?apikey=abc123&latitude=37.775&longitude=-122.418').
47
- should have_been_made
48
- legislators_locate['count'].should == 3
49
- legislators_locate['results'].first.bioguide_id.should == "P000197"
43
+ expect(a_get('/legislators/locate?latitude=37.775&longitude=-122.418').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
44
+ expect(legislators_locate['count']).to eq(3)
45
+ expect(legislators_locate['results'].first.bioguide_id).to eq('P000197')
46
+ end
47
+ end
48
+ context 'with an address passed' do
49
+ before do
50
+ stub_get('/legislators/locate?latitude=37.775&longitude=-122.418').
51
+ to_return(:status => 200, :body => fixture('legislators_locate.json'))
52
+ stub_request(:get, gmaps_api).to_return(:status => 200, :body => fixture('google_geocoding.json'))
53
+ end
54
+ it 'fetches representatives and senators for an address' do
55
+ legislators_locate = @client.legislators_locate('2169 Mission Street, San Francisco, CA 94110')
56
+ expect(a_request(:get, gmaps_api)).to have_been_made
57
+ expect(a_get('/legislators/locate?latitude=37.775&longitude=-122.418').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
58
+ expect(legislators_locate['count']).to eq(3)
59
+ expect(legislators_locate['results'].first.bioguide_id).to eq('P000197')
50
60
  end
51
61
  end
52
- context "with no arguments passed" do
53
- it "raises an argument error" do
54
- lambda {
55
- @client.legislators_locate
56
- }.should raise_error ArgumentError
62
+ context 'with no arguments passed' do
63
+ it 'raises an argument error' do
64
+ expect { @client.legislators_locate }.to raise_error ArgumentError
57
65
  end
58
66
  end
59
67
  end
60
68
 
61
69
  describe '#districts_locate' do
62
- context "with a zip code passed" do
70
+ context 'with a zip code passed' do
63
71
  before do
64
- stub_get('/districts/locate?apikey=abc123&zip=94107').
72
+ stub_get('/districts/locate?zip=94107').
65
73
  to_return(:status => 200, :body => fixture('districts_locate.json'))
66
74
  end
67
- it "fetches congressional districts for a zip code" do
75
+ it 'fetches congressional districts for a zip code' do
68
76
  districts_locate = @client.districts_locate(94107)
69
- a_get('/districts/locate?apikey=abc123&zip=94107').
70
- should have_been_made
71
- districts_locate['count'].should == 1
72
- districts_locate['results'].first.district.should == 12
77
+ expect(a_get('/districts/locate?zip=94107').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
78
+ expect(districts_locate['count']).to eq(1)
79
+ expect(districts_locate['results'].first.district).to eq(12)
73
80
  end
74
81
  end
75
- context "with a latitude and longitude passed" do
82
+ context 'with a latitude and longitude passed' do
76
83
  before do
77
- stub_get('/districts/locate?apikey=abc123&latitude=37.775&longitude=-122.418').
84
+ stub_get('/districts/locate?latitude=37.775&longitude=-122.418').
78
85
  to_return(:status => 200, :body => fixture('districts_locate.json'))
79
86
  end
80
- it "fetches congressional districts for a latitude/longitude pair" do
87
+ it 'fetches congressional districts for a latitude/longitude pair' do
81
88
  districts_locate = @client.districts_locate(37.775, -122.418)
82
- a_get('/districts/locate?apikey=abc123&latitude=37.775&longitude=-122.418').
83
- should have_been_made
84
- districts_locate['count'].should == 1
85
- districts_locate['results'].first.district.should == 12
89
+ expect(a_get('/districts/locate?latitude=37.775&longitude=-122.418').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
90
+ expect(districts_locate['count']).to eq(1)
91
+ expect(districts_locate['results'].first.district).to eq(12)
92
+ end
93
+ end
94
+ context 'with an address passed' do
95
+ before do
96
+ stub_get('/districts/locate?latitude=37.775&longitude=-122.418').
97
+ to_return(:status => 200, :body => fixture('districts_locate.json'))
98
+ stub_request(:get, gmaps_api).to_return(:status => 200, :body => fixture('google_geocoding.json'))
99
+ end
100
+ it 'fetches congressional districts for an address' do
101
+ districts_locate = @client.districts_locate('2169 Mission Street, San Francisco, CA 94110')
102
+ expect(a_request(:get, gmaps_api)).to have_been_made
103
+ expect(a_get('/districts/locate?latitude=37.775&longitude=-122.418').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
104
+ expect(districts_locate['count']).to eq(1)
105
+ expect(districts_locate['results'].first.district).to eq(12)
86
106
  end
87
107
  end
88
- context "with no arguments passed" do
89
- it "raises an argument error" do
90
- lambda {
91
- @client.districts_locate
92
- }.should raise_error ArgumentError
108
+ context 'with no arguments passed' do
109
+ it 'raises an argument error' do
110
+ expect { @client.districts_locate }.to raise_error ArgumentError
93
111
  end
94
112
  end
95
113
  end
96
114
 
97
115
  describe '#committees' do
98
116
  before do
99
- stub_get('/committees?apikey=abc123').
117
+ stub_get('/committees').
100
118
  to_return(:status => 200, :body => fixture('committees.json'))
101
119
  end
102
- it "fetches current committees, subcommittees, and their membership" do
120
+ it 'fetches current committees, subcommittees, and their membership' do
103
121
  committees = @client.committees
104
- a_get('/committees?apikey=abc123').
105
- should have_been_made
106
- committees['count'].should == 120
107
- committees['results'].first.chamber.should == "senate"
122
+ expect(a_get('/committees').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
123
+ expect(committees['count']).to eq(120)
124
+ expect(committees['results'].first.chamber).to eq('senate')
108
125
  end
109
126
  end
110
127
 
111
128
  describe '#bills' do
112
129
  before do
113
- stub_get('/bills?apikey=abc123').
130
+ stub_get('/bills').
114
131
  to_return(:status => 200, :body => fixture('bills.json'))
115
132
  end
116
- it "fetches legislation in the House and Senate" do
133
+ it 'fetches legislation in the House and Senate' do
117
134
  bills = @client.bills
118
- a_get('/bills?apikey=abc123').
119
- should have_been_made
120
- bills['count'].should == 28614
121
- bills['results'].first.bill_id.should == "s730-113"
135
+ expect(a_get('/bills').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
136
+ expect(bills['count']).to eq(28614)
137
+ expect(bills['results'].first.bill_id).to eq('s730-113')
122
138
  end
123
139
  end
124
140
 
125
141
  describe '#bills_search' do
126
142
  before do
127
- stub_get('/bills/search?apikey=abc123').
143
+ stub_get('/bills/search').
128
144
  to_return(:status => 200, :body => fixture('bills_search.json'))
129
145
  end
130
- it "fetches legislation" do
146
+ it 'fetches legislation' do
131
147
  bills_search = @client.bills_search
132
- a_get('/bills/search?apikey=abc123').
133
- should have_been_made
134
- bills_search['count'].should == 28614
135
- bills_search['results'].first.bill_type.should == "hr"
148
+ expect(a_get('/bills/search').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
149
+ expect(bills_search['count']).to eq(28614)
150
+ expect(bills_search['results'].first.bill_type).to eq('hr')
136
151
  end
137
152
  end
138
153
 
139
154
  describe '#votes' do
140
155
  before do
141
- stub_get('/votes?apikey=abc123').
156
+ stub_get('/votes').
142
157
  to_return(:status => 200, :body => fixture('votes.json'))
143
158
  end
144
- it "fetches roll call votes in Congress" do
159
+ it 'fetches roll call votes in Congress' do
145
160
  votes = @client.votes
146
- a_get('/votes?apikey=abc123').
147
- should have_been_made
148
- votes['count'].should == 4647
149
- votes['results'].first.roll_id.should == "h106-2013"
161
+ expect(a_get('/votes').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
162
+ expect(votes['count']).to eq(4647)
163
+ expect(votes['results'].first.roll_id).to eq('h106-2013')
150
164
  end
151
165
  end
152
166
 
153
167
  describe '#floor_updates' do
154
168
  before do
155
- stub_get('/floor_updates?apikey=abc123').
169
+ stub_get('/floor_updates').
156
170
  to_return(:status => 200, :body => fixture('floor_updates.json'))
157
171
  end
158
- it "fetches to-the-minute updates from the floor of the House and Senate" do
172
+ it 'fetches to-the-minute updates from the floor of the House and Senate' do
159
173
  floor_updates = @client.floor_updates
160
- a_get('/floor_updates?apikey=abc123').
161
- should have_been_made
162
- floor_updates['count'].should == 3066
163
- floor_updates['results'].first.chamber.should == "senate"
174
+ expect(a_get('/floor_updates').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
175
+ expect(floor_updates['count']).to eq(3066)
176
+ expect(floor_updates['results'].first.chamber).to eq('senate')
164
177
  end
165
178
  end
166
179
 
167
180
  describe '#hearings' do
168
181
  before do
169
- stub_get('/hearings?apikey=abc123').
182
+ stub_get('/hearings').
170
183
  to_return(:status => 200, :body => fixture('hearings.json'))
171
184
  end
172
- it "fetches committee hearings in Congress" do
185
+ it 'fetches committee hearings in Congress' do
173
186
  hearings = @client.hearings
174
- a_get('/hearings?apikey=abc123').
175
- should have_been_made
176
- hearings['count'].should == 1279
177
- hearings['results'].first.committee_id.should == "SSFR"
187
+ expect(a_get('/hearings').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
188
+ expect(hearings['count']).to eq(1279)
189
+ expect(hearings['results'].first.committee_id).to eq('SSFR')
178
190
  end
179
191
  end
180
192
 
181
193
  describe '#upcoming_bills' do
182
194
  before do
183
- stub_get('/upcoming_bills?apikey=abc123').
195
+ stub_get('/upcoming_bills').
184
196
  to_return(:status => 200, :body => fixture('upcoming_bills.json'))
185
197
  end
186
- it "fetches bills scheduled for debate in the future, as announced by party leadership" do
198
+ it 'fetches bills scheduled for debate in the future, as announced by party leadership' do
187
199
  upcoming_bills = @client.upcoming_bills
188
- a_get('/upcoming_bills?apikey=abc123').
189
- should have_been_made
190
- upcoming_bills['count'].should == 9
191
- upcoming_bills['results'].first.bill_id.should == "s3457-113"
200
+ expect(a_get('/upcoming_bills').with(:headers => {'X-APIKEY' => 'abc123'})).to have_been_made
201
+ expect(upcoming_bills['count']).to eq(9)
202
+ expect(upcoming_bills['results'].first.bill_id).to eq('s3457-113')
192
203
  end
193
204
  end
194
205
 
@@ -1,18 +1,35 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Congress do
4
- describe ".new" do
5
- it "should return a Congress::Client" do
6
- Congress.new.should be_a Congress::Client
4
+ before do
5
+ Congress.key = 'abc1234'
6
+ end
7
+ describe '.new' do
8
+ it 'returns a Congress::Client' do
9
+ expect(Congress.new).to be_a Congress::Client
10
+ end
11
+ it 'sets key with a blog' do
12
+ Congress.new do |c|
13
+ c.key = 'abc123'
14
+ end
15
+ expect(Congress.key).to eq('abc123')
16
+ end
17
+ context 'with no api key' do
18
+ it 'raises an argument error' do
19
+ expect { Congress.new(nil) }.to raise_error ArgumentError
20
+ end
7
21
  end
8
22
  end
9
23
 
10
- describe ".configure" do
11
- it "should set 'name' and 'pass'" do
12
- Congress.configure do |c|
13
- c.key = "abc123"
14
- end
15
- Congress.key.should == "abc123"
24
+ describe '.method_missing' do
25
+ before do
26
+ stub_get('/legislators').
27
+ to_return(:status => 200, :body => fixture('legislators.json'))
28
+ end
29
+ it 'delegates to an instance of Congress::Client' do
30
+ client = Congress.new
31
+ expect(client).to receive(:legislators)
32
+ Congress.legislators
16
33
  end
17
34
  end
18
35
  end
@@ -0,0 +1 @@
1
+ {"results":[{"address_components":[{"long_name":"2169","short_name":"2169","types":["street_number"]},{"long_name":"Mission Street","short_name":"Mission St","types":["route"]},{"long_name":"Mission District","short_name":"Mission District","types":["neighborhood","political"]},{"long_name":"San Francisco","short_name":"SF","types":["locality","political"]},{"long_name":"San Francisco County","short_name":"San Francisco County","types":["administrative_area_level_2","political"]},{"long_name":"California","short_name":"CA","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},{"long_name":"94110","short_name":"94110","types":["postal_code"]}],"formatted_address":"2169 Mission Street, San Francisco, CA 94110, USA","geometry":{"bounds":{"northeast":{"lat":37.7626032,"lng":-122.4194605},"southwest":{"lat":37.7626021,"lng":-122.4194789}},"location":{"lat":37.775,"lng":-122.418},"location_type":"RANGE_INTERPOLATED","viewport":{"northeast":{"lat":37.7639516302915,"lng":-122.4181207197085},"southwest":{"lat":37.7612536697085,"lng":-122.4208186802915}}},"types":["street_address"]}],"status":"OK"}
data/spec/helper.rb CHANGED
@@ -5,20 +5,30 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
5
  SimpleCov::Formatter::HTMLFormatter,
6
6
  Coveralls::SimpleCov::Formatter
7
7
  ]
8
- SimpleCov.start
8
+
9
+ SimpleCov.start do
10
+ add_filter '/spec/'
11
+ minimum_coverage(100)
12
+ end
9
13
 
10
14
  require 'congress'
11
15
  require 'rspec'
12
16
  require 'webmock/rspec'
13
17
 
18
+ RSpec.configure do |config|
19
+ config.expect_with :rspec do |c|
20
+ c.syntax = :expect
21
+ end
22
+ end
23
+
14
24
  WebMock.disable_net_connect!(:allow => 'coveralls.io')
15
25
 
16
26
  def a_get(path)
17
- a_request(:get, 'http://congress.api.sunlightfoundation.com' + path)
27
+ a_request(:get, Congress::Client::ENDPOINT + path)
18
28
  end
19
29
 
20
30
  def stub_get(path)
21
- stub_request(:get, 'http://congress.api.sunlightfoundation.com' + path)
31
+ stub_request(:get, Congress::Client::ENDPOINT + path)
22
32
  end
23
33
 
24
34
  def fixture_path
data.tar.gz.sig CHANGED
Binary file