Class: Encosion::Video

Inherits:
Base
  • Object
show all
Defined in:
lib/encosion/video.rb

Constant Summary collapse

ENUMS =
{ :economics => { :free => 'FREE', :ad_supported => 'AD_SUPPORTED'}}

Instance Attribute Summary collapse

Attributes inherited from Base

#read_token, #write_token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

all, error_check, find, get, post

Constructor Details

#initialize(args = {}) ⇒ Video

Instance methods



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/encosion/video.rb', line 251

def initialize(args={})
  @id = args[:id]
  @name = args[:name]
  @short_description = args[:short_description]
  @long_description = args[:long_description]
  @creation_date = args[:creation_date]
  @published_date = args[:published_date]
  @last_modified_date = args[:last_modified_date]
  @link_url = args[:link_url]
  @link_text = args[:link_text]
  @tags = args[:tags]
  @video_still_url = args[:video_still_url]
  @thumbnail_url = args[:thumbnail_url]
  @reference_id = args[:reference_id]
  @length = args[:length]
  @economics = self.economics = args[:economics]
  @plays_total = args[:plays_total]
  @plays_trailing_week = args[:plays_trailing_week]
  @encode_to = args[:encode_to] || 'MP4'
  @create_multiple_renditions = args[:create_multiple_renditions] || true
  @flv_url = args[:flv_url]
  @renditions = args[:renditions]
  @file = args[:file]
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



21
22
23
# File 'lib/encosion/video.rb', line 21

def 
  @account_id
end

#create_multiple_renditionsObject

Returns the value of attribute create_multiple_renditions.



10
11
12
# File 'lib/encosion/video.rb', line 10

def create_multiple_renditions
  @create_multiple_renditions
end

#creation_dateObject (readonly)

Returns the value of attribute creation_date.



21
22
23
# File 'lib/encosion/video.rb', line 21

def creation_date
  @creation_date
end

#economicsObject

Returns the value of attribute economics.



10
11
12
# File 'lib/encosion/video.rb', line 10

def economics
  @economics
end

#encode_toObject

Returns the value of attribute encode_to.



10
11
12
# File 'lib/encosion/video.rb', line 10

def encode_to
  @encode_to
end

#fileObject

Returns the value of attribute file.



10
11
12
# File 'lib/encosion/video.rb', line 10

def file
  @file
end

#flv_urlObject (readonly)

Returns the value of attribute flv_url.



21
22
23
# File 'lib/encosion/video.rb', line 21

def flv_url
  @flv_url
end

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/encosion/video.rb', line 21

def id
  @id
end

#last_modified_dateObject (readonly)

Returns the value of attribute last_modified_date.



21
22
23
# File 'lib/encosion/video.rb', line 21

def last_modified_date
  @last_modified_date
end

#lengthObject (readonly)

Returns the value of attribute length.



21
22
23
# File 'lib/encosion/video.rb', line 21

def length
  @length
end

Returns the value of attribute link_text.



10
11
12
# File 'lib/encosion/video.rb', line 10

def link_text
  @link_text
end

Returns the value of attribute link_url.



10
11
12
# File 'lib/encosion/video.rb', line 10

def link_url
  @link_url
end

#long_descriptionObject

Returns the value of attribute long_description.



10
11
12
# File 'lib/encosion/video.rb', line 10

def long_description
  @long_description
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/encosion/video.rb', line 10

def name
  @name
end

#plays_totalObject (readonly)

Returns the value of attribute plays_total.



21
22
23
# File 'lib/encosion/video.rb', line 21

def plays_total
  @plays_total
end

#plays_trailing_weekObject (readonly)

Returns the value of attribute plays_trailing_week.



21
22
23
# File 'lib/encosion/video.rb', line 21

def plays_trailing_week
  @plays_trailing_week
end

#published_dateObject (readonly)

Returns the value of attribute published_date.



21
22
23
# File 'lib/encosion/video.rb', line 21

def published_date
  @published_date
end

#reference_idObject

Returns the value of attribute reference_id.



10
11
12
# File 'lib/encosion/video.rb', line 10

def reference_id
  @reference_id
end

#renditionsObject (readonly)

Returns the value of attribute renditions.



21
22
23
# File 'lib/encosion/video.rb', line 21

def renditions
  @renditions
end

#short_descriptionObject

Returns the value of attribute short_description.



10
11
12
# File 'lib/encosion/video.rb', line 10

def short_description
  @short_description
end

#tagsObject

Returns the value of attribute tags.



10
11
12
# File 'lib/encosion/video.rb', line 10

def tags
  @tags
end

#thumbnail_urlObject (readonly)

Returns the value of attribute thumbnail_url.



21
22
23
# File 'lib/encosion/video.rb', line 21

def thumbnail_url
  @thumbnail_url
end

#video_still_urlObject (readonly)

Returns the value of attribute video_still_url.



21
22
23
# File 'lib/encosion/video.rb', line 21

def video_still_url
  @video_still_url
end

Class Method Details

.delete_by_reference_id(*args) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/encosion/video.rb', line 135

def delete_by_reference_id(*args)
  options = extract_options(args)
  id = args.flatten.compact.uniq.first

  if id.class == String
    options.merge!({:reference_id => id})
  else
    options.merge!({:video_id => id})
  end

  if response = write('delete_video', options)
    return response["error"].nil? ? true : response["error"]
  else
    return nil
  end
end

.find_by_reference_id(*args) ⇒ Object

Find a video by reference_id. Invokes Brightcove Media API command ‘find_video_by_reference_id’ or ‘find_videos_by_reference_ids’ depending on whether you call one or multiple ids

Encosion::Video.find_by_reference_id('mycompany_1')
Encosion::Video.find_by_reference_id('mycompany_1','mycompany_2','mycompany_3')


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/encosion/video.rb', line 44

def find_by_reference_id(*args)
  options = extract_options(args)
  ids = args.flatten.compact.uniq

  case ids.size
    when 0
      raise AssetNotFound, "Couldn't find #{self.class} without a reference ID"
    when 1
      options.merge!({:reference_id => ids.first})
      response = read('find_video_by_reference_id',options)
      return self.parse(response)
    else
      options.merge!({:reference_ids => ids.join(',')})
      response = read('find_videos_by_reference_ids',options)
      return response['items'].collect { |item| self.parse(item) }
    end
end

.find_by_tags(*args) ⇒ Object

Find a video by tag search. Invokes Brightcove Media API command ‘find_videos_by_tags’

Encosion::Video.find_by_tags('bloopers','gagreel','funny')


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/encosion/video.rb', line 95

def find_by_tags(*args)
  options = extract_options(args)
  tags = args.flatten.compact.uniq

  case tags.size
    when 0
      raise AssetNotFound, "Couldn't find #{self.class} without tags"
    else
      options.merge!({:and_tags => tags.join(',')})
      if response = read('find_videos_by_tags',options)
        return response['items'].collect { |item| self.parse(item) }
      else
        return nil
      end
    end
end

.find_by_text(*args) ⇒ Object

Find a video by text search. Invokes Brightcove Media API command ‘find_videos_by_text’

Encosion::Video.find_by_text('funny videos')

Raises:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/encosion/video.rb', line 65

def find_by_text(*args)
  options = extract_options(args)
  text = args.flatten.compact.uniq
  raise AssetNotFound, "Couldn't find #{self.class} without text" if text == ''
  options.merge!({:text => text.first})
  if response = read('find_videos_by_text',options)
    return response['items'].collect { |item| self.parse(item) }
  else
    nil
  end
end

Find videos related to the given video_id. Invokes Brightcove Media API command ‘find_related_videos’

Encosion::Video.find_related(123456)

Raises:



81
82
83
84
85
86
87
88
89
# File 'lib/encosion/video.rb', line 81

def find_related(*args)
  options = extract_options(args)
  raise AssetNotFound, "Cannot find related #{self.class}s without a video_id or reference_id" if options[:video_id].nil? && options[:reference_id].nil?
  if response = read('find_related_videos',options)
    return response['items'].collect { |item| self.parse(item) }
  else
    return nil
  end
end

.read(method, options) ⇒ Object

the actual method that calls a get (user can use this directly if they want to call a method that’s not included here)



154
155
156
157
158
159
160
161
162
163
# File 'lib/encosion/video.rb', line 154

def read(method,options)
  # options.merge!(Encosion.options)
  options.merge!({:token => Encosion.options[:read_token]}) unless options[:token]
  get(  Encosion.options[:server],
        Encosion.options[:port],
        Encosion.options[:secure],
        Encosion.options[:read_path],
        method,
        options)
end

.status(*args) ⇒ Object

Returns the status of a video upload (returns one of :uploading | :processing | :complete | :error ) Takes either Brightcove’s video_id or your own reference_id. If you pass an integer it’s assumed to be a video_id, if you pass a string it’s assumed to be a reference_id.

Encosion::Video.status(12345)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/encosion/video.rb', line 118

def status(*args)
  options = extract_options(args)
  id = args.flatten.compact.uniq.first

  if id.class == String
    options.merge!({:reference_id => id})
  else
    options.merge!({:video_id => id})
  end

  if response = write('get_upload_status',options)
    return response['result'].downcase.to_sym
  else
    return nil
  end
end

.write(method, options) ⇒ Object

the actual method that calls a post (user can use this directly if they want to call a method that’s not included here)



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/encosion/video.rb', line 167

def write(method,options)
  # options.merge!(Encosion.options)
  options.merge!({:token => Encosion.options[:write_token]}) unless options[:token]
  Video.post( Encosion.options[:server],
              Encosion.options[:port],
              Encosion.options[:secure],
              Encosion.options[:write_path],
              method,
              options,
              self)
end

Instance Method Details

#best_rendition(max_rate = 262144) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/encosion/video.rb', line 236

def best_rendition(max_rate = 262144)
  return flv_url unless renditions

  urls = {}
  best_rate = renditions.map do |r|
    urls[r[:encodingRate]] = r[:url]
    r[:encodingRate]
  end.sort.reverse.select { |rate| rate <= max_rate }.first

  urls[best_rate] || flv_url || urls.values.first
end

#save(args = {}) ⇒ Object

Saves a video to Brightcove. Returns the Brightcove ID for the video that was just uploaded.

new_video = Encosion::Video.new(:file => File.new('/path/to/file'), :name => "My Awesome Video", :short_description => "A video of some awesome happenings", :tags => ['awesome','sweet'])
brightcove_id = new_video.save(:token => '123abc')

Raises:



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/encosion/video.rb', line 281

def save(args={})
  # check to make sure we have everything needed for a create_video call
  raise NoFile, "You need to attach a file to this video before you can upload it: Video.file = File.new('/path/to/file')" if @file.nil?
  options = args.merge({ 'video' => self.to_brightcove })   # take the parameters of this video and make them a valid video object for upload
  options.merge!({ :token => Encosion.options[:write_token] }) unless options[:token]
  options.merge!({ 'encode_to' => @encode_to }) unless options[:encode_to]
  options.merge!({ 'create_multiple_renditions' => @create_multiple_renditions }) unless options[:create_multiple_renditions]
  response = Video.post(Encosion.options[:server],
                        Encosion.options[:port],
                        Encosion.options[:secure],
                        Encosion.options[:write_path],
                        'create_video',
                        options,
                        self)
  return response['result']    # returns the Brightcove ID of the video that was just uploaded
end

#to_brightcoveObject

Outputs the video object into Brightcove’s expected format



323
324
325
326
327
328
329
330
331
332
# File 'lib/encosion/video.rb', line 323

def to_brightcove
  { 'name' => @name,
    'shortDescription' => @short_description,
    'longDescription' => @long_description,
    'linkURL' => @link_url,
    'linkText' => @link_text,
    'tags' => @tags,
    'referenceId' => @reference_id,
    'economics' => ENUMS[:economics][@economics] }
end

#to_jsonObject

Output the video as JSON



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/encosion/video.rb', line 300

def to_json
  { :id => @id,
    :name => @name,
    :short_description => @short_description,
    :long_description => @long_description,
    :creation_date => @creation_date,
    :published_date => @published_date,
    :last_modified_date => @last_modified_date,
    :link_url => @link_url,
    :link_text => @link_text,
    :tags => @tags,
    :video_still_url => @video_still_url,
    :thumbnail_url => @thumbnail_url,
    :reference_id => @reference_id,
    :length => @length,
    :economics => @economics,
    :plays_total => @plays_total,
    :flv_url => @flv_url,
    :plays_trailing_week => @plays_trailing_week }.to_json
end