Sha256: 2698411b07a18500e02f866f987d5f05e245e4034704aa8d3d064e8d2fb084a0

Contents?: true

Size: 1.92 KB

Versions: 15

Compression:

Stored size: 1.92 KB

Contents

module KnapsackPro
  module Client
    class Connection
      TIMEOUT = 15

      def initialize(action)
        @action = action
      end

      def call
        send(action.http_method)
      end

      def success?
        !!response
      end

      def errors?
        !!(response && (response['errors'] || response['error']))
      end

      private

      attr_reader :action, :response

      def logger
        KnapsackPro.logger
      end

      def endpoint
        KnapsackPro::Config::Env.endpoint
      end

      def endpoint_url
        endpoint + action.endpoint_path
      end

      def request_hash
        action
        .request_hash
        .merge({
          test_suite_token: test_suite_token
        })
      end

      def request_body
        request_hash.to_json
      end

      def test_suite_token
        KnapsackPro::Config::Env.test_suite_token
      end

      def json_headers
        {
          'Content-Type' => 'application/json',
          'Accept' => 'application/json'
        }
      end

      def parse_response(body)
        return '' if body == '' || body.nil?
        JSON.parse(body)
      rescue JSON::ParserError
        nil
      end

      def post
        uri = URI.parse(endpoint_url)
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = (uri.scheme == 'https')
        http.open_timeout = TIMEOUT
        http.read_timeout = TIMEOUT

        http_response = http.post(uri.path, request_body, json_headers)
        @response = parse_response(http_response.body)

        request_uuid = http_response.header['X-Request-Id']

        logger.info("API request UUID: #{request_uuid}")
        logger.info('API response:')
        if errors?
          logger.error(response)
        else
          logger.info(response)
        end

        response
      rescue Errno::ECONNREFUSED, EOFError, Net::OpenTimeout, Net::ReadTimeout => e
        logger.warn(e.inspect)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
knapsack_pro-0.9.0 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.8.0 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.7.2 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.7.1 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.7.0 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.6.1 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.6.0 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.5.0 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.4.0 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.3.0 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.2.1 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.2.0 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.1.2 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.1.1 lib/knapsack_pro/client/connection.rb
knapsack_pro-0.1.0 lib/knapsack_pro/client/connection.rb