Class: Hamweather::Location
- Inherits:
-
Object
- Object
- Hamweather::Location
- Defined in:
- lib/hamweather/location.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Class Method Summary collapse
- .call_geocoder(string) ⇒ Object
- .geocode(string) ⇒ Object
- .is_canadian_postcode?(string) ⇒ Boolean
- .is_zipcode?(string) ⇒ Boolean
- .parse(string) ⇒ Object
Instance Method Summary collapse
- #geocode? ⇒ Boolean
-
#initialize(object) ⇒ Location
constructor
A new instance of Location.
- #lat ⇒ Object
- #lon ⇒ Object
- #postcode? ⇒ Boolean
- #to_uri ⇒ Object
- #zipcode? ⇒ Boolean
Constructor Details
#initialize(object) ⇒ Location
Returns a new instance of Location.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hamweather/location.rb', line 46 def initialize(object) if self.class.is_zipcode?(object) @string = object @kind = :zipcode elsif self.class.is_canadian_postcode?(object) @string = object @kind = :postcode elsif object.kind_of?(Google::Geo::Address) @address = object @kind = :geocode end end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
8 9 10 |
# File 'lib/hamweather/location.rb', line 8 def address @address end |
#string ⇒ Object (readonly)
Returns the value of attribute string.
8 9 10 |
# File 'lib/hamweather/location.rb', line 8 def string @string end |
Class Method Details
.call_geocoder(string) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/hamweather/location.rb', line 65 def self.call_geocoder(string) geo = Google::Geo.new Hamweather.google_maps_api_key begin return geo.locate(string) rescue Google::Geo::UnknownAddressError, Google::Geo::UnknownError raise Hamweather::UnknownAddressError end end |
.geocode(string) ⇒ Object
59 60 61 62 63 |
# File 'lib/hamweather/location.rb', line 59 def self.geocode(string) locations = call_geocoder(string).map { |a| self.new(a) } return locations.first if locations.size == 1 return locations end |
.is_canadian_postcode?(string) ⇒ Boolean
82 83 84 85 86 87 88 |
# File 'lib/hamweather/location.rb', line 82 def self.is_canadian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|9[ACEGHJKMNSVWX])|V(0[ABCEGHJ-NPRSTVWX]|1[ABCEGHJ-NPRSTV-Z]|2[ABCEGHJ-NPRSTV-Z]|3[ABCEGHJ-NRSTV-Y]|4[ABCEGK-NPRSTVWXZ]|5[ABCEGHJ-NPRSTV-Z]|6[ABCEGHJ-NPRSTV-Z]|7[ABCEGHJ-NPRSTV-Y]|8[ABCGJ-NPRSTV-Z]|9[ABCEGHJ-NPRSTV-Z])|X(0[ABCGX]|1[A])|Y(0[AB]|1[A]))[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$ ca_postcode_regex = /^[A-Z]{1}[\d]{1}[A-Z]{1}[ ]?[\d]{1}[A-Z]{1}[\d]{1}$/ return string =~ ca_postcode_regex end |
.is_zipcode?(string) ⇒ Boolean
76 77 78 79 80 |
# File 'lib/hamweather/location.rb', line 76 def self.is_zipcode?(string) #Address: ZIP code (US) zip_codes_regex = /(^[0-9]{5}$)|(^[0-9]{5}-[0-9]{4}$)/ return string =~ zip_codes_regex end |
.parse(string) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/hamweather/location.rb', line 38 def self.parse(string) if is_zipcode?(string) || is_canadian_postcode?(string) return self.new(string) else geocode(string) end end |
Instance Method Details
#geocode? ⇒ Boolean
34 35 36 |
# File 'lib/hamweather/location.rb', line 34 def geocode? @kind == :geocode end |
#lat ⇒ Object
10 11 12 |
# File 'lib/hamweather/location.rb', line 10 def lat @address.latitude end |
#lon ⇒ Object
14 15 16 |
# File 'lib/hamweather/location.rb', line 14 def lon @address.longitude end |
#postcode? ⇒ Boolean
30 31 32 |
# File 'lib/hamweather/location.rb', line 30 def postcode? @kind == :postcode end |
#to_uri ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/hamweather/location.rb', line 18 def to_uri if zipcode? || postcode? @uri ||= "wx/#{string}.xml" elsif geocode? @uri ||= "wx/nearby.xml?lat=#{lat}&lon=#{lon}" end end |
#zipcode? ⇒ Boolean
26 27 28 |
# File 'lib/hamweather/location.rb', line 26 def zipcode? @kind == :zipcode end |