afmotion 2.2.0 → 3.0
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.
- checksums.yaml +5 -5
- data/.gitignore +2 -1
- data/.travis.yml +17 -4
- data/AFMotion.gemspec +3 -2
- data/README.md +44 -49
- data/Rakefile +5 -1
- data/lib/afmotion.rb +2 -2
- data/lib/afmotion/client_shared.rb +65 -76
- data/lib/afmotion/ext/AFHTTPSessionManager.rb +73 -0
- data/lib/afmotion/{patch → ext}/NSString_NSUrl.rb +0 -0
- data/lib/afmotion/{patch → ext}/UIImageView_url.rb +0 -0
- data/lib/afmotion/http.rb +28 -11
- data/lib/afmotion/http_result.rb +18 -15
- data/lib/afmotion/operation.rb +3 -52
- data/lib/afmotion/serializer.rb +0 -2
- data/lib/afmotion/session_client.rb +15 -116
- data/lib/afmotion/session_client_dsl.rb +143 -0
- data/lib/afmotion/version.rb +1 -1
- data/motionuser12_avatar.png +0 -0
- data/motionuser12_avatar@2x.png +0 -0
- data/railsuser3_avatar.png +0 -0
- data/railsuser3_avatar@2x.png +0 -0
- data/spec/http_spec.rb +32 -2
- data/spec/integration_spec.rb +77 -0
- data/spec/session_client_spec.rb +62 -25
- data/swiftlytalking4_avatar.png +0 -0
- data/swiftlytalking4_avatar@2x.png +0 -0
- data/vendor/Podfile.lock +20 -18
- metadata +41 -22
- data/Gemfile.lock +0 -64
- data/lib/afmotion/http_client.rb +0 -109
- data/spec/http_client_spec.rb +0 -186
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 | 
            -
             | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: b0972df3e642f569351b4c4f0897ca9969188a776751add18a3a58e0b9df0fbb
         | 
| 4 | 
            +
              data.tar.gz: 7d6968c3d2cadd9b9b9a8185e912307bf43c2f7ed1fd26a49b0437eececa1bf3
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: fff5abc13b536be3b7318989271362532ebecfb9a50c7bbbfc52379eed79c575c0574f8794a1dfa13b1343fb7b0afec3d586a2481ac7a9787c3d387c05c523c1
         | 
| 7 | 
            +
              data.tar.gz: ea3ae7b135bd574f31b0dd16ba990cc672c83823f56d6a1defe1fecdd0cbece06c066460347da7027ef7ad0b193b5ec191aa58f8582c18946dc6cb34e5740583
         | 
    
        data/.gitignore
    CHANGED
    
    
    
        data/.travis.yml
    CHANGED
    
    | @@ -2,11 +2,24 @@ language: objective-c | |
| 2 2 | 
             
            before_install:
         | 
| 3 3 | 
             
            - (ruby --version)
         | 
| 4 4 | 
             
            - sudo chown -R travis ~/Library/RubyMotion
         | 
| 5 | 
            -
            - mkdir -p ~/Library/RubyMotion/build
         | 
| 5 | 
            +
            - sudo mkdir -p ~/Library/RubyMotion/build
         | 
| 6 | 
            +
            - sudo chown -R travis ~/Library/RubyMotion/build
         | 
| 7 | 
            +
            - sudo motion update
         | 
| 8 | 
            +
            install:
         | 
| 6 9 | 
             
            - bundle install
         | 
| 7 | 
            -
            - pod setup
         | 
| 8 | 
            -
            - bundle exec rake pod:install
         | 
| 10 | 
            +
            - pod setup > /dev/null
         | 
| 11 | 
            +
            - bundle exec rake pod:install > /dev/null
         | 
| 12 | 
            +
            gemfile:
         | 
| 13 | 
            +
            - Gemfile
         | 
| 9 14 | 
             
            script: bundle exec rake spec
         | 
| 10 15 | 
             
            env:
         | 
| 11 16 | 
             
              global:
         | 
| 12 | 
            -
             | 
| 17 | 
            +
                - COCOAPODS_NO_REPO_UPDATE_OUTPUT=true
         | 
| 18 | 
            +
            deploy:
         | 
| 19 | 
            +
              provider: rubygems
         | 
| 20 | 
            +
              api_key:
         | 
| 21 | 
            +
                secure: cCwC+P6XvkgW+iE3LY9YpzL8rSwCVVlUQ9dThepSGFN9mmMOYYMhohVm9NS3kkFzCFKCzu80ATi65ndKukOCwLEgsQO4HM9fH2y0XfmYp/aooJUUxzakDA7XI9kWc0SpqURSRujZBobcJnt4HctMLIogihYYQ/DlILgyy+0fU7U=
         | 
| 22 | 
            +
              gem: afmotion
         | 
| 23 | 
            +
              on:
         | 
| 24 | 
            +
                tags: true
         | 
| 25 | 
            +
                repo: clayallsopp/afmotion
         | 
    
        data/AFMotion.gemspec
    CHANGED
    
    | @@ -15,7 +15,8 @@ Gem::Specification.new do |s| | |
| 15 15 | 
             
              s.test_files    = s.files.grep(%r{^(test|spec|features)/})
         | 
| 16 16 | 
             
              s.require_paths = ["lib"]
         | 
| 17 17 |  | 
| 18 | 
            -
              s.add_dependency "motion-cocoapods", ">= 1. | 
| 18 | 
            +
              s.add_dependency "motion-cocoapods", ">= 1.9.1"
         | 
| 19 19 | 
             
              s.add_dependency "motion-require", ">= 0.1"
         | 
| 20 20 | 
             
              s.add_development_dependency 'rake'
         | 
| 21 | 
            -
             | 
| 21 | 
            +
              s.add_development_dependency 'webstub', "~> 1.1.6"
         | 
| 22 | 
            +
            end
         | 
    
        data/README.md
    CHANGED
    
    | @@ -1,8 +1,8 @@ | |
| 1 1 | 
             
            # AFMotion
         | 
| 2 2 |  | 
| 3 | 
            -
            [](https://travis-ci.org/clayallsopp/afmotion) [](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fclayallsopp%2Fafmotion?ref=badge_small)
         | 
| 4 4 |  | 
| 5 | 
            -
            AFMotion is a thin RubyMotion wrapper for [AFNetworking](https://github.com/AFNetworking/AFNetworking), the absolute best networking library on iOS.
         | 
| 5 | 
            +
            AFMotion is a thin RubyMotion wrapper for [AFNetworking](https://github.com/AFNetworking/AFNetworking), the absolute best networking library on iOS and OS X.
         | 
| 6 6 |  | 
| 7 7 | 
             
            ## Usage
         | 
| 8 8 |  | 
| @@ -33,32 +33,45 @@ end | |
| 33 33 | 
             
            end
         | 
| 34 34 | 
             
            ```
         | 
| 35 35 |  | 
| 36 | 
            -
             | 
| 36 | 
            +
            #### Migration from AFMotion 2.x
         | 
| 37 37 |  | 
| 38 | 
            -
             | 
| 38 | 
            +
            _Breaking Change_
         | 
| 39 | 
            +
            Parameters must now be specified with the `params:` keyword arg.
         | 
| 39 40 |  | 
| 40 | 
            -
             | 
| 41 | 
            +
            AFMotion 2.x
         | 
| 41 42 |  | 
| 42 43 | 
             
            ```ruby
         | 
| 43 | 
            -
             | 
| 44 | 
            +
            AFMotion::HTTP.get("http://google.com", q: "rubymotion") do |result|
         | 
| 45 | 
            +
              # sends request to http://google.com?q=rubymotion
         | 
| 46 | 
            +
            end
         | 
| 47 | 
            +
            ```
         | 
| 44 48 |  | 
| 45 | 
            -
             | 
| 46 | 
            -
              header "Accept", "application/json"
         | 
| 49 | 
            +
            AFMotion 3.x
         | 
| 47 50 |  | 
| 48 | 
            -
             | 
| 51 | 
            +
            ```ruby
         | 
| 52 | 
            +
            AFMotion::HTTP.get("http://google.com", params: { q: "rubymotion" }) do |result|
         | 
| 53 | 
            +
              # sends request to http://google.com?q=rubymotion
         | 
| 49 54 | 
             
            end
         | 
| 50 55 | 
             
            ```
         | 
| 51 56 |  | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
            If you're using iOS7, you can use [`AFHTTPSessionManager`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Classes/AFHTTPSessionManager.html):
         | 
| 57 | 
            +
            This allows you to also pass in a progress_block or additional headers on the fly:
         | 
| 55 58 |  | 
| 56 59 | 
             
            ```ruby
         | 
| 57 | 
            -
             | 
| 60 | 
            +
            AFMotion::HTTP.get("http://url.com/large_file.mov", params: { quality: "high" }, progress_block: proc { |progress| update_progress(progress) }, headers: {}) do |result|
         | 
| 61 | 
            +
              # sends request to http://google.com?q=rubymotion
         | 
| 62 | 
            +
            end
         | 
| 63 | 
            +
            ```
         | 
| 64 | 
            +
             | 
| 65 | 
            +
            For grouping similar requests (AFHTTPSession), use `AFMotion::Client` (now exactly the same as `AFMotion::SessionClient`)
         | 
| 66 | 
            +
             | 
| 67 | 
            +
            #### AFMotion::Client
         | 
| 68 | 
            +
             | 
| 69 | 
            +
            If you're interacting with a web service, you can use [`AFHTTPRequestOperationManager`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Classes/AFHTTPRequestOperationManager.html) with this nice wrapper:
         | 
| 58 70 |  | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 71 | 
            +
            ```ruby
         | 
| 72 | 
            +
            # DSL Mapping to properties of AFHTTPRequestOperationManager
         | 
| 61 73 |  | 
| 74 | 
            +
            @client = AFMotion::Client.build("https://alpha-api.app.net/") do
         | 
| 62 75 | 
             
              header "Accept", "application/json"
         | 
| 63 76 |  | 
| 64 77 | 
             
              response_serializer :json
         | 
| @@ -91,7 +104,7 @@ You can also request arbitrary images: | |
| 91 104 |  | 
| 92 105 | 
             
            1. `gem install afmotion`
         | 
| 93 106 |  | 
| 94 | 
            -
            2. `require 'afmotion'` or add to your `Gemfile` (`gem 'afmotion' | 
| 107 | 
            +
            2. `require 'afmotion'` or add to your `Gemfile` (`gem 'afmotion'`)
         | 
| 95 108 |  | 
| 96 109 | 
             
            3. `rake pod:install`
         | 
| 97 110 |  | 
| @@ -103,8 +116,8 @@ Each AFMotion wrapper callback yields an `AFMotion::HTTPResult` object. This obj | |
| 103 116 |  | 
| 104 117 | 
             
            ```ruby
         | 
| 105 118 | 
             
            AFMotion::some_function do |result|
         | 
| 106 | 
            -
               | 
| 107 | 
            -
              p result. | 
| 119 | 
            +
              p result.task.inspect
         | 
| 120 | 
            +
              p result.status_code
         | 
| 108 121 |  | 
| 109 122 | 
             
              if result.success?
         | 
| 110 123 | 
             
                # result.object depends on the type of operation.
         | 
| @@ -134,7 +147,7 @@ end | |
| 134 147 | 
             
            Example:
         | 
| 135 148 |  | 
| 136 149 | 
             
            ```ruby
         | 
| 137 | 
            -
            AFMotion::HTTP.get("http://google.com", q: "rubymotion") do |result|
         | 
| 150 | 
            +
            AFMotion::HTTP.get("http://google.com", params: { q: "rubymotion" }) do |result|
         | 
| 138 151 | 
             
              # sends request to http://google.com?q=rubymotion
         | 
| 139 152 | 
             
            end
         | 
| 140 153 | 
             
            ```
         | 
| @@ -147,7 +160,7 @@ end | |
| 147 160 |  | 
| 148 161 | 
             
            ### HTTP Client
         | 
| 149 162 |  | 
| 150 | 
            -
            If you're constantly accesing a web service, it's a good idea to use an ` | 
| 163 | 
            +
            If you're constantly accesing a web service, it's a good idea to use an `AFHTTPSessionManager`. Things lets you add a common base URL and request headers to all the requests issued through it, like so:
         | 
| 151 164 |  | 
| 152 165 | 
             
            ```ruby
         | 
| 153 166 | 
             
            client = AFMotion::Client.build("https://alpha-api.app.net/") do
         | 
| @@ -162,42 +175,23 @@ client.get("stream/0/posts/stream/global") do |result| | |
| 162 175 | 
             
            end
         | 
| 163 176 | 
             
            ```
         | 
| 164 177 |  | 
| 165 | 
            -
            If you're using iOS7, you can use [`AFHTTPSessionManager`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Classes/AFHTTPSessionManager.html):
         | 
| 166 | 
            -
             | 
| 167 | 
            -
            ```ruby
         | 
| 168 | 
            -
            # DSL Mapping to properties of AFHTTPSessionManager
         | 
| 169 | 
            -
             | 
| 170 | 
            -
            client = AFMotion::SessionClient.build("https://alpha-api.app.net/") do
         | 
| 171 | 
            -
              session_configuration :default
         | 
| 172 | 
            -
             | 
| 173 | 
            -
              header "Accept", "application/json"
         | 
| 174 | 
            -
             | 
| 175 | 
            -
              response_serializer :json
         | 
| 176 | 
            -
            end
         | 
| 177 | 
            -
             | 
| 178 | 
            -
            client.get("stream/0/posts/stream/global") do |result|
         | 
| 179 | 
            -
              # result.task exists
         | 
| 180 | 
            -
              ...
         | 
| 181 | 
            -
            end
         | 
| 182 | 
            -
            ```
         | 
| 183 | 
            -
             | 
| 184 178 | 
             
            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`.
         | 
| 185 179 |  | 
| 186 180 | 
             
            `AFHTTPRequestOperationManager` & `AFHTTPSessionManager` support methods of the form `Client#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:
         | 
| 187 181 |  | 
| 188 182 | 
             
            ```ruby
         | 
| 189 | 
            -
            client.get("users", id: 1) do |result|
         | 
| 183 | 
            +
            client.get("users", params: { id: 1 }) do |result|
         | 
| 190 184 | 
             
              ...
         | 
| 191 185 | 
             
            end
         | 
| 192 186 |  | 
| 193 | 
            -
            client.post("users", name: "@clayallsopp", library: "AFMotion") do |result|
         | 
| 187 | 
            +
            client.post("users", params: { name: "@clayallsopp", library: "AFMotion" }) do |result|
         | 
| 194 188 | 
             
              ...
         | 
| 195 189 | 
             
            end
         | 
| 196 190 | 
             
            ```
         | 
| 197 191 |  | 
| 198 192 | 
             
            #### Multipart Requests
         | 
| 199 193 |  | 
| 200 | 
            -
            ` | 
| 194 | 
            +
            `AFHTTPSessionManager` support multipart form requests (i.e. for image uploading) - simply use `multipart_post` and it'll convert your parameters into properly encoded multipart data. For all other types of request data, use the `form_data` object passed to your callback:
         | 
| 201 195 |  | 
| 202 196 | 
             
            ```ruby
         | 
| 203 197 | 
             
            # an instance of UIImage
         | 
| @@ -207,7 +201,7 @@ data = UIImagePNGRepresentation(image) | |
| 207 201 | 
             
            client.multipart_post("avatars") do |result, form_data|
         | 
| 208 202 | 
             
              if form_data
         | 
| 209 203 | 
             
                # Called before request runs
         | 
| 210 | 
            -
                # see:  | 
| 204 | 
            +
                # see: http://cocoadocs.org/docsets/AFNetworking/2.5.0/Protocols/AFMultipartFormData.html
         | 
| 211 205 | 
             
                form_data.appendPartWithFileData(data, name: "avatar", fileName:"avatar.png", mimeType: "image/png")
         | 
| 212 206 | 
             
              elsif result.success?
         | 
| 213 207 | 
             
                ...
         | 
| @@ -219,17 +213,14 @@ end | |
| 219 213 |  | 
| 220 214 | 
             
            This is an instance of [`AFMultipartFormData`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Protocols/AFMultipartFormData.html).
         | 
| 221 215 |  | 
| 222 | 
            -
            If you want to track upload progress,  | 
| 216 | 
            +
            If you want to track upload progress, simply add a progress_block (Taking a single arg: `NSProgress`)
         | 
| 223 217 |  | 
| 224 218 | 
             
            ```ruby
         | 
| 225 | 
            -
            client.multipart_post("avatars") do |result, form_data | 
| 219 | 
            +
            client.multipart_post("avatars", progress_block: proc { |progress| update_progress(progress) }) do |result, form_data|
         | 
| 226 220 | 
             
              if form_data
         | 
| 227 221 | 
             
                # Called before request runs
         | 
| 228 222 | 
             
                # see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
         | 
| 229 223 | 
             
                form_data.appendPartWithFileData(data, name: "avatar", fileName:"avatar.png", mimeType: "image/png")
         | 
| 230 | 
            -
              elsif progress
         | 
| 231 | 
            -
                # 0.0 < progress < 1.0
         | 
| 232 | 
            -
                my_widget.update_progress(progress)
         | 
| 233 224 | 
             
              else
         | 
| 234 225 | 
             
              ...
         | 
| 235 226 | 
             
            end
         | 
| @@ -256,7 +247,7 @@ The `AFMotion::Client` & `AFMotion::SessionClient` DSLs allows the following pro | |
| 256 247 |  | 
| 257 248 | 
             
            - `header(header, value)`
         | 
| 258 249 | 
             
            - `authorization(username: ___, password: ____)` for HTTP Basic auth, or `authorization(token: ____)` for Token based auth.
         | 
| 259 | 
            -
            - `request_serializer(serializer)`. Allows you to set an [`AFURLRequestSerialization`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Protocols/AFURLRequestSerialization.html) for all your client's requests, which determines how data is encoded on the way to the server. So if your API is always going to be JSON, you should set `operation(:json)`. Accepts `:json` and `:plist`, or any instance of `AFURLRequestSerialization | 
| 250 | 
            +
            - `request_serializer(serializer)`. Allows you to set an [`AFURLRequestSerialization`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Protocols/AFURLRequestSerialization.html) for all your client's requests, which determines how data is encoded on the way to the server. So if your API is always going to be JSON, you should set `operation(:json)`. Accepts `:json` and `:plist`, or any instance of `AFURLRequestSerialization` and must be called before calling `header` or `authorization` or else the [headers will not be applied](https://github.com/clayallsopp/afmotion/issues/78).
         | 
| 260 251 | 
             
            - `response_serializer(serializer)`. Allows you to set an [`AFURLResponseSerialization`](http://cocoadocs.org/docsets/AFNetworking/2.0.0/Protocols/AFURLResponseSerialization.html), which determines how data is decoded once the server respnds. Accepts `:json`, `:xml`, `:plist`, `:image`, `:http`, or any instance of `AFURLResponseSerialization`.
         | 
| 261 252 |  | 
| 262 253 | 
             
            For `AFMotion::SessionClient` only:
         | 
| @@ -272,3 +263,7 @@ client = AFMotion::SessionClient.build("https://alpha-api.app.net/") do |client| | |
| 272 263 | 
             
              client.header "Accept", @custom_header
         | 
| 273 264 | 
             
            end
         | 
| 274 265 | 
             
            ```
         | 
| 266 | 
            +
             | 
| 267 | 
            +
            ## License
         | 
| 268 | 
            +
             | 
| 269 | 
            +
            [](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fclayallsopp%2Fafmotion?ref=badge_large)
         | 
    
        data/Rakefile
    CHANGED
    
    | @@ -1,14 +1,18 @@ | |
| 1 1 | 
             
            # -*- coding: utf-8 -*-
         | 
| 2 2 | 
             
            $:.unshift("/Library/RubyMotion/lib")
         | 
| 3 | 
            +
            $:.unshift("~/.rubymotion/rubymotion-templates") # Add this line
         | 
| 4 | 
            +
            require 'motion/project/template/gem/gem_tasks'
         | 
| 3 5 | 
             
            require 'motion/project/template/ios'
         | 
| 4 | 
            -
            require "bundler/gem_tasks"
         | 
| 5 6 | 
             
            require "bundler/setup"
         | 
| 6 7 | 
             
            Bundler.require :default
         | 
| 7 8 |  | 
| 8 9 | 
             
            $:.unshift("./lib/")
         | 
| 9 10 | 
             
            require './lib/afmotion'
         | 
| 10 11 |  | 
| 12 | 
            +
            require 'webstub'
         | 
| 13 | 
            +
             | 
| 11 14 | 
             
            Motion::Project::App.setup do |app|
         | 
| 12 15 | 
             
              # Use `rake config' to see complete project settings.
         | 
| 13 16 | 
             
              app.name = 'AFMotion'
         | 
| 17 | 
            +
              app.deployment_target = "10.0"
         | 
| 14 18 | 
             
            end
         | 
    
        data/lib/afmotion.rb
    CHANGED
    
    
| @@ -1,31 +1,4 @@ | |
| 1 1 | 
             
            module AFMotion
         | 
| 2 | 
            -
              # ported from https://github.com/AFNetworking/AFNetworking/blob/master/UIKit%2BAFNetworking/UIProgressView%2BAFNetworking.m
         | 
| 3 | 
            -
              class SessionObserver
         | 
| 4 | 
            -
             | 
| 5 | 
            -
                def initialize(task, callback)
         | 
| 6 | 
            -
                  @callback = callback
         | 
| 7 | 
            -
                  task.addObserver(self, forKeyPath:"state", options:0, context:nil)
         | 
| 8 | 
            -
                  task.addObserver(self, forKeyPath:"countOfBytesSent", options:0, context:nil)
         | 
| 9 | 
            -
                end
         | 
| 10 | 
            -
             | 
| 11 | 
            -
                def observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
         | 
| 12 | 
            -
                  if keyPath == "countOfBytesSent"
         | 
| 13 | 
            -
                    # Could be -1, see https://github.com/AFNetworking/AFNetworking/issues/1354
         | 
| 14 | 
            -
                    expectation = (object.countOfBytesExpectedToSend > 0) ? object.countOfBytesExpectedToSend.to_f : nil
         | 
| 15 | 
            -
                    @callback.call(nil, object.countOfBytesSent.to_f, expectation)
         | 
| 16 | 
            -
                  end
         | 
| 17 | 
            -
             | 
| 18 | 
            -
                  if keyPath == "state" && object.state == NSURLSessionTaskStateCompleted
         | 
| 19 | 
            -
                    begin
         | 
| 20 | 
            -
                      object.removeObserver(self, forKeyPath: "state")
         | 
| 21 | 
            -
                      object.removeObserver(self, forKeyPath: "countOfBytesSent")
         | 
| 22 | 
            -
                      @callback = nil
         | 
| 23 | 
            -
                    rescue
         | 
| 24 | 
            -
                    end
         | 
| 25 | 
            -
                  end
         | 
| 26 | 
            -
                end
         | 
| 27 | 
            -
              end
         | 
| 28 | 
            -
             | 
| 29 2 | 
             
              module ClientShared
         | 
| 30 3 | 
             
                def headers
         | 
| 31 4 | 
             
                  requestSerializer.headers
         | 
| @@ -39,29 +12,25 @@ module AFMotion | |
| 39 12 | 
             
                  requestSerializer.authorization = authorization
         | 
| 40 13 | 
             
                end
         | 
| 41 14 |  | 
| 42 | 
            -
                def multipart_post(path,  | 
| 43 | 
            -
                   | 
| 15 | 
            +
                def multipart_post(path, options = {}, &callback)
         | 
| 16 | 
            +
                  create_multipart_task(:post, path, options, &callback)
         | 
| 44 17 | 
             
                end
         | 
| 45 18 |  | 
| 46 | 
            -
                def multipart_put(path,  | 
| 47 | 
            -
                   | 
| 19 | 
            +
                def multipart_put(path, options = {}, &callback)
         | 
| 20 | 
            +
                  create_multipart_task(:put, path, options, &callback)
         | 
| 48 21 | 
             
                end
         | 
| 49 22 |  | 
| 50 | 
            -
                def  | 
| 51 | 
            -
                   | 
| 23 | 
            +
                def create_multipart_task(http_method, path, options = {}, &callback)
         | 
| 24 | 
            +
                  parameters = options[:params]
         | 
| 25 | 
            +
                  headers = options.fetch(:headers, {})
         | 
| 26 | 
            +
                  progress = options[:progress_block]
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  inner_callback = Proc.new do |result, form_data|
         | 
| 52 29 | 
             
                    case callback.arity
         | 
| 53 30 | 
             
                    when 1
         | 
| 54 31 | 
             
                      callback.call(result)
         | 
| 55 32 | 
             
                    when 2
         | 
| 56 33 | 
             
                      callback.call(result, form_data)
         | 
| 57 | 
            -
                    when 3
         | 
| 58 | 
            -
                      progress = nil
         | 
| 59 | 
            -
                      if total_bytes_written && total_bytes_expect
         | 
| 60 | 
            -
                        progress = total_bytes_written.to_f / total_bytes_expect.to_f
         | 
| 61 | 
            -
                      end
         | 
| 62 | 
            -
                      callback.call(result, form_data, progress)
         | 
| 63 | 
            -
                    when 5
         | 
| 64 | 
            -
                      callback.call(result, form_data, bytes_written_now, total_bytes_written, total_bytes_expect)
         | 
| 65 34 | 
             
                    end
         | 
| 66 35 | 
             
                  end
         | 
| 67 36 |  | 
| @@ -72,57 +41,77 @@ module AFMotion | |
| 72 41 | 
             
                    }
         | 
| 73 42 | 
             
                  end
         | 
| 74 43 |  | 
| 75 | 
            -
                  upload_callback = nil
         | 
| 76 | 
            -
                  if callback.arity > 2
         | 
| 77 | 
            -
                    upload_callback = lambda { |bytes_written_now, total_bytes_written, total_bytes_expect|
         | 
| 78 | 
            -
                      inner_callback.call(nil, nil, bytes_written_now, total_bytes_written, total_bytes_expect)
         | 
| 79 | 
            -
                    }
         | 
| 80 | 
            -
                  end
         | 
| 81 | 
            -
             | 
| 82 44 | 
             
                  http_method = http_method.to_s.upcase
         | 
| 83 45 | 
             
                  if http_method == "POST"
         | 
| 84 | 
            -
                     | 
| 46 | 
            +
                    task = self.POST(path,
         | 
| 85 47 | 
             
                      parameters: parameters,
         | 
| 48 | 
            +
                      headers: headers,
         | 
| 86 49 | 
             
                      constructingBodyWithBlock: multipart_callback,
         | 
| 87 | 
            -
                       | 
| 88 | 
            -
                       | 
| 50 | 
            +
                      progress: progress,
         | 
| 51 | 
            +
                      success: success_block_for_http_method(:post, inner_callback),
         | 
| 52 | 
            +
                      failure: failure_block(inner_callback))
         | 
| 89 53 | 
             
                  else
         | 
| 90 | 
            -
                     | 
| 54 | 
            +
                    task = self.PUT(path,
         | 
| 91 55 | 
             
                      parameters: parameters,
         | 
| 56 | 
            +
                      headers: headers,
         | 
| 92 57 | 
             
                      constructingBodyWithBlock: multipart_callback,
         | 
| 93 | 
            -
                       | 
| 94 | 
            -
                       | 
| 58 | 
            +
                      progress: progress,
         | 
| 59 | 
            +
                      success: success_block_for_http_method(:post, inner_callback),
         | 
| 60 | 
            +
                      failure: failure_block(inner_callback))
         | 
| 95 61 | 
             
                  end
         | 
| 96 | 
            -
                   | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
             | 
| 101 | 
            -
             | 
| 102 | 
            -
             | 
| 62 | 
            +
                  task
         | 
| 63 | 
            +
                end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                def create_task(http_method, path, options = {}, &callback)
         | 
| 66 | 
            +
                  parameters = options.fetch(:params, {})
         | 
| 67 | 
            +
                  headers = options.fetch(:headers, {})
         | 
| 68 | 
            +
                  progress = options[:progress_block]
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                  method_signature = "#{http_method.to_s.upcase}:parameters:headers:progress:success:failure"
         | 
| 71 | 
            +
                  success = success_block_for_http_method(http_method, callback)
         | 
| 72 | 
            +
                  failure = failure_block(callback)
         | 
| 73 | 
            +
                  method_and_args = [method_signature, path, parameters, headers, progress, success, failure]
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                  # HEAD doesn't take a progress arg
         | 
| 76 | 
            +
                  if http_method.to_s.upcase == "HEAD"
         | 
| 77 | 
            +
                    method_signature.gsub!("progress:", "")
         | 
| 78 | 
            +
                    method_and_args.delete_at(4)
         | 
| 103 79 | 
             
                  end
         | 
| 104 | 
            -
             | 
| 80 | 
            +
             | 
| 81 | 
            +
                  self.public_send(*method_and_args)
         | 
| 105 82 | 
             
                end
         | 
| 106 83 |  | 
| 107 | 
            -
                def  | 
| 108 | 
            -
                   | 
| 109 | 
            -
             | 
| 110 | 
            -
             | 
| 111 | 
            -
             | 
| 84 | 
            +
                def success_block_for_http_method(http_method, callback)
         | 
| 85 | 
            +
                  if http_method.downcase.to_sym == :head
         | 
| 86 | 
            +
                    return ->(task) {
         | 
| 87 | 
            +
                      result = AFMotion::HTTPResult.new(task, nil, nil)
         | 
| 88 | 
            +
                      callback.call(result)
         | 
| 89 | 
            +
                    }
         | 
| 90 | 
            +
                  end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                  ->(task, responseObject) {
         | 
| 93 | 
            +
                    result = AFMotion::HTTPResult.new(task, responseObject, nil)
         | 
| 94 | 
            +
                    callback.call(result)
         | 
| 95 | 
            +
                  }
         | 
| 112 96 | 
             
                end
         | 
| 113 97 |  | 
| 114 | 
            -
                 | 
| 98 | 
            +
                def failure_block(callback)
         | 
| 99 | 
            +
                  ->(task, error) {
         | 
| 100 | 
            +
                    result = AFMotion::HTTPResult.new(task, nil, error)
         | 
| 101 | 
            +
                    callback.call(result)
         | 
| 102 | 
            +
                  }
         | 
| 103 | 
            +
                end
         | 
| 115 104 |  | 
| 116 105 | 
             
                private
         | 
| 117 106 | 
             
                # To force RubyMotion pre-compilation of these methods
         | 
| 118 107 | 
             
                def dummy
         | 
| 119 | 
            -
                  self.GET("", parameters: nil, success: nil, failure: nil)
         | 
| 120 | 
            -
                  self.HEAD("", parameters: nil, success: nil, failure: nil)
         | 
| 121 | 
            -
                  self.POST("", parameters: nil, success: nil, failure: nil)
         | 
| 122 | 
            -
                  self.POST("", parameters: nil, constructingBodyWithBlock: nil, success: nil, failure: nil)
         | 
| 123 | 
            -
                  self.PUT("", parameters: nil, success: nil, failure: nil)
         | 
| 124 | 
            -
                  self.DELETE("", parameters: nil, success: nil, failure: nil)
         | 
| 125 | 
            -
                  self.PATCH("", parameters: nil, success: nil, failure: nil)
         | 
| 108 | 
            +
                  self.GET("", parameters: nil, headers: nil, progress: nil, success: nil, failure: nil)
         | 
| 109 | 
            +
                  self.HEAD("", parameters: nil, headers: nil, success: nil, failure: nil)
         | 
| 110 | 
            +
                  self.POST("", parameters: nil, headers: nil, progress: nil, success: nil, failure: nil)
         | 
| 111 | 
            +
                  self.POST("", parameters: nil, headers: nil, constructingBodyWithBlock: nil, progress: nil, success: nil, failure: nil)
         | 
| 112 | 
            +
                  self.PUT("", parameters: nil, headers: nil, progress: nil, success: nil, failure: nil)
         | 
| 113 | 
            +
                  self.DELETE("", parameters: nil, headers: nil, progress: nil, success: nil, failure: nil)
         | 
| 114 | 
            +
                  self.PATCH("", parameters: nil, headers: nil, progress: nil, success: nil, failure: nil)
         | 
| 126 115 | 
             
                end
         | 
| 127 116 | 
             
              end
         | 
| 128 | 
            -
            end
         | 
| 117 | 
            +
            end
         |