Class: Graticule::Location
- Inherits:
-
Object
- Object
- Graticule::Location
- Defined in:
- lib/graticule/location.rb
Overview
A geographic location
Instance Attribute Summary collapse
-
#country ⇒ Object
Returns the value of attribute country.
-
#geocoder ⇒ Object
Returns the value of attribute geocoder.
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#locality ⇒ Object
(also: #city)
Returns the value of attribute locality.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
-
#postal_code ⇒ Object
(also: #zip)
Returns the value of attribute postal_code.
-
#precision ⇒ Object
Returns the value of attribute precision.
-
#premise ⇒ Object
Returns the value of attribute premise.
-
#region ⇒ Object
(also: #state)
Returns the value of attribute region.
-
#street ⇒ Object
Returns the value of attribute street.
-
#warning ⇒ Object
Returns the value of attribute warning.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#antipode ⇒ Object
(also: #antipodal_location)
Where would I be if I dug through the center of the earth?.
- #attributes ⇒ Object
-
#coordinates ⇒ Object
Returns an Array with latitude and longitude.
-
#distance_to(destination, options = {}) ⇒ Object
Calculate the distance to another location.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(attrs = {}) ⇒ Location
constructor
A new instance of Location.
- #normalized_country ⇒ Object
- #normalized_locality ⇒ Object
- #normalized_premise ⇒ Object
- #normalized_region ⇒ Object
- #normalized_street ⇒ Object
- #postal_code_base ⇒ Object
- #to_csv(options = {}) ⇒ Object
- #to_hash(options = {}) ⇒ Object
- #to_json(options = {}) ⇒ Object
- #to_s(options = {}) ⇒ Object
- #to_xml(options = {}) ⇒ Object
Constructor Details
Instance Attribute Details
#country ⇒ Object
Returns the value of attribute country.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def country @country end |
#geocoder ⇒ Object
Returns the value of attribute geocoder.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def geocoder @geocoder end |
#latitude ⇒ Object
Returns the value of attribute latitude.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def latitude @latitude end |
#locality ⇒ Object Also known as: city
Returns the value of attribute locality.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def locality @locality end |
#longitude ⇒ Object
Returns the value of attribute longitude.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def longitude @longitude end |
#postal_code ⇒ Object Also known as: zip
Returns the value of attribute postal_code.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def postal_code @postal_code end |
#precision ⇒ Object
Returns the value of attribute precision.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def precision @precision end |
#premise ⇒ Object
Returns the value of attribute premise.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def premise @premise end |
#region ⇒ Object Also known as: state
Returns the value of attribute region.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def region @region end |
#street ⇒ Object
Returns the value of attribute street.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def street @street end |
#warning ⇒ Object
Returns the value of attribute warning.
7 8 9 |
# File 'lib/graticule/location.rb', line 7 def warning @warning end |
Instance Method Details
#==(other) ⇒ Object
64 65 66 |
# File 'lib/graticule/location.rb', line 64 def ==(other) other.respond_to?(:attributes) ? attributes == other.attributes : false end |
#antipode ⇒ Object Also known as: antipodal_location
Where would I be if I dug through the center of the earth?
84 85 86 |
# File 'lib/graticule/location.rb', line 84 def antipode Location.new :latitude => -latitude, :longitude => longitude + (longitude >= 0 ? -180 : 180) end |
#attributes ⇒ Object
19 20 21 22 23 24 |
# File 'lib/graticule/location.rb', line 19 def attributes [:latitude, :longitude, :street, :locality, :region, :postal_code, :country, :precision].inject({}) do |result,attr| result[attr] = self.send(attr) unless self.send(attr).blank? result end end |
#coordinates ⇒ Object
Returns an Array with latitude and longitude.
60 61 62 |
# File 'lib/graticule/location.rb', line 60 def coordinates [latitude, longitude] end |
#distance_to(destination, options = {}) ⇒ Object
Calculate the distance to another location. See the various Distance formulas for more information
78 79 80 81 |
# File 'lib/graticule/location.rb', line 78 def distance_to(destination, = {}) = {:formula => :haversine, :units => :miles}.merge() "Graticule::Distance::#{[:formula].to_s.titleize}".constantize.distance(self, destination, [:units]) end |
#eql?(other) ⇒ Boolean
68 69 70 |
# File 'lib/graticule/location.rb', line 68 def eql?(other) self == other end |
#hash ⇒ Object
72 73 74 |
# File 'lib/graticule/location.rb', line 72 def hash attributes.to_s.hash end |
#normalized_country ⇒ Object
26 27 28 |
# File 'lib/graticule/location.rb', line 26 def normalized_country normalize_string(country) end |
#normalized_locality ⇒ Object
34 35 36 |
# File 'lib/graticule/location.rb', line 34 def normalized_locality normalize_string(normalize_abbreviations(locality)) end |
#normalized_premise ⇒ Object
42 43 44 |
# File 'lib/graticule/location.rb', line 42 def normalized_premise normalize_string(normalize_abbreviations(premise)) end |
#normalized_region ⇒ Object
30 31 32 |
# File 'lib/graticule/location.rb', line 30 def normalized_region normalize_string(region) end |
#normalized_street ⇒ Object
38 39 40 |
# File 'lib/graticule/location.rb', line 38 def normalized_street normalize_string(normalize_abbreviations(street)) end |
#postal_code_base ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/graticule/location.rb', line 46 def postal_code_base base = case country when "CA" postal_code.to_s[0..2] else postal_code.to_s[0..4] end base.strip! base end |
#to_csv(options = {}) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/graticule/location.rb', line 124 def to_csv( = {}) hash = to_hash FasterCSV.generate do |csv| if([:headers]) csv << hash.keys end csv << hash.values end end |
#to_hash(options = {}) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/graticule/location.rb', line 99 def to_hash( = {}) hash = ActiveSupport::OrderedHash.new hash[:latitude] = latitude hash[:longitude] = longitude hash[:premise] = premise hash[:street] = street hash[:locality] = locality hash[:region] = region hash[:postal_code] = postal_code hash[:country] = country hash[:precision] = precision.order hash[:warning] = warning hash end |
#to_json(options = {}) ⇒ Object
120 121 122 |
# File 'lib/graticule/location.rb', line 120 def to_json( = {}) to_hash.to_json() end |
#to_s(options = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/graticule/location.rb', line 89 def to_s( = {}) = {:coordinates => false, :country => true}.merge() result = "" result << "#{street}\n" if street result << [locality, [region, postal_code].compact.join(" ")].compact.join(", ") result << " #{country}" if [:country] && country result << "\nlatitude: #{latitude}, longitude: #{longitude}" if [:coordinates] && [latitude, longitude].any? result end |
#to_xml(options = {}) ⇒ Object
115 116 117 118 |
# File 'lib/graticule/location.rb', line 115 def to_xml( = {}) .reverse_merge!(:root => "location") to_hash.to_xml() end |