propublica-nonprofits 0.2.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: 6437649a923bd364755463869cb49f25ae57b91011daaf0cd9db49c409296580
4
- data.tar.gz: faed90b755e0059d4717041570ced6600a88e3b9901303db6596ec221aa45411
3
+ metadata.gz: 5ee18bd7818572112bdf4f432addfdf9b92b4f7b668615aa892f69415a61b6da
4
+ data.tar.gz: 7dedc8936f70be2bbf9895dd55ab7f1fd8a3233b6fe618a588a76f85699344c3
5
5
  SHA512:
6
- metadata.gz: eb357db6551a934f3f8bf1ef40ddbc9b1577129835d16ca258d2c6925107045af42bc6efa6f0c7b8cbb4146d4a10055efbc77deae5bedb86e544500648f7d03a
7
- data.tar.gz: 47c904c59ab652d679b34be83483102509fee8a1344cbb0ec5c83d17f2741b7760bfbac47984fc2b830dedbba73e1b86c42146b1a1f340c870c4f085ba153300
6
+ metadata.gz: b6a531cfd9d39703efcf5c8c9c3a8c53cf91d9f37bf43e7258b7f1bca2f8a9a7cca4792a2b10d460a62c606eba8dac9a3fea53dacd972ec042d20b0df6e7e8d1
7
+ data.tar.gz: 8ee15efdd7e79e57b2860a21ed5dc4d2d2aa9cb067683593e1ed97baafd4320c5f615092f4f8e4b2324273f104dd2bb36de94034b44135a6c3a026a4f6c62cb8
@@ -1,21 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- propublica-nonprofits (0.1.0)
4
+ propublica-nonprofits (0.3.0)
5
5
  faraday
6
6
  json
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- addressable (2.5.2)
12
- public_suffix (>= 2.0.2, < 4.0)
11
+ addressable (2.7.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
13
  coderay (1.1.2)
14
14
  crack (0.4.3)
15
15
  safe_yaml (~> 1.0.0)
16
- faraday (0.15.2)
16
+ faraday (0.15.4)
17
17
  multipart-post (>= 1.2, < 3)
18
- hashdiff (0.3.7)
18
+ hashdiff (1.0.0)
19
19
  json (2.1.0)
20
20
  method_source (0.9.0)
21
21
  minitest (5.11.3)
@@ -25,14 +25,14 @@ GEM
25
25
  pry (0.11.3)
26
26
  coderay (~> 1.1.0)
27
27
  method_source (~> 0.9.0)
28
- public_suffix (3.0.2)
28
+ public_suffix (4.0.1)
29
29
  rake (10.5.0)
30
- safe_yaml (1.0.4)
30
+ safe_yaml (1.0.5)
31
31
  vcr (4.0.0)
32
- webmock (3.4.2)
32
+ webmock (3.7.6)
33
33
  addressable (>= 2.3.6)
34
34
  crack (>= 0.3.2)
35
- hashdiff
35
+ hashdiff (>= 0.4.0, < 2.0.0)
36
36
 
37
37
  PLATFORMS
38
38
  ruby
@@ -48,4 +48,4 @@ DEPENDENCIES
48
48
  webmock
49
49
 
50
50
  BUNDLED WITH
51
- 1.16.5
51
+ 1.17.2
@@ -1,17 +1,46 @@
1
1
  require "propublica/nonprofits/version"
2
2
  require "propublica/nonprofits/organization"
3
3
 
4
- require 'faraday'
5
- require 'json'
4
+ require "faraday"
5
+ require "json"
6
6
 
7
- API_BASE_URL = %(https://projects.propublica.org/nonprofits/api/v2)
7
+ API_BASE_URL = %(https://projects.propublica.org)
8
+ API_SEARCH_PATH = %(/nonprofits/api/v2/search.json)
8
9
 
9
10
  module Propublica
10
11
  module Nonprofits
11
- def self.search(term)
12
- response = Faraday.get("#{API_BASE_URL}/search.json?q='#{term}'")
13
- attributes = JSON.parse(response.body)
14
- organizations = attributes.dig("organizations")
12
+ def self.search(term, state: nil, page: nil, fetch_all: false)
13
+ organizations = []
14
+ more_pages = true
15
+
16
+ page =
17
+ case
18
+ when page
19
+ page
20
+ when fetch_all && page
21
+ raise "Page is set but we are fetching all, chose one or the other"
22
+ else
23
+ 0
24
+ end
25
+
26
+ while (more_pages)
27
+ response = connection.get do |request|
28
+ request.url(API_SEARCH_PATH)
29
+
30
+ request.params["q"] = term
31
+ request.params["state[id]"] = state if state
32
+ request.params["page"] = page if page
33
+ end
34
+
35
+
36
+ attributes = JSON.parse(response.body)
37
+ new_organizations = attributes.fetch("organizations", [])
38
+ organizations.push(*new_organizations)
39
+
40
+ more_pages = fetch_all && new_organizations.any?
41
+ page += 1
42
+ end
43
+
15
44
  organizations.lazy.map do |basic_attrs|
16
45
  Propublica::Nonprofits::Organization.new("basic" => basic_attrs)
17
46
  end
@@ -23,10 +52,14 @@ module Propublica
23
52
  end
24
53
 
25
54
  def self.find_attributes(ein)
26
- response = Faraday.get("#{API_BASE_URL}/organizations/#{ein}.json")
55
+ response = connection.get("/nonprofits/api/v2/organizations/#{ein}.json")
27
56
  JSON.parse(response.body)
28
57
  end
29
58
 
59
+ def self.connection
60
+ @connection ||= Faraday.new(url: API_BASE_URL)
61
+ end
62
+
30
63
  class Error < StandardError; end
31
64
  class DataNotFetched < Error; end
32
65
  end
@@ -1,5 +1,5 @@
1
1
  module Propublica
2
2
  module Nonprofits
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: propublica-nonprofits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricky Chilcott
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-30 00:00:00.000000000 Z
11
+ date: 2019-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -182,8 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
182
  - !ruby/object:Gem::Version
183
183
  version: '0'
184
184
  requirements: []
185
- rubyforge_project:
186
- rubygems_version: 2.7.6
185
+ rubygems_version: 3.0.3
187
186
  signing_key:
188
187
  specification_version: 4
189
188
  summary: Ruby wrapper for the Propublica Nonprofits API https://projects.propublica.org/nonprofits/api/v2