Created
January 17, 2010 23:55
-
-
Save dje/279665 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework 'Cocoa' | |
NSApplication.sharedApplication | |
class Request | |
def get( url, &block ) | |
@buffer = NSMutableData.new | |
@block = block | |
request = NSMutableURLRequest.requestWithURL NSURL.URLWithString( url ) | |
NSURLConnection.alloc.initWithRequest( request, delegate:self ) | |
end | |
def connection( conn, didReceiveResponse:resp ) | |
@response = resp | |
@buffer.setLength 0 | |
end | |
def connection( conn, didReceiveData:data ) | |
@buffer.appendData data | |
end | |
def connection( conn, didFailWithError:err ) | |
NSLog "Request failed" | |
end | |
def connectionDidFinishLoading( conn ) | |
@block.call( NSString.alloc.initWithData @buffer, encoding:NSUTF8StringEncoding ) | |
end | |
end | |
queue = Dispatch::Queue.new 'test' | |
10.times do |get_uri| | |
queue.async do | |
req = Request.new | |
req.get( "http://macruby.com/" ) do |data| | |
NSLog data.size | |
end | |
end | |
end | |
NSApp.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment