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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/encosion/video.rb', line 216

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]
  @file = args[:file]
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



19
20
21
# File 'lib/encosion/video.rb', line 19

def 
  @account_id
end

#creation_dateObject (readonly)

Returns the value of attribute creation_date.



19
20
21
# File 'lib/encosion/video.rb', line 19

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

#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.



19
20
21
# File 'lib/encosion/video.rb', line 19

def flv_url
  @flv_url
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/encosion/video.rb', line 19

def id
  @id
end

#last_modified_dateObject (readonly)

Returns the value of attribute last_modified_date.



19
20
21
# File 'lib/encosion/video.rb', line 19

def last_modified_date
  @last_modified_date
end

#lengthObject (readonly)

Returns the value of attribute length.



19
20
21
# File 'lib/encosion/video.rb', line 19

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.



19
20
21
# File 'lib/encosion/video.rb', line 19

def plays_total
  @plays_total
end

#plays_trailing_weekObject (readonly)

Returns the value of attribute plays_trailing_week.



19
20
21
# File 'lib/encosion/video.rb', line 19

def plays_trailing_week
  @plays_trailing_week
end

#published_dateObject (readonly)

Returns the value of attribute published_date.



19
20
21
# File 'lib/encosion/video.rb', line 19

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

#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.



19
20
21
# File 'lib/encosion/video.rb', line 19

def thumbnail_url
  @thumbnail_url
end

#video_still_urlObject (readonly)

Returns the value of attribute video_still_url.



19
20
21
# File 'lib/encosion/video.rb', line 19

def video_still_url
  @video_still_url
end

Class Method Details

.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')


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

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')


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

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:



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/encosion/video.rb', line 62

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:



78
79
80
81
82
83
84
85
86
# File 'lib/encosion/video.rb', line 78

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)



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/encosion/video.rb', line 134

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],
        Encosion.options[:read_timeout],
        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)


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

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)



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/encosion/video.rb', line 148

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],
              Encosion.options[:send_timeout],
              method,
              options,
              self)
end

Instance Method Details

#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:



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/encosion/video.rb', line 242

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]
  response = Video.post(Encosion.options[:server],
                        Encosion.options[:port],
                        Encosion.options[:secure],
                        Encosion.options[:write_path],
                        Encosion.options[:send_timeout],
                        '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



282
283
284
285
286
287
288
289
290
291
# File 'lib/encosion/video.rb', line 282

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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/encosion/video.rb', line 260

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,
    :plays_trailing_week => @plays_trailing_week }.to_json
end