Sunday, August 12, 2007

another Smallworld/Magik blog

It seems that another Smallworld/Magik blog has entered the blogosphere. Welcome to Mark at Smallworld Technical Blog!

Thursday, August 2, 2007

Magik HTTP requests; Open Source Magik; Reading RSS

Brad packaged a new release (in .zip file) of the Magik Components source tree here. Be sure to choose the latest version of "mclib CST 4.0 SWAF". This will include the dialog designer that I mentioned yesterday.

I wanted to highlight one of the cool components that I have found in this package. The module is called :mclib_http_interface. It provides a Magik interface to making HTTP requests to a server and then return the results back to you as a simple_xml_document. You can then use the API on simple_xml_document to parse information out.

To highlight the ease and power of this capability, I quickly hacked together some code that allows you to connect to the Yahoo!Weather RSS feed and get a weather forecast for your location. This is a simple example and you can clearly extend it to your heart's content.

Download the Magik Components product and register the accompanying product.def file in your curent image.

Load the :mclib_http_interface module (eg., sw_module_manager.load_module(:mclib_http_interface)

Now run the following block of code. As written, it will give you a weather forecast for Boulder, CO, USA in degrees Celsius. The example also shows how you can glean lat/long information from a feed and convert it to a Magik coordinate.

_block
# see http://developer.yahoo.com/weather/ for more information
# about the Weather RSS feed
_constant BASE_URL << "http://weather.yahooapis.com/forecastrss"
_constant QUERY_CHAR << %?
_constant QUERY << "p=80305&u=c" # forecast for USA ZipCode 80305 in Celsius
_constant URL << write_string(BASE_URL,QUERY_CHAR,QUERY)

_local hr << mclib:http_request.new_for_url(_unset,_unset,URL)
_local response << hr.connect()
_local a_xml_doc << response.smart_result()

_local rss_channel << a_doc.top_element.element_matching_name(:channel)

_local item << rss_channel.element_matching_name(:item)

_local long << item.element_matching_name("geo:long").elements[1].as_number()
_local lat << item.element_matching_name("geo:lat").elements[1].as_number()

write()
write(item.element_matching_name(:title).elements[1]," at ",coordinate.new(long,lat))


_local forecast_attributes

_for a_forecast _over item.elements_matching_name("yweather:forecast")
_loop
forecast_attributes << a_forecast.attributes

write(forecast_attributes[:day]," (",forecast_attributes[:date],") ",forecast_attributes[:text]," low=",forecast_attributes[:low]," high=",forecast_attributes[:high]," ")
_endloop
_endblock
$


Incidentally, you can get more information about the Yahoo!Weather API at http://developer.yahoo.com/weather

Where I think this http_interface could come in really handy is for you to write GeoRSS parsers that would allow you to show location events on a Smallworld map (maybe as a SOM or a post_render_set). Have a look at Direction Magazine's Fun with GeoRSS article if you are interested in other ideas for interfacing with other data sources in real time from Smallworld.