class ConvertApi::Client

Constants

DEFAULT_HEADERS
NET_HTTP_EXCEPTIONS
USER_AGENT

Public Instance Methods

get(path, params = {}, options = {}) click to toggle source
# File lib/convert_api/client.rb, line 31
def get(path, params = {}, options = {})
  handle_response do
    request = Net::HTTP::Get.new(request_uri(path, params), DEFAULT_HEADERS)

    http(options).request(request)
  end
end
post(path, params, options = {}) click to toggle source
# File lib/convert_api/client.rb, line 39
def post(path, params, options = {})
  handle_response do
    request = Net::HTTP::Post.new(request_uri(path), DEFAULT_HEADERS)
    request.form_data = build_form_data(params)

    http(options).request(request)
  end
end
upload(io, filename) click to toggle source
# File lib/convert_api/client.rb, line 48
def upload(io, filename)
  handle_response do
    request_uri = base_uri.path + 'upload'
    encoded_filename = CGI.escape(filename)

    headers = DEFAULT_HEADERS.merge(
      'Content-Type' => 'application/octet-stream',
      'Transfer-Encoding' => 'chunked',
      'Content-Disposition' => "attachment; filename*=UTF-8''#{encoded_filename}",
    )

    request = Net::HTTP::Post.new(request_uri, headers)
    request.body_stream = io

    http(read_timeout: config.upload_timeout).request(request)
  end
end

Private Instance Methods

base_uri() click to toggle source
# File lib/convert_api/client.rb, line 126
def base_uri
  config.base_uri
end
build_form_data(params) click to toggle source
# File lib/convert_api/client.rb, line 112
def build_form_data(params)
  data = {}

  params.each do |key, value|
    if value.is_a?(Array)
      value.each_with_index { |v, i| data["#{key}[#{i}]"] = v }
    else
      data[key] = value
    end
  end

  data
end
config() click to toggle source
# File lib/convert_api/client.rb, line 130
def config
  ConvertApi.config
end
handle_http_exceptions() { || ... } click to toggle source
# File lib/convert_api/client.rb, line 86
def handle_http_exceptions
  yield
rescue *NET_HTTP_EXCEPTIONS => e
  raise(ConnectionFailed, e)
rescue Timeout::Error, Errno::ETIMEDOUT => e
  raise(TimeoutError, e)
end
handle_response() { || ... } click to toggle source
# File lib/convert_api/client.rb, line 68
def handle_response
  handle_http_exceptions do
    response = yield
    status = response.code.to_i

    if status != 200
      raise(
        ClientError,
        status: status,
        body: response.body,
        headers: response.each_header.to_h,
      )
    end

    JSON.parse(response.body)
  end
end
http(options = {}) click to toggle source
# File lib/convert_api/client.rb, line 94
def http(options = {})
  http = Net::HTTP.new(base_uri.host, base_uri.port)
  http.open_timeout = config.connect_timeout
  http.read_timeout = options.fetch(:read_timeout, config.read_timeout)
  http.use_ssl = base_uri.scheme == 'https'
  # http.set_debug_output $stderr
  http
end
request_uri(path, params = {}) click to toggle source
# File lib/convert_api/client.rb, line 103
def request_uri(path, params = {})
  raise(SecretError, 'API secret not configured') if config.api_secret.nil?

  params_with_secret = params.merge(Secret: config.api_secret)
  query = URI.encode_www_form(params_with_secret)

  base_uri.path + path + '?' + query
end