faraday_hal_middleware 0.1.0 → 0.2.0

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
- SHA1:
3
- metadata.gz: 7dfcf33673587d8a191c777693982e0a57fbb62c
4
- data.tar.gz: 4574a17a050286f76b88dcbe37dc0d48b79f36d5
2
+ SHA256:
3
+ metadata.gz: 129ae7965c9e0d8c0e7b4e83386f4555a1b694d704b559f18e7f62e16b815851
4
+ data.tar.gz: 639ae15211c5ec76fe2e29492a7c0fc2791127eddfd65360fb9442453cd7d825
5
5
  SHA512:
6
- metadata.gz: b64e1ff0ae0429c6bea0d9cf35c186af9f813299599da0cea2485b5e5361a42e672adc2b4909237121c111e700e8eb535ce16843c2afde1e42aaf51ff9408942
7
- data.tar.gz: dbe371ddfa9e31b8d30ae33953e7a25d873d4f69e3bf36424af9c81aa1d82d646c2fdc69c2472de9a96fd64c721b7e2b8eb54d651a63ebef9584eea575396bf0
6
+ metadata.gz: cd2271ad2df1acec1af6c6eeaba8ba187869a4196e0bb7d3a0c1b7b44f9fd699fe73d2affc7286d136f4e933c672673b236ef5aee11d9c40910f4cd7013442d5
7
+ data.tar.gz: 85c9cac01aa86c83994a19336db03754e9530b0dbe047c2a7fa11c51c6f996ea4921fdde98c51cf2c5c44360be6be2c428c227b87ce55c4fcd01014c460ac523
@@ -0,0 +1,16 @@
1
+ name: lint
2
+ on: [push, pull_request]
3
+ jobs:
4
+ rubocop:
5
+ name: RuboCop
6
+ runs-on: ubuntu-latest
7
+ steps:
8
+ - name: Checkout
9
+ uses: actions/checkout@v3
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7
14
+ bundler-cache: true
15
+ - run: bundle exec rubocop
16
+
@@ -0,0 +1,18 @@
1
+ ---
2
+ name: test
3
+ on: [push, pull_request]
4
+ jobs:
5
+ test:
6
+ runs-on: ubuntu-latest
7
+ strategy:
8
+ fail-fast: false
9
+ matrix:
10
+ ruby: [ '2.7', '3.0', '3.1', '3.2' ]
11
+ name: test (${{ matrix.ruby }})
12
+ steps:
13
+ - uses: actions/checkout@v3
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby }}
17
+ - run: bundle install --jobs=3 --retry=3 --path=vendor/bundle
18
+ - run: bundle exec rake spec
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
2
  --require spec_helper
3
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ Exclude:
4
+ - vendor/**/*
5
+
6
+ require:
7
+ - rubocop-rake
8
+ - rubocop-rspec
9
+
10
+ inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,83 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2024-02-01 13:35:51 UTC using RuboCop version 1.60.2.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: Severity, Include.
11
+ # Include: **/*.gemspec
12
+ Gemspec/RequiredRubyVersion:
13
+ Exclude:
14
+ - 'faraday_hal_middleware.gemspec'
15
+
16
+ # Offense count: 1
17
+ # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
18
+ Metrics/AbcSize:
19
+ Max: 21
20
+
21
+ # Offense count: 10
22
+ # Configuration parameters: Prefixes, AllowedPatterns.
23
+ # Prefixes: when, with, without
24
+ RSpec/ContextWording:
25
+ Exclude:
26
+ - 'spec/unit/request_spec.rb'
27
+ - 'spec/unit/response_spec.rb'
28
+
29
+ # Offense count: 2
30
+ # Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
31
+ # Include: **/*_spec*rb*, **/spec/**/*
32
+ RSpec/FilePath:
33
+ Exclude:
34
+ - 'spec/unit/request_spec.rb'
35
+ - 'spec/unit/response_spec.rb'
36
+
37
+ # Offense count: 4
38
+ # Configuration parameters: .
39
+ # SupportedStyles: have_received, receive
40
+ RSpec/MessageSpies:
41
+ EnforcedStyle: receive
42
+
43
+ # Offense count: 7
44
+ RSpec/MultipleExpectations:
45
+ Max: 2
46
+
47
+ # Offense count: 4
48
+ # Configuration parameters: AllowSubject.
49
+ RSpec/MultipleMemoizedHelpers:
50
+ Max: 6
51
+
52
+ # Offense count: 2
53
+ RSpec/RepeatedExample:
54
+ Exclude:
55
+ - 'spec/unit/response_spec.rb'
56
+
57
+ # Offense count: 2
58
+ # Configuration parameters: Include, CustomTransform, IgnoreMethods, IgnoreMetadata.
59
+ # Include: **/*_spec.rb
60
+ RSpec/SpecFilePathFormat:
61
+ Exclude:
62
+ - '**/spec/routing/**/*'
63
+ - 'spec/unit/request_spec.rb'
64
+ - 'spec/unit/response_spec.rb'
65
+
66
+ # Offense count: 4
67
+ RSpec/StubbedMock:
68
+ Exclude:
69
+ - 'spec/unit/response_spec.rb'
70
+
71
+ # Offense count: 1
72
+ # Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
73
+ RSpec/VerifiedDoubles:
74
+ Exclude:
75
+ - 'spec/unit/response_spec.rb'
76
+
77
+ # Offense count: 1
78
+ # Configuration parameters: AllowedConstants.
79
+ Style/Documentation:
80
+ Exclude:
81
+ - 'spec/**/*'
82
+ - 'test/**/*'
83
+ - 'lib/faraday/hal_json.rb'
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ ### 0.2.0 (2024/02/01)
2
+
3
+ * [#11](https://github.com/fetch/faraday_hal_middleware/pull/11): Refactor into Faraday::HalJson::Request and Response - [@dblock](https://github.com/dblock).
4
+ * [#9](https://github.com/fetch/faraday_hal_middleware/pull/9): Added support for Faraday 2, removed dependency on deprecated `faraday_hal_middleware` - [@dblock](https://github.com/dblock).
5
+
6
+ ### 0.1.1 (2020/05/10)
7
+
8
+ * [#6](https://github.com/fetch/faraday_hal_middleware/pull/6): Added CHANGELOG, CONTRIBUTING and RELEASING - [@dblock](https://github.com/dblock).
9
+ * [#5](https://github.com/fetch/faraday_hal_middleware/pull/5): Added RuboCop, Ruby-style linter - [@dblock](https://github.com/dblock).
10
+ * [#4](https://github.com/fetch/faraday_hal_middleware/pull/4): Added Travis-CI - [@dblock](https://github.com/dblock).
11
+ * [#7](https://github.com/fetch/faraday_hal_middleware/pull/7): Require `faraday_middleware` >= 0.9 - [@dblock](https://github.com/dblock).
12
+
13
+ ### 0.1.0 (2018/01/10)
14
+
15
+ * [#1](https://github.com/fetch/faraday_hal_middleware/pull/1): Loosen Faraday dependency - [@espen](https://github.com/espen).
16
+
17
+ ### 0.0.1 (2014/12/01)
18
+
19
+ * Initial public release - [@koenpunt](https://github.com/koenpunt).
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,125 @@
1
+ # Contributing to FaradayHalMiddleware
2
+
3
+ This project is work of [many contributors](https://github.com/fetch/faraday_hal_middleware/graphs/contributors).
4
+
5
+ You're encouraged to submit [pull requests](https://github.com/fetch/faraday_hal_middleware/pulls), [propose features and discuss issues](https://github.com/fetch/faraday_hal_middleware/issues).
6
+
7
+ In the examples below, substitute your Github username for `contributor` in URLs.
8
+
9
+ ### Fork the Project
10
+
11
+ Fork the [project on Github](https://github.com/fetch/faraday_hal_middleware) and check out your copy.
12
+
13
+ ```
14
+ git clone https://github.com/contributor/faraday_hal_middleware.git
15
+ cd faraday_hal_middleware
16
+ git remote add upstream https://github.com/fetch/faraday_hal_middleware.git
17
+ ```
18
+
19
+ ### Bundle Install and Test
20
+
21
+ Ensure that you can build the project and run tests.
22
+
23
+ ```
24
+ bundle install
25
+ bundle exec rake
26
+ ```
27
+
28
+ ## Contribute Code
29
+
30
+ ### Create a Topic Branch
31
+
32
+ Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
33
+
34
+ ```
35
+ git checkout master
36
+ git pull upstream master
37
+ git checkout -b my-feature-branch
38
+ ```
39
+
40
+ ### Write Tests
41
+
42
+ Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add tests to [spec](spec).
43
+
44
+ We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
45
+
46
+ ### Write Code
47
+
48
+ Implement your feature or bug fix.
49
+
50
+ Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop). Run `bundle exec rubocop` and fix any style issues highlighted, auto-correct issues when possible with `bundle exec rubocop -a`. To silence generally ingored issues, including line lengths or code complexity metrics, run `bundle exec rubocop --auto-gen-config`.
51
+
52
+ Make sure that `bundle exec rake` completes without errors.
53
+
54
+ ### Write Documentation
55
+
56
+ Document any external behavior in the [README](README.md).
57
+
58
+ ### Update Changelog
59
+
60
+ Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*. Don't remove *Your contribution here*.
61
+
62
+ Make it look like every other line, including a link to the issue being fixed, your name and link to your Github account.
63
+
64
+ ### Commit Changes
65
+
66
+ Make sure git knows your name and email address:
67
+
68
+ ```
69
+ git config --global user.name "Your Name"
70
+ git config --global user.email "contributor@example.com"
71
+ ```
72
+
73
+ Writing good commit logs is important. A commit log should describe what changed and why.
74
+
75
+ ```
76
+ git add ...
77
+ git commit
78
+ ```
79
+
80
+ ### Push
81
+
82
+ ```
83
+ git push origin my-feature-branch
84
+ ```
85
+
86
+ ### Make a Pull Request
87
+
88
+ Go to https://github.com/contributor/faraday_hal_middleware and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
89
+
90
+ ### Update CHANGELOG Again
91
+
92
+ Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
93
+
94
+ ```
95
+ * [#123](https://github.com/fetch/faraday_hal_middleware/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
96
+ ```
97
+
98
+ Amend your previous commit and force push the changes.
99
+
100
+ ```
101
+ git commit --amend
102
+ git push origin my-feature-branch -f
103
+ ```
104
+
105
+ ### Rebase
106
+
107
+ If you've been working on a change for a while, rebase with upstream/master.
108
+
109
+ ```
110
+ git fetch upstream
111
+ git rebase upstream/master
112
+ git push origin my-feature-branch -f
113
+ ```
114
+
115
+ ### Check on Your Pull Request
116
+
117
+ Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
118
+
119
+ ### Be Patient
120
+
121
+ It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
122
+
123
+ ## Thank You
124
+
125
+ Please do know that we really appreciate and value your time and work. We love you, really.
data/Gemfile CHANGED
@@ -1,8 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gem 'json'
4
6
  gem 'rake', '>= 0.9'
5
7
 
8
+ group :development do
9
+ gem 'rubocop', '1.60.2'
10
+ gem 'rubocop-rake'
11
+ gem 'rubocop-rspec'
12
+ end
13
+
6
14
  group :test do
7
15
  gem 'rspec', '>= 3'
8
16
  gem 'webmock'
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Koen Punt <koen@fetch.nl>
1
+ Copyright (c) 2014-2018 Koen Punt <koen@fetch.nl> and contributors.
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,29 +1,42 @@
1
1
  # FaradayHalMiddleware
2
2
 
3
- Faraday Middleware for JSON HAL requests and responses
3
+ [Faraday](https://github.com/lostisland/faraday) Middleware for JSON HAL requests and responses with `application/hal+json` content-type.
4
+
5
+ [![test](https://github.com/fetch/faraday_hal_middleware/actions/workflows/test.yml/badge.svg)](https://github.com/fetch/faraday_hal_middleware/actions/workflows/test.yml)
4
6
 
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
8
10
 
9
- gem 'faraday_hal_middleware'
11
+ ```ruby
12
+ gem 'faraday_hal_middleware'
13
+ ```
14
+
15
+ Use version 0.1.1 for Faraday 1.x, and >= 0.2.0 for newer versions of Faraday.
10
16
 
11
- And then execute:
17
+ ## Usage
12
18
 
13
- $ bundle
19
+ Use `faraday_hal_middleware`.
14
20
 
15
- Or install it yourself as:
21
+ ```ruby
22
+ require 'faraday_hal_middleware'
16
23
 
17
- $ gem install faraday_hal_middleware
24
+ connection = Faraday.new 'http://example.com/api' do |conn|
25
+ conn.request :hal_json
26
+ conn.response :hal_json, content_type: /\bjson$/
18
27
 
19
- ## Usage
28
+ conn.adapter Faraday.default_adapter
29
+ end
30
+ ```
20
31
 
21
- TODO: Write usage instructions here
32
+ This gem is notably used in [Hyperclient](https://github.com/codegram/hyperclient), see [hyperclient#81](https://github.com/codegram/hyperclient/pull/81) for details.
22
33
 
23
34
  ## Contributing
24
35
 
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
36
+ See [CONTRIBUTING](CONTRIBUTING.md).
37
+
38
+ ## Copyright & License
39
+
40
+ Copyright (c) 2014-2018 Koen Punt <koen@fetch.nl> and contributors.
41
+
42
+ MIT License, see [LICENSE.txt](LICENSE.txt) for details.
data/RELEASING.md ADDED
@@ -0,0 +1,61 @@
1
+ # Releasing FaradayHalMiddleware
2
+
3
+ There're no hard rules about when to release faraday_hal_middleware. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
4
+
5
+ ### Release
6
+
7
+ Run tests, check that all tests succeed locally.
8
+
9
+ ```
10
+ bundle install
11
+ rake
12
+ ```
13
+
14
+ Check that the last build succeeded in [Travis CI](https://travis-ci.org/fetch/faraday_hal_middleware) for all supported platforms.
15
+
16
+ Change "Next" in [CHANGELOG.md](CHANGELOG.md) to the current date.
17
+
18
+ ```
19
+ ### 0.2.2 (7/10/2015)
20
+ ```
21
+
22
+ Remove the line with "Your contribution here.", since there will be no more contributions to this release.
23
+
24
+ Commit your changes.
25
+
26
+ ```
27
+ git add CHANGELOG.md
28
+ git commit -m "Preparing for release, 0.2.2."
29
+ git push origin master
30
+ ```
31
+
32
+ Release.
33
+
34
+ ```
35
+ $ rake release
36
+
37
+ faraday_hal_middleware 0.2.2 built to pkg/faraday_hal_middleware-0.2.2.gem.
38
+ Tagged v0.2.2.
39
+ Pushed git commits and tags.
40
+ Pushed faraday_hal_middleware 0.2.2 to rubygems.org.
41
+ ```
42
+
43
+ ### Prepare for the Next Version
44
+
45
+ Add the next release to [CHANGELOG.md](CHANGELOG.md).
46
+
47
+ ```
48
+ ### 0.2.3 (Next)
49
+
50
+ * Your contribution here.
51
+ ```
52
+
53
+ Increment the third version number in [lib/google-finance/version.rb](lib/google-finance/version.rb).
54
+
55
+ Commit your changes.
56
+
57
+ ```
58
+ git add CHANGELOG.md lib/google-finance/version.rb
59
+ git commit -m "Preparing for next development iteration, 0.2.3."
60
+ git push origin master
61
+ ```
data/Rakefile CHANGED
@@ -1,6 +1,11 @@
1
- require 'bundler/gem_tasks'
1
+ # frozen_string_literal: true
2
2
 
3
- task :default => [:spec]
3
+ require 'bundler/gem_tasks'
4
4
 
5
5
  require 'rspec/core/rake_task'
6
6
  RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+ RuboCop::RakeTask.new
10
+
11
+ task default: %i[rubocop spec]
@@ -1,25 +1,24 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'faraday_hal_middleware/version'
5
+
6
+ require 'faraday/hal_json/version'
5
7
 
6
8
  Gem::Specification.new do |spec|
7
9
  spec.name = 'faraday_hal_middleware'
8
- spec.version = FaradayHalMiddleware::VERSION
10
+ spec.version = Faraday::HalJson::VERSION
9
11
  spec.authors = ['Koen Punt']
10
12
  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.summary = 'Faraday Middleware for JSON HAL requests and responses.'
14
+ spec.description = 'Faraday Middleware for JSON HAL requests and responses.'
13
15
  spec.homepage = 'https://github.com/fetch/faraday_hal_middleware'
14
16
  spec.license = 'MIT'
15
17
 
16
18
  spec.files = `git ls-files -z`.split("\x0")
17
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
20
  spec.require_paths = ['lib']
20
21
 
21
- spec.add_development_dependency 'bundler', '~> 1.6'
22
-
23
- spec.add_dependency 'faraday_middleware', ['~> 0.9']
24
-
22
+ spec.add_dependency 'faraday', '~> 2.0'
23
+ spec.metadata['rubygems_mfa_required'] = 'true'
25
24
  end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ module Faraday
6
+ module HalJson
7
+ # Request middleware that encodes the body as JSON.
8
+ #
9
+ # Processes only requests with matching Content-type or those without a type.
10
+ # If a request doesn't have a type but has a body, it sets the Content-type
11
+ # to JSON MIME-type.
12
+ #
13
+ # Doesn't try to encode bodies that already are in string form.
14
+ class Request < Faraday::Middleware
15
+ CONTENT_TYPE = 'Content-Type'
16
+ MIME_TYPE = 'application/hal+json'
17
+
18
+ def on_request(env)
19
+ match_content_type(env) do |data|
20
+ env[:body] = encode(data)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def encode(data)
27
+ if options[:encoder].is_a?(Array) && options[:encoder].size >= 2
28
+ options[:encoder][0].public_send(options[:encoder][1], data)
29
+ elsif options[:encoder].respond_to?(:dump)
30
+ options[:encoder].dump(data)
31
+ else
32
+ ::JSON.generate(data)
33
+ end
34
+ end
35
+
36
+ def match_content_type(env)
37
+ return unless process_request?(env)
38
+
39
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
40
+ yield env[:body] unless env[:body].respond_to?(:to_str)
41
+ end
42
+
43
+ def process_request?(env)
44
+ type = request_type(env)
45
+ body?(env) && (type.empty? || type == MIME_TYPE)
46
+ end
47
+
48
+ def body?(env)
49
+ body = env[:body]
50
+ case body
51
+ when true, false
52
+ true
53
+ when nil
54
+ # NOTE: nil can be converted to `"null"`, but this middleware doesn't process `nil` for the compatibility.
55
+ false
56
+ else
57
+ !(body.respond_to?(:to_str) && body.empty?)
58
+ end
59
+ end
60
+
61
+ def request_type(env)
62
+ type = env[:request_headers][CONTENT_TYPE].to_s
63
+ type = type.split(';', 2).first if type.index(';')
64
+ type
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module HalJson
5
+ # JSON+HAL is just JSON
6
+ class Response < ::Faraday::Response::Json; end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module HalJson
5
+ VERSION = '0.2.0'
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'hal_json/version'
4
+ require_relative 'hal_json/request'
5
+ require_relative 'hal_json/response'
6
+
7
+ module Faraday
8
+ module HalJson
9
+ ::Faraday::Request.register_middleware(hal_json: ::Faraday::HalJson::Request)
10
+ ::Faraday::Response.register_middleware(hal_json: ::Faraday::HalJson::Response)
11
+ end
12
+ end
@@ -1,20 +1,4 @@
1
- require 'faraday_hal_middleware/version'
2
- require 'faraday'
3
- require 'faraday_middleware'
4
-
5
- module FaradayHalMiddleware
6
-
7
- end
1
+ # frozen_string_literal: true
8
2
 
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
3
+ require 'faraday'
4
+ require_relative 'faraday/hal_json'
data/spec/spec_helper.rb CHANGED
@@ -1,43 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rspec'
2
4
  require 'webmock/rspec'
3
5
  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
6
+ require 'faraday_hal_middleware'
37
7
 
38
8
  RSpec.configure do |config|
39
- config.include EnvCompatibility
40
- config.include ResponseMiddlewareExampleGroup, :type => :response
41
9
  config.expect_with :rspec do |c|
42
10
  c.syntax = :expect
43
11
  end
@@ -1,18 +1,26 @@
1
- require 'faraday_middleware/request/encode_hal_json'
1
+ # frozen_string_literal: true
2
2
 
3
- describe FaradayMiddleware::EncodeHalJson do
4
- let(:middleware) { described_class.new(lambda{|env| env}) }
3
+ require 'spec_helper'
4
+
5
+ # mostly https://github.com/lostisland/faraday/blob/main/spec/faraday/request/json_spec.rb
6
+ describe Faraday::HalJson::Request, type: :request do
7
+ let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }) }
5
8
 
6
9
  def process(body, content_type = nil)
7
- env = {:body => body, :request_headers => Faraday::Utils::Headers.new}
10
+ env = { body: body, request_headers: Faraday::Utils::Headers.new }
8
11
  env[:request_headers]['content-type'] = content_type if content_type
9
- middleware.call(faraday_env(env))
12
+ middleware.call(Faraday::Env.from(env)).env
13
+ end
14
+
15
+ def result_body
16
+ result[:body]
10
17
  end
11
18
 
12
- def result_body() result[:body] end
13
- def result_type() result[:request_headers]['content-type'] end
19
+ def result_type
20
+ result[:request_headers]['content-type']
21
+ end
14
22
 
15
- context "no body" do
23
+ context 'no body' do
16
24
  let(:result) { process(nil) }
17
25
 
18
26
  it "doesn't change body" do
@@ -24,7 +32,7 @@ describe FaradayMiddleware::EncodeHalJson do
24
32
  end
25
33
  end
26
34
 
27
- context "empty body" do
35
+ context 'empty body' do
28
36
  let(:result) { process('') }
29
37
 
30
38
  it "doesn't change body" do
@@ -36,42 +44,42 @@ describe FaradayMiddleware::EncodeHalJson do
36
44
  end
37
45
  end
38
46
 
39
- context "string body" do
47
+ context 'string body' do
40
48
  let(:result) { process('{"a":1}') }
41
49
 
42
50
  it "doesn't change body" do
43
51
  expect(result_body).to eq('{"a":1}')
44
52
  end
45
53
 
46
- it "adds content type" do
54
+ it 'adds content type' do
47
55
  expect(result_type).to eq('application/hal+json')
48
56
  end
49
57
  end
50
58
 
51
- context "object body" do
52
- let(:result) { process({:a => 1}) }
59
+ context 'object body' do
60
+ let(:result) { process(a: 1) }
53
61
 
54
- it "encodes body" do
62
+ it 'encodes body' do
55
63
  expect(result_body).to eq('{"a":1}')
56
64
  end
57
65
 
58
- it "adds content type" do
66
+ it 'adds content type' do
59
67
  expect(result_type).to eq('application/hal+json')
60
68
  end
61
69
  end
62
70
 
63
- context "empty object body" do
71
+ context 'empty object body' do
64
72
  let(:result) { process({}) }
65
73
 
66
- it "encodes body" do
74
+ it 'encodes body' do
67
75
  expect(result_body).to eq('{}')
68
76
  end
69
77
  end
70
78
 
71
- context "object body with json type" do
72
- let(:result) { process({:a => 1}, 'application/hal+json; charset=utf-8') }
79
+ context 'object body with json type' do
80
+ let(:result) { process({ a: 1 }, 'application/hal+json; charset=utf-8') }
73
81
 
74
- it "encodes body" do
82
+ it 'encodes body' do
75
83
  expect(result_body).to eq('{"a":1}')
76
84
  end
77
85
 
@@ -80,11 +88,11 @@ describe FaradayMiddleware::EncodeHalJson do
80
88
  end
81
89
  end
82
90
 
83
- context "object body with incompatible type" do
84
- let(:result) { process({:a => 1}, 'application/xml; charset=utf-8') }
91
+ context 'object body with incompatible type' do
92
+ let(:result) { process({ a: 1 }, 'application/xml; charset=utf-8') }
85
93
 
86
94
  it "doesn't change body" do
87
- expect(result_body).to eq({:a => 1})
95
+ expect(result_body).to eq(a: 1)
88
96
  end
89
97
 
90
98
  it "doesn't change content type" do
@@ -0,0 +1,192 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ # copy of https://github.com/lostisland/faraday/blob/main/spec/faraday/response/json_spec.rb
6
+ describe Faraday::HalJson::Response do
7
+ let(:options) { {} }
8
+ let(:headers) { {} }
9
+ let(:middleware) do
10
+ described_class.new(lambda { |env|
11
+ Faraday::Response.new(env)
12
+ }, **options)
13
+ end
14
+
15
+ def process(body, content_type = 'application/json', options = {})
16
+ env = {
17
+ body: body, request: options,
18
+ request_headers: Faraday::Utils::Headers.new,
19
+ response_headers: Faraday::Utils::Headers.new(headers)
20
+ }
21
+ env[:response_headers]['content-type'] = content_type if content_type
22
+ yield(env) if block_given?
23
+ middleware.call(Faraday::Env.from(env))
24
+ end
25
+
26
+ context 'no type matching' do
27
+ it "doesn't change nil body" do
28
+ expect(process(nil).body).to be_nil
29
+ end
30
+
31
+ it 'nullifies empty body' do
32
+ expect(process('').body).to be_nil
33
+ end
34
+
35
+ it 'parses json body' do
36
+ response = process('{"a":1}')
37
+ expect(response.body).to eq('a' => 1)
38
+ expect(response.env[:raw_body]).to be_nil
39
+ end
40
+ end
41
+
42
+ context 'with preserving raw' do
43
+ let(:options) { { preserve_raw: true } }
44
+
45
+ it 'parses json body' do
46
+ response = process('{"a":1}')
47
+ expect(response.body).to eq('a' => 1)
48
+ expect(response.env[:raw_body]).to eq('{"a":1}')
49
+ end
50
+ end
51
+
52
+ context 'with default regexp type matching' do
53
+ it 'parses json body of correct type' do
54
+ response = process('{"a":1}', 'application/x-json')
55
+ expect(response.body).to eq('a' => 1)
56
+ end
57
+
58
+ it 'ignores json body of incorrect type' do
59
+ response = process('{"a":1}', 'text/json-xml')
60
+ expect(response.body).to eq('{"a":1}')
61
+ end
62
+ end
63
+
64
+ context 'with array type matching' do
65
+ let(:options) { { content_type: %w[a/b c/d] } }
66
+
67
+ it 'parses json body of correct type' do
68
+ expect(process('{"a":1}', 'a/b').body).to be_a(Hash)
69
+ expect(process('{"a":1}', 'c/d').body).to be_a(Hash)
70
+ end
71
+
72
+ it 'ignores json body of incorrect type' do
73
+ expect(process('{"a":1}', 'a/d').body).not_to be_a(Hash)
74
+ end
75
+ end
76
+
77
+ it 'chokes on invalid json' do
78
+ expect { process('{!') }.to raise_error(Faraday::ParsingError)
79
+ end
80
+
81
+ it 'includes the response on the ParsingError instance' do
82
+ process('{') { |env| env[:response] = Faraday::Response.new }
83
+ raise 'Parsing should have failed.'
84
+ rescue Faraday::ParsingError => e
85
+ expect(e.response).to be_a(Faraday::Response)
86
+ end
87
+
88
+ context 'HEAD responses' do
89
+ it "nullifies the body if it's only one space" do
90
+ response = process(' ')
91
+ expect(response.body).to be_nil
92
+ end
93
+
94
+ it "nullifies the body if it's two spaces" do
95
+ response = process(' ')
96
+ expect(response.body).to be_nil
97
+ end
98
+ end
99
+
100
+ context 'JSON options' do
101
+ let(:body) { '{"a": 1}' }
102
+ let(:result) { { a: 1 } }
103
+ let(:options) do
104
+ {
105
+ parser_options: {
106
+ symbolize_names: true
107
+ }
108
+ }
109
+ end
110
+
111
+ it 'passes relevant options to JSON parse' do
112
+ expect(JSON).to receive(:parse)
113
+ .with(body, options[:parser_options])
114
+ .and_return(result)
115
+
116
+ response = process(body)
117
+ expect(response.body).to eq(result)
118
+ end
119
+ end
120
+
121
+ context 'with decoder' do
122
+ let(:decoder) do
123
+ double('Decoder').tap do |e|
124
+ allow(e).to receive(:load) { |s, opts| JSON.parse(s, opts) }
125
+ end
126
+ end
127
+
128
+ let(:body) { '{"a": 1}' }
129
+ let(:result) { { a: 1 } }
130
+
131
+ context 'when decoder is passed as object' do
132
+ let(:options) do
133
+ {
134
+ parser_options: {
135
+ decoder: decoder,
136
+ option: :option_value,
137
+ symbolize_names: true
138
+ }
139
+ }
140
+ end
141
+
142
+ it 'passes relevant options to specified decoder\'s load method' do
143
+ expect(decoder).to receive(:load)
144
+ .with(body, { option: :option_value, symbolize_names: true })
145
+ .and_return(result)
146
+
147
+ response = process(body)
148
+ expect(response.body).to eq(result)
149
+ end
150
+ end
151
+
152
+ context 'when decoder is passed as an object-method pair' do
153
+ let(:options) do
154
+ {
155
+ parser_options: {
156
+ decoder: [decoder, :load],
157
+ option: :option_value,
158
+ symbolize_names: true
159
+ }
160
+ }
161
+ end
162
+
163
+ it 'passes relevant options to specified decoder\'s method' do
164
+ expect(decoder).to receive(:load)
165
+ .with(body, { option: :option_value, symbolize_names: true })
166
+ .and_return(result)
167
+
168
+ response = process(body)
169
+ expect(response.body).to eq(result)
170
+ end
171
+ end
172
+
173
+ context 'when decoder is not passed' do
174
+ let(:options) do
175
+ {
176
+ parser_options: {
177
+ symbolize_names: true
178
+ }
179
+ }
180
+ end
181
+
182
+ it 'passes relevant options to JSON parse' do
183
+ expect(JSON).to receive(:parse)
184
+ .with(body, { symbolize_names: true })
185
+ .and_return(result)
186
+
187
+ response = process(body)
188
+ expect(response.body).to eq(result)
189
+ end
190
+ end
191
+ end
192
+ end
metadata CHANGED
@@ -1,69 +1,64 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_hal_middleware
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
  - Koen Punt
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-10 00:00:00.000000000 Z
11
+ date: 2024-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
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'
19
+ version: '2.0'
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
24
  - - "~>"
39
25
  - !ruby/object:Gem::Version
40
- version: '0.9'
41
- description: Faraday Middleware for JSON HAL requests and responses
26
+ version: '2.0'
27
+ description: Faraday Middleware for JSON HAL requests and responses.
42
28
  email:
43
29
  - koen@fetch.nl
44
30
  executables: []
45
31
  extensions: []
46
32
  extra_rdoc_files: []
47
33
  files:
34
+ - ".github/workflows/lint.yml"
35
+ - ".github/workflows/test.yml"
48
36
  - ".gitignore"
49
37
  - ".rspec"
38
+ - ".rubocop.yml"
39
+ - ".rubocop_todo.yml"
40
+ - CHANGELOG.md
41
+ - CONTRIBUTING.md
50
42
  - Gemfile
51
43
  - LICENSE.txt
52
44
  - README.md
45
+ - RELEASING.md
53
46
  - Rakefile
54
47
  - faraday_hal_middleware.gemspec
48
+ - lib/faraday/hal_json.rb
49
+ - lib/faraday/hal_json/request.rb
50
+ - lib/faraday/hal_json/response.rb
51
+ - lib/faraday/hal_json/version.rb
55
52
  - lib/faraday_hal_middleware.rb
56
- - lib/faraday_hal_middleware/version.rb
57
- - lib/faraday_middleware/request/encode_hal_json.rb
58
- - lib/faraday_middleware/response/parse_hal_json.rb
59
53
  - spec/spec_helper.rb
60
- - spec/unit/encode_hal_json_spec.rb
61
- - spec/unit/parse_hal_json_spec.rb
54
+ - spec/unit/request_spec.rb
55
+ - spec/unit/response_spec.rb
62
56
  homepage: https://github.com/fetch/faraday_hal_middleware
63
57
  licenses:
64
58
  - MIT
65
- metadata: {}
66
- post_install_message:
59
+ metadata:
60
+ rubygems_mfa_required: 'true'
61
+ post_install_message:
67
62
  rdoc_options: []
68
63
  require_paths:
69
64
  - lib
@@ -78,12 +73,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
73
  - !ruby/object:Gem::Version
79
74
  version: '0'
80
75
  requirements: []
81
- rubyforge_project:
82
- rubygems_version: 2.6.8
83
- signing_key:
76
+ rubygems_version: 3.1.6
77
+ signing_key:
84
78
  specification_version: 4
85
- summary: Faraday Middleware for JSON HAL requests and responses
86
- test_files:
87
- - spec/spec_helper.rb
88
- - spec/unit/encode_hal_json_spec.rb
89
- - spec/unit/parse_hal_json_spec.rb
79
+ summary: Faraday Middleware for JSON HAL requests and responses.
80
+ test_files: []
@@ -1,3 +0,0 @@
1
- module FaradayHalMiddleware
2
- VERSION = "0.1.0"
3
- end
@@ -1,52 +0,0 @@
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
@@ -1,7 +0,0 @@
1
- require 'faraday_middleware/response/parse_json'
2
-
3
- module FaradayMiddleware
4
- # JSON+HAL is just JSON
5
- class ParseHalJson < ParseJson
6
- end
7
- end
@@ -1,79 +0,0 @@
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'].each do |data|
64
- expect{ process(data) }.to raise_error(Faraday::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