Skip to content

Commit cdcbec8

Browse files
author
Bart Zonneveld
committed
Better parsing of thumbnail options
1 parent 85f909f commit cdcbec8

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

app/controllers/api/presets_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,12 @@ def update
9898
if params[:preset]
9999
params[:name] = params[:preset][:name]
100100
params[:parameters] = params[:preset][:parameters]
101+
params[:thumbnail_options] = params[:preset][:thumbnail_options]
101102
end
102103

103104
preset = Preset.find(params[:id])
104105

105-
if preset.update_attributes(:name => params[:name], :parameters => params[:parameters])
106+
if preset.update_attributes(:name => params[:name], :parameters => params[:parameters], :thumbnail_options => params[:thumbnail_options])
106107
respond_with preset, :location => api_preset_url(preset) do |format|
107108
format.html { redirect_to presets_path }
108109
end

app/models/preset.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@ class Preset < ActiveRecord::Base
33

44
validates :name, :parameters, :presence => true
55
validates :name, :uniqueness => true
6+
7+
validate :thumbnail_options, :valid_json_options, :if => Proc.new { |p| p.thumbnail_options.present? }
68

79
def self.from_api(attributes)
810
attributes = attributes[:preset] if attributes[:preset]
911
create(:name => attributes['name'],
1012
:parameters => attributes['parameters'],
1113
:thumbnail_options => attributes['thumbnail_options'])
1214
end
15+
16+
private
17+
def valid_json_options
18+
begin
19+
JSON.parse(self.thumbnail_options)
20+
true
21+
rescue JSON::ParserError
22+
errors.add(:thumbnail_options, "must be valid JSON")
23+
false
24+
end
25+
end
1326
end

app/models/transcoder.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ def schedule(opts)
1212
end
1313

1414
def job_to_json(job)
15+
if job.preset.thumbnail_options.present?
16+
thumb_opts = JSON.parse(job.preset.thumbnail_options)
17+
else
18+
thumb_opts = nil
19+
end
20+
1521
{
1622
'source_file' => job.source_file,
1723
'destination_file' => job.destination_file,
1824
'encoder_options' => job.preset.parameters,
19-
'thumbnail_options' => job.preset.thumbnail_options,
25+
'thumbnail_options' => thumb_opts,
2026
'callback_urls' => [ job.callback_url ]
2127
}.to_json
2228
end
@@ -38,16 +44,25 @@ def post(url, *attrs)
3844
def get(url, *attrs)
3945
call_transcoder(:get, url, *attrs)
4046
end
47+
48+
def log(str)
49+
logger.info "#{Time.now} - #{str}"
50+
end
4151

4252
private
4353
def call_transcoder(method, url, *attrs)
4454
begin
55+
log "Calling #{url} with #{attrs}"
4556
attrs << { :content_type => :json, :accept => :json, :timeout => 2 }
4657
response = RestClient.send(method, url, *attrs)
4758
JSON::parse response
4859
rescue Errno::ECONNREFUSED, SocketError, Errno::ENETUNREACH, Errno::EHOSTUNREACH, RestClient::Exception, JSON::ParserError
4960
false
5061
end
5162
end
63+
64+
def logger
65+
@_logger = ActiveSupport::BufferedLogger.new(Rails.root.join('log/transcoder.log'))
66+
end
5267
end
5368
end

spec/factories.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
factory :preset do |p|
1111
p.name 'h264'
1212
p.parameters 'params'
13-
p.thumbnail_options 'thumbs'
13+
p.thumbnail_options '{"seconds":1}'
1414
end
1515

1616
factory :host do |h|

spec/models/transcoder_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def do_schedule
4040
'source_file' => 'source',
4141
'destination_file' => 'dest',
4242
'encoder_options' => 'params',
43-
'thumbnail_options' => 'thumbs',
43+
'thumbnail_options' => {seconds: 1},
4444
'callback_urls' => ["callback_url"]
4545
}.to_json
4646
end

0 commit comments

Comments
 (0)