Class: HDCloud::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/hdcloud/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, pass, source_id = nil, destination_id = nil) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
# File 'lib/hdcloud/base.rb', line 13

def initialize(key, pass, source_id = nil, destination_id = nil)
  @source_id = source_id || hd_cloud_stores[:source_id]
  @destination_id = destination_id || hd_cloud_stores[:destination_id]
  self.class.default_params 'job[source_id]' => source_id, 'job[destination_id]' => destination_id
  self.class.basic_auth key, pass
end

Instance Attribute Details

#destination_idObject

Returns the value of attribute destination_id.



3
4
5
# File 'lib/hdcloud/base.rb', line 3

def destination_id
  @destination_id
end

#source_idObject

Returns the value of attribute source_id.



3
4
5
# File 'lib/hdcloud/base.rb', line 3

def source_id
  @source_id
end

Instance Method Details

#completed_jobsObject



51
52
53
54
55
# File 'lib/hdcloud/base.rb', line 51

def completed_jobs
  response = self.class.get('/jobs/completed.json')
  raise_errors(response)
  response
end

#create_job(file_name, encoding_profile_ids = []) ⇒ Object



20
21
22
23
24
# File 'lib/hdcloud/base.rb', line 20

def create_job(file_name, encoding_profile_ids = [])
  response = self.class.post('/jobs.json', :query => { 'files[]' => file_name, 'encoding_profile_ids[]' => encoding_profile_ids })
  raise_errors(response)
  response
end

#current_jobsObject



45
46
47
48
49
# File 'lib/hdcloud/base.rb', line 45

def current_jobs
  response = self.class.get('/jobs/current.json')
  raise_errors(response)
  response
end

#failed_jobsObject



39
40
41
42
43
# File 'lib/hdcloud/base.rb', line 39

def failed_jobs
  response = self.class.get('/jobs/failed.json')
  raise_errors(response)
  response
end

#hd_cloud_storesObject



9
10
11
# File 'lib/hdcloud/base.rb', line 9

def hd_cloud_stores
  HD_CLOUD_STORES || {}
end

#job_status(status_url) ⇒ Object



26
27
28
29
30
31
# File 'lib/hdcloud/base.rb', line 26

def job_status(status_url)
  status_url = status_url + '.json' unless status_url.match(/json$/)
  response = self.class.get(status_url)
  raise_errors(response)
  response
end

#jobsObject



33
34
35
36
37
# File 'lib/hdcloud/base.rb', line 33

def jobs
  response = self.class.get('/jobs.json')
  raise_errors(response)
  response
end

#raise_errors(response) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hdcloud/base.rb', line 57

def raise_errors(response)
  case response.code.to_i
    when 400
      data = parse(response)
      raise RateLimitExceeded.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
    when 401
      data = parse(response)
      raise Unauthorized.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
    when 403
      data = parse(response)
      raise General.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
    when 404
      raise NotFound, "(#{response.code}): #{response.message}"
    when 500
      raise InformHDCloud, "HDCloud had an internal error. Please let them know in the group. (#{response.code}): #{response.message}"
    when 502..503
      raise Unavailable, "(#{response.code}): #{response.message}"
  end
end