Class: Pub
- Inherits:
-
Object
- Object
- Pub
- Defined in:
- lib/thirst/thirst_quencher.rb
Instance Attribute Summary collapse
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .all ⇒ Object
- .find(options = {}) ⇒ Object
- .find_near(location) ⇒ Object
- .get_me_them_pubs ⇒ Object
- .get_xml_from_olframjandet ⇒ Object
- .load_pub_list ⇒ Object
- .location_from_address(address) ⇒ Object
- .location_from_current_ip ⇒ Object
- .one_node_to_pub(landmark) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#latitude ⇒ Object
Returns the value of attribute latitude.
9 10 11 |
# File 'lib/thirst/thirst_quencher.rb', line 9 def latitude @latitude end |
#longitude ⇒ Object
Returns the value of attribute longitude.
9 10 11 |
# File 'lib/thirst/thirst_quencher.rb', line 9 def longitude @longitude end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/thirst/thirst_quencher.rb', line 9 def name @name end |
Class Method Details
.all ⇒ Object
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 = {} if [:address] location = Pub::location_from_address [:address] elsif [:point] location = [:point] else location = Pub::location_from_current_ip end sorted_pubs = Pub::find_near location count = [: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_pubs ⇒ Object
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_olframjandet ⇒ Object
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_list ⇒ Object
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_ip ⇒ Object
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
#location ⇒ Object
11 12 13 |
# File 'lib/thirst/thirst_quencher.rb', line 11 def location [latitude, longitude] end |