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 CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- afmotion (0.3)
4
+ afmotion (0.4)
5
5
  motion-cocoapods (>= 1.2.1)
6
6
 
7
7
  GEM
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 URL request and it'll convert your parameters into properly encoding multipart data:
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, mimeType: "image/png", name: "avatar")
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
@@ -1,6 +1,8 @@
1
1
  class AppDelegate
2
+ attr_accessor :image, :data
2
3
  def application(application, didFinishLaunchingWithOptions:launchOptions)
3
-
4
+ self.image = UIImage.imageNamed("test")
5
+ self.data = UIImagePNGRepresentation(self.image)
4
6
  true
5
7
  end
6
8
  end
@@ -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
- callback.call(nil, formData)
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
@@ -1,5 +1,5 @@
1
1
  module AFMotion
2
- VERSION = "0.3"
2
+ VERSION = "0.4"
3
3
 
4
4
  HTTP_METHODS = [:get, :post, :put, :delete, :patch]
5
5
  end
Binary file
@@ -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.3'
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: -3932181106009024666
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: -3932181106009024666
149
+ hash: -2510227457395499119
149
150
  requirements: []
150
151
  rubyforge_project:
151
152
  rubygems_version: 1.8.23