Class: Eaternet::Agencies::Snhd

Inherits:
Object
  • Object
show all
Includes:
Prototype::AbstractAdapter
Defined in:
lib/eaternet/agencies/snhd.rb

Overview

An adapter for the Southern Nevada Health District (Las Vegas).

The SNHD makes their restaurant inspection data available via a zip file for download. The file contains several Mysql tables converted to CSV. And so, this is an example for how to code adapters for other agencies which publish their data in a similar way.

This code downloads the latest zip file, extracts it, parses the csv, and provides enumerators of Businesses, Inspections, Violations, and ViolationKinds. (These are specified in the Eaternet::Prototype module.)

SNHD's CSV format seems to be non-standard, e.g. using the double-quote character in fields without escaping it. We need to find out what their quote character actually is. In the meantime, the files can be parsed by setting the quote character to something else that doesn't appear in the text, such as a pipe symbol.

Examples:

Print the names of all the Las Vegas businesses

require 'eaternet'

agency = Eaternet::Snhd.new
agency.businesses
    .select { |biz| biz.city == 'Las Vegas' }
    .each { |biz| puts biz.name }

See Also:

Instance Method Summary collapse

Instance Method Details

#businessesEnumerable<BusinessData>

Returns:

  • (Enumerable<BusinessData>)

41
42
43
44
45
46
47
48
49
50
51
# File 'lib/eaternet/agencies/snhd.rb', line 41

def businesses
  lazy_csv_map('restaurant_establishments.csv') do |row|
    Eaternet::Prototype::BusinessData.new(
      orig_key: row['permit_number'],
      name: row['restaurant_name'],
      address: row['address'],
      city: row['city_name'],
      zipcode: row['zip_code']
    )
  end
end

#inspectionsEnumerable<InspectionData>

Returns:

  • (Enumerable<InspectionData>)

54
55
56
57
58
59
60
61
62
63
# File 'lib/eaternet/agencies/snhd.rb', line 54

def inspections
  lazy_csv_map('restaurant_inspections.csv') do |row|
    Eaternet::Prototype::InspectionData.new(
      orig_key: row['serial_number'],
      business_orig_key: row['permit_number'],
      score: row['inspection_grade'],
      date: row['inspection_date']
    )
  end
end

#violation_kindsEnumerable<ViolationKindData>

TODO:

Add to AbstractAdapter

Returns:

  • (Enumerable<ViolationKindData>)

78
79
80
81
82
83
84
85
86
87
# File 'lib/eaternet/agencies/snhd.rb', line 78

def violation_kinds
  lazy_csv_map('restaurant_violations.csv') do |row|
    Eaternet::Prototype::ViolationKindData.new(
      orig_key: row['violation_id'],
      code: row['violation_code'],
      demerits: row['violation_demerits'],
      description: row['violation_description']
    )
  end
end

#violationsEnumerable<ViolationData>

Returns:

  • (Enumerable<ViolationData>)

66
67
68
69
70
71
72
73
74
# File 'lib/eaternet/agencies/snhd.rb', line 66

def violations
  lazy_csv_map('restaurant_inspection_violations.csv') do |row|
    Eaternet::Prototype::ViolationData.new(
      orig_key: row['inspection_violation_id'],
      inspection_id: row['inspection_id'],
      violation_kind_id: row['inspection_violation']
    )
  end
end