sunlight-api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56f8123b9b8d42b35e0b22031b693ec15100c00c
4
- data.tar.gz: 20db42fec5c5cbe1d01ca65416f28a390a9097a7
3
+ metadata.gz: 29f14cd81fbe7f26a12f211ac7b159a6a6f7148a
4
+ data.tar.gz: 1f46624a95211fe0a2909588475a19e6204265fc
5
5
  SHA512:
6
- metadata.gz: 1f0e60b00a35da1c664aff76371064b8a8ea1feed0deb21023e87d5732860206a920cd2b646e5052a54530689540d19c22c647d1a5d96114ba2c6a6f9346d173
7
- data.tar.gz: b3a02752ab6cc2aab829ef4fd7221f8fe02fe49fb831c370bc1d7778a3881be02ec95cc02078defba984783b27234f5ca6c83a9dfb9109ee1df491a433fdb09e
6
+ metadata.gz: 25edabe9e2c5452add7a775ad853a54b9b495a6464bc38748ffbc5629c3983cf933fe460806a8fb73179f3681ca0fd93f2a691b6dc6ebd3a0300b6e158e84976
7
+ data.tar.gz: 60fa01db3e1ae38c62441cfdf265ad035f9f7d54c4b679ab577678a8cd10474cc14f9978c4456f08646cf9fae3944fd56574a1773b71bd77a86dd099aef93833
data/README.md CHANGED
@@ -27,6 +27,8 @@ Sunlight::Congress::Legislator.find_by_state_rank('senior') # All legislators wi
27
27
  ```
28
28
  The ```find``` method accepts either a string or hash of parameters. Strings will only query the first, middle, last, and nicknames of legislators. You can also ```find_by_``` certain attributes. To see a list of accepted attributes (also applies to parameters passed into the ```find``` method), [look here](https://sunlightlabs.github.io/congress/legislators.html)
29
29
 
30
+ By default, results are capped to the first 20 legislators. To overcome this, pass in the ```per_page: 'all'``` option. But keep in mind that this option automatically sets ```in_office: true```. Alternatively, you can pass a ```page: X``` option to get more legislators.
31
+
30
32
  Legislator location and searching methods return an array of ```Legislator``` objects
31
33
  ```ruby
32
34
  legislators = Sunlight::Congress::Legislator.find(gender: 'F', party: 'R')
@@ -34,7 +36,6 @@ legislators #=> [#<Sunlight::Congress::Legislator:0x007f8fb7834b88 @bioguide_id=
34
36
  legislators[0].office #=> "825 B&c Hart Senate Office Building"
35
37
  ```
36
38
 
37
-
38
39
  ## Installation
39
40
 
40
41
  Add this line to your application's Gemfile:
@@ -3,5 +3,29 @@ require 'net/http'
3
3
  module Sunlight
4
4
  class Congress
5
5
  BASE_URI = 'https://congress.api.sunlightfoundation.com'
6
+
7
+ class CongressApi
8
+ class << self
9
+ def encode_uri(location, options)
10
+ uri = URI("#{BASE_URI}/#{location}")
11
+ uri.query = URI.encode_www_form(options.merge(apikey: '6d4acf5a753c40e1824857bb12679d89'))
12
+
13
+ uri
14
+ end
15
+
16
+ def create_from_response(response)
17
+ json = JSON.parse(response.body)
18
+
19
+ if results = json['results']
20
+ results.map do |attributes|
21
+ self.new(attributes)
22
+ end
23
+ else
24
+ json
25
+ end
26
+ end
27
+
28
+ end
29
+ end
6
30
  end
7
31
  end
@@ -0,0 +1,31 @@
1
+ module Sunlight
2
+ class Congress
3
+ class District < CongressApi
4
+
5
+ attr_accessor :state, :district
6
+
7
+ def initialize(options = {})
8
+ options.each do |attribute, value|
9
+ instance_variable_set("@#{attribute}", value)
10
+ end
11
+ end
12
+
13
+ class << self
14
+ def locate(options)
15
+ if options.is_a?(String) || options[:address]
16
+ coords = Geocoder.coordinates(options)
17
+ response = Net::HTTP.get_response(locate_uri(latitude: coords[0], longitude: coords[1]))
18
+ else
19
+ response = Net::HTTP.get_response(locate_uri(options))
20
+ end
21
+
22
+ create_from_response(response)
23
+ end
24
+
25
+ def locate_uri(options = {})
26
+ encode_uri('legislators/locate', options)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -2,7 +2,7 @@ require 'geocoder'
2
2
 
3
3
  module Sunlight
4
4
  class Congress
5
- class Legislator
5
+ class Legislator < CongressApi
6
6
 
7
7
  SEARCH_ATTRIBUTES = [:first_name, :middle_name, :last_name, :name_suffix,
8
8
  :nickname, :state, :state_name, :state_rank, :party,
@@ -44,6 +44,11 @@ module Sunlight
44
44
  encode_uri('legislators/locate', options)
45
45
  end
46
46
 
47
+ def all
48
+ response = Net::HTTP.get_response(find_uri)
49
+ create_from_response(response)
50
+ end
51
+
47
52
  def find(query)
48
53
  if query.is_a?(String)
49
54
  response = Net::HTTP.get_response(find_uri(query: query))
@@ -66,24 +71,6 @@ module Sunlight
66
71
  encode_uri('legislators', options)
67
72
  end
68
73
 
69
- def encode_uri(location, options)
70
- uri = URI("#{BASE_URI}/#{location}")
71
- uri.query = URI.encode_www_form(options.merge(apikey: '6d4acf5a753c40e1824857bb12679d89'))
72
-
73
- uri
74
- end
75
-
76
- def create_from_response(response)
77
- json = JSON.parse(response.body)
78
-
79
- if results = json['results']
80
- results.map do |legislator_attrs|
81
- self.new(legislator_attrs)
82
- end
83
- elsif errors = json['error']
84
- json
85
- end
86
- end
87
74
  end
88
75
  end
89
76
  end
@@ -1,3 +1,3 @@
1
1
  module Sunlight
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunlight-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Moore
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-09 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,6 +67,7 @@ files:
67
67
  - lib/sunlight-api.rb
68
68
  - lib/sunlight/base.rb
69
69
  - lib/sunlight/congress.rb
70
+ - lib/sunlight/congress/district.rb
70
71
  - lib/sunlight/congress/legislator.rb
71
72
  - lib/sunlight/version.rb
72
73
  - sunlight-api.gemspec