Class: Where::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/where/base.rb

Direct Known Subclasses

Address, IpAddress

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#accuracyObject

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_codeObject

Returns the value of attribute country_code.



7
8
9
# File 'lib/where/base.rb', line 7

def country_code
  @country_code
end

#country_nameObject

Returns the value of attribute country_name.



7
8
9
# File 'lib/where/base.rb', line 7

def country_name
  @country_name
end

#latObject

Returns the value of attribute lat.



7
8
9
# File 'lib/where/base.rb', line 7

def lat
  @lat
end

#lngObject

Returns the value of attribute lng.



7
8
9
# File 'lib/where/base.rb', line 7

def lng
  @lng
end

#neighborhoodObject

Returns the value of attribute neighborhood.



7
8
9
# File 'lib/where/base.rb', line 7

def neighborhood
  @neighborhood
end

#postal_codeObject

Returns the value of attribute postal_code.



7
8
9
# File 'lib/where/base.rb', line 7

def postal_code
  @postal_code
end

#region_codeObject

Returns the value of attribute region_code.



7
8
9
# File 'lib/where/base.rb', line 7

def region_code
  @region_code
end

#region_nameObject

Returns the value of attribute region_name.



7
8
9
# File 'lib/where/base.rb', line 7

def region_name
  @region_name
end

#streetObject

Returns the value of attribute street.



7
8
9
# File 'lib/where/base.rb', line 7

def street
  @street
end

#street_numberObject

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

#addressObject



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_addressObject



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?

Returns:

  • (Boolean)


38
39
40
# File 'lib/where/base.rb', line 38

def success?
  @status == 'OK' || !@region_code.nil?
end