# encoding: UTF-8 require 'open-uri' require 'nokogiri' module Spontaneous module FieldTypes class WebVideoField < Field include Spontaneous::Plugins::Field::EditorClass def outputs [:type, :id] end def id value(:id) end def video_type value(:type) end def generate_outputs(input) values = {} values[:html] = escape_html(input) case input when "", nil # ignore this when /youtube\.com.*\?.*v=([^&]+)/, /youtu.be\/([^&]+)/ id = $1 values.update(retrieve_youtube_metadata(id)) values[:id] = id values[:type] = "youtube" when /vimeo\.com\/(\d+)/ id = $1 values[:type] = "vimeo" values.update(retrieve_vimeo_metadata(id)) values[:id] = id.to_s else logger.warn "WebVideo field doesn't recognise the URL '#{input}'" end values end def render(format=:html, *args) case format when :html to_html(*args) when :json to_json(*args) else value(format) end end def to_html(*args) opts = args.extract_options! case video_type when "youtube" to_youtube_html(opts) when "vimeo" to_vimeo_html(opts) else value(:html) end end def to_json(*args) opts = args.extract_options! params = \ case video_type when "youtube" youtube_attributes(opts) when "vimeo" vimeo_attributes(opts) else {:tagname => "iframe", :tag => ") end def vimeo_attributes(options = {}) o = make_vimeo_options(options) attributes = { :type => "text/html", :frameborder => "0", :width => o.delete(:width), :height => o.delete(:height) } attributes.update(:webkitAllowFullScreen => "yes", :allowFullScreen => "yes") if o[:fullscreen] make_query_options!(o) attributes[:src] = vimeo_src_with_options(o) {:tagname => "iframe", :tag => ") end def youtube_attributes(options = {}) o = make_youtube_options(options) attributes = { :type => "text/html", :frameborder => "0", :width => o.delete(:width), :height => o.delete(:height) } make_query_options!(o) attributes[:src] = youtube_src_with_options(o) attributes.update(:webkitAllowFullScreen => "yes", :allowFullScreen => "yes") if o[:fullscreen] {:tagname => "iframe", :tag => "