Class: ConvertApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/convert_api/client.rb

Constant Summary collapse

NET_HTTP_EXCEPTIONS =
[
  IOError,
  Errno::ECONNABORTED,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::EINVAL,
  Errno::ENETUNREACH,
  Errno::EPIPE,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError,
  SocketError,
  Zlib::GzipFile::Error,
]
USER_AGENT =
"ConvertAPI-Ruby/#{VERSION}"
DEFAULT_HEADERS =
{
  'User-Agent' => USER_AGENT,
  'Accept' => 'application/json'
}

Instance Method Summary collapse

Instance Method Details

#get(path, params = {}, options = {}) ⇒ Object



31
32
33
34
35
36
37
# 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 = {}) ⇒ Object



39
40
41
42
43
44
45
46
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/convert_api/client.rb', line 48

def upload(io, filename)
  handle_response do
    request_uri = base_uri.path + 'upload'
    encoded_filename = URI.encode(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