Sha256: 14bc76e3f312dfe635402336512fd8cb30ad4abb69019dfc858ede8dab963228

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

module DeepL
  module Requests
    class Base
      API_VERSION = 'v1'.freeze

      attr_reader :api, :response

      def initialize(api)
        @api = api
      end

      def request
        raise NotImplementedError
      end

      private

      def post(payload)
        request = Net::HTTP::Post.new(uri.request_uri)
        request.set_form_data(payload.compact)
        response = http.request(request)

        validate_response!(request, response)
        [request, response]
      end

      def http
        @http ||= begin
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = uri.scheme == 'https'
          http
        end
      end

      def validate_response!(request, response)
        return if response.is_a?(Net::HTTPSuccess)

        case response.code
        when '400' then raise Exceptions::BadRequest.new(request, response)
        when '403' then raise Exceptions::AuthorizationFailed.new(request, response)
        when '429' then raise Exceptions::LimitExceeded.new(request, response)
        else raise Exceptions::RequestError.new(request, response)
        end
      end

      def path
        raise NotImplementedError
      end

      def url
        "#{host}/#{API_VERSION}/#{path}"
      end

      def uri
        @uri ||= begin
          uri = URI(url)
          new_query = URI.decode_www_form(uri.query || '') + query_params.to_a
          uri.query = URI.encode_www_form(new_query)
          uri
        end
      end

      def host
        api.configuration.host
      end

      def query_params
        { auth_key: api.configuration.auth_key }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deepl-rb-0.0.1 lib/deepl/requests/base.rb
deepl-rb-0.0.0 lib/deepl/requests/base.rb