class CloudConvert::Client

Attributes

api_key[R]
sandbox[R]

Public Class Methods

new(options = {}) click to toggle source

Initializes a new Client object

@param options [Hash] @return [CloudConvert::Client]

# File lib/cloudconvert/client.rb, line 9
def initialize(options = {})
  schema = Schemacop::Schema.new do
    req! :api_key, :string
    opt! :sandbox, :boolean, default: false
  end

  schema.validate! options.reverse_merge!({
    api_key: ENV["CLOUDCONVERT_API_KEY"],
    sandbox: ENV["CLOUDCONVERT_SANDBOX"].to_s.downcase == "true",
  })

  @api_key = options[:api_key]
  @sandbox = options[:sandbox]
end

Public Instance Methods

delete(path, params = {}, &block) click to toggle source

@param path [String] @param params [Hash] @return [OpenStruct]

# File lib/cloudconvert/client.rb, line 66
def delete(path, params = {}, &block)
  request(:delete, path, params, &block)
end
download(url, *args, **options) click to toggle source

@param url [String] @return [Tempfile]

# File lib/cloudconvert/client.rb, line 72
def download(url, *args, **options)
  options[:headers] ||= {}
  options[:headers]["User-Agent"] = USER_AGENT
  Down.download(url, *args, **options)
end
get(path, params = {}, &block) click to toggle source

@param path [String] @param params [Hash] @return [OpenStruct]

# File lib/cloudconvert/client.rb, line 52
def get(path, params = {}, &block)
  request(:get, path, params, &block)
end
jobs() click to toggle source

@return [Resources::Jobs]

# File lib/cloudconvert/client.rb, line 25
def jobs
  @jobs ||= Resources::Jobs.new(self)
end
post(path, params = {}, &block) click to toggle source

@param path [String] @param params [Hash] @return [OpenStruct]

# File lib/cloudconvert/client.rb, line 59
def post(path, params = {}, &block)
  request(:post, path, params, &block)
end
request(method, path, params = {}, &block) click to toggle source

@param method [Symbol] @param path [String] @param params [Hash] @return [OpenStruct]

# File lib/cloudconvert/client.rb, line 43
def request(method, path, params = {}, &block)
  response = connection.send(method, path, params, &block)
  raise CloudConvert::Error.from_response(response) unless response.success?
  response.body unless response.body.blank?
end
tasks() click to toggle source

@return [Resources::Tasks]

# File lib/cloudconvert/client.rb, line 30
def tasks
  @tasks ||= Resources::Tasks.new(self)
end
users() click to toggle source

@return [Resources::Users]

# File lib/cloudconvert/client.rb, line 35
def users
  @users ||= Resources::Users.new(self)
end

Private Instance Methods

api_host() click to toggle source

@return [String]

# File lib/cloudconvert/client.rb, line 81
def api_host
  @api_host ||= sandbox ? SANDBOX_URL : API_URL
end
connection() click to toggle source

@return [Faraday::Client]

# File lib/cloudconvert/client.rb, line 86
def connection
  @connection ||= Faraday.new(url: api_host, headers: headers) do |f|
    f.adapter Faraday.default_adapter
    f.request :json
    f.request :multipart
    f.use CloudConvert::Middleware::ParseJson, content_type: /\bjson$/
    f.use FaradayMiddleware::FollowRedirects, callback: lambda { |response, redirect|
      redirect.request_headers.delete("Content-Length")
      redirect.request_headers.delete("Content-Type")
    }
  end
end
headers() click to toggle source

@return [Hash]

# File lib/cloudconvert/client.rb, line 100
def headers
  @headers ||= {
    "Authorization": "Bearer #{api_key}",
    "User-Agent": USER_AGENT,
  }
end