2015-03-29 18:19:18 +00:00
|
|
|
class OembedController < ApplicationController
|
|
|
|
|
|
|
|
def show
|
|
|
|
url = URI.parse(params[:url])
|
|
|
|
|
|
|
|
if url.path =~ %r{^/a/(\d+)$}
|
|
|
|
id = $1
|
|
|
|
asciicast = AsciicastDecorator.new(Asciicast.find(id))
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2015-03-30 10:33:45 +00:00
|
|
|
render json: oembed_response(asciicast)
|
2015-03-29 18:19:18 +00:00
|
|
|
end
|
|
|
|
format.xml do
|
2015-03-30 10:33:45 +00:00
|
|
|
render xml: oembed_response(asciicast).to_xml(root: 'oembed')
|
2015-03-29 18:19:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
head :bad_request
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def oembed_response(asciicast)
|
2015-03-30 10:33:45 +00:00
|
|
|
asciicast_image_generator.generate(asciicast) if asciicast.image_stale?
|
|
|
|
|
2015-06-16 18:53:40 +00:00
|
|
|
width, height = size_smaller_than(
|
|
|
|
asciicast.image_width,
|
|
|
|
asciicast.image_height,
|
|
|
|
params[:maxwidth] || asciicast.image_width,
|
|
|
|
params[:maxheight] || asciicast.image_height
|
|
|
|
)
|
2015-03-29 18:19:18 +00:00
|
|
|
|
|
|
|
oembed = {
|
|
|
|
type: 'rich',
|
|
|
|
version: 1.0,
|
|
|
|
title: asciicast.title,
|
|
|
|
author_name: asciicast.user.display_name,
|
|
|
|
author_url: profile_url(asciicast.user),
|
|
|
|
provider_name: 'asciinema',
|
|
|
|
provider_url: root_url,
|
2015-03-30 16:26:22 +00:00
|
|
|
thumbnail_url: asciicast_url(asciicast, format: :png),
|
2015-03-29 18:19:18 +00:00
|
|
|
thumbnail_width: width,
|
|
|
|
thumbnail_height: height,
|
|
|
|
html: render_html(asciicast, width),
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_html(asciicast, width)
|
|
|
|
render_to_string(
|
|
|
|
template: 'oembed/show.html.erb',
|
|
|
|
layout: false,
|
|
|
|
locals: { asciicast: asciicast, width: width }
|
|
|
|
).chomp
|
|
|
|
end
|
|
|
|
|
|
|
|
def size_smaller_than(width, height, max_width, max_height)
|
|
|
|
fw = Rational(max_width, width)
|
|
|
|
fh = Rational(max_height, height)
|
|
|
|
|
|
|
|
if fw > 1 && fh > 1
|
|
|
|
[width, height]
|
|
|
|
else
|
|
|
|
f = [fw, fh].min
|
|
|
|
[(Rational(width) * f).to_i, (Rational(height) * f).to_i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-30 10:33:45 +00:00
|
|
|
def asciicast_image_generator
|
|
|
|
AsciicastImageGenerator.new(self)
|
|
|
|
end
|
|
|
|
|
2015-03-29 18:19:18 +00:00
|
|
|
end
|