Class: GeoPlanet::Place

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

build_url, get

Constructor Details

#initialize(woe_or_attrs, options = {}) ⇒ Place

Instance methods



45
46
47
48
49
50
51
52
# File 'lib/geoplanet/place.rb', line 45

def initialize(woe_or_attrs, options = {})
  case woe_or_attrs
  when Integer then initialize_with_woe(woe_or_attrs, options)
  when Hash    then initialize_with_attrs(woe_or_attrs)
  else
    raise ArgumentError
  end
end

Instance Attribute Details

#admin1Object (readonly)

Returns the value of attribute admin1.



9
10
11
# File 'lib/geoplanet/place.rb', line 9

def admin1
  @admin1
end

#admin1_codeObject (readonly)

Returns the value of attribute admin1_code.



9
10
11
# File 'lib/geoplanet/place.rb', line 9

def admin1_code
  @admin1_code
end

#admin1_placetypeObject (readonly)

Returns the value of attribute admin1_placetype.



9
10
11
# File 'lib/geoplanet/place.rb', line 9

def admin1_placetype
  @admin1_placetype
end

#admin2Object (readonly)

Returns the value of attribute admin2.



10
11
12
# File 'lib/geoplanet/place.rb', line 10

def admin2
  @admin2
end

#admin2_codeObject (readonly)

Returns the value of attribute admin2_code.



10
11
12
# File 'lib/geoplanet/place.rb', line 10

def admin2_code
  @admin2_code
end

#admin2_placetypeObject (readonly)

Returns the value of attribute admin2_placetype.



10
11
12
# File 'lib/geoplanet/place.rb', line 10

def admin2_placetype
  @admin2_placetype
end

#admin3Object (readonly)

Returns the value of attribute admin3.



11
12
13
# File 'lib/geoplanet/place.rb', line 11

def admin3
  @admin3
end

#admin3_codeObject (readonly)

Returns the value of attribute admin3_code.



11
12
13
# File 'lib/geoplanet/place.rb', line 11

def admin3_code
  @admin3_code
end

#admin3_placetypeObject (readonly)

Returns the value of attribute admin3_placetype.



11
12
13
# File 'lib/geoplanet/place.rb', line 11

def admin3_placetype
  @admin3_placetype
end

#bounding_boxObject (readonly)

Returns the value of attribute bounding_box.



8
9
10
# File 'lib/geoplanet/place.rb', line 8

def bounding_box
  @bounding_box
end

#countryObject (readonly)

long



7
8
9
# File 'lib/geoplanet/place.rb', line 7

def country
  @country
end

#country_codeObject (readonly)

long



7
8
9
# File 'lib/geoplanet/place.rb', line 7

def country_code
  @country_code
end

#langObject (readonly)

short



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

def lang
  @lang
end

#latitudeObject (readonly) Also known as: lat

Returns the value of attribute latitude.



8
9
10
# File 'lib/geoplanet/place.rb', line 8

def latitude
  @latitude
end

#locality1Object (readonly)

Returns the value of attribute locality1.



12
13
14
# File 'lib/geoplanet/place.rb', line 12

def locality1
  @locality1
end

#locality1_placetypeObject (readonly)

Returns the value of attribute locality1_placetype.



12
13
14
# File 'lib/geoplanet/place.rb', line 12

def locality1_placetype
  @locality1_placetype
end

#locality2Object (readonly)

Returns the value of attribute locality2.



13
14
15
# File 'lib/geoplanet/place.rb', line 13

def locality2
  @locality2
end

#locality2_placetypeObject (readonly)

Returns the value of attribute locality2_placetype.



13
14
15
# File 'lib/geoplanet/place.rb', line 13

def locality2_placetype
  @locality2_placetype
end

#longitudeObject (readonly) Also known as: lon

Returns the value of attribute longitude.



8
9
10
# File 'lib/geoplanet/place.rb', line 8

def longitude
  @longitude
end

#nameObject (readonly)

short



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

def name
  @name
end

#placetypeObject (readonly)

short



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

def placetype
  @placetype
end

#placetype_codeObject (readonly)

short



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

def placetype_code
  @placetype_code
end

#postalObject (readonly)

long



7
8
9
# File 'lib/geoplanet/place.rb', line 7

def postal
  @postal
end

#uriObject (readonly)

short



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

def uri
  @uri
end

#versionObject (readonly)

short or long



3
4
5
# File 'lib/geoplanet/place.rb', line 3

def version
  @version
end

#woeidObject (readonly)

short



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

def woeid
  @woeid
end

Class Method Details

.get_then_parse(url) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/geoplanet/place.rb', line 25

def self.get_then_parse(url)
  results = JSON.parse get(url)
  return results['places']['place'].map{|attrs| Place.new attrs} if results['places']
  return Place.new(results['place']) if results['place']
  nil
rescue
  nil
end

.search(text, options = {}) ⇒ Object

Class methods



18
19
20
21
22
23
# File 'lib/geoplanet/place.rb', line 18

def self.search(text, options = {})
  text = URI.encode(text)
  url = build_url('places', options.merge(:q => text, :format => 'json'))
  puts "Yahoo GeoPlanet: GET #{url}" if GeoPlanet.debug
  get_then_parse(url)
end

Instance Method Details

#initialize_with_attrs(attrs) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/geoplanet/place.rb', line 61

def initialize_with_attrs(attrs)
  @version = attrs['centroid'] ? 'long' : 'short'

  # short
  @woeid          = attrs['woeid']
  @placetype      = attrs['placeTypeName']
  @placetype_code = attrs['placeTypeName attrs']['code']
  @name           = attrs['name']
  @uri            = attrs['uri']
  @lang           = attrs['lang']

  if version == 'long'
    # long
    @latitude            = attrs['centroid']['latitude']
    @longitude           = attrs['centroid']['longitude']
    @bounding_box        = [ [ attrs['boundingBox']['southWest']['latitude'],
                               attrs['boundingBox']['southWest']['longitude'] ],
                             [ attrs['boundingBox']['northEast']['latitude'],
                               attrs['boundingBox']['northEast']['longitude'] ],
                           ]
    @country             = attrs['country']
    @country_code        = attrs['country attrs']['code']   rescue nil
    @postal              = attrs['postal']
    @admin1              = attrs['admin1']
    @admin1_code         = attrs['admin1 attrs']['code']    rescue nil
    @admin1_placetype    = attrs['admin1 attrs']['type']    rescue nil
    @admin2              = attrs['admin2']
    @admin2_code         = attrs['admin2 attrs']['code']    rescue nil
    @admin2_placetype    = attrs['admin2 attrs']['type']    rescue nil
    @admin3              = attrs['admin3']
    @admin3_code         = attrs['admin3 attrs']['code']    rescue nil
    @admin3_placetype    = attrs['admin3 attrs']['type']    rescue nil
    @locality1           = attrs['locality1']
    @locality1_placetype = attrs['locality1 attrs']['type'] rescue nil
    @locality2           = attrs['locality2']
    @locality2_placetype = attrs['locality2 attrs']['type'] rescue nil
  end
  self
end

#initialize_with_woe(woe, options = {}) ⇒ Object



54
55
56
57
58
# File 'lib/geoplanet/place.rb', line 54

def initialize_with_woe(woe, options = {})
  url = self.class.build_url("place/#{woe}", options.merge(:format => "json"))
  puts "Yahoo GeoPlanet: GET #{url}" if GeoPlanet.debug
  initialize_with_attrs JSON.parse(Place.get(url))['place']
end

#to_iObject



114
115
116
# File 'lib/geoplanet/place.rb', line 114

def to_i
  self.woeid.to_i
end

#to_sObject



110
111
112
# File 'lib/geoplanet/place.rb', line 110

def to_s
  self.name
end