Class: CloudConvert::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CloudConvert::Client

Initializes a new Client object

Parameters:

  • options (Hash) (defaults to: {})


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'lib/cloudconvert/client.rb', line 3

def api_key
  @api_key
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



3
4
5
# File 'lib/cloudconvert/client.rb', line 3

def sandbox
  @sandbox
end

Instance Method Details

#api_hostString

Returns:

  • (String)


79
80
81
# File 'lib/cloudconvert/client.rb', line 79

def api_host
  @api_host ||= sandbox ? SANDBOX_URL : API_URL
end

#api_sync_hostString

Returns:

  • (String)


84
85
86
# File 'lib/cloudconvert/client.rb', line 84

def api_sync_host
  @api_sync_host ||= sandbox ? SANDBOX_SYNC_URL : API_SYNC_URL
end

#delete(path, params = {}, &block) ⇒ OpenStruct

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})

Returns:

  • (OpenStruct)


66
67
68
# File 'lib/cloudconvert/client.rb', line 66

def delete(path, params = {}, &block)
  request(:delete, path, params, &block)
end

#download(url, *args, **options) ⇒ Tempfile

Parameters:

  • url (String)

Returns:

  • (Tempfile)


72
73
74
75
76
# 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) ⇒ OpenStruct

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})

Returns:

  • (OpenStruct)


52
53
54
# File 'lib/cloudconvert/client.rb', line 52

def get(path, params = {}, &block)
  request(:get, path, params, &block)
end

#jobsResources::Jobs

Returns:



25
26
27
# File 'lib/cloudconvert/client.rb', line 25

def jobs
  @jobs ||= Resources::Jobs.new(self)
end

#post(path, params = {}, &block) ⇒ OpenStruct

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})

Returns:

  • (OpenStruct)


59
60
61
# File 'lib/cloudconvert/client.rb', line 59

def post(path, params = {}, &block)
  request(:post, path, params, &block)
end

#request(method, path, params = {}, &block) ⇒ OpenStruct

Parameters:

  • method (Symbol)
  • path (String)
  • params (Hash) (defaults to: {})

Returns:

  • (OpenStruct)


43
44
45
46
47
# 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

#tasksResources::Tasks

Returns:



30
31
32
# File 'lib/cloudconvert/client.rb', line 30

def tasks
  @tasks ||= Resources::Tasks.new(self)
end

#usersResources::Users

Returns:



35
36
37
# File 'lib/cloudconvert/client.rb', line 35

def users
  @users ||= Resources::Users.new(self)
end