faraday-encoding 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +2 -0
- data/faraday-encoding.gemspec +25 -0
- data/lib/faraday-encoding.rb +1 -0
- data/lib/faraday/encoding.rb +19 -0
- data/spec/faraday/encoding_spec.rb +34 -0
- data/spec/spec_helper.rb +5 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c9e20bcab08b07b48ae880d62f8134eae931e28a
|
4
|
+
data.tar.gz: 8fff202baf5f45df5777cc604ff459c36559511d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 04412b83b8449b3b2f34f54f5375eef6db2ef6de29062480b2148cddf34477b82b50de3e04d854bbd9da3c34428900ac0c001d948addfe6902297592e80d8fee
|
7
|
+
data.tar.gz: 1676dd3ed7d6c54106f036d7d159584c5d6ac256a13003054452df2fd4343894ac54b6a1dd7149c835ee44408d5c2445a9e2e8b842f502d4aa37d3b202c01695
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Takayuki Matsubara
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Faraday::Encoding
|
2
|
+
|
3
|
+
A Faraday Middleware sets body encoding when specified by server.
|
4
|
+
|
5
|
+
## Motivation
|
6
|
+
|
7
|
+
Response body's encoding is set always ASCII-8BIT using with net/http adapter.
|
8
|
+
Net::HTTP doesn't handle encoding when server specifies encoding in content-type header.
|
9
|
+
Sometimes we caught an Error such as the following:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
body = Faraday.new(url: 'https://example.com').get('/').body
|
13
|
+
# body contains utf-8 string. ex: "赤坂"
|
14
|
+
body.to_json
|
15
|
+
# => raise Encoding::UndefinedConversionError: "\xE8" from ASCII-8BIT to UTF-8
|
16
|
+
```
|
17
|
+
|
18
|
+
That's why I wrote Farday::Encoding gem.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'faraday-encoding'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install faraday-encoding
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'faraday/encoding'
|
40
|
+
|
41
|
+
conn = Faraday.new do |connection|
|
42
|
+
connection.response :encoding # use Faraday::Encoding middleware
|
43
|
+
connection.adapter Faraday.default_adapter # net/http
|
44
|
+
end
|
45
|
+
|
46
|
+
response = conn.get '/nya.html' # content-type is specified as 'text/plain; charset=utf-8'
|
47
|
+
response.body.encoding
|
48
|
+
# => #<Encoding:UTF-8>
|
49
|
+
```
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it ( https://github.com/ma2gedev/faraday-encoding/fork )
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "faraday-encoding"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.authors = ["Takayuki Matsubara"]
|
9
|
+
spec.email = ["takayuki.1229@m3.com"]
|
10
|
+
spec.summary = %q{A Faraday Middleware sets body encoding when specified by server.}
|
11
|
+
spec.description = %q{A Faraday Middleware sets body encoding when specified by server.}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "faraday"
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'faraday/encoding'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "faraday"
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Faraday::Encoding < Faraday::Middleware
|
5
|
+
|
6
|
+
def call(environment)
|
7
|
+
@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
|
11
|
+
end
|
12
|
+
env[:body].force_encoding @content_charset if @content_charset
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Faraday::Response.register_middleware :encoding => Faraday::Encoding
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Faraday::Encoding do
|
4
|
+
let(:client) do
|
5
|
+
Faraday.new do |connection|
|
6
|
+
connection.response :encoding
|
7
|
+
connection.adapter :test, stubs
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:stubs) do
|
12
|
+
Faraday::Adapter::Test::Stubs.new do |stub|
|
13
|
+
stub.get('/') do
|
14
|
+
[response_status, response_headers, response_body]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:response_status) { 200 }
|
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)
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'http adapter return binary encoded body' do
|
28
|
+
it 'set encoding specified by http header' do
|
29
|
+
response = client.get('/')
|
30
|
+
expect(response.body.encoding).to eq(Encoding::UTF_8)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faraday-encoding
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Takayuki Matsubara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A Faraday Middleware sets body encoding when specified by server.
|
70
|
+
email:
|
71
|
+
- takayuki.1229@m3.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- faraday-encoding.gemspec
|
82
|
+
- lib/faraday-encoding.rb
|
83
|
+
- lib/faraday/encoding.rb
|
84
|
+
- spec/faraday/encoding_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
homepage: ''
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: A Faraday Middleware sets body encoding when specified by server.
|
110
|
+
test_files:
|
111
|
+
- spec/faraday/encoding_spec.rb
|
112
|
+
- spec/spec_helper.rb
|