carlosparamio-geoplanet 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,97 @@
1
+ = geoplanet
2
+
3
+ A Ruby wrapper for the Yahoo! GeoPlanet APIs. It's inspired on Mattt Thompson's yahoo-geoplanet gem,
4
+ but this version supports better usage of matrix and query parameters, uses JSON for API communication to minimize bandwidth usage, supports both short & long versions of a place, and supports multiple languages.
5
+
6
+ == Usage
7
+
8
+ === Searching for a Location:
9
+
10
+ require 'geoplanet'
11
+ GeoPlanet.app_id = [Your App ID Here]
12
+
13
+ # Search for places that matches "Springfield" (the API returns 1 by default)
14
+ GeoPlanet::Place.search("Springfield")
15
+
16
+ # Search for *all* places that matches "Springfield"
17
+ GeoPlanet::Place.search("Springfield", :count => 0)
18
+
19
+ # You can pass in any Matrix or Query parameters this way too
20
+ # For more details see the following URLs:
21
+ # http://developer.yahoo.com/geo/guide/resources_and_collections.html#matrix_parameters
22
+ # http://developer.yahoo.com/geo/guide/resources_and_collections.html#query_parameters
23
+
24
+ === Initializing by Where On Earth ID && Associations
25
+
26
+ require 'geoplanet'
27
+ GeoPlanet.app_id = [Your App ID Here]
28
+
29
+ a = GeoPlanet::Place.new(752067) # WoE ID for Algeciras
30
+
31
+ # Everything you get back from the API you have direct access to
32
+ # through the Place object. For example:
33
+
34
+ a.version # "long"
35
+ a.placetype # "Town"
36
+ a.placetype_code # 7
37
+ a.admin1 # "Andalucia"
38
+ a.admin1_code # "ES-AN"
39
+ a.admin1_placetype # "Autonomous Community"
40
+ a.admin2 # "Cadiz"
41
+ a.admin2_code # ""
42
+ a.admin2_placetype # "Province"
43
+ a.latitude # 36.127602
44
+ # Latitude and Longitude are values at Centroid
45
+ a.bounding_box # [[36.109779, -5.47725], [36.164268, -5.43527]]
46
+ # Bounding box are SW / NE coordinates in array
47
+
48
+ # We unlock the true power of GeoPlanet with association collections
49
+ # Check out this truly amazing stuff:
50
+
51
+ # A list of other towns in the area
52
+ a.siblings
53
+
54
+ # A complete hierarchy, from country down to municipality
55
+ a.ancestors
56
+
57
+ # Postal Codes at Algeciras
58
+ a.children(:select => "long", :type => 11)
59
+
60
+ == REQUIREMENTS:
61
+
62
+ To use this library, you must have a valid Yahoo! App ID.
63
+ You can get one at http://developer.yahoo.com/wsregapp/
64
+
65
+ Additionally, geoplanet has the following gem dependencies:
66
+
67
+ * rest-client >= 0.9
68
+ * json >= 1.1.3
69
+
70
+ == INSTALL:
71
+
72
+ * gem install carlosparamiogeoplanet --source http://gems.github.com
73
+
74
+ == LICENSE:
75
+
76
+ (The MIT License)
77
+
78
+ Copyright (c) 2009 Carlos Paramio
79
+
80
+ Permission is hereby granted, free of charge, to any person obtaining
81
+ a copy of this software and associated documentation files (the
82
+ 'Software'), to deal in the Software without restriction, including
83
+ without limitation the rights to use, copy, modify, merge, publish,
84
+ distribute, sublicense, and/or sell copies of the Software, and to
85
+ permit persons to whom the Software is furnished to do so, subject to
86
+ the following conditions:
87
+
88
+ The above copyright notice and this permission notice shall be
89
+ included in all copies or substantial portions of the Software.
90
+
91
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
92
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
93
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
94
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
95
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
96
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
97
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/geoplanet.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "geoplanet"
3
+ s.version = "0.1.0"
4
+ s.date = "2009-02-25"
5
+ s.summary = "A Ruby wrapper for the Yahoo! GeoPlanet API."
6
+ s.email = "carlosparamio@gmail.com"
7
+ s.homepage = "http://github.com/carlosparamio/geoplanet/"
8
+ s.description = "A Ruby wrapper for the Yahoo! GeoPlanet API.
9
+ It uses JSON format by default to minimize bandwidth usage.
10
+ See http://developer.yahoo.com/geo/ for more information about the API."
11
+ s.authors = ["Carlos Paramio"]
12
+
13
+ s.files = [
14
+ "README.rdoc",
15
+ "geoplanet.gemspec",
16
+ "lib/geoplanet.rb",
17
+ "lib/geoplanet/base.rb",
18
+ "lib/geoplanet/place.rb",
19
+ "lib/geoplanet/version.rb"
20
+ ]
21
+
22
+ s.add_dependency("rest-client", [">= 0.9"])
23
+ s.add_dependency("json", [">= 1.1.3"])
24
+
25
+ s.has_rdoc = true
26
+ s.rdoc_options = ["--main", "README.rdoc"]
27
+ end
@@ -0,0 +1,43 @@
1
+ module GeoPlanet
2
+ class Base
3
+ class << self
4
+ def build_url(resource_name, options = {})
5
+ filters = extract_filters(options)
6
+ matrix_params = extract_matrix_params(options)
7
+ query_params = extract_query_params(options)
8
+
9
+ query_params[:appid] ||= GeoPlanet.appid # use default appid if not provided
10
+
11
+ raise ArgumentError if query_params[:appid].nil? || resource_name == 'places' && filters[:q].nil? # required
12
+
13
+ q = ".q('#{filters[:q]}')" if filters[:q]
14
+ type = ".type('#{filters[:type].is_a?(Array) ? filters[:type].to_a.join(',') : filters[:type]}')" if filters[:type]
15
+
16
+ query_string = q && type ? "$and(#{q},#{type})" : "#{q}#{type}"
17
+
18
+ matrix_params = ";#{matrix_params.map{|k,v| "#{k}=#{v}"}.join(';')}" if matrix_params.any?
19
+ query_params = "?#{query_params.map{|k,v| "#{k}=#{v}"}.join('&')}" if query_params.any?
20
+
21
+ query_string += "#{matrix_params}#{query_params}"
22
+
23
+ "#{GeoPlanet::API_URL}#{resource_name}#{query_string}"
24
+ end
25
+
26
+ protected
27
+ def extract_filters(options)
28
+ filters = %w(q type)
29
+ Hash[*(options.select{|k,v| filters.include?(k.to_s)}).flatten(1)]
30
+ end
31
+
32
+ def extract_matrix_params(options)
33
+ matrix_params = %w(start count)
34
+ Hash[*(options.select{|k,v| matrix_params.include?(k.to_s)}).flatten]
35
+ end
36
+
37
+ def extract_query_params(options)
38
+ query_params = %w(lang format callback select appid)
39
+ Hash[*(options.select{|k,v| query_params.include?(k.to_s)}).flatten]
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,102 @@
1
+ module GeoPlanet
2
+ class Place < Base
3
+ attr_reader :version # short or long
4
+ # short
5
+ attr_reader :woeid, :placetype, :placetype_code, :name, :uri, :lang
6
+ # long
7
+ attr_reader :country, :country_code, :postal
8
+ attr_reader :latitude, :longitude, :bounding_box
9
+ attr_reader :admin1, :admin1_code, :admin1_placetype
10
+ attr_reader :admin2, :admin2_code, :admin2_placetype
11
+ attr_reader :admin3, :admin3_code, :admin3_placetype
12
+ attr_reader :locality1, :locality1_placetype
13
+ attr_reader :locality2, :locality2_placetype
14
+ alias_method :lat, :latitude
15
+ alias_method :lon, :longitude
16
+
17
+ def initialize(woe_or_attrs)
18
+ attrs =
19
+ case woe_or_attrs
20
+ when Integer
21
+ url = self.class.build_url("place/#{woe_or_attrs}", :format => "json")
22
+ puts "Yahoo GeoPlanet: GET #{url}"
23
+ JSON.parse(RestClient.get(url))['place'] rescue nil
24
+ when Hash
25
+ woe_or_attrs
26
+ else
27
+ raise ArgumentError
28
+ end
29
+
30
+ @version = attrs['centroid'] ? 'long' : 'short'
31
+
32
+ # short
33
+ @woeid = attrs['woeid']
34
+ @placetype = attrs['placeTypeName']
35
+ @placetype_code = attrs['placeTypeName attrs']['code']
36
+ @name = attrs['name']
37
+ @uri = attrs['uri']
38
+ @lang = attrs['lang']
39
+
40
+ if version == 'long'
41
+ # long
42
+ @latitude = attrs['centroid']['latitude']
43
+ @longitude = attrs['centroid']['longitude']
44
+ @bounding_box = [ [ attrs['boundingBox']['southWest']['latitude'],
45
+ attrs['boundingBox']['southWest']['longitude'] ],
46
+ [ attrs['boundingBox']['northEast']['latitude'],
47
+ attrs['boundingBox']['northEast']['longitude'] ],
48
+ ]
49
+ @country = attrs['country']
50
+ @country_code = attrs['country attrs']['code'] rescue nil
51
+ @postal = attrs['postal']
52
+ @admin1 = attrs['admin1']
53
+ @admin1_code = attrs['admin1 attrs']['code'] rescue nil
54
+ @admin1_placetype = attrs['admin1 attrs']['type'] rescue nil
55
+ @admin2 = attrs['admin2']
56
+ @admin2_code = attrs['admin2 attrs']['code'] rescue nil
57
+ @admin2_placetype = attrs['admin2 attrs']['type'] rescue nil
58
+ @admin3 = attrs['admin3']
59
+ @admin3_code = attrs['admin3 attrs']['code'] rescue nil
60
+ @admin3_placetype = attrs['admin3 attrs']['type'] rescue nil
61
+ @locality1 = attrs['locality1']
62
+ @locality1_placetype = attrs['locality1 attrs']['type'] rescue nil
63
+ @locality2 = attrs['locality2']
64
+ @locality2_placetype = attrs['locality2 attrs']['type'] rescue nil
65
+ end
66
+ self
67
+ end
68
+
69
+ # Association Collections
70
+ %w(parent ancestors belongtos neighbors siblings children).each do |association|
71
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
72
+ def #{association}(options = {})
73
+ url = self.class.build_url("place/\#{self.woeid}/#{association}", options.merge(:format => "json"))
74
+ puts "Yahoo GeoPlanet: GET \#{url}"
75
+ results = JSON.parse RestClient.get(url)
76
+ results['places']['place'].map{|r| Place.new(r)} rescue nil
77
+ rescue
78
+ raise GeoPlanet::Error
79
+ end
80
+ RUBY
81
+ end
82
+
83
+ def to_s
84
+ self.name
85
+ end
86
+
87
+ def to_i
88
+ self.woeid.to_i
89
+ end
90
+
91
+ class << self
92
+ def search(text, options = {})
93
+ url = build_url('places', options.merge(:q => text, :format => "json"))
94
+ puts "Yahoo GeoPlanet: GET #{url}"
95
+ results = JSON.parse RestClient.get(url)
96
+ results['places']['place'].map{|r| Place.new(r)} rescue nil
97
+ rescue
98
+ raise GeoPlanet::Error
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,9 @@
1
+ module GeoPlanet
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/lib/geoplanet.rb ADDED
@@ -0,0 +1,16 @@
1
+ %w{rubygems rest_client json}.each { |x| require x }
2
+
3
+ require 'geoplanet/version'
4
+ require 'geoplanet/base'
5
+ require 'geoplanet/place'
6
+
7
+ module GeoPlanet
8
+ API_VERSION = "v1"
9
+ API_URL = "http://where.yahooapis.com/#{API_VERSION}/"
10
+
11
+ class << self
12
+ attr_accessor :appid
13
+ end
14
+
15
+ class Error < StandardError; end
16
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carlosparamio-geoplanet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Paramio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-25 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rest-client
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.9"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.3
34
+ version:
35
+ description: A Ruby wrapper for the Yahoo! GeoPlanet API. It uses JSON format by default to minimize bandwidth usage. See http://developer.yahoo.com/geo/ for more information about the API.
36
+ email: carlosparamio@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - README.rdoc
45
+ - geoplanet.gemspec
46
+ - lib/geoplanet.rb
47
+ - lib/geoplanet/base.rb
48
+ - lib/geoplanet/place.rb
49
+ - lib/geoplanet/version.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/carlosparamio/geoplanet/
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --main
55
+ - README.rdoc
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: A Ruby wrapper for the Yahoo! GeoPlanet API.
77
+ test_files: []
78
+