faraday_middleware-multi_json 0.0.4 → 0.0.5
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/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/README.md +8 -3
- data/faraday_middleware-multi_json.gemspec +1 -1
- data/lib/faraday_middleware/multi_json.rb +21 -5
- data/spec/faraday_middleware/multi_json_spec.rb +25 -3
- metadata +3 -3
- data/.rvmrc +0 -1
    
        data/.travis.yml
    ADDED
    
    
    
        data/Gemfile
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            # FaradayMiddleware::MultiJson
         | 
| 2 2 |  | 
| 3 | 
            -
             | 
| 3 | 
            +
            Simple Faraday middleware that uses MultiJson to unobtrusively encode JSON requests and parse JSON responses.
         | 
| 4 4 |  | 
| 5 5 | 
             
            ## Installation
         | 
| 6 6 |  | 
| @@ -20,20 +20,25 @@ Or install it yourself as: | |
| 20 20 |  | 
| 21 21 | 
             
            The same as FaradayMiddleware::ParseJson:
         | 
| 22 22 |  | 
| 23 | 
            -
            ```
         | 
| 23 | 
            +
            ```ruby
         | 
| 24 24 | 
             
            require 'faraday_middleware/multi_json'
         | 
| 25 25 |  | 
| 26 26 | 
             
            connection = Faraday.new do |conn|
         | 
| 27 | 
            +
              conn.request :multi_json
         | 
| 27 28 | 
             
              conn.response :multi_json
         | 
| 28 29 | 
             
              conn.adapter  Faraday.default_adapter
         | 
| 29 30 | 
             
            end
         | 
| 30 31 |  | 
| 31 32 | 
             
            connection.get('http://example.com/example.json')
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            resp = connection.post 'http://example.com/example.json' do |req|
         | 
| 35 | 
            +
              req.body = {:hello => 'world'}
         | 
| 36 | 
            +
            end
         | 
| 32 37 | 
             
            ```
         | 
| 33 38 |  | 
| 34 39 | 
             
            ### Passing parser options
         | 
| 35 40 |  | 
| 36 | 
            -
            ```
         | 
| 41 | 
            +
            ```ruby
         | 
| 37 42 | 
             
            conn.response :multi_json, symbolize_keys: true
         | 
| 38 43 | 
             
            ```
         | 
| 39 44 |  | 
| @@ -12,7 +12,7 @@ Gem::Specification.new do |gem| | |
| 12 12 | 
             
              gem.test_files    = gem.files.grep(%r{^(test|spec|features)/})
         | 
| 13 13 | 
             
              gem.name          = 'faraday_middleware-multi_json'
         | 
| 14 14 | 
             
              gem.require_paths = ['lib']
         | 
| 15 | 
            -
              gem.version       = '0.0. | 
| 15 | 
            +
              gem.version       = '0.0.5'
         | 
| 16 16 |  | 
| 17 17 | 
             
              gem.add_dependency 'faraday_middleware'
         | 
| 18 18 | 
             
              gem.add_dependency 'multi_json'
         | 
| @@ -1,13 +1,29 @@ | |
| 1 1 | 
             
            require 'faraday_middleware/response_middleware'
         | 
| 2 | 
            +
            require 'faraday_middleware/request/encode_json'
         | 
| 2 3 |  | 
| 3 4 | 
             
            module FaradayMiddleware
         | 
| 4 | 
            -
               | 
| 5 | 
            -
                 | 
| 5 | 
            +
              module MultiJson
         | 
| 6 | 
            +
                class ParseJson < FaradayMiddleware::ResponseMiddleware
         | 
| 7 | 
            +
                  dependency 'multi_json'
         | 
| 6 8 |  | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            +
                  def parse(body)
         | 
| 10 | 
            +
                    ::MultiJson.load(body, @options) rescue body
         | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                class EncodeJson < FaradayMiddleware::EncodeJson
         | 
| 15 | 
            +
                  dependency 'multi_json'
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def initialize(app, *)
         | 
| 18 | 
            +
                    super(app)
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  def encode(data)
         | 
| 22 | 
            +
                    ::MultiJson.dump data
         | 
| 23 | 
            +
                  end
         | 
| 9 24 | 
             
                end
         | 
| 10 25 | 
             
              end
         | 
| 11 26 | 
             
            end
         | 
| 12 27 |  | 
| 13 | 
            -
            Faraday.register_middleware :response, :multi_json => FaradayMiddleware::MultiJson
         | 
| 28 | 
            +
            Faraday.register_middleware :response, :multi_json => FaradayMiddleware::MultiJson::ParseJson
         | 
| 29 | 
            +
            Faraday.register_middleware :request, :multi_json => FaradayMiddleware::MultiJson::EncodeJson
         | 
| @@ -1,7 +1,8 @@ | |
| 1 1 | 
             
            require 'spec_helper'
         | 
| 2 2 |  | 
| 3 | 
            -
            describe FaradayMiddleware::MultiJson do
         | 
| 4 | 
            -
              let(: | 
| 3 | 
            +
            describe FaradayMiddleware::MultiJson::ParseJson do
         | 
| 4 | 
            +
              let(:response) { {:a => 1, :b => 2} }
         | 
| 5 | 
            +
              let(:json) { ::MultiJson.dump(response) }
         | 
| 5 6 |  | 
| 6 7 | 
             
              def connection(options={})
         | 
| 7 8 | 
             
                Faraday.new do |builder|
         | 
| @@ -19,6 +20,27 @@ describe FaradayMiddleware::MultiJson do | |
| 19 20 | 
             
              end
         | 
| 20 21 |  | 
| 21 22 | 
             
              it 'should delegate options given by builder' do
         | 
| 22 | 
            -
                connection(symbolize_keys | 
| 23 | 
            +
                connection(:symbolize_keys => true).get('/').body.should == response
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
            end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            describe FaradayMiddleware::MultiJson::EncodeJson do
         | 
| 28 | 
            +
              let(:request) { {:a => 1, :b => 2} }
         | 
| 29 | 
            +
              let(:json) { ::MultiJson.dump(request) }
         | 
| 30 | 
            +
             | 
| 31 | 
            +
              def connection
         | 
| 32 | 
            +
                Faraday.new do |builder|
         | 
| 33 | 
            +
                  builder.request :multi_json
         | 
| 34 | 
            +
                  builder.adapter :test do |stub|
         | 
| 35 | 
            +
                    stub.post('/update', json) do
         | 
| 36 | 
            +
                      [200, {}, json]
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              it 'should parse the request body' do
         | 
| 43 | 
            +
                resp = connection.post('/update', request)
         | 
| 44 | 
            +
                resp.body.should == json
         | 
| 23 45 | 
             
              end
         | 
| 24 46 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: faraday_middleware-multi_json
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.5
         | 
| 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 | 
            +
            date: 2012-12-23 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: faraday_middleware
         | 
| @@ -68,7 +68,7 @@ extra_rdoc_files: [] | |
| 68 68 | 
             
            files:
         | 
| 69 69 | 
             
            - .gitignore
         | 
| 70 70 | 
             
            - .rspec
         | 
| 71 | 
            -
            - . | 
| 71 | 
            +
            - .travis.yml
         | 
| 72 72 | 
             
            - Gemfile
         | 
| 73 73 | 
             
            - LICENSE
         | 
| 74 74 | 
             
            - README.md
         | 
    
        data/.rvmrc
    DELETED
    
    | @@ -1 +0,0 @@ | |
| 1 | 
            -
            rvm use --create ruby-1.9.3-p194@faraday_middleware-multi_json
         |