Class: Vimeo::Easy

Inherits:
Object
  • Object
show all
Defined in:
lib/easy-vimeo.rb

Constant Summary collapse

UPLOAD_URL =
"http://vimeo.com/services/upload"
PRIVACY_MODES =
[:anybody, :contacts, :nobody, :users]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Easy

This method initializes a new instance. You should pass an options Hash containing : :api_key # The API key of your account :secret_key # The secret key :auth_token # (optional) The authorization token, needed to upload a video :video_id # (optional) The ID of the video, if it is already uploaded



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/easy-vimeo.rb', line 23

def initialize(options = {})
  raise "You must pass :api_key and :secret_key at least to initialize the class." unless options[:api_key] && options[:secret_key]
  self.api_key = options[:api_key]
  self.auth_token = options[:auth_token]
  self.video_id = options[:video_id]
  self.vimeo_upload = Vimeo::Advanced::Upload.new(self.api_key, options[:secret_key])
  self.vimeo_video = Vimeo::Advanced::Video.new(self.api_key, options[:secret_key])
  self.privacy = :anybody
  @new_record = true
  
  if self.video_id
    self.reload
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



13
14
15
# File 'lib/easy-vimeo.rb', line 13

def api_key
  @api_key
end

#auth_tokenObject

Returns the value of attribute auth_token.



13
14
15
# File 'lib/easy-vimeo.rb', line 13

def auth_token
  @auth_token
end

#descriptionObject

Returns the value of attribute description.



13
14
15
# File 'lib/easy-vimeo.rb', line 13

def description
  @description
end

#fileObject

Returns the value of attribute file.



13
14
15
# File 'lib/easy-vimeo.rb', line 13

def file
  @file
end

#privacyObject

Returns the value of attribute privacy.



14
15
16
# File 'lib/easy-vimeo.rb', line 14

def privacy
  @privacy
end

#tagsObject

Returns the value of attribute tags.



14
15
16
# File 'lib/easy-vimeo.rb', line 14

def tags
  @tags
end

#thumbnailObject

Returns the value of attribute thumbnail.



14
15
16
# File 'lib/easy-vimeo.rb', line 14

def thumbnail
  @thumbnail
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/easy-vimeo.rb', line 13

def title
  @title
end

#video_idObject

Returns the value of attribute video_id.



13
14
15
# File 'lib/easy-vimeo.rb', line 13

def video_id
  @video_id
end

#vimeo_uploadObject

Returns the value of attribute vimeo_upload.



13
14
15
# File 'lib/easy-vimeo.rb', line 13

def vimeo_upload
  @vimeo_upload
end

#vimeo_videoObject

Returns the value of attribute vimeo_video.



13
14
15
# File 'lib/easy-vimeo.rb', line 13

def vimeo_video
  @vimeo_video
end

Class Method Details

.find(video_id, options = {}) ⇒ Object



38
39
40
# File 'lib/easy-vimeo.rb', line 38

def self.find(video_id, options = {})
  self.new(options.merge(:video_id => video_id))
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/easy-vimeo.rb', line 67

def available?
  !!@availability
end

#destroyObject

This method destroys the Vimeo Video



58
59
60
61
62
63
64
65
# File 'lib/easy-vimeo.rb', line 58

def destroy
  check_presence_of :video_id, :auth_token
  
  self.vimeo_video.delete(self.auth_token, self.video_id)
  true
rescue
  false
end

#new_record?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/easy-vimeo.rb', line 42

def new_record?
  @new_record
end

#reloadObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/easy-vimeo.rb', line 71

def reload
  check_presence_of :video_id

  begin
    video_response = self.vimeo_video.get_info(self.video_id)['video'][0]
    self.title = video_response['title']
    self.description = video_response['description']
    self.privacy = video_response['privacy'].to_sym
    self.thumbnail = (video_response['thumbnails']['thumbnail'].first rescue nil)
    @tags = (video_response['tags']['tag'].join(', ') rescue nil)
    @availability = video_response['is_transcoding'].to_i.zero? && video_response['is_uploading'].to_i.zero?
    @new_record = false

    true
  rescue
    false
  end
end

#saveObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/easy-vimeo.rb', line 90

def save
  if self.file
    raise "Cannot upload a video file on an existing video! Create a new instance instead." unless new_record?
    post_video!
  end
  check_presence_of :video_id, :auth_token
  
  begin
    self.vimeo_video.set_privacy(self.auth_token, self.video_id, self.privacy.to_s)
    self.vimeo_video.set_description(self.auth_token, self.video_id, self.description) if self.description
    self.vimeo_video.set_title(self.auth_token, self.video_id, self.title) if self.title
    if @new_tags
      self.vimeo_video.clear_tags(self.auth_token, self.video_id)
      self.vimeo_video.add_tags(self.auth_token, self.video_id, self.tags)
      @new_tags = nil
    end
  rescue
    return false
  end
  
  @new_record = false
  @file = nil
  true
end