Sha256: a2d86986d908441df769c9ba1fff53da6b6d442f4461b841b8af85102a2fe946

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

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

      attr_reader :api, :response, :options

      def initialize(api, options = {})
        @api = api
        @options = options
      end

      def request
        raise NotImplementedError
      end

      private

      def option(name)
        options[name.to_s] || options[name.to_sym]
      end

      def post(payload)
        request = Net::HTTP::Post.new(uri.request_uri)
        request.set_form_data(payload.reject { |_, v| v.nil? })
        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 }.merge(options)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deepl-rb-1.0.1 lib/deepl/requests/base.rb
deepl-rb-1.0.0 lib/deepl/requests/base.rb