Skip to content

Commit

Permalink
Adds faraday 2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
jasondoc3 committed Apr 27, 2024
1 parent e9b3747 commit ebc36fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
41 changes: 37 additions & 4 deletions lib/pager_duty/connection.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -106,7 +106,7 @@ def call(env)
end
end

class ParseTimeStrings < Faraday::Response::Middleware
class ParseTimeStrings < Faraday::Middleware
TIME_KEYS = %w(
at
created_at
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions pager_duty-connection.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit ebc36fb

Please sign in to comment.