Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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