Class: FlixCloud::Job

Inherits:
Record
  • Object
show all
Defined in:
lib/flix_cloud/job.rb

Instance Attribute Summary collapse

Attributes inherited from Record

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#attributes=, record_column

Constructor Details

#initialize(attrs = {}) ⇒ Job

Returns a new instance of Job.



7
8
9
10
# File 'lib/flix_cloud/job.rb', line 7

def initialize(attrs={})
  super
  self.shortcut_attributes = attrs
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/flix_cloud/job.rb', line 3

def api_key
  @api_key
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/flix_cloud/job.rb', line 3

def id
  @id
end

#initialized_atObject

Returns the value of attribute initialized_at.



3
4
5
# File 'lib/flix_cloud/job.rb', line 3

def initialized_at
  @initialized_at
end

#notification_urlObject

Returns the value of attribute notification_url.



3
4
5
# File 'lib/flix_cloud/job.rb', line 3

def notification_url
  @notification_url
end

#recipe_idObject

Returns the value of attribute recipe_id.



3
4
5
# File 'lib/flix_cloud/job.rb', line 3

def recipe_id
  @recipe_id
end

#recipe_nameObject

Returns the value of attribute recipe_name.



3
4
5
# File 'lib/flix_cloud/job.rb', line 3

def recipe_name
  @recipe_name
end

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/flix_cloud/job.rb', line 3

def response
  @response
end

Class Method Details

.create(attrs = {}) ⇒ Object



58
59
60
61
62
# File 'lib/flix_cloud/job.rb', line 58

def self.create(attrs={})
  job = new(attrs)
  job.save
  job
end

.create!(attrs = {}) ⇒ Object



64
65
66
67
68
# File 'lib/flix_cloud/job.rb', line 64

def self.create!(attrs={})
  job = create(attrs)
  raise FlixCloud::CreateError unless job.id
  job
end

Instance Method Details

#saveObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/flix_cloud/job.rb', line 38

def save
  return false unless valid?

  self.response = post('api/flixcloud/jobs', to_xml)

  if response.success?
    self.id = response.body_as_hash['job']['id']
    self.initialized_at = response.body_as_hash['job']['initialized_job_at']
  else
    self.errors = response.errors
  end

  response.success?
end

#save!Object



53
54
55
56
# File 'lib/flix_cloud/job.rb', line 53

def save!
  raise FlixCloud::SaveError unless save
  true
end

#to_xmlObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/flix_cloud/job.rb', line 70

def to_xml
  xml = Builder::XmlMarkup.new

  xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"

  xml.tag!("api-request") do
    xml.tag!("api-key", api_key)

    if recipe_name
      xml.tag!("recipe-name", recipe_name)
    else
      xml.tag!("recipe-id", recipe_id)
    end

    if notification_url
      xml.tag!("notification-url", notification_url)
    end

    if file_locations
      xml.tag!("file-locations") do
        if file_locations.input
          xml.input do
            xml.url(file_locations.input.url)
            if file_locations.input.parameters
              xml.parameters do
                xml.user(file_locations.input.parameters.user)
                xml.password(file_locations.input.parameters.password)
              end
            end
          end
        end

        if file_locations.output
          xml.output do
            xml.url(file_locations.output.url)
            if file_locations.output.parameters
              xml.parameters do
                xml.user(file_locations.output.parameters.user)
                xml.password(file_locations.output.parameters.password)
              end
            end
          end
        end

        if file_locations.watermark
          xml.watermark do
            xml.url(file_locations.watermark.url)
            if file_locations.watermark.parameters
              xml.parameters do
                xml.user(file_locations.watermark.parameters.user)
                xml.password(file_locations.watermark.parameters.password)
              end
            end
          end
        end

        if file_locations.thumbnails
          xml.thumbnails do
            xml.url(file_locations.thumbnails.url)
            xml.prefix "thumb_"
          end
        end
      end
    end
  end

  xml.target!
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/flix_cloud/job.rb', line 12

def valid?
  self.errors = []

  if file_locations
    unless file_locations.valid?
      self.errors << {:file_locations => file_locations.errors}
    end
  else
    self.errors << "file_locations is required"
  end

  if recipe_id || recipe_name
    if recipe_id && recipe_name
      self.errors << "recipe_id and recipe_name cannot both be used"
    end
  else
    self.errors << "recipe_id or recipe_name is required"
  end

  unless api_key
    self.errors << "api_key is required"
  end

  errors.empty?
end