From ebc36fbafb2d248a68dda512257071a88d80ea9a Mon Sep 17 00:00:00 2001 From: Jason Dougherty Date: Sat, 27 Apr 2024 15:53:14 -0700 Subject: [PATCH] Adds faraday 2 support --- lib/pager_duty/connection.rb | 41 +++++++++++++++++++++++++++++++---- pager_duty-connection.gemspec | 3 +-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/pager_duty/connection.rb b/lib/pager_duty/connection.rb index d506965..d9a4a99 100644 --- a/lib/pager_duty/connection.rb +++ b/lib/pager_duty/connection.rb @@ -1,5 +1,5 @@ require 'faraday' -require 'faraday_middleware' +require 'hashie' require 'active_support' require 'active_support/core_ext' require 'active_support/time_with_zone' @@ -106,7 +106,7 @@ def call(env) end end - class ParseTimeStrings < Faraday::Response::Middleware + class ParseTimeStrings < Faraday::Middleware TIME_KEYS = %w( at created_at @@ -137,6 +137,10 @@ class ParseTimeStrings < Faraday::Response::Middleware pending_actions ) + def on_complete(env) + parse(env[:body]) + end + def parse(body) case body when Hash, ::Hashie::Mash @@ -179,13 +183,34 @@ def parse_object_times(object) end end + class Mashify < Faraday::Middleware + def on_complete(env) + env[:body] = parse(env[:body]) + end + + def parse(body) + case body + when Hash + ::Hashie::Mash.new(body) + when Array + body.map { |item| parse(item) } + else + body + end + end + end + def initialize(token, token_type: :Token, url: API_PREFIX, debug: false) @connection = Faraday.new do |conn| conn.url_prefix = url case token_type when :Token - conn.request :token_auth, token + if faraday_v1? + conn.request :token_auth, token + else + conn.request :authorization, 'Token', token + end when :Bearer conn.request :authorization, 'Bearer', token else raise ArgumentError, "invalid token_type: #{token_type.inspect}" @@ -199,7 +224,7 @@ def initialize(token, token_type: :Token, url: API_PREFIX, debug: false) # json back, mashify it conn.use ParseTimeStrings - conn.response :mashify + conn.use Mashify conn.response :json conn.response :logger, ::Logger.new(STDOUT), bodies: true if debug @@ -248,6 +273,14 @@ def delete(path, request = {}) private + def faraday_v1? + faraday_version < Gem::Version.new("2") + end + + def faraday_version + @faraday_version ||= Gem.loaded_specs["faraday"].version + end + def run_request(method, path, body: {}, headers: {}, query_params: {}) path = path.gsub(/^\//, '') # strip leading slash, to make sure relative things happen on the connection diff --git a/pager_duty-connection.gemspec b/pager_duty-connection.gemspec index 3b22203..7369f85 100644 --- a/pager_duty-connection.gemspec +++ b/pager_duty-connection.gemspec @@ -17,8 +17,7 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ["lib"] - gem.add_dependency "faraday", "~> 1.0" - gem.add_dependency "faraday_middleware", "~> 1.0" + gem.add_dependency "faraday", ">= 1.10", "< 3" gem.add_dependency "activesupport", ">= 3.2", "< 8.0" gem.add_dependency "hashie", ">= 1.2"