Sha256: 98e9634fee61cd81c0f5c3e3cd5e6ef95d04e77c774f957756adccd4b2fae1c8

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

require_relative "preconditions"
require_relative "result"
require_relative "errors"

module Freno
  class Client
    class Request

      include Freno::Client::Preconditions

      attr_reader :faraday, :args, :options
      attr_reader :raise_on_timeout

      def self.perform(**kwargs)
        new(**kwargs).perform
      end

      def initialize(**kwargs)
        @args    = kwargs
        @faraday = kwargs.delete(:faraday) || nil
        @options = kwargs.delete(:options) || Hash.new

        @raise_on_timeout = options.fetch(:raise_on_timeout, true)
        @verb = options.fetch(:verb, :head)
      end

      def perform
        response = request(verb, path, params)
        process_response(response)
      rescue Faraday::TimeoutError => ex
        raise Freno::Error.new(ex) if raise_on_timeout
        Result.from_meaning(:request_timeout)
      rescue => ex
        raise Freno::Error.new(ex)
      end

      protected

      def request(verb, path, params)
        faraday.send(verb, path, params)
      end

      def path
        @path || begin
          raise NotImplementedError("must be overriden in specific requests, or memoized in @path")
        end
      end

      def verb
        @verb || begin
          raise NotImplementedError("must be overriden in specific requests, or memoized in @verb")
        end
      end

      def params
        @params ||= {}
      end

      def process_response(response)
        Result.from_faraday_response(response)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
freno-client-0.8.1 lib/freno/client/request.rb
freno-client-0.8.0 lib/freno/client/request.rb