module Thoth::Helper::Ysearch
The YSearch helper provides search results using the Yahoo! Search API. Requires the json or json_pure gem.
Constants
- API_ID
Yahoo! Developer API key. Feel free to replace this with your own key.
- API_URL
Yahoo! Search API URL.
Private Instance Methods
yahoo_search(query, options = {})
click to toggle source
Performs a web search using the Yahoo! Search API and returns the results as a Hash. For details on the available options, see developer.yahoo.com/search/web/V1/webSearch.html
# File lib/thoth/helper/ysearch.rb, line 51 def yahoo_search(query, options = {}) options = {:format => 'html'}.merge(options).collect{|key, val| "#{key.to_s}=#{::CGI.escape(val.to_s)}"}.join('&') request = "#{API_URL}?appid=#{API_ID}&query=#{::CGI.escape(query)}&" + options + '&output=json' r = JSON.parse(open(request).read)['ResultSet'] # Parse the response into a more Rubyish format. data = { :available => r['totalResultsAvailable'], :end => r['totalResultsReturned'] + r['firstResultPosition'] - 1, :results => [], :start => r['firstResultPosition'], :returned => r['totalResultsReturned'] } r['Result'].each do |result| data[:results] << { :cache_size => result['Cache'] ? result['Cache']['Size'].to_i : 0, :cache_url => result['Cache'] ? result['Cache']['Url'] : '', :click_url => result['ClickUrl'], :mime => result['MimeType'], :modified => Time.at(result['ModificationDate']), :summary => result['Summary'], :title => result['Title'], :url => result['Url'] } end return data rescue => e raise SearchError, "Unable to retrieve search results: #{e}" end