About
This Sleep code helps you access web services with REST style access. Also included are functions to process through XML.
Download: websrvc.tgz
Usage
This script provides the following Sleep functions to help you out:
$ post("http://url/to/service", %parameters)
Connects to the specified URL and does a POST request with the specified parameters. Returns a $handle for reading the results.
% buildDataStructure("<xml>...</xml>")
Creates a multidimensional Sleep hash from the XML document. This function returns the root element of the document. Each element is represented
as its own hash with the following keys:
- name (string) - this is the name of the tag
- text (string) - this is the raw contents of tag
- attributes (hash) - this is the attributes for the tag, a bunch of string key/value pairs
- children (array) - an array of elements (with this same structure)
% childrenToHash(%element)
Creates a Sleep hash from the children of the specified element. The hash contains each tag name as the key associated with its text value.
walkData(%element, &function)
Walks through the element's children and then the element itself. &function is called for each element with $0 set to the tagname and $1 to the element itself.
printNice(%data)
Pretty prints a Sleep array, hash, or scalar in a tree fashion. Useful for deducing what you got back with buildDataStructure.
Example
#
# Demo of Yahoo Search service from a Sleep script
#
# Adapted from: http://developer.yahoo.com/java/howto-reqRestJava.html
#
debug(7 | 34);
include("webservice.sl");
global('%parameters $handle $results $data');
#
# do the search
#
%parameters = %(
appid => "YahooDemo",
query => @ARGV[0],
results => 10);
$handle = post("http://api.search.yahoo.com/WebSearchService/V1/webSearch", %parameters);
$results = readb($handle, -1);
closef($handle);
#
# care to see what we got back? uncomment this line
#
#printNice(buildDataStructure($results));
#
# process the results--I provide some toys (childrenToHash, walkData) to help you.
#
local('%functions');
sub walkit
{
if ($0 eq "Result")
{
local('%data')
%data = childrenToHash($1);
println(%data['Title'] . ' @ ' . %data['Url'] . ' (' . formatDate(long(%data['ModificationDate']) * 1000, 'dd/MMM/yyyy:kk:mm:ss z') . ')');
}
}
$data = buildDataStructure($results);
println("Total results: " . $data["attributes"]["totalResultsAvailable"]);
walkData($data, &walkit);
To use this demo: java -jar sleep.jar yahoodemo.sl "Sleep 2.1"
Total results: 7800000
Sleep 2.1 - Java Scripting Language @ http://sleep.dashnine.org/ (03/Jul/2008:03:00:00 EDT)
Sleep 2.1 - What's New? @ http://sleep.dashnine.org/changelog.html (25/Jun/2008:03:00:00 EDT)
Sleep 2.1 b1 @ http://today.java.net/pub/n/4543 (05/Jul/2008:03:00:00 EDT)
Sleep 2.1-b3 @ http://today.java.net/pub/n/4821 (30/Jun/2008:03:00:00 EDT)
Sleep 2.1 Manual [Paperback] | Target Official Site @ http://www.target.com/Sleep-2-1-Manual-Mudge-Raphael/dp/143822723X (11/Jul/2008:03:00:00 EDT)
Sleep 2.1: Java Scripting Language | Javalobby @ http://java.dzone.com/announcements/sleep-21-java-scripting-langua (03/Jul/2008:03:00:00 EDT)
Sleep 2.1-b13 @ http://www.javalobby.org/java/forums/t91506.html (08/Jul/2008:03:00:00 EDT)
Sleep 2.1-b8 @ http://www.javalobby.org/java/forums/t83564.html (04/Jul/2008:03:00:00 EDT)
VSS Sleep Systems @ http://www.vsssleep.com/ (06/Feb/2006:03:00:00 EST)
Amazon.com: Sleep 2.1 Manual: Raphael Mudge: Books @ http://www.amazon.com/Sleep-2-1-Manual-Raphael-Mudge/dp/143822723X (11/Jul/2008:03:00:00 EDT)
Bugs
None that I know of. This is a really basic initial release. I've tested it with two different services so far. Please improve on it. :)
Contact
Raphael Mudge (rsmudge@gmail.com)