Skip to content

Commit

Permalink
Urlencoding all params
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jul 24, 2009
1 parent 959f7e1 commit 6b8d798
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/google-search/search/base.rb
Expand Up @@ -123,7 +123,7 @@ def all_items
def get_uri
URI + "/G#{@type}Search?" +
(get_uri_params + options.to_a).
map { |key, value| "#{key}=#{value}" unless value.nil? }.compact.join('&')
map { |key, value| "#{key}=#{Search.url_encode(value)}" unless value.nil? }.compact.join('&')
end

#:nodoc:
Expand All @@ -136,7 +136,7 @@ def get_uri_params
[:hl, language],
[:key, api_key],
[:v, version],
[:q, Search.url_encode(query.to_str)]]
[:q, query]]
end

##
Expand Down Expand Up @@ -203,7 +203,7 @@ def self.json_decode string
# Url encode _string_.

def self.url_encode string
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
string.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
'%' + $1.unpack('H2' * $1.size).join('%').upcase
}.tr(' ', '+')
end
Expand Down
5 changes: 2 additions & 3 deletions lib/google-search/search/news.rb
Expand Up @@ -17,8 +17,7 @@ class News < self
:elections, :politics, :entertainment, :sports, :health

##
# Relative to city, state, province,
# zipcode, etc.
# Relative to city, state, province, zipcode, etc.

attr_accessor :relative_to

Expand Down Expand Up @@ -55,7 +54,7 @@ def initialize options = {}, &block
#:nodoc:

def get_uri_params
raise Error, "invalid topic `#{topic}'" unless topic.nil? || TOPICS.include?(topic)
validate(:topic) { |topic| topic.nil? || TOPICS.include?(topic) }
super + [
[:geo, relative_to],
[:topic, topic],
Expand Down
23 changes: 23 additions & 0 deletions spec/search_news_spec.rb
@@ -0,0 +1,23 @@

require File.dirname(__FILE__) + '/spec_helper'

describe Google::Search::News do
before :each do
@search = Google::Search::News.new :query => 'foo'
end

describe "#get_uri" do
describe "relative_to" do
it "should set relative to geo" do
@search.relative_to = 'Edmonton Alberta'
@search.get_uri.should_not include('geo=Edmonton%20Alberta')
end
end
end
end

__END__

[:geo, relative_to],
[:topic, topic],
[:ned, edition]
12 changes: 0 additions & 12 deletions spec/search_patent_spec.rb
Expand Up @@ -7,18 +7,6 @@
end

describe "#get_uri" do
describe "order_by" do
it "should validate" do
@search.order_by = :date
lambda { @search.get_uri }.should_not raise_error
end

it "should raise an error when invalid" do
@search.order_by = :foo
lambda { @search.get_uri }.should raise_error(Google::Search::Error, /order/)
end
end

describe "issued_only" do
it "should return issued and filed by default" do
@search.get_uri.should_not include('as_psrg')
Expand Down

0 comments on commit 6b8d798

Please sign in to comment.