Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP module: Adds payload as query string in GET requests #12

Merged
merged 1 commit into from
May 9, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def initialize(url, http_method = :get, options={})
@payload = options.delete(:payload)
@credentials = options.delete(:credentials) || {}
@credentials = {:username => '', :password => ''}.merge(@credentials)
@timeout = options.delete(:timeout) || 30.0
headers = options.delete(:headers)
if headers
@headers = {}
Expand All @@ -127,17 +128,24 @@ def initialize(url, http_method = :get, options={})
def initiate_request(url_string)
# http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsrunloop_Class/Reference/Reference.html#//apple_ref/doc/constant_group/Run_Loop_Modes
# NSConnectionReplyMode

unless @payload.nil?
@payload = @payload.map{|k,v| "#{k}=#{v}"}.join('&') if @payload.is_a?(Hash)
if @method == "GET"
url_string = "#{url_string}?#{@payload}"
end
end

p "HTTP building a NSRequest for #{url_string}"# if SETTINGS[:debug]
@url = NSURL.URLWithString(url_string)
@request = NSMutableURLRequest.requestWithURL(@url,
cachePolicy:NSURLRequestUseProtocolCachePolicy,
timeoutInterval:30.0)
timeoutInterval:@timeout)
@request.setHTTPMethod @method
@request.setAllHTTPHeaderFields(@headers) if @headers

# @payload needs to be converted to data
unless method == :get || @payload.nil?
@payload = @payload.map{|k,v| "#{k}=#{v}"}.join('&') if @payload.is_a?(Hash)
unless @method == "GET" || @payload.nil?
@payload = @payload.to_s.dataUsingEncoding(NSUTF8StringEncoding)
@request.setHTTPBody @payload
end
Expand Down