Create a gist now

Instantly share code, notes, and snippets.

MacRuby NSURLConnection example.
framework 'AppKit'
class Download
attr_reader :response, :responseBody
def start(request)
puts "START!"
NSURLConnection.connectionWithRequest(request, delegate:self)
end
def connection(connection, didReceiveResponse:response)
@response = response
@downloadData = NSMutableData.data
end
def connection(connection, didReceiveData:data)
@downloadData.appendData(data)
end
def connectionDidFinishLoading(connection)
case @response.statusCode
when 200...300
@responseBody = NSString.alloc.initWithData(@downloadData, encoding:NSUTF8StringEncoding)
puts "Downloaded: #{@responseBody}"
when 300...400
puts "TODO: Handle redirect!"
else
puts "Oh noes, an error occurred: #{@response.statusCode}"
end
NSApplication.sharedApplication.terminate(self)
end
end
# GET request:
#
request = NSMutableURLRequest.requestWithURL(NSURL.URLWithString("http://eekthecat.8m.com/"))
# POST request:
#
# postBody = "Current status: Kumbaya!"
# request.setHTTPMethod("POST")
# request.setHTTPBody(postBody.dataUsingEncoding(NSUTF8StringEncoding))
d = Download.new
d.start(request)
NSApplication.sharedApplication.run
@alloy
Owner

This is async. A sync call is even easier, you just need to use: http://bit.ly/efASex

@alloy
Owner

Run this example from the command-line. To use the code in an application, remove the messages sent to NSApplication#sharedInstance.

@colevoss

This works great and it returns the data that i am looking for. Only I canoot seem to access the data from anywhere but the connectionDidFinishLoading.

How might I get the data from there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment