honkster-encosion 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +3 -0
- data/LICENSE +20 -0
- data/README.rdoc +85 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/encosion.gemspec +60 -0
- data/lib/encosion/base.rb +152 -0
- data/lib/encosion/cue_point.rb +0 -0
- data/lib/encosion/exceptions.rb +4 -0
- data/lib/encosion/image.rb +90 -0
- data/lib/encosion/playlist.rb +24 -0
- data/lib/encosion/rendition.rb +0 -0
- data/lib/encosion/video.rb +304 -0
- data/lib/encosion.rb +43 -0
- data/test/encosion_test.rb +7 -0
- data/test/movie.mov +0 -0
- data/test/test_helper.rb +10 -0
- metadata +93 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Rob Cameron
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
Encosion (en-co-shen) is a Ruby library for working with Brightcove's Media API.
|
2
|
+
|
3
|
+
= Installation
|
4
|
+
|
5
|
+
To get the gem:
|
6
|
+
gem sources -a http://gems.github.com
|
7
|
+
sudo gem install cannikin-encosion
|
8
|
+
|
9
|
+
= Usage
|
10
|
+
|
11
|
+
First thing is first. Tell Encosion what your read and write API tokens are:
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'encosion'
|
15
|
+
|
16
|
+
Encosion.options[:read_token] = '123abc'
|
17
|
+
Encosion.options[:write_token] = 'abc123'
|
18
|
+
|
19
|
+
You don't have to set these ahead of time, you can pass in the token with each request
|
20
|
+
if you want, but do it here and you won't have to worry about it any more.
|
21
|
+
|
22
|
+
Encosion has several class methods that are similar to those found in ActiveRecord
|
23
|
+
for finding records. You'll pass a hash of options which correspond to those expected
|
24
|
+
by Brightcove (see http://docs.brightcove.com/en/media/). All calls will require you
|
25
|
+
to include either your read or write token depending on whether you are using the
|
26
|
+
read or write methods.
|
27
|
+
|
28
|
+
== Read Methods
|
29
|
+
|
30
|
+
# find all videos, 25 at a time, and return the first page
|
31
|
+
videos = Encosion::Video.find(:all, :page_size => 25, :page_number => 1)
|
32
|
+
|
33
|
+
# find a single video by Brightcove video id
|
34
|
+
video = Encosion::Video.find(12345)
|
35
|
+
|
36
|
+
# find several videos by their Brightcove video ids
|
37
|
+
videos = Encosion::Video.find(12345, 67890, 24680)
|
38
|
+
|
39
|
+
# find videos by your own reference id
|
40
|
+
video = Encosion::Video.find_by_reference_id('our_internal_id')
|
41
|
+
|
42
|
+
# check the status of a video (technically this uses the write API so it requires your write token)
|
43
|
+
status = Encosion::Video.status(12345) # finds by Brightcove video_id
|
44
|
+
status = Encosion::Video.status('our_internal_id') # finds by reference_id (string instead of integer)
|
45
|
+
|
46
|
+
These examples all assume that you've set your :read_token (and :write_token, in the case of
|
47
|
+
Video.status) ahead of time. If not you can simply include the token in your call:
|
48
|
+
|
49
|
+
videos = Encosion::Video.find(:all, :page_size => 25, :page_number => 1, :token => '123abc')
|
50
|
+
|
51
|
+
See Encosion::Video for all the available find methods and their variants.
|
52
|
+
|
53
|
+
== Write Methods
|
54
|
+
|
55
|
+
To write a video to Brightcove you will instantiate Encosion::Video, set a few required fields
|
56
|
+
and then save it:
|
57
|
+
|
58
|
+
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'])
|
59
|
+
brightcove_id = new_video.save
|
60
|
+
|
61
|
+
The save() method returns Brightcove's ID for the video (assuming the save was successful).
|
62
|
+
|
63
|
+
Again, the above examples assume that you've pre-set your :write_token. If not, just include it
|
64
|
+
in the save() call:
|
65
|
+
|
66
|
+
brightcove_id = new_video.save(:token => 'abc123')
|
67
|
+
|
68
|
+
Brightcove requires a name and short description before it will let you save a video. The fields
|
69
|
+
that you can set are those that Brightcove considers writable:
|
70
|
+
|
71
|
+
* name (string)
|
72
|
+
* short_description (string)
|
73
|
+
* long_description (string)
|
74
|
+
* link_url (string)
|
75
|
+
* link_text (string)
|
76
|
+
* tags (array of strings)
|
77
|
+
* reference_id (string)
|
78
|
+
* economics (enumerable, either :free or :ad_supported)
|
79
|
+
|
80
|
+
And of course the video file itself. Just give :file a File object.
|
81
|
+
|
82
|
+
= To Do
|
83
|
+
|
84
|
+
* Implement the remaining Video write methods: update_video, delete_video, share_video, add_image
|
85
|
+
* Implement the Playlist API read/write methods
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "honkster-encosion"
|
8
|
+
gem.summary = %q{Ruby library for working with the Brightcove API}
|
9
|
+
gem.email = "cannikinn@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/cannikin/encosion"
|
11
|
+
gem.authors = ["Rob Cameron"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
gem.add_dependency('httpclient', '>= 2.1.5.2')
|
14
|
+
gem.add_dependency('json', '>= 1.1.7')
|
15
|
+
end
|
16
|
+
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
if File.exist?('VERSION.yml')
|
47
|
+
config = YAML.load(File.read('VERSION.yml'))
|
48
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
49
|
+
else
|
50
|
+
version = ""
|
51
|
+
end
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "encosion #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
58
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0
|
data/encosion.gemspec
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{encosion}
|
5
|
+
s.version = "0.3.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Rob Cameron"]
|
9
|
+
s.date = %q{2009-08-04}
|
10
|
+
s.email = %q{cannikinn@gmail.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"encosion.gemspec",
|
23
|
+
"lib/encosion.rb",
|
24
|
+
"lib/encosion/base.rb",
|
25
|
+
"lib/encosion/cue_point.rb",
|
26
|
+
"lib/encosion/exceptions.rb",
|
27
|
+
"lib/encosion/image.rb",
|
28
|
+
"lib/encosion/playlist.rb",
|
29
|
+
"lib/encosion/rendition.rb",
|
30
|
+
"lib/encosion/video.rb",
|
31
|
+
"test/encosion_test.rb",
|
32
|
+
"test/movie.mov",
|
33
|
+
"test/test_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/cannikin/encosion}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.4}
|
39
|
+
s.summary = %q{Ruby library for working with the Brightcove API}
|
40
|
+
s.test_files = [
|
41
|
+
"test/encosion_test.rb",
|
42
|
+
"test/test_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_runtime_dependency(%q<httpclient>, [">= 2.1.5.2"])
|
51
|
+
s.add_runtime_dependency(%q<json>, [">= 1.1.7"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<httpclient>, [">= 2.1.5.2"])
|
54
|
+
s.add_dependency(%q<json>, [">= 1.1.7"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<httpclient>, [">= 2.1.5.2"])
|
58
|
+
s.add_dependency(%q<json>, [">= 1.1.7"])
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'httpclient'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Encosion
|
7
|
+
|
8
|
+
# Generic Encosion error class
|
9
|
+
class EncosionError < StandardError
|
10
|
+
end
|
11
|
+
|
12
|
+
# Raised when there is no token (required to use the Brightcove API)
|
13
|
+
class MissingToken < EncosionError
|
14
|
+
end
|
15
|
+
|
16
|
+
# Raised when some parameter is missing that we need in order to do a search
|
17
|
+
class AssetNotFound < EncosionError
|
18
|
+
end
|
19
|
+
|
20
|
+
# Raised when Brightcove doesn't like the call that was made for whatever reason
|
21
|
+
class BrightcoveException < EncosionError
|
22
|
+
end
|
23
|
+
|
24
|
+
# Raised when Brightcove doesn't like the call that was made for whatever reason
|
25
|
+
class NoFile < EncosionError
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# The base for all Encosion objects
|
30
|
+
class Base
|
31
|
+
|
32
|
+
attr_accessor :read_token, :write_token
|
33
|
+
|
34
|
+
#
|
35
|
+
# Class methods
|
36
|
+
#
|
37
|
+
class << self
|
38
|
+
|
39
|
+
# Does a GET to search photos and other good stuff
|
40
|
+
def find(*args)
|
41
|
+
options = extract_options(args)
|
42
|
+
case args.first
|
43
|
+
when :all then find_all(options)
|
44
|
+
else find_from_ids(args,options)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# This is an alias for find(:all)
|
49
|
+
def all(*args)
|
50
|
+
find(:all, *args)
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
# Performs an HTTP GET
|
55
|
+
def get(server,port,secure,path,command,options)
|
56
|
+
http = HTTPClient.new
|
57
|
+
url = secure ? 'https://' : 'http://'
|
58
|
+
url += "#{server}:#{port}#{path}"
|
59
|
+
|
60
|
+
options.merge!({'command' => command })
|
61
|
+
query_string = options.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
|
62
|
+
|
63
|
+
response = http.get(url, query_string)
|
64
|
+
|
65
|
+
body = response.body.content.strip == 'null' ? nil : JSON.parse(response.body.content.strip) # if the call returns 'null' then there were no valid results
|
66
|
+
header = response.header
|
67
|
+
|
68
|
+
error_check(header,body)
|
69
|
+
|
70
|
+
# puts "url: #{url}\nquery_string:#{query_string}"
|
71
|
+
|
72
|
+
return body
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
# Performs an HTTP POST
|
77
|
+
def post(server,port,secure,path,command,options,instance)
|
78
|
+
http = HTTPClient.new
|
79
|
+
url = secure ? 'https://' : 'http://'
|
80
|
+
url += "#{server}:#{port}#{path}"
|
81
|
+
|
82
|
+
content = { 'json' => { 'method' => command, 'params' => options}.to_json } # package up the variables as a JSON-RPC string
|
83
|
+
content.merge!({ 'file' => instance.file }) if instance.respond_to?('file') # and add a file if there is one
|
84
|
+
|
85
|
+
response = http.post(url, content)
|
86
|
+
# get the header and body for error checking
|
87
|
+
body = JSON.parse(response.body.content.strip)
|
88
|
+
header = response.header
|
89
|
+
|
90
|
+
error_check(header,body)
|
91
|
+
# if we get here then no exceptions were raised
|
92
|
+
return body
|
93
|
+
end
|
94
|
+
|
95
|
+
# Checks the HTTP response and handles any errors
|
96
|
+
def error_check(header,body)
|
97
|
+
if header.status_code == 200
|
98
|
+
return true if body.nil?
|
99
|
+
puts body['error']
|
100
|
+
if body.has_key? 'error' && !body['error'].nil?
|
101
|
+
message = "Brightcove responded with an error: #{body['error']} (code #{body['code']})"
|
102
|
+
body['errors'].each do |error|
|
103
|
+
message += "\n#{error.values.first} (code #{error.values.last})"
|
104
|
+
end if body.has_key? 'errors'
|
105
|
+
raise BrightcoveException, message
|
106
|
+
end
|
107
|
+
else
|
108
|
+
# should only happen if the Brightcove API is unavailable (even BC errors return a 200)
|
109
|
+
raise BrightcoveException, body + " (status code: #{header.status_code})"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
protected
|
114
|
+
# Pulls any Hash off the end of an array of arguments and returns it
|
115
|
+
def extract_options(opts)
|
116
|
+
opts.last.is_a?(::Hash) ? opts.pop : {}
|
117
|
+
end
|
118
|
+
|
119
|
+
# Find an asset from a single or array of ids
|
120
|
+
def find_from_ids(ids, options)
|
121
|
+
expects_array = ids.first.kind_of?(Array)
|
122
|
+
return ids.first if expects_array && ids.first.empty?
|
123
|
+
|
124
|
+
ids = ids.flatten.compact.uniq
|
125
|
+
|
126
|
+
case ids.size
|
127
|
+
when 0
|
128
|
+
raise AssetNotFound, "Couldn't find #{self.class} without an ID"
|
129
|
+
when 1
|
130
|
+
result = find_one(ids.first, options)
|
131
|
+
expects_array ? [ result ] : result
|
132
|
+
else
|
133
|
+
find_some(ids, options)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Turns a hash into a query string and appends the token
|
138
|
+
def queryize_args(args, type)
|
139
|
+
case type
|
140
|
+
when :read
|
141
|
+
raise MissingToken, 'No read token found' if @read_token.nil?
|
142
|
+
args.merge!({ :token => @read_token })
|
143
|
+
when :write
|
144
|
+
raise MissingToken, 'No write token found' if @write_token.nil?
|
145
|
+
args.merge!({ :token => @write_token })
|
146
|
+
end
|
147
|
+
return args.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
File without changes
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Encosion
|
2
|
+
|
3
|
+
class Image < Base
|
4
|
+
ENUMS = { :image_type => { :thumbnail => "THUMBNAIL", :video_still => "VIDEO_STILL"}}
|
5
|
+
|
6
|
+
attr_accessor(
|
7
|
+
:name,
|
8
|
+
:type,
|
9
|
+
:reference_id,
|
10
|
+
:remote_url,
|
11
|
+
:video_id
|
12
|
+
)
|
13
|
+
|
14
|
+
#
|
15
|
+
# Class methods
|
16
|
+
#
|
17
|
+
class << self
|
18
|
+
|
19
|
+
# the actual method that calls a post (user can use this directly if they want to call a method that's not included here)
|
20
|
+
def write(method, options)
|
21
|
+
# options.merge!(Encosion.options)
|
22
|
+
options.merge!({:token => Encosion.options[:write_token]}) unless options[:token]
|
23
|
+
|
24
|
+
Image.post( Encosion.options[:server],
|
25
|
+
Encosion.options[:port],
|
26
|
+
Encosion.options[:secure],
|
27
|
+
Encosion.options[:write_path],
|
28
|
+
method,
|
29
|
+
options,
|
30
|
+
self)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Instance methods
|
37
|
+
#
|
38
|
+
def initialize(args={})
|
39
|
+
@video_id = args[:video_id]
|
40
|
+
@name = args[:name]
|
41
|
+
@reference_id = args[:reference_id]
|
42
|
+
@remote_url = args[:remote_url]
|
43
|
+
@type = args[:type]
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
# Saves an image to Brightcove. Returns the Brightcove ID for the image that was just uploaded.
|
48
|
+
# new_image = Encosion::Image.new(:remote_file => "http://example.com/image.jpg", :display_name => "My Awesome Image", :type => "VIDEO_STILL", :video_id = > "brightcove_video_id")
|
49
|
+
# brightcove_id = new_image.save(:token => '123abc')
|
50
|
+
|
51
|
+
def save(args={})
|
52
|
+
# check to make sure we have everything needed for a create_video call
|
53
|
+
# 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?
|
54
|
+
options = args.merge({ 'image' => self.to_brightcove, :video_id => self.video_id }) # take the parameters of this video and make them a valid video object for upload
|
55
|
+
options.merge!({:token => Encosion.options[:write_token]}) unless options[:token]
|
56
|
+
response = Image.post(Encosion.options[:server],
|
57
|
+
Encosion.options[:port],
|
58
|
+
Encosion.options[:secure],
|
59
|
+
Encosion.options[:write_path],
|
60
|
+
'add_image',
|
61
|
+
options,
|
62
|
+
self)
|
63
|
+
return response['result'] # returns the Brightcove ID of the video that was just uploaded
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
# Output the image as JSON
|
68
|
+
def to_json
|
69
|
+
{
|
70
|
+
:name => @name,
|
71
|
+
:remote_url => @remote_url,
|
72
|
+
:type => ENUMS[:image_type][@type],
|
73
|
+
:reference_id => @reference_id
|
74
|
+
}.to_json
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
# Outputs the image object into Brightcove's expected format
|
79
|
+
def to_brightcove
|
80
|
+
{
|
81
|
+
'displayName' => @name,
|
82
|
+
'remoteUrl' => @remote_url,
|
83
|
+
'type' => ENUMS[:image_type][@type],
|
84
|
+
'referenceId' => @reference_id
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Encosion
|
2
|
+
|
3
|
+
class Playlist
|
4
|
+
|
5
|
+
attr_reader :id, :name, :short_description, :reference_id, :videos
|
6
|
+
|
7
|
+
def initialize(obj)
|
8
|
+
@id = obj['id']
|
9
|
+
@name = obj['name']
|
10
|
+
@short_description = obj['shortDescription']
|
11
|
+
@videos = obj['videos'].collect { |video| Video.new(video) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_json
|
15
|
+
{ :id => @id,
|
16
|
+
:name => @name,
|
17
|
+
:short_description => @short_description,
|
18
|
+
:reference_id => @reference_id,
|
19
|
+
:videos => @videos }.to_json
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
File without changes
|
@@ -0,0 +1,304 @@
|
|
1
|
+
module Encosion
|
2
|
+
# Raised if you try to set an invalid economics value
|
3
|
+
class InvalidEconomicsValue < StandardError;
|
4
|
+
end;
|
5
|
+
|
6
|
+
class Video < Base
|
7
|
+
|
8
|
+
ENUMS = { :economics => { :free => 'FREE', :ad_supported => 'AD_SUPPORTED'}}
|
9
|
+
|
10
|
+
attr_accessor(:name,
|
11
|
+
:short_description,
|
12
|
+
:long_description,
|
13
|
+
:link_url,
|
14
|
+
:link_text,
|
15
|
+
:tags,
|
16
|
+
:reference_id,
|
17
|
+
:economics,
|
18
|
+
:file)
|
19
|
+
attr_reader(:id,
|
20
|
+
:account_id,
|
21
|
+
:flv_url,
|
22
|
+
:creation_date,
|
23
|
+
:published_date,
|
24
|
+
:last_modified_date,
|
25
|
+
:video_still_url,
|
26
|
+
:thumbnail_url,
|
27
|
+
:length,
|
28
|
+
:plays_total,
|
29
|
+
:plays_trailing_week)
|
30
|
+
|
31
|
+
#
|
32
|
+
# Class methods
|
33
|
+
#
|
34
|
+
class << self
|
35
|
+
|
36
|
+
# Find a video by reference_id. Invokes Brightcove Media API command 'find_video_by_reference_id' or
|
37
|
+
# 'find_videos_by_reference_ids' depending on whether you call one or multiple ids
|
38
|
+
# Encosion::Video.find_by_reference_id('mycompany_1')
|
39
|
+
# Encosion::Video.find_by_reference_id('mycompany_1','mycompany_2','mycompany_3')
|
40
|
+
|
41
|
+
def find_by_reference_id(*args)
|
42
|
+
options = extract_options(args)
|
43
|
+
ids = args.flatten.compact.uniq
|
44
|
+
|
45
|
+
case ids.size
|
46
|
+
when 0
|
47
|
+
raise AssetNotFound, "Couldn't find #{self.class} without a reference ID"
|
48
|
+
when 1
|
49
|
+
options.merge!({:reference_id => ids.first})
|
50
|
+
response = read('find_video_by_reference_id', options)
|
51
|
+
return self.parse(response)
|
52
|
+
else
|
53
|
+
options.merge!({:reference_ids => ids.join(',')})
|
54
|
+
response = read('find_videos_by_reference_ids', options)
|
55
|
+
return response['items'].collect { |item| self.parse(item) }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Find a video by text search. Invokes Brightcove Media API command 'find_videos_by_text'
|
60
|
+
# Encosion::Video.find_by_text('funny videos')
|
61
|
+
|
62
|
+
def find_by_text(*args)
|
63
|
+
options = extract_options(args)
|
64
|
+
text = args.flatten.compact.uniq
|
65
|
+
raise AssetNotFound, "Couldn't find #{self.class} without text" if text == ''
|
66
|
+
options.merge!({:text => text.first})
|
67
|
+
if response = read('find_videos_by_text', options)
|
68
|
+
return response['items'].collect { |item| self.parse(item) }
|
69
|
+
else
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
# Find videos related to the given video_id. Invokes Brightcove Media API command 'find_related_videos'
|
76
|
+
# Encosion::Video.find_related(123456)
|
77
|
+
|
78
|
+
def find_related(*args)
|
79
|
+
options = extract_options(args)
|
80
|
+
raise AssetNotFound, "Cannot find related #{self.class}s without a video_id or reference_id" if options[:video_id].nil? && options[:reference_id].nil?
|
81
|
+
if response = read('find_related_videos', options)
|
82
|
+
return response['items'].collect { |item| self.parse(item) }
|
83
|
+
else
|
84
|
+
return nil
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
# Find a video by tag search. Invokes Brightcove Media API command 'find_videos_by_tags'
|
90
|
+
# Encosion::Video.find_by_tags('bloopers','gagreel','funny')
|
91
|
+
|
92
|
+
def find_by_tags(*args)
|
93
|
+
options = extract_options(args)
|
94
|
+
tags = args.flatten.compact.uniq
|
95
|
+
|
96
|
+
case tags.size
|
97
|
+
when 0
|
98
|
+
raise AssetNotFound, "Couldn't find #{self.class} without tags"
|
99
|
+
else
|
100
|
+
options.merge!({:and_tags => tags.join(',')})
|
101
|
+
if response = read('find_videos_by_tags', options)
|
102
|
+
return response['items'].collect { |item| self.parse(item) }
|
103
|
+
else
|
104
|
+
return nil
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
# Returns the status of a video upload (returns one of :uploading | :processing | :complete | :error )
|
111
|
+
# Takes either Brightcove's video_id or your own reference_id. If you pass an integer it's assumed to be
|
112
|
+
# a video_id, if you pass a string it's assumed to be a reference_id.
|
113
|
+
# Encosion::Video.status(12345)
|
114
|
+
|
115
|
+
def status(*args)
|
116
|
+
options = extract_options(args)
|
117
|
+
id = args.flatten.compact.uniq.first
|
118
|
+
|
119
|
+
if id.class == String
|
120
|
+
options.merge!({:reference_id => id})
|
121
|
+
else
|
122
|
+
options.merge!({:video_id => id})
|
123
|
+
end
|
124
|
+
|
125
|
+
if response = write('get_upload_status', options)
|
126
|
+
return response['result'].downcase.to_sym
|
127
|
+
else
|
128
|
+
return nil
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
# 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
|
+
def read(method, options)
|
135
|
+
# options.merge!(Encosion.options)
|
136
|
+
options.merge!({:token => Encosion.options[:read_token]}) unless options[:token]
|
137
|
+
get( Encosion.options[:server],
|
138
|
+
Encosion.options[:port],
|
139
|
+
Encosion.options[:secure],
|
140
|
+
Encosion.options[:read_path],
|
141
|
+
method,
|
142
|
+
options)
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
|
+
# the actual method that calls a post (user can use this directly if they want to call a method that's not included here)
|
147
|
+
def write(method, options)
|
148
|
+
# options.merge!(Encosion.options)
|
149
|
+
options.merge!({:token => Encosion.options[:write_token]}) unless options[:token]
|
150
|
+
Video.post( Encosion.options[:server],
|
151
|
+
Encosion.options[:port],
|
152
|
+
Encosion.options[:secure],
|
153
|
+
Encosion.options[:write_path],
|
154
|
+
method,
|
155
|
+
options,
|
156
|
+
self)
|
157
|
+
end
|
158
|
+
|
159
|
+
protected
|
160
|
+
# Find some videos by ids
|
161
|
+
def find_one(id, options)
|
162
|
+
options.merge!({:video_id => id})
|
163
|
+
response = read('find_video_by_id', options)
|
164
|
+
return self.parse(response)
|
165
|
+
end
|
166
|
+
|
167
|
+
# Find mutliple videos by id
|
168
|
+
def find_some(ids, options)
|
169
|
+
options.merge!({:video_ids => ids.join(',')})
|
170
|
+
response = read('find_videos_by_ids', options)
|
171
|
+
return response['items'].collect { |item| self.parse(item) }
|
172
|
+
end
|
173
|
+
|
174
|
+
# Find all videos
|
175
|
+
def find_all(options)
|
176
|
+
response = read('find_all_videos', options)
|
177
|
+
|
178
|
+
return response['items'].collect { |item| self.parse(item) }
|
179
|
+
end
|
180
|
+
|
181
|
+
def destroy(options)
|
182
|
+
response = read('delete_video', options)
|
183
|
+
return response['result']
|
184
|
+
end
|
185
|
+
|
186
|
+
# Creates a new Video object from a Ruby hash (used to create a video from a parsed API call)
|
187
|
+
def parse(obj)
|
188
|
+
if obj
|
189
|
+
args = {:id => obj['id'].to_i,
|
190
|
+
:name => obj['name'],
|
191
|
+
:short_description => obj['shortDescription'],
|
192
|
+
:long_description => obj['longDescription'],
|
193
|
+
:creation_date => Time.at(obj['creationDate'].to_i/1000),
|
194
|
+
:published_date => Time.at(obj['publishedDate'].to_i/1000),
|
195
|
+
:last_modified_date => Time.at(obj['lastModifiedDate'].to_i/1000),
|
196
|
+
:link_url => obj['linkURL'],
|
197
|
+
:link_text => obj['linkText'],
|
198
|
+
:tags => obj['tags'],
|
199
|
+
:video_still_url => obj['videoStillURL'],
|
200
|
+
:thumbnail_url => obj['thumbnailURL'],
|
201
|
+
:reference_id => obj['referenceID'],
|
202
|
+
:length => obj['length'].to_i,
|
203
|
+
:economics => obj['economics'] ? ENUMS[:economics].find { |key, value| value == obj['economics'] }.first : nil,
|
204
|
+
:plays_total => obj['playsTotal'].to_i,
|
205
|
+
:plays_trailing_week => obj['playsTrailingWeek'].to_i } unless obj.nil?
|
206
|
+
return self.new(args)
|
207
|
+
else
|
208
|
+
return nil
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
#
|
215
|
+
# Instance methods
|
216
|
+
#
|
217
|
+
def initialize(args={})
|
218
|
+
@id = args[:id]
|
219
|
+
@name = args[:name]
|
220
|
+
@short_description = args[:short_description]
|
221
|
+
@long_description = args[:long_description]
|
222
|
+
@creation_date = args[:creation_date]
|
223
|
+
@published_date = args[:published_date]
|
224
|
+
@last_modified_date = args[:last_modified_date]
|
225
|
+
@link_url = args[:link_url]
|
226
|
+
@link_text = args[:link_text]
|
227
|
+
@tags = args[:tags]
|
228
|
+
@video_still_url = args[:video_still_url]
|
229
|
+
@thumbnail_url = args[:thumbnail_url]
|
230
|
+
@reference_id = args[:reference_id]
|
231
|
+
@length = args[:length]
|
232
|
+
@economics = self.economics = args[:economics]
|
233
|
+
@plays_total = args[:plays_total]
|
234
|
+
@plays_trailing_week = args[:plays_trailing_week]
|
235
|
+
@file = args[:file]
|
236
|
+
end
|
237
|
+
|
238
|
+
# Saves a video to Brightcove. Returns the Brightcove ID for the video that was just uploaded.
|
239
|
+
# 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'])
|
240
|
+
# brightcove_id = new_video.save(:token => '123abc')
|
241
|
+
def save(args={})
|
242
|
+
# check to make sure we have everything needed for a create_video call
|
243
|
+
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?
|
244
|
+
options = args.merge({ 'video' => self.to_brightcove }) # take the parameters of this video and make them a valid video object for upload
|
245
|
+
options.merge!({:token => Encosion.options[:write_token]}) unless options[:token]
|
246
|
+
response = Video.post(Encosion.options[:server],
|
247
|
+
Encosion.options[:port],
|
248
|
+
Encosion.options[:secure],
|
249
|
+
Encosion.options[:write_path],
|
250
|
+
'create_video',
|
251
|
+
options,
|
252
|
+
self)
|
253
|
+
return response['result'] # returns the Brightcove ID of the video that was just uploaded
|
254
|
+
end
|
255
|
+
|
256
|
+
# Output the video as JSON
|
257
|
+
def to_json
|
258
|
+
{
|
259
|
+
:id => @id,
|
260
|
+
:name => @name,
|
261
|
+
:short_description => @short_description,
|
262
|
+
:long_description => @long_description,
|
263
|
+
:creation_date => @creation_date,
|
264
|
+
:published_date => @published_date,
|
265
|
+
:last_modified_date => @last_modified_date,
|
266
|
+
:link_url => @link_url,
|
267
|
+
:link_text => @link_text,
|
268
|
+
:tags => @tags,
|
269
|
+
:video_still_url => @video_still_url,
|
270
|
+
:thumbnail_url => @thumbnail_url,
|
271
|
+
:reference_id => @reference_id,
|
272
|
+
:length => @length,
|
273
|
+
:economics => @economics,
|
274
|
+
:plays_total => @plays_total,
|
275
|
+
:plays_trailing_week => @plays_trailing_week
|
276
|
+
}.to_json
|
277
|
+
end
|
278
|
+
|
279
|
+
# Outputs the video object into Brightcove's expected format
|
280
|
+
def to_brightcove
|
281
|
+
{
|
282
|
+
'name' => @name,
|
283
|
+
'shortDescription' => @short_description,
|
284
|
+
'longDescription' => @long_description,
|
285
|
+
'linkURL' => @link_url,
|
286
|
+
'linkText' => @link_text,
|
287
|
+
'tags' => @tags,
|
288
|
+
'referenceId' => @reference_id,
|
289
|
+
'economics' => ENUMS[:economics][@economics]
|
290
|
+
}
|
291
|
+
end
|
292
|
+
|
293
|
+
# Makes sure that the economics set on this video is one of a predetermined list
|
294
|
+
def economics=(sym)
|
295
|
+
unless sym.nil?
|
296
|
+
unless ENUMS[:economics].has_key?(sym)
|
297
|
+
raise InvalidEconomicsValue, "A video's economics value must be one of #{ENUMS[:economics].collect { |key, value| key.inspect }.join(', ')}"
|
298
|
+
else
|
299
|
+
@economics = sym
|
300
|
+
end
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
data/lib/encosion.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) # for use/testing when no gem is installed
|
2
|
+
|
3
|
+
# external
|
4
|
+
require 'net/http'
|
5
|
+
require 'net/https'
|
6
|
+
require 'uri'
|
7
|
+
require 'cgi'
|
8
|
+
require 'logger'
|
9
|
+
require 'json'
|
10
|
+
require 'yaml'
|
11
|
+
|
12
|
+
# internal
|
13
|
+
require 'encosion/base'
|
14
|
+
require 'encosion/video'
|
15
|
+
require 'encosion/image'
|
16
|
+
require 'encosion/playlist'
|
17
|
+
require 'encosion/exceptions'
|
18
|
+
|
19
|
+
module Encosion
|
20
|
+
|
21
|
+
VERSION = '0.3.0'
|
22
|
+
LOGGER = Logger.new(STDOUT)
|
23
|
+
|
24
|
+
SERVER = 'api.brightcove.com'
|
25
|
+
PORT = 80
|
26
|
+
SECURE = false
|
27
|
+
READ_PATH = '/services/library'
|
28
|
+
WRITE_PATH = '/services/post'
|
29
|
+
DEFAULT_OPTIONS = { :debug => false }
|
30
|
+
|
31
|
+
@options = { :read_token => nil,
|
32
|
+
:write_token => nil,
|
33
|
+
:server => SERVER,
|
34
|
+
:port => PORT,
|
35
|
+
:secure => SECURE,
|
36
|
+
:read_path => READ_PATH,
|
37
|
+
:write_path => WRITE_PATH }
|
38
|
+
attr_accessor :options
|
39
|
+
|
40
|
+
# make @options available so it can be set externally when using the library
|
41
|
+
extend self
|
42
|
+
|
43
|
+
end
|
data/test/movie.mov
ADDED
Binary file
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: honkster-encosion
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Cameron
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-06 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: httpclient
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.1.5.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.7
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: cannikinn@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- encosion.gemspec
|
52
|
+
- lib/encosion.rb
|
53
|
+
- lib/encosion/base.rb
|
54
|
+
- lib/encosion/cue_point.rb
|
55
|
+
- lib/encosion/exceptions.rb
|
56
|
+
- lib/encosion/image.rb
|
57
|
+
- lib/encosion/playlist.rb
|
58
|
+
- lib/encosion/rendition.rb
|
59
|
+
- lib/encosion/video.rb
|
60
|
+
- test/encosion_test.rb
|
61
|
+
- test/movie.mov
|
62
|
+
- test/test_helper.rb
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: http://github.com/cannikin/encosion
|
65
|
+
licenses: []
|
66
|
+
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options:
|
69
|
+
- --charset=UTF-8
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: "0"
|
83
|
+
version:
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.3.5
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Ruby library for working with the Brightcove API
|
91
|
+
test_files:
|
92
|
+
- test/encosion_test.rb
|
93
|
+
- test/test_helper.rb
|