faraday_middleware-multi_json 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ script: rspec
2
+
3
+ language: ruby
4
+
5
+ rvm:
6
+ - rbx-18mode
7
+ - rbx-19mode
8
+ - jruby-18mode
9
+ - jruby-19mode
10
+ - 1.8.7
11
+ - 1.9.2
12
+ - 1.9.3
13
+ - ruby-head
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in faraday_middleware-multi_json.gemspec
4
4
  gemspec
5
+
6
+ gem 'json', :platforms => [:ruby_18, :jruby]
7
+
8
+ gem 'rake', :group => :test
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # FaradayMiddleware::MultiJson
2
2
 
3
- A simple Faraday middleware that parses JSON responses with MultiJson for unobtrusiveness.
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.4'
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
- class MultiJson < ResponseMiddleware
5
- dependency 'multi_json'
5
+ module MultiJson
6
+ class ParseJson < FaradayMiddleware::ResponseMiddleware
7
+ dependency 'multi_json'
6
8
 
7
- def parse(body)
8
- ::MultiJson.load(body, @options) unless body.strip.empty?
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(:json) { ::MultiJson.dump(a:1, b:2) }
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: true).get('/').body.should == {a:1, b:2}
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
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-10-22 00:00:00.000000000 Z
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
- - .rvmrc
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