Skip to content

Instantly share code, notes, and snippets.

@trevorturk
Created June 27, 2010 15:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trevorturk/454990 to your computer and use it in GitHub Desktop.
Save trevorturk/454990 to your computer and use it in GitHub Desktop.
class Video < ActiveRecord::Base
before_validation :set_attrs_via_embedly, :on => :create
def set_attrs_via_embedly
# example url: http://www.youtube.com/watch?v=oHg5SJYRHA0
attrs = Embedly.get_attrs url
errors[:base] << "is not a video" unless attrs['type'] == 'video'
self.attributes = attrs
end
end
class Embedly
def self.get_attrs(url)
embedly_url = "http://api.embed.ly/v1/api/oembed?url=#{url}"
response = RestClient.get embedly_url
attrs = JSON.parse response.body
rescue
{}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment