Created
October 9, 2008 06:00
-
-
Save technicalpickles/15707 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'httparty' | |
module BOSS | |
class Options | |
cattr_accessor :appid | |
end | |
class Search | |
include HTTParty | |
base_uri 'http://boss.yahooapis.com' | |
def self.web(query) | |
options = { :query => {:appid => BOSS::Options.appid } } | |
results = get("/ysearch/web/v1/#{query}", options) | |
ResultSet.new(results['ysearchresponse']) | |
end | |
end | |
class ResultSet | |
attr_accessor :count, :start, :total_hits, :deep_hits, :results | |
def initialize(hash) | |
@count = hash['count'] | |
@start = hash['start'] | |
@total_hits = hash['totalhits'] | |
@deep_hits = hash['deephits'] | |
@results = hash['resultset_web'].map do |result_hash| | |
Result.new(result_hash) | |
end | |
end | |
def method_missing(sym, *args) | |
if @results.respond_to?(sym) | |
@results.send(sym, *args) | |
else | |
super | |
end | |
end | |
def respond_to?(*args) | |
@results.respond_to?(*args) | |
end | |
end | |
class Result | |
attr_accessor :abstract, :display, :click_url, :size, :title, :url | |
def initialize(hash) | |
@abstract = hash['abstract'] | |
@display = hash['display'] | |
@click_url = hash['clickurl'] | |
@size = hash['size'] | |
@title = hash['title'] | |
@url = hash['url'] | |
end | |
end | |
end | |
BOSS::Options.appid = 'GET UR OWN DANG KEY' | |
results = BOSS::Search.web('ruby') | |
puts results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment