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, Organization

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.find(ein) ⇒ Object



79
80
81
82
# File 'lib/propublica/nonprofits.rb', line 79

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

.find_attributes(ein) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/propublica/nonprofits.rb', line 84

def self.find_attributes(ein)
  response = Faraday.get("#{API_BASE_URL}/nonprofits/api/v2/organizations/#{ein}.json")
  if response.body.is_a?(Hash)
    response.body
  else
    begin
      JSON.parse(response.body)
    rescue JSON::ParserError => e
      raise JSON::ParserError.new("Propublica API Parsing Error: #{e.message}")
    end
  end
end

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



32
33
34
35
36
# File 'lib/propublica/nonprofits.rb', line 32

def self.lazy_search(term, state: nil, ntee: nil, page: nil, fetch_all: false)
  search_results(term, state: state, ntee: ntee, page: page, fetch_all: fetch_all)
    .lazy
    .flat_map(&:itself)
end

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



27
28
29
30
# File 'lib/propublica/nonprofits.rb', line 27

def self.search(term, state: nil, ntee: nil, page: nil, fetch_all: false)
  search_results(term, state: state, ntee: ntee, page: page, fetch_all: fetch_all)
    .flat_map(&:itself)
end

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

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/propublica/nonprofits.rb', line 38

def self.search_results(term, state: nil, ntee: nil, page: nil, fetch_all: false)
  raise ArgumentError.new("`page` and `fetch_all` are both: choose one or the other") if fetch_all && page
  page ||= 0
  max_pages = nil

  Enumerator.new do |yielder|
    loop do
      params = {}
      params["q"] = term
      params["state[id]"] = state if state
      params["ntee[id]"] = ntee if ntee
      params["page"] = page if page

      response = Faraday.default_connection.get("#{API_BASE_URL}/#{API_SEARCH_PATH}", params)
      parsed_response =
        if response.body.is_a?(Hash)
          response.body
        else
          begin
            JSON.parse(response.body)
          rescue JSON::ParserError => e
            raise JSON::ParserError.new("Propublica API Parsing Error: #{e.message}")
          end
        end

      max_pages = parsed_response.dig("num_pages") || max_pages

      yielder <<
        parsed_response
          .fetch("organizations", [])
          .map { |basic_attrs| Propublica::Nonprofits::Organization.new("basic" => basic_attrs) }

      if fetch_all && page + 1 < max_pages
        page += 1
      else
        raise(StopIteration)
      end
    end
  end
end