faraday_hal_middleware 0.0.1
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 +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/faraday_hal_middleware.gemspec +25 -0
- data/lib/faraday_hal_middleware.rb +20 -0
- data/lib/faraday_hal_middleware/version.rb +3 -0
- data/lib/faraday_middleware/request/encode_hal_json.rb +52 -0
- data/lib/faraday_middleware/response/parse_hal_json.rb +7 -0
- data/spec/spec_helper.rb +44 -0
- data/spec/unit/encode_hal_json_spec.rb +94 -0
- data/spec/unit/parse_hal_json_spec.rb +79 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c3637cc5447e62741f3caaa3e072397a77f1f0cb
|
4
|
+
data.tar.gz: 8e04a568630bc6483f97244fca384369c4c45550
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 281711339d4ba5c6b6269ba6d030401554d503b7da0c90f52138416364c27770a96d0f772ab5c31656a2de410f8869da4007b0bcafdc9d7c8d603a763959dc3a
|
7
|
+
data.tar.gz: a52c62b9784cde48d71dc48f1dea6ae4588283c4e3a953ca0d03b78263ef32f40d746bca51b030901ab89af520a36956d62deee8cddc713fd9d99bf616f873eb
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Koen Punt <koen@fetch.nl>
|
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,29 @@
|
|
1
|
+
# FaradayHalMiddleware
|
2
|
+
|
3
|
+
Faraday Middleware for JSON HAL requests and responses
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'faraday_hal_middleware'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install faraday_hal_middleware
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/fetch/faraday_hal_middleware/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
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
|
+
require 'faraday_hal_middleware/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'faraday_hal_middleware'
|
8
|
+
spec.version = FaradayHalMiddleware::VERSION
|
9
|
+
spec.authors = ['Koen Punt']
|
10
|
+
spec.email = ['koen@fetch.nl']
|
11
|
+
spec.summary = %q{Faraday Middleware for JSON HAL requests and responses}
|
12
|
+
spec.description = %q{Faraday Middleware for JSON HAL requests and responses}
|
13
|
+
spec.homepage = 'https://github.com/fetch/faraday_hal_middleware'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
|
23
|
+
spec.add_dependency 'faraday_middleware', ['>= 0.9', '< 0.10']
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'faraday_hal_middleware/version'
|
2
|
+
require 'faraday'
|
3
|
+
require 'faraday_middleware'
|
4
|
+
|
5
|
+
module FaradayHalMiddleware
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
module FaradayMiddleware
|
10
|
+
autoload :EncodeHalJson, 'faraday_middleware/request/encode_hal_json'
|
11
|
+
autoload :ParseHalJson, 'faraday_middleware/response/parse_hal_json'
|
12
|
+
|
13
|
+
if Faraday::Middleware.respond_to? :register_middleware
|
14
|
+
Faraday::Request.register_middleware \
|
15
|
+
:hal_json => lambda { EncodeHalJson }
|
16
|
+
|
17
|
+
Faraday::Response.register_middleware \
|
18
|
+
:hal_json => lambda { ParseHalJson }
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module FaradayMiddleware
|
4
|
+
# Request middleware that encodes the body as JSON.
|
5
|
+
#
|
6
|
+
# Processes only requests with matching Content-type or those without a type.
|
7
|
+
# If a request doesn't have a type but has a body, it sets the Content-type
|
8
|
+
# to JSON MIME-type.
|
9
|
+
#
|
10
|
+
# Doesn't try to encode bodies that already are in string form.
|
11
|
+
class EncodeHalJson < Faraday::Middleware
|
12
|
+
CONTENT_TYPE = 'Content-Type'.freeze
|
13
|
+
MIME_TYPE = 'application/hal+json'.freeze
|
14
|
+
|
15
|
+
dependency do
|
16
|
+
require 'json' unless defined?(::JSON)
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(env)
|
20
|
+
match_content_type(env) do |data|
|
21
|
+
env[:body] = encode data
|
22
|
+
end
|
23
|
+
@app.call env
|
24
|
+
end
|
25
|
+
|
26
|
+
def encode(data)
|
27
|
+
::JSON.dump data
|
28
|
+
end
|
29
|
+
|
30
|
+
def match_content_type(env)
|
31
|
+
if process_request?(env)
|
32
|
+
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
|
33
|
+
yield env[:body] unless env[:body].respond_to?(:to_str)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def process_request?(env)
|
38
|
+
type = request_type(env)
|
39
|
+
has_body?(env) and (type.empty? or type == MIME_TYPE)
|
40
|
+
end
|
41
|
+
|
42
|
+
def has_body?(env)
|
43
|
+
body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
|
44
|
+
end
|
45
|
+
|
46
|
+
def request_type(env)
|
47
|
+
type = env[:request_headers][CONTENT_TYPE].to_s
|
48
|
+
type = type.split(';', 2).first if type.index(';')
|
49
|
+
type
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'webmock/rspec'
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module EnvCompatibility
|
6
|
+
def faraday_env(env)
|
7
|
+
if defined?(Faraday::Env)
|
8
|
+
Faraday::Env.from(env)
|
9
|
+
else
|
10
|
+
env
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ResponseMiddlewareExampleGroup
|
16
|
+
def self.included(base)
|
17
|
+
base.let(:options) { Hash.new }
|
18
|
+
base.let(:headers) { Hash.new }
|
19
|
+
base.let(:middleware) {
|
20
|
+
described_class.new(lambda {|env|
|
21
|
+
Faraday::Response.new(env)
|
22
|
+
}, options)
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def process(body, content_type = nil, options = {})
|
27
|
+
env = {
|
28
|
+
:body => body, :request => options,
|
29
|
+
:request_headers => Faraday::Utils::Headers.new,
|
30
|
+
:response_headers => Faraday::Utils::Headers.new(headers)
|
31
|
+
}
|
32
|
+
env[:response_headers]['content-type'] = content_type if content_type
|
33
|
+
yield(env) if block_given?
|
34
|
+
middleware.call(faraday_env(env))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
RSpec.configure do |config|
|
39
|
+
config.include EnvCompatibility
|
40
|
+
config.include ResponseMiddlewareExampleGroup, :type => :response
|
41
|
+
config.expect_with :rspec do |c|
|
42
|
+
c.syntax = :expect
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'faraday_middleware/request/encode_hal_json'
|
2
|
+
|
3
|
+
describe FaradayMiddleware::EncodeHalJson do
|
4
|
+
let(:middleware) { described_class.new(lambda{|env| env}) }
|
5
|
+
|
6
|
+
def process(body, content_type = nil)
|
7
|
+
env = {:body => body, :request_headers => Faraday::Utils::Headers.new}
|
8
|
+
env[:request_headers]['content-type'] = content_type if content_type
|
9
|
+
middleware.call(faraday_env(env))
|
10
|
+
end
|
11
|
+
|
12
|
+
def result_body() result[:body] end
|
13
|
+
def result_type() result[:request_headers]['content-type'] end
|
14
|
+
|
15
|
+
context "no body" do
|
16
|
+
let(:result) { process(nil) }
|
17
|
+
|
18
|
+
it "doesn't change body" do
|
19
|
+
expect(result_body).to be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "doesn't add content type" do
|
23
|
+
expect(result_type).to be_nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "empty body" do
|
28
|
+
let(:result) { process('') }
|
29
|
+
|
30
|
+
it "doesn't change body" do
|
31
|
+
expect(result_body).to be_empty
|
32
|
+
end
|
33
|
+
|
34
|
+
it "doesn't add content type" do
|
35
|
+
expect(result_type).to be_nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "string body" do
|
40
|
+
let(:result) { process('{"a":1}') }
|
41
|
+
|
42
|
+
it "doesn't change body" do
|
43
|
+
expect(result_body).to eq('{"a":1}')
|
44
|
+
end
|
45
|
+
|
46
|
+
it "adds content type" do
|
47
|
+
expect(result_type).to eq('application/hal+json')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "object body" do
|
52
|
+
let(:result) { process({:a => 1}) }
|
53
|
+
|
54
|
+
it "encodes body" do
|
55
|
+
expect(result_body).to eq('{"a":1}')
|
56
|
+
end
|
57
|
+
|
58
|
+
it "adds content type" do
|
59
|
+
expect(result_type).to eq('application/hal+json')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "empty object body" do
|
64
|
+
let(:result) { process({}) }
|
65
|
+
|
66
|
+
it "encodes body" do
|
67
|
+
expect(result_body).to eq('{}')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "object body with json type" do
|
72
|
+
let(:result) { process({:a => 1}, 'application/hal+json; charset=utf-8') }
|
73
|
+
|
74
|
+
it "encodes body" do
|
75
|
+
expect(result_body).to eq('{"a":1}')
|
76
|
+
end
|
77
|
+
|
78
|
+
it "doesn't change content type" do
|
79
|
+
expect(result_type).to eq('application/hal+json; charset=utf-8')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "object body with incompatible type" do
|
84
|
+
let(:result) { process({:a => 1}, 'application/xml; charset=utf-8') }
|
85
|
+
|
86
|
+
it "doesn't change body" do
|
87
|
+
expect(result_body).to eq({:a => 1})
|
88
|
+
end
|
89
|
+
|
90
|
+
it "doesn't change content type" do
|
91
|
+
expect(result_type).to eq('application/xml; charset=utf-8')
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'faraday_middleware/response/parse_hal_json'
|
2
|
+
|
3
|
+
describe FaradayMiddleware::ParseHalJson, :type => :response do
|
4
|
+
context "no type matching" do
|
5
|
+
it "doesn't change nil body" do
|
6
|
+
expect(process(nil).body).to be_nil
|
7
|
+
end
|
8
|
+
|
9
|
+
it "nullifies empty body" do
|
10
|
+
expect(process('').body).to be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "parses json body" do
|
14
|
+
response = process('{"a":1}')
|
15
|
+
expect(response.body).to eq('a' => 1)
|
16
|
+
expect(response.env[:raw_body]).to be_nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "with preserving raw" do
|
21
|
+
let(:options) { {:preserve_raw => true} }
|
22
|
+
|
23
|
+
it "parses json body" do
|
24
|
+
response = process('{"a":1}')
|
25
|
+
expect(response.body).to eq('a' => 1)
|
26
|
+
expect(response.env[:raw_body]).to eq('{"a":1}')
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can opt out of preserving raw" do
|
30
|
+
response = process('{"a":1}', nil, :preserve_raw => false)
|
31
|
+
expect(response.env[:raw_body]).to be_nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "with regexp type matching" do
|
36
|
+
let(:options) { {:content_type => /\bjson$/} }
|
37
|
+
|
38
|
+
it "parses json body of correct type" do
|
39
|
+
response = process('{"a":1}', 'application/x-json')
|
40
|
+
expect(response.body).to eq('a' => 1)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "ignores json body of incorrect type" do
|
44
|
+
response = process('{"a":1}', 'text/json-xml')
|
45
|
+
expect(response.body).to eq('{"a":1}')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "with array type matching" do
|
50
|
+
let(:options) { {:content_type => %w[a/b c/d]} }
|
51
|
+
|
52
|
+
it "parses json body of correct type" do
|
53
|
+
expect(process('{"a":1}', 'a/b').body).to be_a(Hash)
|
54
|
+
expect(process('{"a":1}', 'c/d').body).to be_a(Hash)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "ignores json body of incorrect type" do
|
58
|
+
expect(process('{"a":1}', 'a/d').body).not_to be_a(Hash)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it "chokes on invalid json" do
|
63
|
+
['{!', '"a"', 'true', 'null', '1'].each do |data|
|
64
|
+
expect{ process(data) }.to raise_error(Faraday::Error::ParsingError)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "HEAD responses" do
|
69
|
+
it "nullifies the body if it's only one space" do
|
70
|
+
response = process(' ')
|
71
|
+
expect(response.body).to be_nil
|
72
|
+
end
|
73
|
+
|
74
|
+
it "nullifies the body if it's two spaces" do
|
75
|
+
response = process(' ')
|
76
|
+
expect(response.body).to be_nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faraday_hal_middleware
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Koen Punt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-01 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.9'
|
34
|
+
- - <
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0.10'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0.9'
|
44
|
+
- - <
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0.10'
|
47
|
+
description: Faraday Middleware for JSON HAL requests and responses
|
48
|
+
email:
|
49
|
+
- koen@fetch.nl
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rspec
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- faraday_hal_middleware.gemspec
|
61
|
+
- lib/faraday_hal_middleware.rb
|
62
|
+
- lib/faraday_hal_middleware/version.rb
|
63
|
+
- lib/faraday_middleware/request/encode_hal_json.rb
|
64
|
+
- lib/faraday_middleware/response/parse_hal_json.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
- spec/unit/encode_hal_json_spec.rb
|
67
|
+
- spec/unit/parse_hal_json_spec.rb
|
68
|
+
homepage: https://github.com/fetch/faraday_hal_middleware
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.2.2
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Faraday Middleware for JSON HAL requests and responses
|
92
|
+
test_files:
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- spec/unit/encode_hal_json_spec.rb
|
95
|
+
- spec/unit/parse_hal_json_spec.rb
|
96
|
+
has_rdoc:
|