Skip to content

Instantly share code, notes, and snippets.

@Marchino
Last active December 14, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marchino/5014076 to your computer and use it in GitHub Desktop.
Save Marchino/5014076 to your computer and use it in GitHub Desktop.
class Video < ActiveRecord::Base
attr_accessible :credits, :image, :link, :title, :video_category_id, :homepage, :client
belongs_to :video_category
before_validation :update_attributes_through_api
mount_uploader :image, ImageUploader
acts_as_list
def vimeo_url
return "http://vimeo.com/api/v2/video/#{self.link}.json"
end
private
def update_attributes_through_api
if self.title.blank? or self.credits.blank? or self.image.blank?
video = JSON.parse(open(self.vimeo_url).read).last
if video.present?
self.title = video["title"] unless self.title.present?
self.credits = video["description"] unless self.credits.present?
self.image = video["thumbnail_large"] unless self.image.present?
end
end
return self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment