Created
June 16, 2011 19:28
-
-
Save dominic/1030032 to your computer and use it in GitHub Desktop.
Briquette App Request Handling
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
# Request.rb | |
# briquette | |
# | |
# Created by Dominic Dagradi on 11/13/10. | |
# Copyright 2010 Bearded. All rights reserved. | |
require 'rubygems' | |
require 'crack' | |
require 'TextHelper' | |
class Request < ASIHTTPRequest | |
include TextHelper | |
# Overrides intiWithURL to access string instead of NSURL | |
def initWithURL(_url) | |
url = NSURL.URLWithString(_url) | |
super(url) | |
self | |
end | |
def startSynchronous | |
setShouldAttemptPersistentConnection false | |
super | |
end | |
# TODO: why is this not 'startAsynchronous'? | |
def aync sender, selector | |
setShouldAttemptPersistentConnection false | |
setDelegate sender | |
setDidFinishSelector NSSelectorFromString(selector) | |
setDidFailSelector NSSelectorFromString(selector) | |
startAsynchronous | |
end | |
# Overrides NSString *responseString to parse JSON | |
def responseString | |
string = super | |
if string.nil? || string.strip.empty? | |
unless [200,201].include? responseStatusCode | |
Debug.log "Response string was empty with error #{responseStatusCode}: #{self.url.absoluteString}" | |
end | |
"" | |
else | |
if @parsed_response | |
# Return parsed response hash if it exists for performance reasons | |
@parsed_response | |
else | |
begin | |
# Convert to Ruby string | |
string = "" + string | |
# Force encoding hardcore | |
string = string.force_encoding("UTF-8").encode("UTF-8", {:invalid => :replace, :undef => :replace}) | |
Crack::JSON.parse(string) | |
rescue | |
puts "Crack parse error: " + string | |
"" | |
end | |
end | |
end | |
end | |
def handleStreamComplete | |
@parsed_response = self.responseString | |
super | |
end | |
#TODO: Implement this? | |
def requestFailed request | |
end | |
####################################### | |
# Class request methods | |
####################################### | |
def self.get url, delegate = nil, callback = "requestFinished:", site = nil, options = {} | |
url = site.nil? ? url : site.baseURL + url | |
# Set appropriate request configuration | |
request = Request.alloc.initWithURL(url) | |
request.addRequestHeader("Content-Type", :value => "application/json") # TODO: Make this configurable | |
request.setCredentials site, options | |
# If given a delegate, create an asynchronous request for it | |
if delegate | |
request.async delegate, callback | |
else | |
# Otherwise, block the main thread and wait for a response | |
request.startSynchronous | |
end | |
request | |
end | |
def self.post url, content = nil, delegate = nil, callback = "requestFinished:", site = nil, options = {} | |
url = site.nil? ? url : site.baseURL + url | |
# Set appropriate request configuration | |
request = Request.alloc.initWithURL(url) | |
request.setRequestMethod "POST" | |
request.addRequestHeader("Content-Type", :value => "application/json") # TODO: Make this configurable | |
request.setCredentials site, options | |
# Set POST body | |
request.appendPostData(content.to_data) unless content.nil? | |
# If given a delegate, create an asynchronous request for it | |
if delegate | |
request.async delegate, callback | |
else | |
# Otherwise, block the main thread and wait for a response | |
request.startSynchronous | |
end | |
request | |
end | |
def setCredentials site, options | |
options["username"] ||= Request.username | |
options["password"] ||= Request.password | |
if site.nil? | |
setUsername options["username"] | |
setPassword options["password"] | |
else | |
setUsername site.token | |
setPassword 'x' | |
end | |
end | |
####################################### | |
# Class configuration options | |
####################################### | |
def self.username= username | |
@username = username | |
end | |
def self.username | |
@username = "" if @username.nil? | |
@username | |
end | |
def self.password= password | |
@password = password | |
end | |
def self.password | |
@password = "" if @password.nil? | |
@password | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lol - thanks!!