Class: Location

Inherits:
Object
  • Object
show all
Defined in:
lib/flickru/location.rb

Constant Summary collapse

DEFAULT_ACCURACY =
"street"
COORD_PATTERN =
"-?[0-9]+\.?[0-9]*"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(photo) ⇒ Location

Returns a new instance of Location.

Raises:

  • (RuntimeError)


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

#accuracyObject

Returns the value of attribute accuracy.



17
18
19
# File 'lib/flickru/location.rb', line 17

def accuracy
  @accuracy
end

#latitudeObject

Returns the value of attribute latitude.



17
18
19
# File 'lib/flickru/location.rb', line 17

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



17
18
19
# File 'lib/flickru/location.rb', line 17

def longitude
  @longitude
end

#nameObject

Returns the value of attribute name.



17
18
19
# File 'lib/flickru/location.rb', line 17

def name
  @name
end

#placeObject

Returns the value of attribute place.



17
18
19
# File 'lib/flickru/location.rb', line 17

def place
  @place
end

Instance Method Details

#nil?Boolean

Returns:

  • (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_sObject



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