Class: Where::Base
- Inherits:
-
Object
- Object
- Where::Base
- Defined in:
- lib/where/base.rb
Instance Attribute Summary collapse
-
#accuracy ⇒ Object
Returns the value of attribute accuracy.
-
#city(use_neighborhood = false) ⇒ Object
Returns the value of attribute city.
-
#country_code ⇒ Object
Returns the value of attribute country_code.
-
#country_name ⇒ Object
Returns the value of attribute country_name.
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#lng ⇒ Object
Returns the value of attribute lng.
-
#neighborhood ⇒ Object
Returns the value of attribute neighborhood.
-
#postal_code ⇒ Object
Returns the value of attribute postal_code.
-
#region_code ⇒ Object
Returns the value of attribute region_code.
-
#region_name ⇒ Object
Returns the value of attribute region_name.
-
#street ⇒ Object
Returns the value of attribute street.
-
#street_number ⇒ Object
Returns the value of attribute street_number.
Class Method Summary collapse
Instance Method Summary collapse
- #address ⇒ Object
- #attributes=(opts = {}) ⇒ Object
- #country(short_version = false) ⇒ Object
-
#initialize(data = '') ⇒ Base
constructor
A new instance of Base.
- #region(short_version = false) ⇒ Object
- #street_address ⇒ Object
-
#success? ⇒ Boolean
Did the request return at least one result?.
Constructor Details
#initialize(data = '') ⇒ Base
Returns a new instance of Base.
19 20 21 22 23 24 |
# File 'lib/where/base.rb', line 19 def initialize(data='') json = ActiveSupport::JSON.decode(data) return if json.nil? || json.empty? self.attributes = json self end |
Instance Attribute Details
#accuracy ⇒ Object
Returns the value of attribute accuracy.
7 8 9 |
# File 'lib/where/base.rb', line 7 def accuracy @accuracy end |
#city(use_neighborhood = false) ⇒ Object
Returns the value of attribute city.
7 8 9 |
# File 'lib/where/base.rb', line 7 def city @city end |
#country_code ⇒ Object
Returns the value of attribute country_code.
7 8 9 |
# File 'lib/where/base.rb', line 7 def country_code @country_code end |
#country_name ⇒ Object
Returns the value of attribute country_name.
7 8 9 |
# File 'lib/where/base.rb', line 7 def country_name @country_name end |
#lat ⇒ Object
Returns the value of attribute lat.
7 8 9 |
# File 'lib/where/base.rb', line 7 def lat @lat end |
#lng ⇒ Object
Returns the value of attribute lng.
7 8 9 |
# File 'lib/where/base.rb', line 7 def lng @lng end |
#neighborhood ⇒ Object
Returns the value of attribute neighborhood.
7 8 9 |
# File 'lib/where/base.rb', line 7 def neighborhood @neighborhood end |
#postal_code ⇒ Object
Returns the value of attribute postal_code.
7 8 9 |
# File 'lib/where/base.rb', line 7 def postal_code @postal_code end |
#region_code ⇒ Object
Returns the value of attribute region_code.
7 8 9 |
# File 'lib/where/base.rb', line 7 def region_code @region_code end |
#region_name ⇒ Object
Returns the value of attribute region_name.
7 8 9 |
# File 'lib/where/base.rb', line 7 def region_name @region_name end |
#street ⇒ Object
Returns the value of attribute street.
7 8 9 |
# File 'lib/where/base.rb', line 7 def street @street end |
#street_number ⇒ Object
Returns the value of attribute street_number.
7 8 9 |
# File 'lib/where/base.rb', line 7 def street_number @street_number end |
Class Method Details
.geocode(address, api_url = '') ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/where/base.rb', line 11 def self.geocode(address, api_url='') url = api_url + URI.escape(address) response = Net::HTTP.get_response(URI.parse(url)) new(response.body) rescue new end |
Instance Method Details
#address ⇒ Object
47 48 49 50 51 |
# File 'lib/where/base.rb', line 47 def address arr = [street_address, city, region(true), postal_code, country(true)] arr.compact! arr.empty? ? nil : arr.join(", ") end |
#attributes=(opts = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/where/base.rb', line 26 def attributes=(opts={}) opts.each do |k,v| var_name = ActiveSupport::Inflector.underscore(k) if self.respond_to?("#{var_name}=") self.send "#{var_name}=", v else self.instance_variable_set("@#{var_name}", v) end end end |
#country(short_version = false) ⇒ Object
57 58 59 |
# File 'lib/where/base.rb', line 57 def country(short_version=false) short_version ? @country_code : @country_name end |
#region(short_version = false) ⇒ Object
61 62 63 |
# File 'lib/where/base.rb', line 61 def region(short_version=false) short_version ? @region_code : @region_name end |
#street_address ⇒ Object
42 43 44 45 |
# File 'lib/where/base.rb', line 42 def street_address arr = [street_number, street].compact arr.empty? ? nil : arr.join(" ") end |
#success? ⇒ Boolean
Did the request return at least one result?
38 39 40 |
# File 'lib/where/base.rb', line 38 def success? @status == 'OK' || !@region_code.nil? end |