The Code
Unlike with most GUI frameworks, creating GUIs is a breeze in REBOL.
TIP
In the following commented code, note that comments in REBOL begin with a semicolon.
Save the following code to a file called yahoo.r and be sure to include your own Yahoo! application ID:
REBOL [
Title: "Yahoo Search Web Services"
File: %yahoo.r
Date: 22-May-2005
Author: "Premshree Pillai"
Purpose: {
Yahoo! Search Web services demo
}
]
;;
; load the SAX XML Parser
;;
do %xml-parse.r
;;
; set the Application ID
;;
APP_ID: 'insert App ID'
;;
; This function takes two parameters
; 1. query
; 2. Application ID
; It returns the results as an array of maps
; The parsing bit is done by the parse handlers within the parser.
;;
ImageSearch: func [query app_id] [
return parse-xml+ read rejoin
[http://api.search.yahoo.com/ImageSearchService/V1/imageSearch '?query='
query '&appid=' app_id]
]
;;
; Define a layout
;;
out: layout [
;;
; This will appear as the heading in the widget
;;
H3 400 {
Yahoo Search Web Services
demo using REBOL
}
;;
; tells REBOL to place following elements across
;;
across
query: field
button "Search" [
ImageSearchResults: ImageSearch query/text 'rebol-yahoo' APP_ID
curr_count: 1 ; current result no.
img: load to-url ImageSearchResults/:curr_count/3/2
backface/image: img
show backface
]
;;
; tells REBOL to place the following elements below
;;
return
;;
; A text box
;;
backface: text 400x300 center
return
;;
; Navigate to previous search result
;;
button "Previous" [
curr_count: curr_count - 1
img: load to-url ImageSearchResults/:curr_count/3/2
backface/image: img
show backface
]
;;
; Navigate to next search result
;;
button "Next" [
curr_count: curr_count + 1
img: load to-url ImageSearchResults/:curr_count/3/2
backface/image: img
show backface
]
pad 0x5
]
;;
; Display the widget!
;;
view out
This minimal widget has no error handling. However, this code should give you a sense of how to use Yahoo!'s Search Web Services with REBOL. Note that the work of parsing the XML response is handled by the handlers in the SAX XML Parser. Modifications to the parse handler functions are commented with my name (Premshree Pillai). If you need to use any of the other Yahoo! Search Web Services, you might need to modify the SAX handlers.