faraday_middleware-jsons 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 798aaea05c432235fe458f8b3c542380d98cfed2
4
- data.tar.gz: a77f3d75897f79d64eafdaf4fdc48b0e5455a9d3
3
+ metadata.gz: 48008ac826dd56410a97b505cc303db75d70fb73
4
+ data.tar.gz: e2871f34f3b3063d6665f65985ed6408418c99ee
5
5
  SHA512:
6
- metadata.gz: 67e82f4223807989518403759ba7ccdfc1e85a8d264263446092df25298a0234e60fd6bac1593f17f9ce79fd980207bba1282c0ea24f49e6dd19c19397b408e1
7
- data.tar.gz: 9d087c9057e42542873463a975763495a24e194c78e740639c7ab7e399d848f80a1c66bcfe6b95c162d1d7932e6ee053cc5a6af09585aa5a3563f06fd136c856
6
+ metadata.gz: f8a4f6db4ae060f08c866a7b08fe389581561ed2f764491c8b632e580060f4f7fd33e427172a01a3ec3d03fd68c504dd6db787314a1be7d4b088c8b0b9664b2d
7
+ data.tar.gz: 2fb72595e7f15fc30b38da89661397b8fdc3d565f9f3ade66c15ad32b84dc846aa3bc8f5b78c9b3b75a746563be927b98b9601c04d38c4902a87f6d718ae7095
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # 0.2.0
2
+
3
+ * add response middleware :jsons
4
+
5
+ # 0.1.0
6
+
7
+ * initial release
8
+ * add request middleware :jsons
data/README.md CHANGED
@@ -32,7 +32,7 @@ end
32
32
 
33
33
  ### Request Middleware
34
34
 
35
- #### :jsons
35
+ #### jsons
36
36
 
37
37
  Serialize as json if Content-Type matched.
38
38
  Defalut allows to searialize many variety of json type such as patch+json, hal+json.
@@ -41,9 +41,24 @@ Options:
41
41
 
42
42
  | key | description | default |
43
43
  |-----|-------------|---------|
44
- | :content_type | To be compared with Content-Type using === | /application\/(.*\+)?json/ |
44
+ | :content_type | To be compared with Content-Type using === | /^application\/(.*\+)?json/ |
45
45
  | :pretty | To be passed to MultiJson | false |
46
46
 
47
+ ### Response Middleware
48
+
49
+ #### jsons
50
+
51
+ Parse json if Content-Type matched.
52
+ Defalut allows to load many variety of json type such as patch+json, hal+json.
53
+
54
+ Options:
55
+
56
+ | key | description | default |
57
+ |-----|-------------|---------|
58
+ | :content_type | To be compared with Content-Type using === | /^application\/(.*\+)?json/ |
59
+ | :symbolize_keys | To be passed to MultiJson | false |
60
+ | :raise_error | When parse json failed it raise error. If this is false, body ramains as is | false |
61
+
47
62
  ## Contributing
48
63
 
49
64
  1. Fork it ( https://github.com/okitan/faraday-middleware-jsons/fork )
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -5,10 +5,14 @@ module FaradayMiddleware
5
5
  module Jsons
6
6
  # basic json support
7
7
  require "faraday_middleware/jsons/request/encoder"
8
- #require "faraday_middleware/jsons/response/json"
8
+ require "faraday_middleware/jsons/response/decoder"
9
9
 
10
10
  Faraday::Request.register_middleware(
11
11
  jsons: -> { ::FaradayMiddleware::Jsons::Encoder }
12
12
  )
13
+
14
+ Faraday::Response.register_middleware(
15
+ jsons: -> { ::FaradayMiddleware::Jsons::Decoder }
16
+ )
13
17
  end
14
18
  end
@@ -0,0 +1,38 @@
1
+ module FaradayMiddleware::Jsons
2
+ class Decoder < Faraday::Middleware
3
+ def initialize(app, options = {})
4
+ super(app)
5
+
6
+ @raise_error = options[:raise_error] || false
7
+ @symbolize_keys = options[:symbolize_keys] || false
8
+ @content_type = options[:content_type] || %r!^application/(.*\+)?json!
9
+ end
10
+
11
+ def call(env)
12
+ @app.call(env).on_complete do |env|
13
+ if has_body?(env) && match_content_type?(env)
14
+ env[:body] = parse(env[:body])
15
+ end
16
+ end
17
+ end
18
+
19
+ def parse(body)
20
+ begin
21
+ ::MultiJson.load(body, symbolize_keys: @symbolize_keys)
22
+ rescue => e
23
+ raise e if @raise_error
24
+ body
25
+ end
26
+ end
27
+
28
+ protected
29
+ def match_content_type?(env)
30
+ content_type = env[:response_headers]["Content-Type"]
31
+ @content_type === content_type
32
+ end
33
+
34
+ def has_body?(env)
35
+ (body = env[:body]) && !body.empty?
36
+ end
37
+ end
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_middleware-jsons
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - okitan
@@ -104,6 +104,7 @@ files:
104
104
  - ".gitignore"
105
105
  - ".rspec"
106
106
  - ".travis.yml"
107
+ - CHANGELOG.md
107
108
  - CODE_OF_CONDUCT.md
108
109
  - Gemfile
109
110
  - Guardfile
@@ -116,6 +117,7 @@ files:
116
117
  - faraday_middleware-jsons.gemspec
117
118
  - lib/faraday_middleware/jsons.rb
118
119
  - lib/faraday_middleware/jsons/request/encoder.rb
120
+ - lib/faraday_middleware/jsons/response/decoder.rb
119
121
  homepage: https://github.com/okitan/faraday_middleware-jsons
120
122
  licenses:
121
123
  - MIT