Tải bản đầy đủ - 0 (trang)
Hack 75. Program Yahoo! with Ruby

Hack 75. Program Yahoo! with Ruby

Tải bản đầy đủ - 0trang

#!/usr/bin/ruby

#yahoo_search.rb

#AsimpleYahoo!searchscriptusingthe

#RubyAPI(http://premshree.seacrow.com/code/ruby/yahoo-ruby)

#Usagerubyyahoo_search.rb

#includetheyahoo-rubyAPI

require'yahoo-ruby'

#getthequeryparameter

query=ARGV[0]?ARGV[0]:exit

##

#createawebsearchobject:

#Arguments:

#1.AppID(Youcangetoneathttp://developer.yahoo.net)

#2.Thequery

#3.typecanbeoneof:'all','any'or'phrase'

#4.Theno.ofresults

##

obj=WebSearch.new('insertappID',query,'all',3)

#storetheresults--returnsanarrayofhashes

results=obj.parse_results



#nowloopovereachiteminresults,anddisplaythetitle,su

results.each{|result|





print"Title:\t#{result['Title']}\n"





print"Summary:\t#{result['Summary']}\n"





print"URL:\t#{result['Url']}\n"





print"=====================================\n\

}



Thefirstthingthisscriptlooksforistheyahoo-rubyAPI.Thenthe



scriptlooksforthequeryargument;ifanargumentisnot

present,thescriptexits.Afterfetchingthequeryfromthe

commandline,thescriptcreatesaWebSearchobjectandthen

callstheparse_resultsfunctiontogettheresultsasanarrayof

hashes.

TouseadifferentYahoo!SearchserviceImagesearch,for

exampleyouneedtocreateadifferentobject.



4.10.2.RunningtheHack

Runthescriptonthecommandline:

rubyyahoo_search.rbinsertquery



Soasearchforthetermrubywouldlooklikethis:

rubyyahoo_search.rbruby



Figure4-11.Yahoo!Searchresultsfor"ruby"



Figure4-11.showstheresultsofthesearch.



Asyoucansee,usingtheyahoo-rubylibrarymeansyoucan

integrateYahoo!datawithinyourRubyapplicationswithafew

linesofcode.

PremshreePillai



Hack76.ProgramYahoo!withREBOL



WithREBOL,youcanbuildagraphicalinterfaceto

Yahoo!withminimalcode.

RelativeExpression-basedObjectLanguage(REBOL)isa

technologythatprovidesalightweightmethodofdistributed

computingandcommunication.REBOLisamessaging

language,whichmeansitwasbuiltspecificallytosend

messagesacrossInternetprotocols.REBOLisavailableforfree,

andtherearetwovariationsavailablefordownload:



REBOL/Core

Thisisthekernel,theheartofREBOL,withacommand-line

interface.



REBOL/View

Thisisagraphicalclientversion,anextensionof

REBOL/Corefordeveloperswhowanttoworkvisually.

ThishackcreatesasimpleYahoo!Imagesearchwidgetwiththe

REBOL/Viewvariation.Thewidgetallowsyoutoentera

keyword,displaythefirstimageintheresults,andnavigate

betweenotherresultswithPreviousandNextbuttons.Thistype

ofgraphicaluserinterface(GUI),whichusesbuttonsina

familiardesktop-styleapplication,isquiteabitdifferentfrom

thescripting,command-lineapplicationsinthisbook.



Tobegin,downloadREBOL/Viewforyourplatformat

http://www.rebol.com/view-platforms.html.Next,you'llneedto

downloadamodifiedversionofGavinF.McKenzie'sSAXXML

parserathttp://premshree.seacrow.com/code/rebol/xmlparse.r/view.Placexml-parse.rintheworkingdirectorywhere

you'llcreatethewidget.



4.11.1.TheCode

UnlikewithmostGUIframeworks,creatingGUIsisabreezein

REBOL.



Inthefollowingcommentedcode,notethatcommentsinREBOLbegin

withasemicolon.



Savethefollowingcodetoafilecalledyahoo.randbesureto

includeyourownYahoo!applicationID:

REBOL[











Title:"YahooSearchWebServices"

File:%yahoo.r

Date:22-May-2005

Author:"PremshreePillai"





Purpose:{





Yahoo!SearchWebservicesdemo



}

]

;;

;loadtheSAXXMLParser

;;

do%xml-parse.r



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 75. Program Yahoo! with Ruby

Tải bản đầy đủ ngay(0 tr)

×