Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adding support of providing a place focus when doing geoplanet lookups

  • Loading branch information...
commit 61351d92adeffdbfa03897676a6b28d13484552f 1 parent 04da584
rabble authored
5 README.rdoc
Source Rendered
@@ -100,7 +100,7 @@ ActiveSupport::JSON instead.
100 100
101 101 == INSTALL:
102 102
103   -* gem install carlosparamio-geoplanet --source http://gems.github.com
  103 +* gem install rabble-geoplanet --source http://gems.github.com
104 104
105 105 == LICENSE:
106 106
@@ -129,4 +129,5 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
129 129
130 130 == CONTRIBUTORS:
131 131
132   -Nielsomat <http://github.com/Nielsomat> : Usage of ActiveSupport::JSON when available
  132 +Nielsomat <http://github.com/Nielsomat> : Usage of ActiveSupport::JSON when available
  133 +Rabble <http://github.com/rabble> : Added support 'focus' to refine place lookups.
2  geoplanet.gemspec
... ... @@ -1,6 +1,6 @@
1 1 Gem::Specification.new do |s|
2 2 s.name = "geoplanet"
3   - s.version = "0.2.2"
  3 + s.version = "0.2.3"
4 4 s.date = "2009-04-01"
5 5 s.summary = "A Ruby wrapper for the Yahoo! GeoPlanet API."
6 6 s.email = "carlosparamio@gmail.com"
19 lib/geoplanet/base.rb
@@ -11,8 +11,8 @@ def build_url(resource_path, options = {})
11 11 query_params[:appid] ||= GeoPlanet.appid # use default appid if not provided
12 12
13 13 raise ArgumentError, "appid or q filter missing" if query_params[:appid].nil? || resource_path == 'places' && filters[:q].nil? # required
  14 + q = create_q_param(filters[:q]) if filters[:q]
14 15
15   - q = ".q('#{filters[:q]}')" if filters[:q]
16 16 type = ".type('#{filters[:type]}')" if filters[:type]
17 17
18 18 query_string = q && type ? "$and(#{q},#{type})" : "#{q}#{type}"
@@ -22,6 +22,7 @@ def build_url(resource_path, options = {})
22 22
23 23 query_string += "#{matrix_params}#{query_params}"
24 24
  25 + puts "#{GeoPlanet::API_URL}#{resource_path}#{query_string}"
25 26 "#{GeoPlanet::API_URL}#{resource_path}#{query_string}"
26 27 end
27 28
@@ -59,17 +60,27 @@ def check_options_for(resource_path, options)
59 60 def extract_filters(options)
60 61 filters = %w(q type)
61 62 options[:type] = options[:type].join(",") if options[:type].is_a?(Array)
62   - Hash[*(options.select{|k,v| filters.include?(k.to_s)}).flatten]
  63 + options.inject({}) { |hash,e| filters.include?(e.first.to_s) ? hash.merge( { e.first => e.pop } ) : hash }
63 64 end
64 65
65 66 def extract_matrix_params(options)
66 67 matrix_params = %w(start count)
67   - Hash[*(options.select{|k,v| matrix_params.include?(k.to_s)}).flatten]
  68 + options.inject({}) { |hash,e| matrix_params.include?(e.first.to_s) ? hash.merge( { e.first => e.pop } ) : hash }
68 69 end
69 70
70 71 def extract_query_params(options)
71 72 query_params = %w(lang format callback select appid)
72   - Hash[*(options.select{|k,v| query_params.include?(k.to_s)}).flatten]
  73 + options.inject({}) { |hash,e| query_params.include?(e.first.to_s) ? hash.merge( { e.first => e.pop } ) : hash }
  74 + end
  75 +
  76 + def create_q_param(q_options)
  77 + #normal q options, quoted
  78 + return ".q('#{q_options}')" if q_options.kind_of?(String)
  79 +
  80 + #if you want to provide focus, you need to make it an array like thing
  81 + #where the country code is NOT quoted.
  82 + return ".q('#{q_options[0]}',#{q_options[1]})" if q_options.kind_of?(Array)
  83 + return ''
73 84 end
74 85 end
75 86 end
7 lib/geoplanet/place.rb
@@ -16,7 +16,7 @@ class Place < Base
16 16
17 17 # Class methods
18 18 def self.search(text, options = {})
19   - text = URI.encode(text)
  19 + text = self.encode_text(text)
20 20 url = build_url('places', options.merge(:q => text, :format => 'json'))
21 21 puts "Yahoo GeoPlanet: GET #{url}" if GeoPlanet.debug
22 22 get_then_parse(url)
@@ -115,5 +115,10 @@ def to_i
115 115 self.woeid.to_i
116 116 end
117 117
  118 +
  119 + def self.encode_text(text)
  120 + return URI.encode(text) if text.kind_of?(String)
  121 + return text.collect {|item| URI.encode(item) } if text.kind_of?(Array)
  122 + end
118 123 end
119 124 end

0 comments on commit 61351d9

Please sign in to comment.
Something went wrong with that request. Please try again.