afmotion 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- afmotion (0.2)
4
+ afmotion (0.3)
5
5
  motion-cocoapods (>= 1.2.1)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (3.2.8)
10
+ activesupport (3.2.9)
11
11
  i18n (~> 0.6)
12
12
  multi_json (~> 1.0)
13
13
  addressable (2.3.2)
@@ -32,12 +32,12 @@ GEM
32
32
  json (1.7.5)
33
33
  motion-cocoapods (1.2.1)
34
34
  cocoapods (>= 0.14.0)
35
- multi_json (1.3.7)
35
+ multi_json (1.5.0)
36
36
  multipart-post (1.1.5)
37
- octokit (1.18.0)
37
+ octokit (1.19.0)
38
38
  addressable (~> 2.2)
39
39
  faraday (~> 0.8)
40
- faraday_middleware (~> 0.8)
40
+ faraday_middleware (~> 0.9)
41
41
  hashie (~> 1.2)
42
42
  multi_json (~> 1.3)
43
43
  open4 (1.3.0)
data/README.md CHANGED
@@ -162,6 +162,40 @@ end
162
162
 
163
163
  If you're constantly used one web service, you can use the `AFMotion::Client.shared` variable have a common reference. It can be set like a normal variable or created with `AFMotion::Client.build_shared`.
164
164
 
165
+ `AFHTTPClient` supports methods of the form `AFHTTPClient#get/post/put/patch/delete(url, request_parameters)`. The `request_parameters` is a hash containing your parameters to attach as the request body or URL parameters, depending on request type. For example:
166
+
167
+ ```ruby
168
+ client.get("users", id: 1) do |result|
169
+ ...
170
+ end
171
+
172
+ client.post("users", name: "@clayallsopp", library: "AFMotion") do |result|
173
+ ...
174
+ end
175
+ ```
176
+
177
+ #### Multipart Requests
178
+
179
+ `AFHTTPClient` supports multipart form requests (i.e. for image uploading). Simply prepend `multipart` to any other URL request and it'll convert your parameters into properly encoding multipart data:
180
+
181
+ ```ruby
182
+ # an instance of UIImage
183
+ image = my_function.get_image
184
+ data = UIImagePNGRepresentation(image)
185
+
186
+ client.multipart.post("avatars") do |result, form_data|
187
+ if form_data
188
+ # Called before request runs
189
+ # see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
190
+ form_data.appendPartWithFileData(data, mimeType: "image/png", name: "avatar")
191
+ elsif result.success?
192
+ ...
193
+ else
194
+ ...
195
+ end
196
+ end
197
+ ```
198
+
165
199
  #### Client Building DSL
166
200
 
167
201
  The `AFMotion::Client` DSL allows the following properties:
@@ -80,18 +80,51 @@ class AFHTTPClient
80
80
  AFMotion::HTTP_METHODS.each do |method|
81
81
  # EX client.get('my/resource.json')
82
82
  define_method "#{method}", -> (path, parameters = {}, &callback) do
83
- fn = "#{method}Path:parameters:success:failure:"
84
- self.send(fn, path, parameters,
85
- lambda {|operation, responseObject|
86
- result = AFMotion::HTTPResult.new(operation, responseObject, nil)
87
- callback.call(result)
88
- }, lambda {|operation, error|
89
- result = AFMotion::HTTPResult.new(operation, nil, error)
90
- callback.call(result)
91
- })
83
+ if @multipart
84
+ multipart_callback = callback.arity == 1 ? nil : lambda { |formData|
85
+ callback.call(nil, formData)
86
+ }
87
+ request = self.multipartFormRequestWithMethod(method, path: path,
88
+ parameters: parameters,constructingBodyWithBlock: multipart_callback)
89
+ operation = self.HTTPRequestOperationWithRequest(request,
90
+ success: lambda {|operation, responseObject|
91
+ result = AFMotion::HTTPResult.new(operation, responseObject, nil)
92
+ case callback.arity
93
+ when 1
94
+ callback.call(result)
95
+ when 2
96
+ callback.call(result, nil)
97
+ end
98
+ }, failure: lambda {|operation, error|
99
+ result = AFMotion::HTTPResult.new(operation, nil, error)
100
+ case callback.arity
101
+ when 1
102
+ callback.call(result)
103
+ when 2
104
+ callback.call(result, nil)
105
+ end
106
+ })
107
+ self.enqueueHTTPRequestOperation(operation)
108
+ @multipart = nil
109
+ else
110
+ fn = "#{method}Path:parameters:success:failure:"
111
+ self.send(fn, path, parameters,
112
+ lambda {|operation, responseObject|
113
+ result = AFMotion::HTTPResult.new(operation, responseObject, nil)
114
+ callback.call(result)
115
+ }, lambda {|operation, error|
116
+ result = AFMotion::HTTPResult.new(operation, nil, error)
117
+ callback.call(result)
118
+ })
119
+ end
92
120
  end
93
121
  end
94
122
 
123
+ def multipart
124
+ @multipart = true
125
+ self
126
+ end
127
+
95
128
  # options can be
96
129
  # - {username: ___, password: ____}
97
130
  # or
@@ -1,5 +1,5 @@
1
1
  module AFMotion
2
- VERSION = "0.2"
2
+ VERSION = "0.3"
3
3
 
4
4
  HTTP_METHODS = [:get, :post, :put, :delete, :patch]
5
5
  end
@@ -101,4 +101,37 @@ describe "AFHTTPClient" do
101
101
  @client.defaultValueForHeader("Authorization").split[0].should == "Token"
102
102
  end
103
103
  end
104
+
105
+ describe "#multipart" do
106
+ it "should trigger multipart logic" do
107
+ @client.multipart.should == @client
108
+ @client.instance_variable_get("@multipart").should == true
109
+ end
110
+
111
+ it "should trigger multipart request" do
112
+ @client.multipart.post("", test: "Herp") do |result|
113
+ @result = result
114
+ resume
115
+ end
116
+
117
+ wait_max(10) do
118
+ @result.should.not == nil
119
+ @result.operation.request.valueForHTTPHeaderField("Content-Type").include?("multipart/form-data").should == true
120
+ end
121
+ end
122
+
123
+ it "should work with form data" do
124
+ @client.multipart.post("", test: "Herp") do |result, form_data|
125
+ if result
126
+ resume
127
+ else
128
+ @form_data = form_data
129
+ end
130
+ end
131
+
132
+ wait_max(10) do
133
+ @form_data.should.not == nil
134
+ end
135
+ end
136
+ end
104
137
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: afmotion
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-08 00:00:00.000000000 Z
12
+ date: 2012-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: motion-cocoapods
@@ -136,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  segments:
138
138
  - 0
139
- hash: 77784384284444777
139
+ hash: -3932181106009024666
140
140
  required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  none: false
142
142
  requirements:
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  segments:
147
147
  - 0
148
- hash: 77784384284444777
148
+ hash: -3932181106009024666
149
149
  requirements: []
150
150
  rubyforge_project:
151
151
  rubygems_version: 1.8.23