Class: Eaternet::Agencies::Nyc

Inherits:
Lives_1_0::Adapter show all
Includes:
Lives_1_0::CsvParser, Loggable
Defined in:
lib/eaternet/agencies/nyc.rb

Overview

A data source for New York City food service health inspections. It retrieves the latest CSV export from the official source and makes it easy to work with. See our NYC wiki page for details about the data set.

Output is produced in the LIVES 1.0 format developed by Yelp and the cities of San Francisco and New York.

The downloaded CSV is cached for twelve hours in a temporary file.

Examples:

Print the names of all the restaurants in New York City

require 'eaternet'
nyc = Eaternet::Nyc.new
puts nyc.businesses.map(&:name)

Compute the average inspection score

# The library is optimized for memory use at the expense
# of speed. E.g., each call to #inspections will iterate
# through the raw CSV. So here, we first retrieve the
# inspections into an array.
inspections = nyc.inspections.to_a
sum = inspections
        .map(&:score)
        .reduce(0, :+)
count = inspections.count

puts "Average inspection score: #{sum / count}"

See the Instance Method Details for more examples.

See Also:

Instance Method Summary collapse

Methods included from Lives_1_0::CsvParser

#convert, #csv_map, #csv_rows, #map_csv, #try_to_create, #zip_dir, #zip_file_url

Methods included from Loggable

#logger

Constructor Details

#initialize(csv_path: nil) ⇒ Nyc

Create an NYC data-source, ready for querying.

Examples:

nyc = Eaternet::Nyc.new

Parameters:

  • csv_path (String) (defaults to: nil)

    for unit testing



59
60
61
# File 'lib/eaternet/agencies/nyc.rb', line 59

def initialize(csv_path: nil)
  @fixture_table_file = csv_path
end

Instance Method Details

#businessesEnumerable<Business>

Examples:

Print the number of restaurants in New York.

puts nyc.businesses.count

Returns:

  • (Enumerable<Business>)


67
68
69
70
71
# File 'lib/eaternet/agencies/nyc.rb', line 67

def businesses
  map_csv { |row| try_to_create(:business, from_csv_row: row) }
    .uniq
    .compact
end

#feed_infoFeedInfo

Examples:

Print the name & URL of NYC's health agency.

puts nyc.feed_info.municipality_name
puts nyc.feed_info.municipality_url

Returns:

  • (FeedInfo)


104
105
106
107
108
109
110
111
112
# File 'lib/eaternet/agencies/nyc.rb', line 104

def feed_info
  # Anyone know a contact email?
  Eaternet::Lives_1_0::FeedInfo.new do |fi|
    fi.feed_date = Date.today
    fi.feed_version = '1.0'
    fi.municipality_name = 'New York City'
    fi.municipality_url = 'http://www.nyc.gov/html/doh/html/services/restaurant-inspection.shtml'
  end
end

#inspectionsEnumerable<Inspection>

Examples:

Compute the average inspection score for NYC.

# The library is optimized for memory use at the expense
# of speed. E.g., each call to #inspections will iterate
# through the raw CSV. So here, we first retrieve the
# inspections into an array.
inspections = nyc.inspections.to_a
sum = inspections
        .map(&:score)
        .reduce(0, :+)
count = inspections.count

puts "Average inspection score: #{sum / count}"

Returns:

  • (Enumerable<Inspection>)


87
88
89
90
91
# File 'lib/eaternet/agencies/nyc.rb', line 87

def inspections
  map_csv { |row| skip_inspection?(row) ? nil : try_to_create(:inspection, from_csv_row: row) }
    .uniq
    .compact
end

#legendsEnumerable<Legend>

Returns:

  • (Enumerable<Legend>)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/eaternet/agencies/nyc.rb', line 115

def legends
  Eaternet::Lives_1_0::LegendGroup.new do |lg|
    lg.legends = [
      Eaternet::Lives_1_0::Legend.new do |l|
        l.minimum_score = 87
        l.maximum_score = 100
        l.description =   'A'
      end,
      Eaternet::Lives_1_0::Legend.new do |l|
        l.minimum_score = 73
        l.maximum_score = 86
        l.description =   'B'
      end,
      Eaternet::Lives_1_0::Legend.new do |l|
        l.minimum_score = 0
        l.maximum_score = 72
        l.description =   'C'
      end
    ]
  end.legends
end

#violationsEnumerable<Violation>

Returns:

  • (Enumerable<Violation>)


94
95
96
97
# File 'lib/eaternet/agencies/nyc.rb', line 94

def violations
  map_csv { |row| skip_violation?(row) ? nil : violation(row) }
    .compact
end