class VimeoEmbedCache

Constants

FIXED_GEOMETRY
FIXED_HEIGHT_GEOMETRY
FIXED_WIDTH_GEOMETRY
VIMEO_GEOMETRY

Public Class Methods

embed(vid, geometry, configuration = {}) click to toggle source
# File app/models/vimeo_embed_cache.rb, line 16
def self.embed vid, geometry, configuration = {}
  configuration.stringify_keys!
  configuration.merge!(geometry_hash(geometry))
      
  static = configuration.delete("static") if configuration.has_key?("static")
  if static
    geometry =~ FIXED_GEOMETRY ?
      static_embed_code(vid, geometry).html_safe : raise(ArgumentError, "Must use fixed geometry string for static embeds (e.g. 100x240)")
  else
    find_or_create_by_vid_and_configuration(vid, :configuration => YAML.dump(configuration)).code.html_safe
  end
end
geometry_hash(geometry) click to toggle source
# File app/models/vimeo_embed_cache.rb, line 39
def self.geometry_hash geometry
  case geometry
  when FIXED_WIDTH_GEOMETRY
    {:width => $1}
  when FIXED_HEIGHT_GEOMETRY
    {:height => $1}
  when FIXED_GEOMETRY
    {:width => $1, :height => $2}
  else raise ArgumentError, "Didn't recognise the geometry string #{geometry}"
  end
end

Private Class Methods

static_embed_code(vid, geometry) click to toggle source
# File app/models/vimeo_embed_cache.rb, line 53
def self.static_embed_code vid, geometry
  width, height = geometry.split('x')
  "<iframe src=\"http://player.vimeo.com/video/#{vid}?portrait=0\" width=\"#{width}\" height=\"#{height}\" frameborder=\"0\"></iframe>"
end

Public Instance Methods

update_cache() click to toggle source
# File app/models/vimeo_embed_cache.rb, line 29
def update_cache
  cache true
  self.save
end

Private Instance Methods

cache(force = false) click to toggle source
# File app/models/vimeo_embed_cache.rb, line 58
def cache force = false
  if self.code.blank? or force  
    # Escape vimeo url and request oembed code
    url = CGI::escape("http://vimeo.com/#{self.vid}")
    
    response = HTTParty.get "http://vimeo.com/api/oembed.xml?url=#{url}&#{self.configuration.to_query}"
    self.code = response["oembed"]["html"]
  end
end