class MeetingFinder::Search

Public Class Methods

by_lat_lng(lat, lng) click to toggle source
# File lib/meeting_finder/search.rb, line 45
def by_lat_lng(lat, lng)
  find_by({ "latitude" => lat, "longitude" => lng })
end
by_zip(zip) click to toggle source
# File lib/meeting_finder/search.rb, line 49
def by_zip(zip)
  lat, lng = find_lat_long_from(zip)
  find_by({ "zip" => zip, "latitude" => lat, "longitude" => lng })
end
find_by(values={}) click to toggle source
# File lib/meeting_finder/search.rb, line 10
def find_by(values={})
  search_string = values.map do |parameter, value|
    "&#{parameter}=#{value}"
  end.join

  response = Nokogiri::HTML(open(search_url + search_string))
  meetings = []
  response.css('.all-meetings tr').each_with_object({}).with_index do |(meeting, attributes), i|
    unless i == 0
      attributes['name'] = meeting.children[0].text
      attributes['location'] = meeting.children[2].text
      attributes['address'] = meeting.children[4].text
      attributes['day'] = meeting.children[6].text
      attributes['time'] = meeting.children[8].text
      attributes['fellowship'] = meeting.children[10].text
      # attributes['lat'], attributes['lng'] = find_lat_long_from(attributes['address'])
      meetings << MeetingFinder::Meeting.new(attributes)
    end
  end
  meetings.shift
  meetings
end
find_lat_long_from(address) click to toggle source
# File lib/meeting_finder/search.rb, line 33
def find_lat_long_from(address)
  result = Faraday.get("https://maps.googleapis.com/maps/api/geocode/json?address=#{address}&sensor=false")
  coords = JSON.parse(result.body)
  if coords && !coords['results'].empty?
    lat = coords["results"].first["geometry"]["location"]["lat"]
    lng = coords["results"].first["geometry"]["location"]["lng"]
  else
    lat, lng = 39.7316982, -104.9213643
  end
  [lat, lng]
end
search_url() click to toggle source
# File lib/meeting_finder/search.rb, line 54
def search_url
  'http://meetings.intherooms.com/meetings/search?search=&fellowship=AA&proximity=5'
end