Module: Propublica::Nonprofits

Defined in:
lib/propublica/nonprofits.rb,
lib/propublica/nonprofits/version.rb,
lib/propublica/nonprofits/organization.rb,
lib/propublica/nonprofits/organization/basic_parser.rb,
lib/propublica/nonprofits/organization/details_parser.rb,
lib/propublica/nonprofits/organization/dynamic_parser.rb,
lib/propublica/nonprofits/organization/filings_with_data_parser.rb,
lib/propublica/nonprofits/organization/filings_without_data_parser.rb

Defined Under Namespace

Classes: DataNotFetched, Error, Organization

Constant Summary collapse

VERSION =
"0.4.1"

Class Method Summary collapse

Class Method Details

.connectionObject



59
60
61
# File 'lib/propublica/nonprofits.rb', line 59

def self.connection
  @connection ||= Faraday.new(url: API_BASE_URL)
end

.find(ein) ⇒ Object



49
50
51
52
# File 'lib/propublica/nonprofits.rb', line 49

def self.find(ein)
  attributes = self.find_attributes(ein)
  Propublica::Nonprofits::Organization.new(attributes)
end

.find_attributes(ein) ⇒ Object



54
55
56
57
# File 'lib/propublica/nonprofits.rb', line 54

def self.find_attributes(ein)
  response = connection.get("/nonprofits/api/v2/organizations/#{ein}.json")
  JSON.parse(response.body)
end

.search(term, state: nil, page: nil, fetch_all: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/propublica/nonprofits.rb', line 12

def self.search(term, state: nil, page: nil, fetch_all: false)
  organizations = []
  more_pages = true

  page =
    case
    when page
      page
    when fetch_all && page
      raise "Page is set but we are fetching all, chose one or the other"
    else
      0
    end

  while (more_pages)
    response = connection.get do |request|
      request.url(API_SEARCH_PATH)

      request.params["q"] = term
      request.params["state[id]"] = state if state
      request.params["page"] = page if page
    end
    

    attributes = JSON.parse(response.body)
    new_organizations = attributes.fetch("organizations", [])
    organizations.push(*new_organizations)

    more_pages = fetch_all && new_organizations.any?
    page += 1
  end

  organizations.lazy.map do |basic_attrs|
    Propublica::Nonprofits::Organization.new("basic" => basic_attrs)
  end
end