Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed and extended the download example

Signed-off-by: Dr Nic Williams <drnicwilliams@gmail.com>
  • Loading branch information...
commit ef6b2ec9d1edabe066e736786eb9d068e7694460 1 parent 9de6c43
@mattetti mattetti authored committed
Showing with 20 additions and 9 deletions.
  1. +20 −9 download/lib/application.rb
View
29 download/lib/application.rb
@@ -8,23 +8,34 @@ def start
app.delegate = self
window :frame => [100, 100, 500, 500], :title => "Download And Scrape Html" do |win|
win << label(:text => "Hello from HotCocoa", :layout => {:start => false})
- win << (@didReceiveResponse = label(:text => "...", :layout => {:start => false}, :frame => [0, 0, 300, 20]))
- win << (@didReceiveData = label(:text => "...", :layout => {:start => false}, :frame => [0, 0, 300, 20]))
- url = NSURL.URLWithString("http://github.com/drnic")
- request = NSURLRequest.requestWithURL(url)
- NSURLConnection.connectionWithRequest(request, delegate: self)
+ win << (@status = label(:text => "...", :layout => {:start => false}, :frame => [0, 0, 300, 20]))
+ win << (@data = text_field(:text => "...", :layout => {:start => false}, :frame => [0, 0, 480, 400]))
+
+ initiate_request("http://github.com/drnic", self)
win.will_close { exit }
end
end
end
+ def initiate_request(url_string, delegator)
+ url = NSURL.URLWithString(url_string)
+ request = NSURLRequest.requestWithURL(url)
+ @connection = NSURLConnection.connectionWithRequest(request, delegate: delegator)
+ end
- def connection(connection, didReceiveResponse:didReceiveResponse)
- @didReceiveResponse.text = "connection_didReceiveResponse"
+ # Deal with the data response
+ #
+ # Note: in MacRuby, the method signature makes a method unique
+ # two methods can have the same name but different signatures
+ def connection(connection, didReceiveResponse:response)
+ @status.text = (response.statusCode == 200) ? "Retrieving latest videos" : "There was an issue while trying to access the latest videos"
end
- def connection(connection, didReceiveData:didReceiveData)
- @didReceiveData.text = "connection_didReceiveData"
+ def connection(connection, didReceiveData:receivedData)
+ @status.text = "Data retrieved"
+ page = NSString.alloc.initWithData(receivedData, encoding:NSUTF8StringEncoding)
+ NSLog(page)
+ @data.text = page
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.