Permalink
Browse files

Support parameters to one-off URL requests

  • Loading branch information...
1 parent 3b3d1a3 commit c7c8011c33c45bc9df0898eef3952146c548c876 @clayallsopp committed Nov 16, 2012
Showing with 257 additions and 135 deletions.
  1. +1 −0 .gitignore
  2. +2 −2 Gemfile.lock
  3. +9 −1 README.md
  4. +4 −0 Rakefile
  5. +21 −0 lib/afmotion/http.rb
  6. +47 −0 lib/afmotion/patch/NSURLRequest_params.rb
  7. +1 −1 lib/afmotion/version.rb
  8. +41 −0 spec/ns_url_request_spec.rb
  9. +131 −131 vendor/Pods/Pods.bridgesupport
View
@@ -14,3 +14,4 @@ nbproject
*~
*.sw[po]
.eprj
+pkg/*
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- afmotion (0.0.4)
+ afmotion (0.1)
motion-cocoapods (>= 1.2.1)
GEM
@@ -32,7 +32,7 @@ GEM
json (1.7.5)
motion-cocoapods (1.2.1)
cocoapods (>= 0.14.0)
- multi_json (1.3.6)
+ multi_json (1.3.7)
multipart-post (1.1.5)
octokit (1.18.0)
addressable (~> 2.2)
View
@@ -115,11 +115,19 @@ end
There are wrappers which automatically run a URL request for a given URL and HTTP method, of the form:
```ruby
-AFMotion::[Operation Type].[HTTP method](url) do |result|
+AFMotion::[Operation Type].[HTTP method](url, [Parameters = {}]) do |result|
...
end
```
+Example:
+
+```ruby
+AFMotion::HTTP.get("http://google.com", q: "rubymotion") do |result|
+ # sends request to http://google.com?q=1
+end
+```
+
- `AFMotion::HTTP.get/post/put/patch/delete(url)...`
- `AFMotion::JSON.get/post/put/patch/delete(url)...`
- `AFMotion::XML.get/post/put/patch/delete(url)...`
View
@@ -11,4 +11,8 @@ require './lib/afmotion'
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'AFMotion'
+
+ app.pods do
+ pod "AFNetworking"
+ end
end
View
@@ -7,6 +7,11 @@ def self.included(base)
if !request.is_a?(NSURLRequest)
request = NSMutableURLRequest.requestWithURL(request_or_url.to_url)
request.HTTPMethod = method.upcase
+ if [:get, :head].member? method.downcase.to_sym
+ request.HTTPShouldUsePipelining = true
+ end
+ # SEE NSURLRequest_params.rb
+ request.parameters = parameters.merge(__encoding__: self.parameter_encoding)
end
operation = (self.request_module.for_request(request) do |result|
@@ -27,6 +32,10 @@ module HTTP
def request_module
AFMotion::Operation::HTTP
end
+
+ def parameter_encoding
+ AFFormURLParameterEncoding
+ end
end
module JSON
@@ -36,6 +45,10 @@ module JSON
def request_module
AFMotion::Operation::JSON
end
+
+ def parameter_encoding
+ AFJSONParameterEncoding
+ end
end
module XML
@@ -45,6 +58,10 @@ module XML
def request_module
AFMotion::Operation::XML
end
+
+ def parameter_encoding
+ AFFormURLParameterEncoding
+ end
end
module PLIST
@@ -54,5 +71,9 @@ module PLIST
def request_module
AFMotion::Operation::PLIST
end
+
+ def parameter_encoding
+ AFPropertyListParameterEncoding
+ end
end
end
@@ -0,0 +1,47 @@
+class NSMutableURLRequest
+ alias_method :url, :URL
+
+ def url=(url)
+ self.setURL(url.to_url)
+ end
+
+ def content_type=(content_type)
+ self.setValue(content_type, forHTTPHeaderField: "Content-Type")
+ end
+
+ def parameters=(params = {})
+ method = self.HTTPMethod
+
+ af_encoding = params[:__encoding__] || AFFormURLParameterEncoding
+ string_encoding = params[:__string_encoding__] || NSUTF8StringEncoding
+
+ params.delete :__encoding__
+ params.delete :__string_encoding__
+
+ if ["GET", "HEAD", "DELETE", "PUT"].member? method
+ url_string = String.new(self.url.absoluteString)
+ has_query = url_string.rangeOfString("?").location == NSNotFound
+ format = has_query ? "?" : "&"
+ encoded = AFQueryStringFromParametersWithEncoding(params, string_encoding)
+ self.url = (url_string << format) << encoded
+ else
+ charset = CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(string_encoding))
+ error = Pointer.new(:object)
+ case af_encoding
+ when AFFormURLParameterEncoding
+ self.content_type = "application/x-www-form-urlencoded; charset=%@" << charset
+ self.HTTPBody = AFQueryStringFromParametersWithEncoding(params, string_encoding).dataUsingEncoding(string_encoding)
+ when AFJSONParameterEncoding
+ self.content_type = "application/json; charset=%@" << charset
+ self.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: 0, error: error)
+ when AFPropertyListParameterEncoding
+ self.content_type = "application/x-plist; charset=%@" << charset
+ self.HTTPBody = NSPropertyListSerialization.dataWithPropertyList(params, format: NSPropertyListXMLFormat_v1_0, options: 0, error: error)
+ end
+
+ if error[0]
+ p "NSURLRequest #{self.inspect}#parameters=#{params.inspect} ERROR: #{error.localizedDescription}"
+ end
+ end
+ end
+end
View
@@ -1,5 +1,5 @@
module AFMotion
- VERSION = "0.0.4"
+ VERSION = "0.1"
HTTP_METHODS = [:get, :post, :put, :delete, :patch]
end
@@ -0,0 +1,41 @@
+describe "NSMutableURLRequest" do
+ describe "parameters=" do
+ before do
+ @base_url = "http://google.com"
+ end
+
+ ["GET", "HEAD", "DELETE", "PUT"].each do |method|
+ it "should work with #{method} requests" do
+ @request = NSMutableURLRequest.requestWithURL(NSURL.URLWithString(@base_url))
+
+ @request.HTTPMethod = method
+
+ @request.parameters = {herp: "derp", "another" => 3}
+ @request.url.absoluteString.should == "http://google.com?herp=derp&another=3"
+ end
+ end
+
+ [:default, :URL, :JSON, :XML].each do |encoding|
+ it "POST should work with #{encoding} encoding" do
+ @request = NSMutableURLRequest.requestWithURL(NSURL.URLWithString(@base_url))
+ @request.HTTPMethod = "POST"
+
+ parameters = {herp: "derp", "another" => 3}
+ case encoding
+ when :URL
+ @request.parameters = parameters.merge({__encoding__: AFFormURLParameterEncoding})
+ String.new(@request.HTTPBody).should == "herp=derp&another=3"
+ when :JSON
+ @request.parameters = parameters.merge({__encoding__: AFJSONParameterEncoding})
+ String.new(@request.HTTPBody).should == "{\"herp\":\"derp\",\"another\":3}"
+ when :XML
+ @request.parameters = parameters.merge({__encoding__: AFPropertyListParameterEncoding})
+ String.new(@request.HTTPBody).should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>another</key>\n\t<integer>3</integer>\n\t<key>herp</key>\n\t<string>derp</string>\n</dict>\n</plist>\n"
+ else
+ @request.parameters = parameters
+ String.new(@request.HTTPBody).should == "herp=derp&another=3"
+ end
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit c7c8011

Please sign in to comment.