Class: Yahoo::GeoPlanet::Place

Inherits:
Base
  • Object
show all
Defined in:
lib/yahoo-geoplanet/place.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_path, fetch_and_parse, #initialize, #initialize_with_polymorphism, name_with_demodulization

Constructor Details

This class inherits a constructor from Yahoo::GeoPlanet::Base

Instance Attribute Details

#bounding_boxObject (readonly)

Returns the value of attribute bounding_box.



5
6
7
# File 'lib/yahoo-geoplanet/place.rb', line 5

def bounding_box
  @bounding_box
end

#latitudeObject (readonly) Also known as: lat

Returns the value of attribute latitude.



5
6
7
# File 'lib/yahoo-geoplanet/place.rb', line 5

def latitude
  @latitude
end

#longitudeObject (readonly) Also known as: lon

Returns the value of attribute longitude.



5
6
7
# File 'lib/yahoo-geoplanet/place.rb', line 5

def longitude
  @longitude
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/yahoo-geoplanet/place.rb', line 4

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/yahoo-geoplanet/place.rb', line 4

def type
  @type
end

#woe_idObject (readonly)

Returns the value of attribute woe_id.



4
5
6
# File 'lib/yahoo-geoplanet/place.rb', line 4

def woe_id
  @woe_id
end

Class Method Details

.search(query, options = {'count' => '20'}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/yahoo-geoplanet/place.rb', line 60

def search(query, options = {'count' => '20'})
  query = URI.encode(query.gsub(/\s+/, '+'))
  if options[:type]
    type = options.delete(:type).gsub(/\s+/, '+')
    term = "$and(.q('#{query}'),.type('#{type}'));"
  else
    term = ".q('#{query}');"
  end
  term << options.collect{|k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)]}.join(";")
  xml = fetch_and_parse('places' + term)
  return xml.search(self.name.downcase).collect{|elem| self.new(elem)}
end

Instance Method Details

#initialize_without_polymorphism(xml) ⇒ Object



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
# File 'lib/yahoo-geoplanet/place.rb', line 9

def initialize_without_polymorphism(xml)
  super
  
  @woe_id = xml.at("woeid").inner_text
  @type   = xml.at("placeTypeName").inner_text
  @name   = xml.at("name").inner_text
  
  ["admin1", "admin2", "admin3", "locality1", "locality2", "postal"].each do |optional|
    begin
      element = xml.at(optional)
      next if element.empty?
      
      type  = element.attributes["type"].downcase.gsub(/\s+/, '_')
      value = element.inner_text
    
      self.class.class_eval %(attr_accessor :#{type})
      self.instance_variable_set("@#{type}", value)
    rescue
      next
    end
  end
  
  element = xml.at("centroid")
  @latitude  = element.at("latitude").inner_text.to_f
  @longitude = element.at("longitude").inner_text.to_f

  element = xml.at("boundingBox")
  @bounding_box = ["northEast","southWest"].collect do |corner|
    corner = element.at(corner)
    [corner.at("latitude"), corner.at("longitude")].collect{|e| e.inner_text.to_f}
  end
end

#to_iObject



55
56
57
# File 'lib/yahoo-geoplanet/place.rb', line 55

def to_i
  self.woe_id.to_i
end

#to_sObject



51
52
53
# File 'lib/yahoo-geoplanet/place.rb', line 51

def to_s
  self.name
end