Class: Location
- Inherits:
-
Object
- Object
- Location
- Defined in:
- lib/flickru/location.rb
Constant Summary collapse
- DEFAULT_ACCURACY =
"street"- COORD_PATTERN =
"-?[0-9]+\.?[0-9]*"
Instance Attribute Summary collapse
-
#accuracy ⇒ Object
Returns the value of attribute accuracy.
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
-
#name ⇒ Object
Returns the value of attribute name.
-
#place ⇒ Object
Returns the value of attribute place.
Class Method Summary collapse
- .geowiki(place) ⇒ Object
- .locate(place) ⇒ Object
- .parse(location) ⇒ Object
- .s_to_accuracy(str) ⇒ Object
Instance Method Summary collapse
-
#initialize(photo) ⇒ Location
constructor
A new instance of Location.
- #nil? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(photo) ⇒ Location
Returns a new instance of Location.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/flickru/location.rb', line 19 def initialize photo dir = File.basename File.dirname(photo) name, place, accuracy = Location.parse dir raise RuntimeError, "#{dir}, name not provided" if name.nil? @name = name begin @place = place.nil? ? name : place @latitude, @longitude = Location.locate @place @accuracy = Location.s_to_accuracy(accuracy ? accuracy : DEFAULT_ACCURACY) rescue RuntimeError raise if place raise RuntimeError, "location #{name} not found" if accuracy @place, @latitude, @longitude, @accuracy = nil # no location end end |
Instance Attribute Details
#accuracy ⇒ Object
Returns the value of attribute accuracy.
17 18 19 |
# File 'lib/flickru/location.rb', line 17 def accuracy @accuracy end |
#latitude ⇒ Object
Returns the value of attribute latitude.
17 18 19 |
# File 'lib/flickru/location.rb', line 17 def latitude @latitude end |
#longitude ⇒ Object
Returns the value of attribute longitude.
17 18 19 |
# File 'lib/flickru/location.rb', line 17 def longitude @longitude end |
#name ⇒ Object
Returns the value of attribute name.
17 18 19 |
# File 'lib/flickru/location.rb', line 17 def name @name end |
#place ⇒ Object
Returns the value of attribute place.
17 18 19 |
# File 'lib/flickru/location.rb', line 17 def place @place end |
Class Method Details
.geowiki(place) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/flickru/location.rb', line 101 def Location.geowiki place wiki = open("http://en.wikipedia.org/wiki/#{URI::encode(place)}").read tool = open(wiki.tr("\"", "\n").split("\n").grep(/geohack/) \ .first.sub(/^\/\//,'http://').sub('&','&')).read geo = tool.split("\n").grep(/<span class="geo"/).first latitude = geo.sub(/^.*"Latitude">/, '').sub(/<\/span>, .*/, '') longitude = geo.sub(/.*"Longitude">/, '').sub(/<\/span>.*$/, '') latitude + ", " + longitude end |
.locate(place) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/flickru/location.rb', line 63 def Location.locate place the_place = place # needed for RuntimeError reporting begin place = Location.geowiki place \ if place !~ /^#{COORD_PATTERN}, *#{COORD_PATTERN}$/ if place =~ /^#{COORD_PATTERN}, *#{COORD_PATTERN}$/ # latitude, longitude [place.sub(/, *#{COORD_PATTERN}$/, ''), place.sub(/^#{COORD_PATTERN}, */, '')] else raise RuntimeError end rescue raise RuntimeError, "location #{the_place} not found" end end |
.parse(location) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/flickru/location.rb', line 51 def Location.parse location # TODO special characters MAY be escaped idx_accuracy = location.index('#') ? location.index('#') : location.length idx_place = location.index('@') ? location.index('@') : idx_accuracy name = location.sub(/[@#].*$/, '').strip | nil place = location.index('@') ? location.sub(/^.*@/, '').sub(/\#.*$/, '').strip : nil accuracy = location.index('#') ? location.sub(/^.*#/, '').strip : nil [name, place, accuracy] end |
.s_to_accuracy(str) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/flickru/location.rb', line 79 def Location.s_to_accuracy str case str.downcase when "world" then 1 when "country" then 3 when "region" then 6 when "city" then 11 when "street" then 16 else raise ArgumentError, "unknown accuracy label '#{str}'" end end |
Instance Method Details
#nil? ⇒ Boolean
35 36 37 38 39 40 41 |
# File 'lib/flickru/location.rb', line 35 def nil? Ruby.assert("@accuracy.nil? implies @latitude.nil? and @longitude.nil?") { @accuracy.nil? or not (@latitude.nil? and @longitude.nil?) } @accuracy.nil? end |
#to_s ⇒ Object
43 44 45 46 47 |
# File 'lib/flickru/location.rb', line 43 def to_s place = @place.nil? ? "an undefined place" : @place "#{@name.black} at #{place.black} (~#{accuracy_to_s.black}) " + "on lat: #{@latitude.black}, lon: #{@longitude.black}" end |