/
endpoints.rb
88 lines (73 loc) · 3.12 KB
/
endpoints.rb
1
2
3
4
5
6
7
8
9
10
11
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'app'
# Sanity method to insure everything is working.
get '/ping' do
halt 200, 'pong'
end
# Displays a faux photo album page.
get '/' do
erb :index
end
# Handles callbacks from the Widget Service and stores them in a simple list.
post '/callbacks' do
PartnerApp.add_callback Callback.new(request.body.read, params[:transactionToken])
halt 201
end
# Show all callbacks received from the Widget Service
get '/callbacks' do
erb :callbacks
end
# Handles a Storyboard from the Widget and creates a Render via the API Gem with it.
# Render a page that will AJAX Poll the status of the Render until it is completed.
get '/finalize' do
client = Animoto::Client.new PartnerApp::Constants::Platform::PLATFORM_USERNAME, PartnerApp::Constants::Platform::PLATFORM_PASSWORD
storyboard = client.find Animoto::Resources::Storyboard, CGI.unescape(params['links']['storyboard'])
manifest = Animoto::Manifests::Rendering.new storyboard, :resolution => "480p", :format => "h264", :framerate => 30
job = client.render! manifest
@job_url = job.url
erb :finalize
end
# See if our API Render is compete or not.
get '/poll' do
content_type :json
client = Animoto::Client.new PartnerApp::Constants::Platform::PLATFORM_USERNAME, PartnerApp::Constants::Platform::PLATFORM_PASSWORD
job = client.find Animoto::Resources::Jobs::Rendering, params['job_url']
if job.completed?
video = client.find Animoto::Resources::Video, job.video_url
{'completed' => true, 'keepGoing' => false, 'url' => "/play?links[file]=#{CGI::escape(video.download_url)}"}.to_json
elsif job.failed?
{'completed' => false, 'keepGoing' => false }.to_json
else
{'completed' => false, 'keepGoing' => true }.to_json
end
end
# View the Animoto Video in a standard web video player.
get '/play' do
@video_url = CGI::escape(params['links']['file'])
erb :play
end
# Calcalates your Widget Signature and displays the widget in an iframe.
get '/widget' do
@params = {}
# Set no cache headers
@meta_cache = true
response.headers["Last-Modified"] = Time.now.httpdate
response.headers["Expires"] = "0"
# HTTP 1.0
response.headers["Pragma"] = "no-cache"
# HTTP 1.1 'pre-check=0, post-check=0' (IE specific)
response.headers["Cache-Control"] = 'no-store, no-cache, must-revalidate, max-age=0, pre-check=0, post-check=0'
# Parameters must be sorted in alphabetical order by key.
@params['appId'] = PartnerApp::Constants::Widget::APP_ID
@params['nonce'] = Time.now.to_f
@params['partnerId'] = PartnerApp::Constants::Partner::PARTNER_ID
@params['partnerSecret'] = PartnerApp::Constants::Partner::PARTNER_SECRET
@params['timestamp'] = Time.now.to_i
source = @params.keys.sort.map { |i| "#{i}=#{@params[i]}" }.join('&')
# Let's generate the signature for our widget
@params['signature'] = Digest::MD5.hexdigest(source)
# This is our transaction token to map Animoto Workflow events back to our application session.
@params['transactionToken'] = PartnerApp.generate_transaction_token
# We don't need the partner secret once the signature is calculated. We also don't want to pass it over HTTP.
@params.delete('partnerSecret')
erb :widget
end