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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ test/random.rb
3
+ pkg
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.
@@ -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
@@ -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 = "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
@@ -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,42 @@
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/playlist'
16
+ require 'encosion/exceptions'
17
+
18
+ module Encosion
19
+
20
+ VERSION = '0.3.0'
21
+ LOGGER = Logger.new(STDOUT)
22
+
23
+ SERVER = 'api.brightcove.com'
24
+ PORT = 80
25
+ SECURE = false
26
+ READ_PATH = '/services/library'
27
+ WRITE_PATH = '/services/post'
28
+ DEFAULT_OPTIONS = { :debug => false }
29
+
30
+ @options = { :read_token => nil,
31
+ :write_token => nil,
32
+ :server => SERVER,
33
+ :port => PORT,
34
+ :secure => SECURE,
35
+ :read_path => READ_PATH,
36
+ :write_path => WRITE_PATH }
37
+ attr_accessor :options
38
+
39
+ # make @options available so it can be set externally when using the library
40
+ extend self
41
+
42
+ end
@@ -0,0 +1,161 @@
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
+
49
+ # This is an alias for find(:all)
50
+ def all(*args)
51
+ find(:all, *args)
52
+ end
53
+
54
+
55
+ # Performs an HTTP GET
56
+ def get(server,port,secure,path,command,options)
57
+ http = HTTPClient.new
58
+ url = secure ? 'https://' : 'http://'
59
+ url += "#{server}:#{port}#{path}"
60
+
61
+ options.merge!({'command' => command })
62
+ query_string = options.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
63
+
64
+ response = http.get(url, query_string)
65
+
66
+ body = response.body.content.strip == 'null' ? nil : JSON.parse(response.body.content.strip) # if the call returns 'null' then there were no valid results
67
+ header = response.header
68
+
69
+ error_check(header,body)
70
+
71
+ # puts "url: #{url}\nquery_string:#{query_string}"
72
+
73
+ return body
74
+ end
75
+
76
+
77
+ # Performs an HTTP POST
78
+ def post(server,port,secure,path,command,options,instance)
79
+ http = HTTPClient.new
80
+ url = secure ? 'https://' : 'http://'
81
+ url += "#{server}:#{port}#{path}"
82
+
83
+ content = { 'json' => { 'method' => command, 'params' => options }.to_json } # package up the variables as a JSON-RPC string
84
+ content.merge!({ 'file' => instance.file }) if instance.respond_to?('file') # and add a file if there is one
85
+
86
+ response = http.post(url, content)
87
+ # get the header and body for error checking
88
+ body = JSON.parse(response.body.content.strip)
89
+ header = response.header
90
+
91
+ error_check(header,body)
92
+ # if we get here then no exceptions were raised
93
+ return body
94
+ end
95
+
96
+
97
+ # Checks the HTTP response and handles any errors
98
+ def error_check(header,body)
99
+ if header.status_code == 200
100
+ return true if body.nil?
101
+ puts body['error']
102
+ if body.has_key? 'error' && !body['error'].nil?
103
+ message = "Brightcove responded with an error: #{body['error']} (code #{body['code']})"
104
+ body['errors'].each do |error|
105
+ message += "\n#{error.values.first} (code #{error.values.last})"
106
+ end if body.has_key? 'errors'
107
+ raise BrightcoveException, message
108
+ end
109
+ else
110
+ # should only happen if the Brightcove API is unavailable (even BC errors return a 200)
111
+ raise BrightcoveException, body + " (status code: #{header.status_code})"
112
+ end
113
+ end
114
+
115
+
116
+ protected
117
+
118
+ # Pulls any Hash off the end of an array of arguments and returns it
119
+ def extract_options(opts)
120
+ opts.last.is_a?(::Hash) ? opts.pop : {}
121
+ end
122
+
123
+
124
+ # Find an asset from a single or array of ids
125
+ def find_from_ids(ids, options)
126
+ expects_array = ids.first.kind_of?(Array)
127
+ return ids.first if expects_array && ids.first.empty?
128
+
129
+ ids = ids.flatten.compact.uniq
130
+
131
+ case ids.size
132
+ when 0
133
+ raise AssetNotFound, "Couldn't find #{self.class} without an ID"
134
+ when 1
135
+ result = find_one(ids.first, options)
136
+ expects_array ? [ result ] : result
137
+ else
138
+ find_some(ids, options)
139
+ end
140
+ end
141
+
142
+
143
+ # Turns a hash into a query string and appends the token
144
+ def queryize_args(args, type)
145
+ case type
146
+ when :read
147
+ raise MissingToken, 'No read token found' if @read_token.nil?
148
+ args.merge!({ :token => @read_token })
149
+ when :write
150
+ raise MissingToken, 'No write token found' if @write_token.nil?
151
+ args.merge!({ :token => @write_token })
152
+ end
153
+ return args.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
154
+ end
155
+
156
+ end
157
+
158
+
159
+ end
160
+
161
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ module EncosionError
2
+ class TokenMissing < StandardError; end;
3
+ class BrightcoveException < StandardError; end;
4
+ end
File without changes
@@ -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
+
3
+ # Raised if you try to set an invalid economics value
4
+ class InvalidEconomicsValue < StandardError; 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
+
168
+ # Find mutliple videos by id
169
+ def find_some(ids, options)
170
+ options.merge!({:video_ids => ids.join(',')})
171
+ response = read('find_videos_by_ids',options)
172
+ return response['items'].collect { |item| self.parse(item) }
173
+ end
174
+
175
+
176
+ # Find all videos
177
+ def find_all(options)
178
+ response = read('find_all_videos', options)
179
+ return response['items'].collect { |item| self.parse(item) }
180
+ end
181
+
182
+
183
+ # Creates a new Video object from a Ruby hash (used to create a video from a parsed API call)
184
+ def parse(obj)
185
+ if obj
186
+ args = {:id => obj['id'].to_i,
187
+ :name => obj['name'],
188
+ :short_description => obj['shortDescription'],
189
+ :long_description => obj['longDescription'],
190
+ :creation_date => Time.at(obj['creationDate'].to_i/1000),
191
+ :published_date => Time.at(obj['publishedDate'].to_i/1000),
192
+ :last_modified_date => Time.at(obj['lastModifiedDate'].to_i/1000),
193
+ :link_url => obj['linkURL'],
194
+ :link_text => obj['linkText'],
195
+ :tags => obj['tags'],
196
+ :video_still_url => obj['videoStillURL'],
197
+ :thumbnail_url => obj['thumbnailURL'],
198
+ :reference_id => obj['referenceID'],
199
+ :length => obj['length'].to_i,
200
+ :economics => obj['economics'] ? ENUMS[:economics].find { |key,value| value == obj['economics'] }.first : nil,
201
+ :plays_total => obj['playsTotal'].to_i,
202
+ :plays_trailing_week => obj['playsTrailingWeek'].to_i } unless obj.nil?
203
+ return self.new(args)
204
+ else
205
+ return nil
206
+ end
207
+ end
208
+
209
+ end
210
+
211
+ #
212
+ # Instance methods
213
+ #
214
+ def initialize(args={})
215
+ @id = args[:id]
216
+ @name = args[:name]
217
+ @short_description = args[:short_description]
218
+ @long_description = args[:long_description]
219
+ @creation_date = args[:creation_date]
220
+ @published_date = args[:published_date]
221
+ @last_modified_date = args[:last_modified_date]
222
+ @link_url = args[:link_url]
223
+ @link_text = args[:link_text]
224
+ @tags = args[:tags]
225
+ @video_still_url = args[:video_still_url]
226
+ @thumbnail_url = args[:thumbnail_url]
227
+ @reference_id = args[:reference_id]
228
+ @length = args[:length]
229
+ @economics = self.economics = args[:economics]
230
+ @plays_total = args[:plays_total]
231
+ @plays_trailing_week = args[:plays_trailing_week]
232
+ @file = args[:file]
233
+ end
234
+
235
+
236
+ # Saves a video to Brightcove. Returns the Brightcove ID for the video that was just uploaded.
237
+ # 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'])
238
+ # brightcove_id = new_video.save(:token => '123abc')
239
+
240
+ def save(args={})
241
+ # check to make sure we have everything needed for a create_video call
242
+ 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?
243
+ options = args.merge({ 'video' => self.to_brightcove }) # take the parameters of this video and make them a valid video object for upload
244
+ options.merge!({:token => Encosion.options[:write_token]}) unless options[:token]
245
+ response = Video.post(Encosion.options[:server],
246
+ Encosion.options[:port],
247
+ Encosion.options[:secure],
248
+ Encosion.options[:write_path],
249
+ 'create_video',
250
+ options,
251
+ self)
252
+ return response['result'] # returns the Brightcove ID of the video that was just uploaded
253
+ end
254
+
255
+
256
+ # Output the video as JSON
257
+ def to_json
258
+ { :id => @id,
259
+ :name => @name,
260
+ :short_description => @short_description,
261
+ :long_description => @long_description,
262
+ :creation_date => @creation_date,
263
+ :published_date => @published_date,
264
+ :last_modified_date => @last_modified_date,
265
+ :link_url => @link_url,
266
+ :link_text => @link_text,
267
+ :tags => @tags,
268
+ :video_still_url => @video_still_url,
269
+ :thumbnail_url => @thumbnail_url,
270
+ :reference_id => @reference_id,
271
+ :length => @length,
272
+ :economics => @economics,
273
+ :plays_total => @plays_total,
274
+ :plays_trailing_week => @plays_trailing_week }.to_json
275
+ end
276
+
277
+
278
+ # Outputs the video object into Brightcove's expected format
279
+ def to_brightcove
280
+ { 'name' => @name,
281
+ 'shortDescription' => @short_description,
282
+ 'longDescription' => @long_description,
283
+ 'linkURL' => @link_url,
284
+ 'linkText' => @link_text,
285
+ 'tags' => @tags,
286
+ 'referenceId' => @reference_id,
287
+ 'economics' => ENUMS[:economics][@economics] }
288
+ end
289
+
290
+
291
+ # Makes sure that the economics set on this video is one of a predetermined list
292
+ def economics=(sym)
293
+ unless sym.nil?
294
+ unless ENUMS[:economics].has_key?(sym)
295
+ raise InvalidEconomicsValue, "A video's economics value must be one of #{ENUMS[:economics].collect { |key,value| key.inspect }.join(', ')}"
296
+ else
297
+ @economics = sym
298
+ end
299
+ end
300
+ end
301
+
302
+ end
303
+
304
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class EncosionTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
Binary file
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'encosion'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 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-08-04 00:00:00 -07: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