[MacRuby-devel] [hotcocoa] http wrapper, what do you think?
Ernest N. Prabhakar, Ph.D.
prabhaka at apple.com
Mon Mar 30 13:43:57 PDT 2009
Hi Matt,
On Mar 30, 2009, at 11:08 AM, Matt Aimonetti wrote:
> As you can see from the example list, the method name download isn't
> always appropriate and maybe I should give it the http_query alias.
> What do you think?
> What's nice with this approach, is that on top of being simple, the
> query and the response handling are async so the rest of your code
> can be run and you get notified only when the response comes back.
>
> The underlying API looks like that: MacRubyHTTP::Query.new( url,
> http_method='GET', options={} )
> Queries don't run in a custom runloop yet but that's something I
> want to look at once IO work on the experimental branch will be done.
>
>
> So, here is my question, do you guys think that this is something
> that should be part of HotCocoa or it's better if it stays its own
> separate lib?
> Also, what do you think about having a default vendor folder for
> hotcocoa apps and autoload the vendor files if available?
>
Good stuff. I think it is worth iterating on as a separate lib for
now, and integrating it with HotCocoa later. For comparison, you may
also want to look at how "Shoes" handles downloads (below).
-- Ernie P.
http://help.shoooes.net/App.html
download(url: a string, styles)
Starts a download thread (much like XMLHttpRequest, if you're familiar
with JavaScript.) This method returns immediately and runs the
download in the background. Each download thread also fires start,
progress and finish events. You can send the download to a file or
just get back a string (in the finish event.)
If you attach a block to a download, it'll get called as the finish
event.
Shoes.app do
stack do
title "Searching Google", :size => 16
@status = para "One moment..."
# Search Google for 'shoes' and print the HTTP headers
download "http://www.google.com/search?q=shoes" do |goog|
@status.text = "Headers: " + goog.response.headers.inspect
end
end
end
And, if we wanted to use the downloaded data, we'd get it using
goog.response.body. This example is truly the simplest form of
download: pulling some web data down into memory and handling it once
it's done.
Another simple use of download is to save some web data to a file,
using the :save style.
Shoes.app do
stack do
title "Downloading Google image", :size => 16
@status = para "One moment..."
download "http://www.google.com/logos/nasa50th.gif",
:save => "nasa50th.gif" do
@status.text = "Okay, is downloaded."
end
end
end
In this case, you can still get the headers for the downloaded file,
but response.body will be nil, since the data wasn't saved to memory.
You will need to open the file to get the downloaded goods.
If you need to send certain headers or actions to the web server, you
can use the :method, :headers and :body styles to customize the HTTP
request. (And, if you need to go beyond these, you can always break
out Ruby's OpenURI class.)
Shoes.app do
stack do
title "POSTing to Google", :size => 16
@status = para "One moment..."
download "http://www.stevex.net/dump.php",
:method => "POST", :body => "v=1.0&q=shoes" do |dump|
require 'hpricot'
@status.text = Hpricot(dump.response.body).inner_text
end
end
end
As you can see from the above example, Shoes includes the Hpricot
library for parsing HTML.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20090330/9cf2fd70/attachment-0001.html>
More information about the MacRuby-devel
mailing list