Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/faraday/rashify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative 'rashify/middleware'
require 'faraday'

module Faraday
# This will be your middleware main module, though the actual middleware implementation will go
# into Faraday::Mashify::Middleware for the correct namespacing.
module Rashify
# Faraday allows you to register your middleware for easier configuration.
# This step is totally optional, but it basically allows users to use a
# custom symbol (in this case, `:mashify`), to use your middleware in their connections.
# After calling this line, the following are both valid ways to set the middleware in a connection:
# * conn.use Faraday::Mashify::Middleware
# * conn.use :mashify
# Without this line, only the former method is valid.
# Faraday::Middleware.register_middleware(mashify: Faraday::Mashify::Middleware)

# Alternatively, you can register your middleware under Faraday::Request or Faraday::Response.
# This will allow to load your middleware using the `request` or `response` methods respectively.
#
# Load middleware with conn.request :mashify
# Faraday::Request.register_middleware(mashify: Faraday::Mashify::Middleware)
#
# Load middleware with conn.response :mashify
Faraday::Response.register_middleware(rashify: Faraday::Rashify::Middleware)
end
end
58 changes: 58 additions & 0 deletions lib/faraday/rashify/middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'faraday'
require 'rash'

module Faraday
# This is a Faraday middleware which turns Hashes into Hashie::Mash::Rash
# objects, using the rash_alt gem.
module Rashify
# This is a middleware which acts on responses, so it implements `on_complete`.
class Middleware < ::Faraday::Middleware
CONTENT_TYPE = 'Content-Type'

# Public: Converts parsed response bodies to a Hashie::Mash::Rash if they were of
# Hash or Array type.
def initialize(app = nil, options = {})
super(app)
@options = options
@content_types = Array(options[:content_type])
end

def on_complete(env)
# return early if we should not process this request
return unless should_process?(env)

env[:body] = parse(env[:body])
end

private

# An empty list of content_types means "match everything".
# A non-empty list is more demanding: match this String or RegExp to process

def should_process?(env)
@content_types.empty? || @content_types.any? do |pattern|
type = response_type(env)

pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
end
end

def response_type(env)
type = env.dig(:response_headers, CONTENT_TYPE).to_s
type = type.split(';', 2).first if type.include?(';')
type
end

def parse(body)
case body
when Hash
::Hashie::Mash::Rash.new(body)
when Array
body.map { |item| parse(item) }
else
body
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pactas_itero/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def bearer_token?
private

def connection
@connection ||= Faraday.new(api_endpoint, connection_options)
@connection ||= Faraday.new({ url: api_endpoint, **connection_options })
end

def request(method, path, params = {})
Expand Down
3 changes: 2 additions & 1 deletion lib/pactas_itero/default.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "pactas_itero/response/raise_error"
require "pactas_itero/version"
require "faraday_middleware"
require "faraday/rashify"
require "faraday"

module PactasItero
# Default configuration options for {Client}
Expand Down
2 changes: 1 addition & 1 deletion pactas_itero.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= 2.6"

spec.add_dependency("faraday_middleware", ">= 1.0")
spec.add_dependency("faraday", ">= 2.0")
spec.add_dependency("rash_alt")

spec.add_development_dependency "bundler"
Expand Down