afmotion 0.3 → 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/README.md +21 -2
- data/app/app_delegate.rb +3 -1
- data/lib/afmotion/http_client.rb +21 -2
- data/lib/afmotion/version.rb +1 -1
- data/resources/test.png +0 -0
- data/spec/http_client_spec.rb +20 -0
- metadata +4 -3
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -176,7 +176,7 @@ end
|
|
|
176
176
|
|
|
177
177
|
#### Multipart Requests
|
|
178
178
|
|
|
179
|
-
`AFHTTPClient` supports multipart form requests (i.e. for image uploading). Simply prepend `multipart` to any other
|
|
179
|
+
`AFHTTPClient` supports multipart form requests (i.e. for image uploading). Simply prepend `multipart` to any other request method and it'll convert your parameters into properly encoded multipart data:
|
|
180
180
|
|
|
181
181
|
```ruby
|
|
182
182
|
# an instance of UIImage
|
|
@@ -187,7 +187,26 @@ client.multipart.post("avatars") do |result, form_data|
|
|
|
187
187
|
if form_data
|
|
188
188
|
# Called before request runs
|
|
189
189
|
# see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
|
|
190
|
-
form_data.appendPartWithFileData(data,
|
|
190
|
+
form_data.appendPartWithFileData(data, name: "avatar", fileName:"avatar.png", mimeType: "image/png")
|
|
191
|
+
elsif result.success?
|
|
192
|
+
...
|
|
193
|
+
else
|
|
194
|
+
...
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
If you want to track upload progress, you can add a third callback argument which returns the upload percentage between 0.0 and 1.0:
|
|
200
|
+
|
|
201
|
+
```ruby
|
|
202
|
+
client.multipart.post("avatars") do |result, form_data, progress|
|
|
203
|
+
if form_data
|
|
204
|
+
# Called before request runs
|
|
205
|
+
# see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
|
|
206
|
+
form_data.appendPartWithFileData(data, name: "avatar", fileName:"avatar.png", mimeType: "image/png")
|
|
207
|
+
elsif progress
|
|
208
|
+
# 0.0 <= progress <= 1.0
|
|
209
|
+
my_widget.update_progress(progress)
|
|
191
210
|
elsif result.success?
|
|
192
211
|
...
|
|
193
212
|
else
|
data/app/app_delegate.rb
CHANGED
data/lib/afmotion/http_client.rb
CHANGED
|
@@ -82,8 +82,16 @@ class AFHTTPClient
|
|
|
82
82
|
define_method "#{method}", -> (path, parameters = {}, &callback) do
|
|
83
83
|
if @multipart
|
|
84
84
|
multipart_callback = callback.arity == 1 ? nil : lambda { |formData|
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
callback.call(nil, formData)
|
|
86
|
+
}
|
|
87
|
+
upload_callback = callback.arity > 2 ? lambda { |bytes_written_now, total_bytes_written, total_bytes_expect|
|
|
88
|
+
case callback.arity
|
|
89
|
+
when 3
|
|
90
|
+
callback.call(nil, nil, total_bytes_written.to_f / total_bytes_expect.to_f)
|
|
91
|
+
when 5
|
|
92
|
+
callback.call(nil, nil, bytes_written_now, total_bytes_written, total_bytes_expect)
|
|
93
|
+
end
|
|
94
|
+
} : nil
|
|
87
95
|
request = self.multipartFormRequestWithMethod(method, path: path,
|
|
88
96
|
parameters: parameters,constructingBodyWithBlock: multipart_callback)
|
|
89
97
|
operation = self.HTTPRequestOperationWithRequest(request,
|
|
@@ -94,6 +102,10 @@ class AFHTTPClient
|
|
|
94
102
|
callback.call(result)
|
|
95
103
|
when 2
|
|
96
104
|
callback.call(result, nil)
|
|
105
|
+
when 3
|
|
106
|
+
callback.call(result, nil, nil)
|
|
107
|
+
when 5
|
|
108
|
+
callback.call(result, nil, nil, nil, nil)
|
|
97
109
|
end
|
|
98
110
|
}, failure: lambda {|operation, error|
|
|
99
111
|
result = AFMotion::HTTPResult.new(operation, nil, error)
|
|
@@ -102,8 +114,15 @@ class AFHTTPClient
|
|
|
102
114
|
callback.call(result)
|
|
103
115
|
when 2
|
|
104
116
|
callback.call(result, nil)
|
|
117
|
+
when 3
|
|
118
|
+
callback.call(result, nil, nil)
|
|
119
|
+
when 5
|
|
120
|
+
callback.call(result, nil, nil, nil, nil)
|
|
105
121
|
end
|
|
106
122
|
})
|
|
123
|
+
if upload_callback
|
|
124
|
+
operation.setUploadProgressBlock(upload_callback)
|
|
125
|
+
end
|
|
107
126
|
self.enqueueHTTPRequestOperation(operation)
|
|
108
127
|
@multipart = nil
|
|
109
128
|
else
|
data/lib/afmotion/version.rb
CHANGED
data/resources/test.png
ADDED
|
Binary file
|
data/spec/http_client_spec.rb
CHANGED
|
@@ -133,5 +133,25 @@ describe "AFHTTPClient" do
|
|
|
133
133
|
@form_data.should.not == nil
|
|
134
134
|
end
|
|
135
135
|
end
|
|
136
|
+
|
|
137
|
+
it "should have upload callback with raw progress" do
|
|
138
|
+
image = UIImage.imageNamed("test")
|
|
139
|
+
@data = UIImagePNGRepresentation(image)
|
|
140
|
+
@client = AFHTTPClient.clientWithBaseURL("http://bing.com/".to_url)
|
|
141
|
+
@client.multipart.post("", test: "Herp") do |result, form_data, progress|
|
|
142
|
+
if form_data
|
|
143
|
+
form_data.appendPartWithFileData(@data, name: "test", fileName:"test.png", mimeType: "image/png")
|
|
144
|
+
elsif progress
|
|
145
|
+
@progress ||= progress
|
|
146
|
+
elsif result
|
|
147
|
+
resume
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
wait_max(20) do
|
|
152
|
+
@progress.should <= 1.0
|
|
153
|
+
@progress.should.not == nil
|
|
154
|
+
end
|
|
155
|
+
end
|
|
136
156
|
end
|
|
137
157
|
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.
|
|
4
|
+
version: '0.4'
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -68,6 +68,7 @@ files:
|
|
|
68
68
|
- lib/afmotion/version.rb
|
|
69
69
|
- pkg/afmotion-0.0.2.gem
|
|
70
70
|
- pkg/afmotion-0.0.3.gem
|
|
71
|
+
- resources/test.png
|
|
71
72
|
- spec/http_client_spec.rb
|
|
72
73
|
- spec/http_spec.rb
|
|
73
74
|
- spec/ns_url_request_spec.rb
|
|
@@ -136,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
136
137
|
version: '0'
|
|
137
138
|
segments:
|
|
138
139
|
- 0
|
|
139
|
-
hash: -
|
|
140
|
+
hash: -2510227457395499119
|
|
140
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
142
|
none: false
|
|
142
143
|
requirements:
|
|
@@ -145,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
145
146
|
version: '0'
|
|
146
147
|
segments:
|
|
147
148
|
- 0
|
|
148
|
-
hash: -
|
|
149
|
+
hash: -2510227457395499119
|
|
149
150
|
requirements: []
|
|
150
151
|
rubyforge_project:
|
|
151
152
|
rubygems_version: 1.8.23
|