Sha256: 0830ab6e2b612e4a75a7a8a661aa37686ad3ba3fcb6e5a312a6e7c922651dce8

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module Shark
  module Client
    class Connection
      attr_reader :site, :connection_options

      def initialize(options = {})
        @site = options.fetch(:site)
        @connection_options = {
          headers: options[:headers] || {},
          params: options[:params] || {}
        }

        @connection = Faraday.new do |conn|
          conn.use Shark::Middleware::ComposeRequest
          conn.use Shark::Middleware::Status
          conn.response :json, content_type: /\bjson$/
          conn.adapter Faraday.default_adapter
        end
      end

      def use(middleware, *args, &block); end

      # @param request_action [Symbol] One of :get, :post, :put, :patch, :delete.
      # @param path [String] The url path
      # @param options [Hash]
      #   - params [Hash] The request params
      #   - headers [Hash] The request headers
      #   - body [Hash]
      # @return [Faraday::Response]
      # @api private
      def run(request_action, path, params: {}, headers: {}, body: nil)
        raise ArgumentError, 'Configuration :site cannot be nil' if site.blank?
        raise ArgumentError, 'Parameter :path cannot be nil' if path.blank?

        url = File.join(site, path)
        request_headers = connection_options_headers.merge(headers || {})
        request_params = connection_options_params.merge(params || {})

        request_headers['Authorization'] = Shark.auth_token if Shark.auth_token.present?

        @connection.send(request_action) do |request|
          request.url(url)
          request.headers.merge!(request_headers)
          request.body = body
          request.params.merge!(request_params)
        end
      end

      # @see request
      alias request run

      private

      def connection_options_headers
        connection_options[:headers] || {}
      end

      def connection_options_params
        connection_options[:params] || {}
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bima-shark-sdk-3.1.1 lib/shark/client/connection.rb
bima-shark-sdk-3.1.0 lib/shark/client/connection.rb