Sha256: ede1f93158cddee932a2ce97279d65d7e9daa7ced47a7741e5ffb10ca752189a

Contents?: true

Size: 1.96 KB

Versions: 7

Compression:

Stored size: 1.96 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 |faraday|
          faraday.use Shark::Middleware::ComposeRequest
          faraday.use Shark::Middleware::Status
          faraday.use JsonApiClient::Middleware::ParseJson
          faraday.adapter :net_http_persistent
        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 || {})

        if Shark.service_token.present?
          request_headers['Authorization'] = "Bearer #{Shark.service_token}"
        end

        @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

7 entries across 7 versions & 1 rubygems

Version Path
bima-shark-sdk-2.4.4 lib/shark/client/connection.rb
bima-shark-sdk-2.4.3 lib/shark/client/connection.rb
bima-shark-sdk-2.4.2 lib/shark/client/connection.rb
bima-shark-sdk-2.4.1 lib/shark/client/connection.rb
bima-shark-sdk-2.4.0 lib/shark/client/connection.rb
bima-shark-sdk-2.3.1 lib/shark/client/connection.rb
bima-shark-sdk-2.3.0 lib/shark/client/connection.rb