Skip to content

Instantly share code, notes, and snippets.

@dje
Created January 17, 2010 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dje/279665 to your computer and use it in GitHub Desktop.
Save dje/279665 to your computer and use it in GitHub Desktop.
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