Class: Hungry::Geolocation
- Inherits:
-
Object
- Object
- Hungry::Geolocation
- Defined in:
- lib/hungry/geolocation.rb
Instance Attribute Summary collapse
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(latitude, longitude) ⇒ Geolocation
constructor
A new instance of Geolocation.
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(latitude, longitude) ⇒ Geolocation
Returns a new instance of Geolocation.
54 55 56 57 |
# File 'lib/hungry/geolocation.rb', line 54 def initialize(latitude, longitude) self.latitude = latitude.to_f self.longitude = longitude.to_f end |
Instance Attribute Details
#latitude ⇒ Object
Returns the value of attribute latitude.
3 4 5 |
# File 'lib/hungry/geolocation.rb', line 3 def latitude @latitude end |
#longitude ⇒ Object
Returns the value of attribute longitude.
3 4 5 |
# File 'lib/hungry/geolocation.rb', line 3 def longitude @longitude end |
Class Method Details
.parse(input) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/hungry/geolocation.rb', line 5 def self.parse(input) # input is already a Geolocation, so we can return it early: return input if input.is_a?(self) if input.respond_to?(:geolocation) # input has a geolocation attribute, so try to use that one first: geolocation = parse(input.geolocation) return geolocation if geolocation end coordinates = [] if input.respond_to?(:latitude) && input.respond_to?(:longitude) # input has latitude and longitude attributes, so use those: coordinates = [input.latitude, input.longitude] elsif input.respond_to?(:lat) && input.respond_to?(:lng) # input has lat and lng attributes, so use those: coordinates = [input.lat, input.lng] elsif input.respond_to?(:match) # Example: "50.8469397,5.6927505" # # input is a String, so we can use a regular expression to extract # latitude and longitude: if match = input.match(/^([0-9\.]+),\s?([0-9\.]+)$/) coordinates = [match[1], match[2]] end elsif input.respond_to?(:keys) # Example: { latitude: 50.8469397, longitude: 5.6927505 } # # input is a Hash, so we can extract values with the keys: coordinates = [input[:latitude] || input[:lat], input[:longitude] || input[:lng]] elsif input.respond_to?(:[]) # Example: [50.8469397, 5.6927505] # # input is an Array, so we need the first and second value: coordinates = input[0], input[1] end coordinates = coordinates.map(&:presence).compact if coordinates.length == 2 && coordinates.all? { |coordinate| Util.is_numeric?(coordinate) } new(*coordinates) end end |
Instance Method Details
#[](key) ⇒ Object
63 64 65 |
# File 'lib/hungry/geolocation.rb', line 63 def [](key) to_h.send(:[], key) end |
#[]=(key, value) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/hungry/geolocation.rb', line 67 def []=(key, value) case key.to_sym when :latitude self.latitude = value.to_f when :longitude self.longitude = value.to_f end end |
#to_h ⇒ Object
76 77 78 79 80 81 |
# File 'lib/hungry/geolocation.rb', line 76 def to_h { latitude: latitude, longitude: longitude } end |
#to_s ⇒ Object
59 60 61 |
# File 'lib/hungry/geolocation.rb', line 59 def to_s [latitude, longitude].join(',') end |