module Eaternet::Agencies::MultcoLib::Misc

Public Class Methods

better_strip(a_string) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 96
def self.better_strip(a_string)
  a_string.sub(/\p{Blank}+\z/, '').sub(/^\p{Blank}+/, '').strip
end
blacklisted?(name:) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 7
def self.blacklisted?(name:)
  return true if name =~ /Chartwells/      # Apparently bad data
  return true if name =~ /Doubletree/      # Apparently bad data
  return true if name =~ /Providence Park/ # Yelp's model mismatch
  false
end
clean_inner_text(elem) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 88
def self.clean_inner_text(elem)
  elem.nil? ? '' : better_strip(elem.inner_text)
end
courtesy_delay() click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 100
def self.courtesy_delay
  # sleep 0.25
end
inspection_pages(business_id) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 23
def self.inspection_pages(business_id)
  results = []
  page = latest_inspection_page(business_id)
  loop do
    results << page
    form = page.forms.first
    button = form.button_with(id: 'NextInspection')
    break if button.nil?
    courtesy_delay
    page = form.submit(button)
  end
  results
end
latest_inspection_page(id) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 37
def self.latest_inspection_page(id)
  agent  = Mechanize.new
  courtesy_delay
  url = "http://www3.multco.us/MCHealthInspect/ListSearch.aspx?id=#{id}"
  agent.get(url)
  agent.page
end
parse_inspection(page:) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 45
def self.parse_inspection(page:)
  result = {}
  cells = page.search('td.DetailsTD')
  return nil if cells.size == 0
  result['inspection_number'] = cells[0].inner_text.gsub(/[^0-9]/, '')
  date_text = cells[2].inner_text.strip.gsub(/[^0-9\/]/, '')
  date_text =~ %r{(\d\d)/(\d\d)/(\d\d\d\d)}
  result['date'] = Date.parse "#{Regexp.last_match(3)}-#{Regexp.last_match(1)}-#{Regexp.last_match(2)}"
  result['score'] = cells[3].inner_text.gsub(/[^0-9]/, '').to_i
  return nil if result['score'] == 0
  result
end
parse_violation_row(tr) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 72
def self.parse_violation_row(tr)
  cells = tr.search('td.DetailsTD')
  result = {
    code_number: clean_inner_text(cells[0]),
    facts: clean_inner_text(cells[2]),
    code_description: clean_inner_text(cells[1])
  }
  valid_result?(parsed_data: result) ? result : nil
end
parse_violations(page:) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 58
def self.parse_violations(page:)
  next_is_a_violation = false
  page.search('tr').map do |tr|
    if tr.inner_text =~ /Law\/Rule/
      next_is_a_violation = true
      next
    end
    next unless next_is_a_violation
    next_is_a_violation = false

    parse_violation_row(tr)
  end.compact
end
parse_violations_from_html(a_string) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 14
def self.parse_violations_from_html(a_string)
  page = Mechanize::Page.new(nil,
                             { 'content-type' => 'text/html' },
                             a_string,
                             nil,
                             Mechanize.new)
  parse_violations(page: page)
end
valid_code_number?(n) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 92
def self.valid_code_number?(n)
  !(n =~ /^(99.*|98[A-Z])$/)
end
valid_result?(parsed_data:) click to toggle source
# File lib/eaternet/agencies/multco_lib/misc.rb, line 82
def self.valid_result?(parsed_data:)
  valid_code_number?(parsed_data[:code_number]) &&
    !parsed_data[:facts].start_with?('INFORMATIONAL') &&
    !parsed_data.values.reduce(:+).blank?
end