Class: Pub

Inherits:
Object
  • Object
show all
Defined in:
lib/thirst/thirst_quencher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#latitudeObject

Returns the value of attribute latitude.



9
10
11
# File 'lib/thirst/thirst_quencher.rb', line 9

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



9
10
11
# File 'lib/thirst/thirst_quencher.rb', line 9

def longitude
  @longitude
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/thirst/thirst_quencher.rb', line 9

def name
  @name
end

Class Method Details

.allObject



36
37
38
# File 'lib/thirst/thirst_quencher.rb', line 36

def Pub::all
  Pub::get_me_them_pubs
end

.find(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/thirst/thirst_quencher.rb', line 15

def Pub::find options = {}
  if options[:address]
    location = Pub::location_from_address options[:address]
  elsif options[:point]
    location = options[:point]
  else
    location = Pub::location_from_current_ip
  end
  sorted_pubs = Pub::find_near location
  count = options[:count] || 1
  count == 1 ? sorted_pubs.first : sorted_pubs[0...count]
end

.find_near(location) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/thirst/thirst_quencher.rb', line 28

def Pub::find_near location
  Pub::all.sort do |one, other|
    distance_to_one   = distance_between location, one.location
    distance_to_other = distance_between location, other.location
    distance_to_one <=> distance_to_other
  end
end

.get_me_them_pubsObject



55
56
57
58
59
60
# File 'lib/thirst/thirst_quencher.rb', line 55

def Pub::get_me_them_pubs
  unless defined? @@pubs
    @@pubs = load_pub_list
  end
  @@pubs
end

.get_xml_from_olframjandetObject



68
69
70
71
72
73
# File 'lib/thirst/thirst_quencher.rb', line 68

def Pub::get_xml_from_olframjandet
  agent = Mechanize.new
  agent.get 'http://www.svenskaolframjandet.se' # seems like you need to get a session to get the file
  xml_page = agent.get 'http://www.svenskaolframjandet.se/system/files/Pubs.lmx'
  xml_page.body
end

.load_pub_listObject



62
63
64
65
66
# File 'lib/thirst/thirst_quencher.rb', line 62

def Pub::load_pub_list
  parsed_xml = Nokogiri.parse get_xml_from_olframjandet
  landmark_nodes = parsed_xml.search './/lm:landmark'
  landmark_nodes.map { |landmark| Pub::one_node_to_pub( landmark ) }
end

.location_from_address(address) ⇒ Object



47
48
49
50
# File 'lib/thirst/thirst_quencher.rb', line 47

def Pub::location_from_address address
  point = Geocoder.search( address ).first.data['geometry']['location']
  [point['lat'], point['lng']]
end

.location_from_current_ipObject



40
41
42
43
44
45
# File 'lib/thirst/thirst_quencher.rb', line 40

def Pub::location_from_current_ip
  agent = Mechanize.new
  page = agent.get 'http://freegeoip.net/json/'
  location = JSON.parse page.body
  [location['latitude'], location['longitude']]
end

.one_node_to_pub(landmark) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/thirst/thirst_quencher.rb', line 75

def Pub::one_node_to_pub landmark
    pub = Pub.new
    pub.name =      landmark.search( './/lm:name' )     .inner_text
    pub.latitude  = landmark.search( './/lm:latitude' ) .inner_text.to_f
    pub.longitude = landmark.search( './/lm:longitude' ).inner_text.to_f
    pub
end

Instance Method Details

#locationObject



11
12
13
# File 'lib/thirst/thirst_quencher.rb', line 11

def location
  [latitude, longitude]
end