refinerycms-vimeo-videos 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. data/.gitignore +45 -0
  2. data/app/controllers/admin/vimeo_account_controller.rb +31 -0
  3. data/app/controllers/admin/vimeo_base_controller.rb +40 -0
  4. data/app/controllers/admin/vimeo_videos_controller.rb +75 -0
  5. data/app/models/vimeo_embed_cache.rb +62 -0
  6. data/app/models/vimeo_meta_cache.rb +56 -0
  7. data/app/models/vimeo_video.rb +21 -0
  8. data/app/views/admin/vimeo_videos/_existing_vimeo_video.html.erb +35 -0
  9. data/app/views/admin/vimeo_videos/_form.html.erb +55 -0
  10. data/app/views/admin/vimeo_videos/_vimeo_video.html.haml +1 -0
  11. data/app/views/admin/vimeo_videos/index.js.erb +12 -0
  12. data/app/views/admin/vimeo_videos/insert.html.erb +60 -0
  13. data/app/views/shared/admin/_vimeo_picker.html.erb +63 -0
  14. data/config/locales/en.yml +40 -0
  15. data/config/locales/lolcat.yml +25 -0
  16. data/config/locales/nb.yml +21 -0
  17. data/config/locales/nl.yml +21 -0
  18. data/config/routes.rb +14 -0
  19. data/db/migrate/create_vimeo_embed_cache.rb +18 -0
  20. data/db/migrate/create_vimeo_meta_cache.rb +26 -0
  21. data/db/migrate/create_vimeo_videos.rb +18 -0
  22. data/db/seeds/vimeo_videos.rb +6 -0
  23. data/features/manage_vimeo_videos.feature +63 -0
  24. data/features/step_definitions/vimeo_video_steps.rb +14 -0
  25. data/features/support/paths.rb +17 -0
  26. data/lib/generators/refinerycms_vimeo_videos_generator.rb +6 -0
  27. data/lib/refinerycms-vimeo-videos.rb +27 -0
  28. data/lib/tasks/vimeo_videos.rake +13 -0
  29. data/public/javascripts/refinery/vimeo_videos.js +178 -0
  30. data/public/stylesheets/admin/vimeo_videos.css +59 -0
  31. data/readme.md +10 -0
  32. data/spec/models/vimeo_video_spec.rb +32 -0
  33. metadata +129 -0
data/.gitignore ADDED
@@ -0,0 +1,45 @@
1
+ *.gemspec
2
+ *.gem
3
+
4
+ # rcov generated
5
+ coverage
6
+
7
+ # rdoc generated
8
+ rdoc
9
+
10
+ # yard generated
11
+ doc
12
+ .yardoc
13
+
14
+ # bundler
15
+ .bundle
16
+
17
+ # jeweler generated
18
+ pkg
19
+
20
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
21
+ #
22
+ # * Create a file at ~/.gitignore
23
+ # * Include files you want ignored
24
+ # * Run: git config --global core.excludesfile ~/.gitignore
25
+ #
26
+ # After doing this, these files will be ignored in all your git projects,
27
+ # saving you from having to 'pollute' every project you touch with them
28
+ #
29
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
30
+ #
31
+ # For MacOS:
32
+ #
33
+ .DS_Store
34
+ #
35
+ # For TextMate
36
+ *.tmproj
37
+ tmtags
38
+ #
39
+ # For emacs:
40
+ #*~
41
+ #\#*
42
+ #.\#*
43
+ #
44
+ # For vim:
45
+ #*.swp
@@ -0,0 +1,31 @@
1
+ module Admin
2
+ class VimeoAccountController < VimeoBaseController
3
+
4
+ def authorization
5
+ if not authorized? and ready_to_authorize?
6
+ base = Vimeo::Advanced::Base.new(
7
+ @account[:consumer_key],
8
+ @account[:consumer_secret])
9
+ request_token = base.get_request_token
10
+ session[:oauth_secret] = request_token.secret
11
+ redirect_to base.authorize_url
12
+ elsif ready_to_authorize?
13
+ callback
14
+ else
15
+ raise ArgumentError, 'Not ready to authorize. Type in consumer_key and consumer_secret.'
16
+ end
17
+ end
18
+
19
+ def callback
20
+ base = Vimeo::Advanced::Base.new(
21
+ @account[:consumer_key],
22
+ @account[:consumer_secret])
23
+ access_token = base.get_access_token(params[:oauth_token], session[:oauth_secret], params[:oauth_verifier])
24
+ RefinerySetting.find_by_name('vimeo_token').update_attribute(:value, access_token.token)
25
+ RefinerySetting.find_by_name('vimeo_secret').update_attribute(:value, access_token.secret)
26
+ flash[:notice] = "You successfully authorized your vimeo account for integration in your backend. You can now start using it."
27
+ redirect_to '/admin'
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,40 @@
1
+ require 'vimeo'
2
+ module Admin
3
+ class VimeoBaseController < Admin::BaseController
4
+
5
+ before_filter :get_account
6
+
7
+ CONSUMER_KEY_FORMAT = /[a-z0-9]{32}/
8
+ CONSUMER_SECRET_FORMAT = /[a-z0-9]{16}/
9
+ TOKEN_FORMAT = /[a-z0-9]{32}/
10
+ SECRET_FORMAT = /[a-z0-9]{40}/
11
+
12
+ protected
13
+
14
+ # Hack because I don't know how to turn off restriction otherwise
15
+ def restrict_plugins
16
+ end
17
+
18
+ # Hack because I don't know how to turn off restriction otherwise
19
+ def restrict_controller
20
+ end
21
+
22
+ def ready_to_authorize?
23
+ @account[:consumer_key] =~ CONSUMER_KEY_FORMAT and @account[:consumer_secret] =~ CONSUMER_SECRET_FORMAT
24
+ end
25
+
26
+ def authorized?
27
+ @account[:token] =~ TOKEN_FORMAT and @account[:secret] =~ SECRET_FORMAT
28
+ end
29
+
30
+ def get_account
31
+ @account = {
32
+ :username => RefinerySetting.find_or_set('vimeo_username', :value => "Username"),
33
+ :consumer_key => RefinerySetting.find_by_name('vimeo_consumer_key').value,
34
+ :consumer_secret => RefinerySetting.find_by_name('vimeo_consumer_secret').value,
35
+ :token => RefinerySetting.find_by_name('vimeo_token').value,
36
+ :secret => RefinerySetting.find_by_name('vimeo_secret').value}
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,75 @@
1
+ module Admin
2
+ class VimeoVideosController < Admin::VimeoBaseController
3
+
4
+ before_filter :init_dialog
5
+
6
+ def index
7
+ if authorized?
8
+ get_videos_on_vimeo_account
9
+ elsif request.xhr?
10
+ render :text => 'You are have not authorized this app to use your vimeo account.'
11
+ else
12
+ raise ArgumentError, 'You are have not authorized this app to use your vimeo account.'
13
+ end
14
+ end
15
+
16
+ def new
17
+ @vimeo_video = VimeoVideo.new
18
+ end
19
+
20
+ def create
21
+
22
+ end
23
+
24
+ def insert
25
+ self.new if @vimeo_video.nil?
26
+
27
+ @url_override = admin_vimeo_videos_url(:dialog => from_dialog?, :insert => true)
28
+
29
+ if params[:conditions].present?
30
+ extra_condition = params[:conditions].split(',')
31
+
32
+ extra_condition[1] = true if extra_condition[1] == "true"
33
+ extra_condition[1] = false if extra_condition[1] == "false"
34
+ extra_condition[1] = nil if extra_condition[1] == "nil"
35
+ paginate_vimeo_videos({extra_condition[0].to_sym => extra_condition[1]})
36
+ else
37
+ paginate_vimeo_videos
38
+ end
39
+ render :action => "insert"
40
+ end
41
+
42
+ protected
43
+ def get_videos_on_vimeo_account
44
+ get_account
45
+ video = Vimeo::Advanced::Video.new(
46
+ @account[:consumer_key],
47
+ @account[:consumer_secret],
48
+ :token => @account[:token],
49
+ :secret => @account[:secret])
50
+ @vimeo_videos = video.get_all(@account[:username], {:full_response => true, :sort => 'upload_date'})["videos"]["video"]
51
+ end
52
+
53
+ def init_dialog
54
+ @app_dialog = params[:app_dialog].present?
55
+ @field = params[:field]
56
+ @update_vimeo_video = params[:update_vimeo_video]
57
+ @update_text = params[:update_text]
58
+ @thumbnail = params[:thumbnail]
59
+ @callback = params[:callback]
60
+ @conditions = params[:conditions]
61
+ @current_link = params[:current_link]
62
+ end
63
+
64
+ def restrict_controller
65
+ super unless action_name == 'insert'
66
+ end
67
+
68
+ def paginate_vimeo_videos(conditions={})
69
+ @vimeo_videos = get_videos_on_vimeo_account.paginate :page => (@paginate_page_number ||= params[:page]),
70
+ :conditions => conditions,
71
+ :order => 'created_at DESC',
72
+ :per_page => VimeoVideo.per_page(from_dialog?)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,62 @@
1
+ require 'httparty'
2
+
3
+ class VimeoEmbedCache < ActiveRecord::Base
4
+
5
+ set_table_name "vimeo_embed_cache"
6
+
7
+ belongs_to :vimeo_video
8
+
9
+ before_save :cache
10
+
11
+ serialize :configuration, Hash
12
+
13
+ validates_presence_of :configuration
14
+ validates_presence_of :vid
15
+
16
+ def self.embed vid, geometry, configuration = {}
17
+ configuration.stringify_keys!
18
+ configuration.merge!(geometry_hash(geometry))
19
+
20
+ static = configuration.delete("static") if configuration.has_key?("static")
21
+ if static
22
+ geometry =~ FIXED_GEOMETRY ?
23
+ static_embed_code(vid, geometry) : raise(ArgumentError, "Must use fixed geometry string for static embeds (e.g. 100x240)")
24
+ else
25
+ find_or_create_by_vid_and_configuration(vid, :configuration => YAML.dump(configuration))
26
+ end
27
+ end
28
+
29
+ FIXED_WIDTH_GEOMETRY = /^(\d+)$/ # e.g. '300'
30
+ FIXED_HEIGHT_GEOMETRY = /^x(\d+)$/ # e.g. 'x200'
31
+ FIXED_GEOMETRY = /^(\d+)x(\d+)$/ # e.g. '300x200'
32
+ VIMEO_GEOMETRY = Regexp.union FIXED_GEOMETRY, FIXED_HEIGHT_GEOMETRY, FIXED_WIDTH_GEOMETRY
33
+
34
+ def self.geometry_hash geometry
35
+ case geometry
36
+ when FIXED_WIDTH_GEOMETRY
37
+ {:width => $1}
38
+ when FIXED_HEIGHT_GEOMETRY
39
+ {:height => $1}
40
+ when FIXED_GEOMETRY
41
+ {:width => $1, :height => $2}
42
+ else raise ArgumentError, "Didn't recognise the geometry string #{self.geometry}"
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def self.static_embed_code vid, geometry
49
+ width, height = geometry.split('x')
50
+ "<iframe src=\"http://player.vimeo.com/video/#{vid}?portrait=0\" width=\"#{width}\" height=\"#{height}\" frameborder=\"0\"></iframe>"
51
+ end
52
+
53
+ def cache force = false
54
+ if self.code.blank? or force
55
+ # Escape vimeo url and request oembed code
56
+ url = CGI::escape("http://vimeo.com/#{self.vid}")
57
+
58
+ response = HTTParty.get "http://vimeo.com/api/oembed.xml?url=#{url}&#{self.configuration.to_query}"
59
+ self.code = response["oembed"]["html"]
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,56 @@
1
+ require 'vimeo'
2
+ class VimeoMetaCache < ActiveRecord::Base
3
+
4
+ set_table_name "vimeo_meta_cache"
5
+
6
+ has_many :vimeo_video_embed_caches, :dependent => :destroy
7
+
8
+ belongs_to :image, :class_name => 'Image'
9
+
10
+ before_save :cache
11
+
12
+ validates_presence_of :vid
13
+
14
+ def embed geometry, options = {}
15
+ VimeoEmbedCache.embed(self.vid, geometry, options).code
16
+ end
17
+
18
+ def url
19
+ "http://www.vimeo.com/#{self.vid}"
20
+ end
21
+
22
+ def force_cache
23
+ cache true
24
+ self.save
25
+ end
26
+
27
+ private
28
+
29
+ def cache force = false
30
+ if self.title.blank? or self.image_id.blank?
31
+ get_account
32
+ video = Vimeo::Advanced::Video.new(
33
+ @account[:consumer_key],
34
+ @account[:consumer_secret],
35
+ :token => @account[:token],
36
+ :secret => @account[:secret])
37
+ video_info = video.get_info(self.vid)["video"].first
38
+
39
+ # Save cached image
40
+ vimeo_thumb_url = video_info["thumbnails"]["thumbnail"].last["_content"]
41
+ self.create_image(:image => URLTempfile.new(vimeo_thumb_url)) if self.image_id.blank? or force
42
+
43
+ # Save cached title
44
+ self.title = video_info["description"] if self.title.blank? or force
45
+ end
46
+ end
47
+
48
+ def get_account
49
+ @account = {
50
+ :username => RefinerySetting.find_or_set('vimeo_username', :value => "Username"),
51
+ :consumer_key => RefinerySetting.find_by_name('vimeo_consumer_key').value,
52
+ :consumer_secret => RefinerySetting.find_by_name('vimeo_consumer_secret').value,
53
+ :token => RefinerySetting.find_by_name('vimeo_token').value,
54
+ :secret => RefinerySetting.find_by_name('vimeo_secret').value}
55
+ end
56
+ end
@@ -0,0 +1,21 @@
1
+ # This model acts as a wrapper for uploaded videos only
2
+ # Everything else happens directly with the advanced vimeo api
3
+
4
+ class VimeoVideo < ActiveRecord::Base
5
+
6
+ # What is the max resource size a user can upload
7
+ MAX_SIZE_IN_MB = 1000
8
+
9
+ # when a dialog pops up with resources, how many resources per page should there be
10
+ PAGES_PER_DIALOG = 18
11
+
12
+ # when listing resources out in the admin area, how many resources should show per page
13
+ PAGES_PER_ADMIN_INDEX = 20
14
+
15
+ class << self
16
+ # How many resources per page should be displayed?
17
+ def per_page(dialog = false)
18
+ dialog ? PAGES_PER_DIALOG : PAGES_PER_ADMIN_INDEX
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ <div id='existing_vimeo_video_area' class='dialog_area' <%= "style='display:none;'" if @vimeo_video.errors.any? %>>
2
+ <input type='hidden' name='selected_vimeo_video' id='selected_vimeo_video' />
3
+ <div id='existing_vimeo_video_area_content' class='clearfix'>
4
+ <div id='vimeos_list'>
5
+ <ul>
6
+ <% @vimeo_videos.each do |vimeo_video| -%>
7
+ <li<%= " class='selected'" if @vimeo_video_id == false %>>
8
+ <%= image_tag vimeo_video["thumbnails"]["thumbnail"].first["_content"], {
9
+ :tooltip => vimeo_video["title"],
10
+ :alt => vimeo_video["title"],
11
+ :title => vimeo_video["title"],
12
+ :"data-image" => vimeo_video["thumbnails"]["thumbnail"][1]["_content"],
13
+ :id => "vimeo_video_#{vimeo_video['id']}",
14
+ :"data-id" => vimeo_video["id"]} %>
15
+ </li>
16
+ <% end %>
17
+ </ul>
18
+ </div>
19
+ </div>
20
+
21
+ <%= render :partial => "/shared/admin/form_actions",
22
+ :locals => {
23
+ :f => nil,
24
+ :cancel_url => '',
25
+ :submit_button_text => t('.button_text'),
26
+ :hide_submit => @vimeo_videos.empty?,
27
+ :hide_cancel => false,
28
+ :hide_delete => true,
29
+ :cancel_title => nil,
30
+ :paginate => {
31
+ :collection => @vimeo_videos,
32
+ :url => {:controller => "/admin/vimeo_videos", :action => "insert", :dialog => from_dialog? }
33
+ }
34
+ } if @app_dialog or @vimeo_videos.any? %>
35
+ </div>
@@ -0,0 +1,55 @@
1
+ <%= form_for [:admin, @vimeo_video], :url => @url_override || @url, :html => {:multipart => true} do |f| %>
2
+
3
+ <%= render :partial => "/shared/admin/error_messages",
4
+ :locals => {
5
+ :object => @vimeo_video,
6
+ :include_object_name => false
7
+ } %>
8
+
9
+ <div class='field'>
10
+ <% if action_name =~ /(edit)|(update)/ %>
11
+ <p>
12
+ <%= t('.use_current_vimeo_video') %>
13
+ <em><%= t('.or') %></em><%= t('.replace_vimeo_video') %>
14
+ </p>
15
+ <p>
16
+ <%= f.file_field :vimeo_video %>
17
+ </p>
18
+ <% else %>
19
+ <% # we must only hint at multiple when it's a new record otherwise update fails. %>
20
+ <%= f.file_field :vimeo_video, :multiple => true %>
21
+ <% end %>
22
+ </div>
23
+
24
+ <div class='field'>
25
+ <label><%= t('.maximum_vimeo_video_size', :megabytes => VimeoVideo::MAX_SIZE_IN_MB) %></label>
26
+ </div>
27
+
28
+ <input type='hidden' name='wymeditor' value='<%= params[:wymeditor] %>'>
29
+
30
+ <%= render :partial => "/shared/admin/form_actions",
31
+ :locals => {
32
+ :f => f,
33
+ :continue_editing => false,
34
+ :hide_cancel => (@app_dialog or action_name == "insert" or from_dialog?),
35
+ :delete_title => t('delete', :scope => 'admin.vimeo_videos'),
36
+ :delete_confirmation => t('message', :scope => 'shared.admin.delete', :title => @vimeo_video.title || @vimeo_video.vid)
37
+ } -%>
38
+
39
+ <% if @app_dialog %>
40
+ <input type='hidden' name='app_dialog' value='<%= @app_dialog %>' />
41
+ <input type='hidden' name='field' value='<%= @field %>' />
42
+ <input type='hidden' name='update_vimeo_video' value='<%= @update_vimeo_video %>' />
43
+ <input type='hidden' name='thumbnail' value='<%= @thumbnail %>' />
44
+ <input type='hidden' name='callback' value='<%= @callback %>' />
45
+ <input type='hidden' name='conditions' value='<%= @conditions %>' />
46
+ <% end %>
47
+ <% end %>
48
+
49
+ <% if action_name =~ /(edit)|(update)/ %>
50
+ <div id='existing_vimeo_video'>
51
+ <label><%=t('.current_vimeo_video') %></label>
52
+ <%#= image_fu @vimeo_video.image, '225x255>', :class => "brown_border" %>
53
+ <%= content_tag :span, @vimeo_video.vid, :class => "brown_border" %>
54
+ </div>
55
+ <% end %>
@@ -0,0 +1 @@
1
+ = image_tag vimeo_video["thumbnails"]["thumbnail"].first["_content"], {:tooltip => vimeo_video["title"], :"data-vimeo-video-uid" => vimeo_video["id"], :class => (vimeo_video['is_used'] ? 'activated' : nil)}
@@ -0,0 +1,12 @@
1
+ $('#vimeo_videos').html("<div class='vimeo_videos'/>");
2
+ $('.vimeo_videos').hide().html("<%= escape_javascript(render(:partial => 'vimeo_video', :collection => @vimeo_videos)) %>").fadeIn();
3
+ $('#vimeo_videos').after('<div id="vimeo_video_uids" style="display:none" />');
4
+ init_tooltips();
5
+ <% if params[:vimeo_videos] %>
6
+ <% for vimeo_video in @vimeo_videos do %>
7
+ $('#vimeo_video_uids').append('<input data-destroy-vimeo-video-id="<%= vimeo_video["vimeo_video_uid"] %>" name="project[vimeo_videos_attributes][<%= vimeo_video["uid"] %>][_destroy]" type="hidden" value=0 />');
8
+ $('#vimeo_video_uids').append('<input name="project[vimeo_videos_attributes][<%= vimeo_video["uid"] %>][vimeo_video_uid]" type="hidden" value="<%= vimeo_video["vimeo_video_uid"] %>" />');
9
+ $('#vimeo_video_uids').append('<input name="project[vimeo_videos_attributes][<%= vimeo_video["uid"] %>][uid]" type="hidden" value="<%= vimeo_video["uid"] %>" />');
10
+ <% end %>
11
+ <% end %>
12
+ $('form .vimeo_videos img').vimeo();