simplified http wrapper for MacRuby/RubyMotion using delegation/ruby block
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

MacRuby HTTP Wrapper

Download helper making file download trivial.

The result of the download can be processed by a block or a delegator. A block will always take precedence over a delegator, meaning that you can't pass a delegator and use a block, you have to choose which approach you want to use. NOTE: in the case of a non blocking download, the delegation block can be called multiple times due to a bug in Cocoa. (or maybe it's an undocumented feature ;)).


url:: url of the resource to download

options:: options used for the query, see +MacRubyHTTP::Query.initialize+ for more details

&block:: block which is called when the file is downloaded


:method<String, Symbol>:: An optional value which represents the HTTP method to use

:payload:: - data to pass to a POST, PUT, DELETE query.

:delegation:: - class or instance to call when the file is downloaded. the handle_query_response method will be called on the passed object

:save_to:: - path to save the response to

:credential:: - should contains the :user key and :password key By default the credential will be saved for the entire session


:progress :: Proc to call everytime the downloader makes some progress



download("", {:save_to => '~/tmp/'.stringByStandardizingPath}) do |macruby|
  NSLog("file downloaded!")

download "" do |mr|
  # The response object has 3 accessors: status_code, headers and body
  NSLog("status: #{mr.status_code}, Headers: #{mr.headers.inspect}")

path = File.expand_path('~/macruby_tmp.html')
window :frame => [100, 100, 500, 500], :title => "HotCocoa" do |win|
  download "", {:save_to => path} do |homepage|
    win << label(:text => "status code: #{homepage.status_code}", :layout => {:start => false})
    win << label(:text => "Headers: #{homepage.headers.inspect}", :layout => {:start => false})

download "", :delegation => @downloaded_file, :method => 'PUT', :payload => {:name => 'matt aimonetti'}

download "", :delegation => self

download "http://localhost:5984/couchrest-test/", :method => 'POST', :payload => '{"user":"","zip":92129}', :delegation => self

download("", {:credential => {:user => 'me', :password => 's3krit'}}) do |test|
  NSLog("response received: #{test.headers} #{test.status_code}")

# We can also do the same thing but synchronously and block the runloop  
download "", :immediate => true, :save_to => '~/tmp/site.html'.stringByStandardizingPath do |mr|
  p "file downloaded, let's continue"