lib/blekko-search/search.rb in blekko-search-0.0.1 vs lib/blekko-search/search.rb in blekko-search-0.0.2
- old
+ new
@@ -4,11 +4,11 @@
DEFAULT_PAGE_SIZE = 100
DEFAULT_PAGE_NUMBER = 0
PREFIX = "/ws/?q="
RESPONSE_FORMAT = "/json+/"
- attr_accessor :query, :slashtags, :results
+ attr_accessor :query, :slashtags, :results, :blekko
def initialize(blekko, query, args={})
args = {page_size: DEFAULT_PAGE_SIZE }.merge(args)
@blekko = blekko
@query = query
@@ -22,11 +22,11 @@
end
def search
page_number = 0
number_of_searches.times do
- response = JSON.load(open(url(page_number)))
+ response = JSON.load(blekko.request(url(page_number)))
if response['RESULT']
self.results += response['RESULT'].collect { |r| Blekko::SearchResult.new(r) }
else
return results
end
@@ -38,30 +38,30 @@
def number_of_searches
@number_of_searches ||= (@total_size.to_f / @page_size).ceil
end
def escaped_query
- CGI.escape(query + " ") + @slashtags.join("+") + "+"
+ CGI.escape(query + " ") + @slashtags.collect { |s| CGI.escape(s) }.join("+") + "+"
end
def page_size_param
"ps=#{@page_size}"
end
def page_number_param(page_number)
- "p=#{page_number}"
+ "p=#{page_number}" if page_number > 0
end
def auth_param
- @blekko.api_key ? "auth=#{@blekko.api_key}" : nil
+ blekko.api_key ? "auth=#{blekko.api_key}" : nil
end
def params(page_number)
[page_size_param, auth_param, page_number_param(page_number)].compact.join("&")
end
def url(page_number)
- @blekko.protocol + @blekko.host + PREFIX + escaped_query + RESPONSE_FORMAT + params(page_number)
+ blekko.protocol + blekko.host + PREFIX + escaped_query + RESPONSE_FORMAT + params(page_number)
end
end
end
\ No newline at end of file