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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9e20bcab08b07b48ae880d62f8134eae931e28a
4
- data.tar.gz: 8fff202baf5f45df5777cc604ff459c36559511d
3
+ metadata.gz: 89fdc80352bd6ee60754f19ea6fbf6b213e43198
4
+ data.tar.gz: 116f0cdfc9c4135a391ca9a2c955a7d5d20ecd07
5
5
  SHA512:
6
- metadata.gz: 04412b83b8449b3b2f34f54f5375eef6db2ef6de29062480b2148cddf34477b82b50de3e04d854bbd9da3c34428900ac0c001d948addfe6902297592e80d8fee
7
- data.tar.gz: 1676dd3ed7d6c54106f036d7d159584c5d6ac256a13003054452df2fd4343894ac54b6a1dd7149c835ee44408d5c2445a9e2e8b842f502d4aa37d3b202c01695
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:
@@ -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.1"
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.}
@@ -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
- @content_charset = nil
9
- if /;\s*charset=\s*(.+?)\s*(;|$)/.match(env[:response_headers][:content_type])
10
- @content_charset = ::Encoding.find $1 rescue nil
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 :encoding => Faraday::Encoding
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' => 'text/plain; charset=utf-8' }
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.1
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: 2015-03-20 00:00:00.000000000 Z
11
+ date: 2016-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler