Class: Eaternet::Agencies::Austin

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

Overview

Class Method Summary collapse

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) ⇒ Austin

Create an Austin data-source, ready for querying.

Examples:

austin = Eaternet::Austin.new

Parameters:

  • csv_path (String) (defaults to: nil)

    for unit testing



18
19
20
# File 'lib/eaternet/agencies/austin.rb', line 18

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

Class Method Details

.csv_urlObject



102
103
104
# File 'lib/eaternet/agencies/austin.rb', line 102

def self.csv_url
  Eaternet::Socrata.csv_url domain: 'data.austintexas.gov', dataset: 'ecmv-9xxi'
end

.download_via_urlObject



98
99
100
# File 'lib/eaternet/agencies/austin.rb', line 98

def self.download_via_url
  Eaternet::Util.download_and_cache(source: csv_url, dest: Tempfile.new('austin'))
end

Instance Method Details

#adapter_nameObject



106
107
108
# File 'lib/eaternet/agencies/austin.rb', line 106

def adapter_name
  'Austin'
end

#business(row) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/eaternet/agencies/austin.rb', line 64

def business(row)
  Eaternet::Lives_1_0::Business.new do |b|
    fail ArgumentError, 'No address found' if row['Address'].nil?

    # Address is a multi-line, multi-value cell
    address_lines = row['Address'].split("\n")
    if address_lines.size != 3
      fail ArgumentError,
           "Address doesn't have three lines: {row['Address']}"
    end

    address = address_lines.first
    city    = address_lines[1].split(',').first
    address_lines[2] =~ /^\(([^,]+), (.+)\)$/
    lat = Regexp.last_match(1)
    lon = Regexp.last_match(2)

    b.business_id = row['Facility ID']
    b.name =        row['Restaurant Name']
    b.address =     address
    b.city =        city
    b.postal_code = row['Zip Code']
    b.state       = 'TX'
    b.latitude    = lat
    b.longitude   = lon
  end
end

#businessesEnumerable<Business>

Examples:

Print the number of restaurants in the city.

puts austin.businesses.count

Returns:

  • (Enumerable<Business>)


26
27
28
29
30
# File 'lib/eaternet/agencies/austin.rb', line 26

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

#feed_infoObject



45
46
47
48
49
50
51
52
# File 'lib/eaternet/agencies/austin.rb', line 45

def feed_info
  Eaternet::Lives_1_0::FeedInfo.new do |fi|
    fi.feed_date = Date.today
    fi.feed_version = '1.0'
    fi.municipality_name = 'Austin'
    fi.municipality_url = 'http://www.austintexas.gov/department/health'
  end
end

#inspection(row) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/eaternet/agencies/austin.rb', line 56

def inspection(row)
  Eaternet::Lives_1_0::Inspection.new do |i|
    i.business_id = row['Facility ID']
    i.date =        Date.strptime(row['Inspection Date'], '%m/%d/%Y')
    i.score =       row['Score'].to_i
  end
end

#inspectionsObject



32
33
34
35
# File 'lib/eaternet/agencies/austin.rb', line 32

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

#legendsObject



41
42
43
# File 'lib/eaternet/agencies/austin.rb', line 41

def legends
  fail Eaternet::UnsupportedError, 'Austin does not assign letter grades'
end

#table_fileObject



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

def table_file
  @table_file ||= Austin.download_via_url
end

#violationsObject



37
38
39
# File 'lib/eaternet/agencies/austin.rb', line 37

def violations
  fail Eaternet::UnsupportedError, "Austin doesn't publish violation data"
end