Class: MeetingFinder::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/meeting_finder/search.rb

Class Method Summary collapse

Class Method Details

.by_lat_lng(lat, lng) ⇒ Object



45
46
47
# File 'lib/meeting_finder/search.rb', line 45

def by_lat_lng(lat, lng)
  find_by({ "latitude" => lat, "longitude" => lng })
end

.by_zip(zip) ⇒ Object



49
50
51
52
# 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 = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# 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_urlObject



54
55
56
# File 'lib/meeting_finder/search.rb', line 54

def search_url
  'http://meetings.intherooms.com/meetings/search?search=&fellowship=AA&proximity=5'
end