faraday-encoding 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +2 -0
- data/faraday-encoding.gemspec +1 -1
- data/lib/faraday/encoding.rb +36 -5
- data/spec/faraday/encoding_spec.rb +37 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89fdc80352bd6ee60754f19ea6fbf6b213e43198
|
4
|
+
data.tar.gz: 116f0cdfc9c4135a391ca9a2c955a7d5d20ecd07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be550d9f77dd7c161b00b1debda2c369c8da41e708af5e4edc06f71341ae2825aa7094a4ada3e1988c6186a7566ced1849ab636972273d57a010f1f15f0bf160
|
7
|
+
data.tar.gz: 6bc1bf936d5a0ac790c1d2da440168e07637b16ac71ea86df030859e5773798426c0d181e3dcf23663fa90319a1ef976aef6f55ff2cad3ddf98e2d6ec6998331
|
data/README.md
CHANGED
@@ -17,6 +17,8 @@ body.to_json
|
|
17
17
|
|
18
18
|
That's why I wrote Farday::Encoding gem.
|
19
19
|
|
20
|
+
SEE ALSO: [response.body is ASCII-8BIT when Content-Type is text/xml; charset=utf-8](https://github.com/lostisland/faraday/issues/139)
|
21
|
+
|
20
22
|
## Installation
|
21
23
|
|
22
24
|
Add this line to your application's Gemfile:
|
data/faraday-encoding.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "faraday-encoding"
|
7
|
-
spec.version = "0.0.
|
7
|
+
spec.version = "0.0.2"
|
8
8
|
spec.authors = ["Takayuki Matsubara"]
|
9
9
|
spec.email = ["takayuki.1229@m3.com"]
|
10
10
|
spec.summary = %q{A Faraday Middleware sets body encoding when specified by server.}
|
data/lib/faraday/encoding.rb
CHANGED
@@ -2,18 +2,49 @@ require "faraday"
|
|
2
2
|
|
3
3
|
module Faraday
|
4
4
|
class Faraday::Encoding < Faraday::Middleware
|
5
|
+
def self.mappings
|
6
|
+
{
|
7
|
+
'utf8' => 'utf-8'
|
8
|
+
}
|
9
|
+
end
|
5
10
|
|
6
11
|
def call(environment)
|
7
12
|
@app.call(environment).on_complete do |env|
|
8
|
-
@
|
9
|
-
|
10
|
-
|
13
|
+
@env = env
|
14
|
+
env[:body].force_encoding(content_charset) if content_charset
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
# @return [Encoding|NilClass] returns Encoding or nil
|
21
|
+
def content_charset
|
22
|
+
@content_charset ||= ::Encoding.find encoding_name rescue nil
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [String] returns a string representing encoding name if it is find in the CONTENT TYPE header
|
26
|
+
def encoding_name
|
27
|
+
@encoding_name ||= begin
|
28
|
+
if /charset=([^;|$]+)/.match(content_type)
|
29
|
+
mapped_encoding(Regexp.last_match(1))
|
11
30
|
end
|
12
|
-
env[:body].force_encoding @content_charset if @content_charset
|
13
31
|
end
|
14
32
|
end
|
15
33
|
|
34
|
+
# @param [String] encoding_name
|
35
|
+
# @return [String] tries to find a mapping for the encoding name
|
36
|
+
# ex: returns 'utf-8' for encoding_name 'utf8'
|
37
|
+
# if mapping is not found - return the same input parameter `encoding_name`
|
38
|
+
# Look at `self.mappings` to see which mappings are available
|
39
|
+
def mapped_encoding(encoding_name)
|
40
|
+
self.class.mappings.fetch(encoding_name, default = encoding_name)
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [String]
|
44
|
+
def content_type
|
45
|
+
@env[:response_headers][:content_type]
|
46
|
+
end
|
16
47
|
end
|
17
48
|
end
|
18
49
|
|
19
|
-
Faraday::Response.register_middleware :
|
50
|
+
Faraday::Response.register_middleware encoding: Faraday::Encoding
|
@@ -18,17 +18,50 @@ describe Faraday::Encoding do
|
|
18
18
|
|
19
19
|
let(:response_status) { 200 }
|
20
20
|
let(:response_headers) do
|
21
|
-
{ 'content-type' =>
|
22
|
-
end
|
23
|
-
let(:response_body) do
|
24
|
-
'ねこ'.force_encoding(Encoding::ASCII_8BIT)
|
21
|
+
{ 'content-type' => "text/plain; charset=#{response_encoding}" }
|
25
22
|
end
|
26
23
|
|
27
24
|
context 'http adapter return binary encoded body' do
|
25
|
+
let(:response_encoding) do
|
26
|
+
'utf-8'
|
27
|
+
end
|
28
|
+
let(:response_body) do
|
29
|
+
'ねこ'.force_encoding(Encoding::ASCII_8BIT)
|
30
|
+
end
|
31
|
+
|
28
32
|
it 'set encoding specified by http header' do
|
29
33
|
response = client.get('/')
|
30
34
|
expect(response.body.encoding).to eq(Encoding::UTF_8)
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
38
|
+
context 'deal correctly with a non standard encoding names' do
|
39
|
+
context 'utf8' do
|
40
|
+
let(:response_encoding) do
|
41
|
+
'utf8'
|
42
|
+
end
|
43
|
+
let(:response_body) do
|
44
|
+
'abc'.force_encoding(Encoding::ASCII_8BIT)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'set encoding to utf-8' do
|
48
|
+
response = client.get('/')
|
49
|
+
expect(response.body.encoding).to eq(Encoding::UTF_8)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'for unknown encoding' do
|
54
|
+
let(:response_encoding) do
|
55
|
+
'unknown-encoding'
|
56
|
+
end
|
57
|
+
let(:response_body) do
|
58
|
+
'abc'.force_encoding(Encoding::ASCII_8BIT)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'does not enforce encoding' do
|
62
|
+
response = client.get('/')
|
63
|
+
expect(response.body.encoding).to eq(Encoding::ASCII_8BIT)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
34
67
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-encoding
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takayuki Matsubara
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|