faraday-rashify 2.0.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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/lib/faraday/rashify/version.rb +7 -0
- data/lib/faraday/rashify.rb +61 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 66a1603cc0eef8702a90ce56e8cdb3297626550510170e330b11060ab7307144
|
4
|
+
data.tar.gz: 0ba94e5b8953636eca00fc20eafe5bbe28466740261d7773eecedf44c53db564
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a04dd7357e708f4f1212b25f5dff00a6746f3561355bd2b6bca4bacd92fed35c4870e6ae054126259c46dee250aad3200ecaa3e1733722d510ada2e40123baf
|
7
|
+
data.tar.gz: 6dcdaf96cce68ad0cd51e1841ecc597b42329825083f818e2ebc3d9110368e32a9a436bdb3ddf48db62a960a71c7a693341391dbe75df8a3be2e8fcd7033d4a0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Olle Jonsson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Faraday::Rashify
|
2
|
+
|
3
|
+
This is a Faraday middleware which turns Hashes into `Hashie::Mash::Rash` objects, using the [`rash_alt`](https://github.com/shishi/rash_alt) gem.
|
4
|
+
|
5
|
+
This very specific middleware has been extracted from the [`faraday_middleware`](https://github.com/lostisland/faraday_middleware) project.
|
6
|
+
|
7
|
+
Original code created by @mislav with contributions by @shishi.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'faraday-rashify'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install faraday-rashify
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
This is a [Faraday middleware](https://lostisland.github.io/faraday/middleware/), and you need to mount it after parsing middlewares, such as `:json`.
|
28
|
+
|
29
|
+
Example:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
Faraday.new(options) do |conn|
|
33
|
+
conn.request :json
|
34
|
+
|
35
|
+
conn.response :rashify
|
36
|
+
conn.response :json, content_type: /\bjson$/
|
37
|
+
conn.response :logger, logger, bodies: true
|
38
|
+
|
39
|
+
conn.adapter Faraday.default_adapter
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
That is, this middleware only acts on Ruby Hashes and Arrays and makes the Hashes in there into `Hashie::Mash::Rash` objects.
|
44
|
+
|
45
|
+
## Development
|
46
|
+
|
47
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
48
|
+
|
49
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/lostisland/faraday-rashify. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/lostisland/faraday-rashify/blob/master/CODE_OF_CONDUCT.md).
|
54
|
+
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
59
|
+
|
60
|
+
## Code of Conduct
|
61
|
+
|
62
|
+
Everyone interacting in the Faraday::Rashify project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/lostisland/faraday-rashify/blob/main/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require 'faraday/rashify/version'
|
5
|
+
require 'rash'
|
6
|
+
|
7
|
+
module Faraday
|
8
|
+
# This is a Faraday middleware which turns Hashes into Hashie::Mash::Rash
|
9
|
+
# objects, using the rash_alt gem.
|
10
|
+
module Rashify
|
11
|
+
# This is a middleware which acts on responses, so it implements `on_complete`.
|
12
|
+
class Middleware < ::Faraday::Middleware
|
13
|
+
CONTENT_TYPE = 'Content-Type'
|
14
|
+
|
15
|
+
# Public: Converts parsed response bodies to a Hashie::Mash::Rash if they were of
|
16
|
+
# Hash or Array type.
|
17
|
+
def initialize(app = nil, options = {})
|
18
|
+
super(app)
|
19
|
+
@options = options
|
20
|
+
@content_types = Array(options[:content_type])
|
21
|
+
end
|
22
|
+
|
23
|
+
def on_complete(env)
|
24
|
+
# return early if we should not process this request
|
25
|
+
return unless should_process?(env)
|
26
|
+
|
27
|
+
env[:body] = parse(env[:body])
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# An empty list of content_types means "match everything".
|
33
|
+
# A non-empty list is more demanding: match this String or RegExp to process
|
34
|
+
|
35
|
+
def should_process?(env)
|
36
|
+
@content_types.empty? || @content_types.any? do |pattern|
|
37
|
+
type = response_type(env)
|
38
|
+
|
39
|
+
pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def response_type(env)
|
44
|
+
type = env.dig(:response_headers, CONTENT_TYPE).to_s
|
45
|
+
type = type.split(';', 2).first if type.include?(';')
|
46
|
+
type
|
47
|
+
end
|
48
|
+
|
49
|
+
def parse(body)
|
50
|
+
case body
|
51
|
+
when Hash
|
52
|
+
::Hashie::Mash::Rash.new(body)
|
53
|
+
when Array
|
54
|
+
body.map { |item| parse(item) }
|
55
|
+
else
|
56
|
+
body
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faraday-rashify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Olle Jonsson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-03-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
description: This middleware was extracted from faraday_middleware.
|
28
|
+
email:
|
29
|
+
- olle.jonsson@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- lib/faraday/rashify.rb
|
37
|
+
- lib/faraday/rashify/version.rb
|
38
|
+
homepage: https://github.com/lostisland/faraday-rashify
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata:
|
42
|
+
homepage_uri: https://github.com/lostisland/faraday-rashify
|
43
|
+
source_code_uri: https://github.com/lostisland/faraday-rashify
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '2.6'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.2.7
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: A Faraday middleware around rash_alt.
|
63
|
+
test_files: []
|