honkster-encosion 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .DS_Store
2
2
  test/random.rb
3
3
  pkg
4
+ .idea/
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake'
4
4
  begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
- gem.name = "honkster-encosion"
7
+ gem.name = "encosion"
8
8
  gem.summary = %q{Ruby library for working with the Brightcove API}
9
9
  gem.email = "cannikinn@gmail.com"
10
10
  gem.homepage = "http://github.com/cannikin/encosion"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
data/encosion.gemspec CHANGED
@@ -1,12 +1,12 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  Gem::Specification.new do |s|
4
- s.name = %q{encosion}
5
- s.version = "0.3.0"
4
+ s.name = %q{honkster-encosion}
5
+ s.version = "0.3.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Rob Cameron"]
9
- s.date = %q{2009-08-04}
9
+ s.date = %q{2009-11-09}
10
10
  s.email = %q{cannikinn@gmail.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
32
32
  "test/movie.mov",
33
33
  "test/test_helper.rb"
34
34
  ]
35
- s.homepage = %q{http://github.com/cannikin/encosion}
35
+ s.homepage = %q{http://github.com/honkster/encosion}
36
36
  s.rdoc_options = ["--charset=UTF-8"]
37
37
  s.require_paths = ["lib"]
38
38
  s.rubygems_version = %q{1.3.4}
data/lib/encosion/base.rb CHANGED
@@ -4,38 +4,38 @@ require 'httpclient'
4
4
  require 'json'
5
5
 
6
6
  module Encosion
7
-
7
+
8
8
  # Generic Encosion error class
9
9
  class EncosionError < StandardError
10
10
  end
11
-
11
+
12
12
  # Raised when there is no token (required to use the Brightcove API)
13
13
  class MissingToken < EncosionError
14
14
  end
15
-
15
+
16
16
  # Raised when some parameter is missing that we need in order to do a search
17
17
  class AssetNotFound < EncosionError
18
18
  end
19
-
19
+
20
20
  # Raised when Brightcove doesn't like the call that was made for whatever reason
21
21
  class BrightcoveException < EncosionError
22
22
  end
23
-
23
+
24
24
  # Raised when Brightcove doesn't like the call that was made for whatever reason
25
25
  class NoFile < EncosionError
26
26
  end
27
-
28
-
27
+
28
+
29
29
  # The base for all Encosion objects
30
30
  class Base
31
-
31
+
32
32
  attr_accessor :read_token, :write_token
33
33
 
34
34
  #
35
35
  # Class methods
36
36
  #
37
37
  class << self
38
-
38
+
39
39
  # Does a GET to search photos and other good stuff
40
40
  def find(*args)
41
41
  options = extract_options(args)
@@ -44,42 +44,43 @@ module Encosion
44
44
  else find_from_ids(args,options)
45
45
  end
46
46
  end
47
-
47
+
48
+
48
49
  # This is an alias for find(:all)
49
50
  def all(*args)
50
51
  find(:all, *args)
51
52
  end
52
-
53
+
53
54
 
54
55
  # Performs an HTTP GET
55
56
  def get(server,port,secure,path,command,options)
56
57
  http = HTTPClient.new
57
58
  url = secure ? 'https://' : 'http://'
58
59
  url += "#{server}:#{port}#{path}"
59
-
60
+
60
61
  options.merge!({'command' => command })
61
62
  query_string = options.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
62
-
63
+
63
64
  response = http.get(url, query_string)
64
-
65
+
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
66
67
  header = response.header
67
-
68
+
68
69
  error_check(header,body)
69
-
70
+
70
71
  # puts "url: #{url}\nquery_string:#{query_string}"
71
72
 
72
73
  return body
73
74
  end
74
-
75
-
75
+
76
+
76
77
  # Performs an HTTP POST
77
78
  def post(server,port,secure,path,command,options,instance)
78
79
  http = HTTPClient.new
79
80
  url = secure ? 'https://' : 'http://'
80
81
  url += "#{server}:#{port}#{path}"
81
-
82
- content = { 'json' => { 'method' => command, 'params' => options}.to_json } # package up the variables as a JSON-RPC string
82
+
83
+ content = { 'json' => { 'method' => command, 'params' => options }.to_json } # package up the variables as a JSON-RPC string
83
84
  content.merge!({ 'file' => instance.file }) if instance.respond_to?('file') # and add a file if there is one
84
85
 
85
86
  response = http.post(url, content)
@@ -91,15 +92,16 @@ module Encosion
91
92
  # if we get here then no exceptions were raised
92
93
  return body
93
94
  end
94
-
95
+
96
+
95
97
  # Checks the HTTP response and handles any errors
96
98
  def error_check(header,body)
97
99
  if header.status_code == 200
98
100
  return true if body.nil?
99
- puts body['error']
101
+ # puts body['error']
100
102
  if body.has_key? 'error' && !body['error'].nil?
101
103
  message = "Brightcove responded with an error: #{body['error']} (code #{body['code']})"
102
- body['errors'].each do |error|
104
+ body['errors'].each do |error|
103
105
  message += "\n#{error.values.first} (code #{error.values.last})"
104
106
  end if body.has_key? 'errors'
105
107
  raise BrightcoveException, message
@@ -109,13 +111,16 @@ module Encosion
109
111
  raise BrightcoveException, body + " (status code: #{header.status_code})"
110
112
  end
111
113
  end
114
+
112
115
 
113
116
  protected
117
+
114
118
  # Pulls any Hash off the end of an array of arguments and returns it
115
119
  def extract_options(opts)
116
120
  opts.last.is_a?(::Hash) ? opts.pop : {}
117
121
  end
118
122
 
123
+
119
124
  # Find an asset from a single or array of ids
120
125
  def find_from_ids(ids, options)
121
126
  expects_array = ids.first.kind_of?(Array)
@@ -133,6 +138,7 @@ module Encosion
133
138
  find_some(ids, options)
134
139
  end
135
140
  end
141
+
136
142
 
137
143
  # Turns a hash into a query string and appends the token
138
144
  def queryize_args(args, type)
@@ -146,7 +152,10 @@ module Encosion
146
152
  end
147
153
  return args.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
148
154
  end
149
-
155
+
150
156
  end
157
+
158
+
151
159
  end
160
+
152
161
  end
@@ -1,43 +1,43 @@
1
1
  module Encosion
2
+
2
3
  # Raised if you try to set an invalid economics value
3
- class InvalidEconomicsValue < StandardError;
4
- end;
5
-
4
+ class InvalidEconomicsValue < StandardError; end;
5
+
6
6
  class Video < Base
7
-
7
+
8
8
  ENUMS = { :economics => { :free => 'FREE', :ad_supported => 'AD_SUPPORTED'}}
9
-
10
- attr_accessor(:name,
11
- :short_description,
9
+
10
+ attr_accessor(:name,
11
+ :short_description,
12
12
  :long_description,
13
- :link_url,
14
- :link_text,
15
- :tags,
16
- :reference_id,
13
+ :link_url,
14
+ :link_text,
15
+ :tags,
16
+ :reference_id,
17
17
  :economics,
18
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
- #
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
32
  # Class methods
33
33
  #
34
34
  class << self
35
-
35
+
36
36
  # Find a video by reference_id. Invokes Brightcove Media API command 'find_video_by_reference_id' or
37
37
  # 'find_videos_by_reference_ids' depending on whether you call one or multiple ids
38
38
  # Encosion::Video.find_by_reference_id('mycompany_1')
39
39
  # Encosion::Video.find_by_reference_id('mycompany_1','mycompany_2','mycompany_3')
40
-
40
+
41
41
  def find_by_reference_id(*args)
42
42
  options = extract_options(args)
43
43
  ids = args.flatten.compact.uniq
@@ -47,48 +47,48 @@ module Encosion
47
47
  raise AssetNotFound, "Couldn't find #{self.class} without a reference ID"
48
48
  when 1
49
49
  options.merge!({:reference_id => ids.first})
50
- response = read('find_video_by_reference_id', options)
50
+ response = read('find_video_by_reference_id',options)
51
51
  return self.parse(response)
52
52
  else
53
53
  options.merge!({:reference_ids => ids.join(',')})
54
- response = read('find_videos_by_reference_ids', options)
54
+ response = read('find_videos_by_reference_ids',options)
55
55
  return response['items'].collect { |item| self.parse(item) }
56
- end
56
+ end
57
57
  end
58
-
58
+
59
59
  # Find a video by text search. Invokes Brightcove Media API command 'find_videos_by_text'
60
60
  # Encosion::Video.find_by_text('funny videos')
61
-
61
+
62
62
  def find_by_text(*args)
63
63
  options = extract_options(args)
64
64
  text = args.flatten.compact.uniq
65
65
  raise AssetNotFound, "Couldn't find #{self.class} without text" if text == ''
66
66
  options.merge!({:text => text.first})
67
- if response = read('find_videos_by_text', options)
67
+ if response = read('find_videos_by_text',options)
68
68
  return response['items'].collect { |item| self.parse(item) }
69
69
  else
70
70
  nil
71
71
  end
72
72
  end
73
-
73
+
74
74
 
75
75
  # Find videos related to the given video_id. Invokes Brightcove Media API command 'find_related_videos'
76
76
  # Encosion::Video.find_related(123456)
77
-
77
+
78
78
  def find_related(*args)
79
79
  options = extract_options(args)
80
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)
81
+ if response = read('find_related_videos',options)
82
82
  return response['items'].collect { |item| self.parse(item) }
83
83
  else
84
84
  return nil
85
85
  end
86
86
  end
87
-
88
-
87
+
88
+
89
89
  # Find a video by tag search. Invokes Brightcove Media API command 'find_videos_by_tags'
90
90
  # Encosion::Video.find_by_tags('bloopers','gagreel','funny')
91
-
91
+
92
92
  def find_by_tags(*args)
93
93
  options = extract_options(args)
94
94
  tags = args.flatten.compact.uniq
@@ -98,53 +98,53 @@ module Encosion
98
98
  raise AssetNotFound, "Couldn't find #{self.class} without tags"
99
99
  else
100
100
  options.merge!({:and_tags => tags.join(',')})
101
- if response = read('find_videos_by_tags', options)
101
+ if response = read('find_videos_by_tags',options)
102
102
  return response['items'].collect { |item| self.parse(item) }
103
103
  else
104
104
  return nil
105
105
  end
106
- end
106
+ end
107
107
  end
108
-
109
-
108
+
109
+
110
110
  # Returns the status of a video upload (returns one of :uploading | :processing | :complete | :error )
111
111
  # Takes either Brightcove's video_id or your own reference_id. If you pass an integer it's assumed to be
112
112
  # a video_id, if you pass a string it's assumed to be a reference_id.
113
113
  # Encosion::Video.status(12345)
114
-
114
+
115
115
  def status(*args)
116
116
  options = extract_options(args)
117
117
  id = args.flatten.compact.uniq.first
118
-
118
+
119
119
  if id.class == String
120
120
  options.merge!({:reference_id => id})
121
121
  else
122
122
  options.merge!({:video_id => id})
123
123
  end
124
-
125
- if response = write('get_upload_status', options)
124
+
125
+ if response = write('get_upload_status',options)
126
126
  return response['result'].downcase.to_sym
127
127
  else
128
128
  return nil
129
129
  end
130
130
  end
131
-
132
-
131
+
132
+
133
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)
134
+ def read(method,options)
135
135
  # options.merge!(Encosion.options)
136
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)
137
+ get( Encosion.options[:server],
138
+ Encosion.options[:port],
139
+ Encosion.options[:secure],
140
+ Encosion.options[:read_path],
141
+ method,
142
+ options)
143
143
  end
144
-
145
-
144
+
145
+
146
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)
147
+ def write(method,options)
148
148
  # options.merge!(Encosion.options)
149
149
  options.merge!({:token => Encosion.options[:write_token]}) unless options[:token]
150
150
  Video.post( Encosion.options[:server],
@@ -157,60 +157,57 @@ module Encosion
157
157
  end
158
158
 
159
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
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)
209
165
  end
210
- end
211
-
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
+
212
209
  end
213
-
210
+
214
211
  #
215
212
  # Instance methods
216
213
  #
@@ -234,14 +231,16 @@ module Encosion
234
231
  @plays_trailing_week = args[:plays_trailing_week]
235
232
  @file = args[:file]
236
233
  end
237
-
234
+
235
+
238
236
  # Saves a video to Brightcove. Returns the Brightcove ID for the video that was just uploaded.
239
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'])
240
238
  # brightcove_id = new_video.save(:token => '123abc')
239
+
241
240
  def save(args={})
242
241
  # check to make sure we have everything needed for a create_video call
243
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?
244
- options = args.merge({ 'video' => self.to_brightcove }) # take the parameters of this video and make them a valid video object for upload
243
+ options = args.merge({ 'video' => self.to_brightcove }) # take the parameters of this video and make them a valid video object for upload
245
244
  options.merge!({:token => Encosion.options[:write_token]}) unless options[:token]
246
245
  response = Video.post(Encosion.options[:server],
247
246
  Encosion.options[:port],
@@ -250,13 +249,13 @@ module Encosion
250
249
  'create_video',
251
250
  options,
252
251
  self)
253
- return response['result'] # returns the Brightcove ID of the video that was just uploaded
252
+ return response['result'] # returns the Brightcove ID of the video that was just uploaded
254
253
  end
255
-
254
+
255
+
256
256
  # Output the video as JSON
257
257
  def to_json
258
- {
259
- :id => @id,
258
+ { :id => @id,
260
259
  :name => @name,
261
260
  :short_description => @short_description,
262
261
  :long_description => @long_description,
@@ -272,33 +271,34 @@ module Encosion
272
271
  :length => @length,
273
272
  :economics => @economics,
274
273
  :plays_total => @plays_total,
275
- :plays_trailing_week => @plays_trailing_week
276
- }.to_json
274
+ :plays_trailing_week => @plays_trailing_week }.to_json
277
275
  end
278
-
276
+
277
+
279
278
  # Outputs the video object into Brightcove's expected format
280
279
  def to_brightcove
281
- {
282
- 'name' => @name,
280
+ { 'name' => @name,
283
281
  'shortDescription' => @short_description,
284
282
  'longDescription' => @long_description,
285
283
  'linkURL' => @link_url,
286
284
  'linkText' => @link_text,
287
285
  'tags' => @tags,
288
286
  'referenceId' => @reference_id,
289
- 'economics' => ENUMS[:economics][@economics]
290
- }
287
+ 'economics' => ENUMS[:economics][@economics] }
291
288
  end
292
-
289
+
290
+
293
291
  # Makes sure that the economics set on this video is one of a predetermined list
294
292
  def economics=(sym)
295
293
  unless sym.nil?
296
294
  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(', ')}"
295
+ raise InvalidEconomicsValue, "A video's economics value must be one of #{ENUMS[:economics].collect { |key,value| key.inspect }.join(', ')}"
298
296
  else
299
297
  @economics = sym
300
298
  end
301
299
  end
302
300
  end
301
+
303
302
  end
303
+
304
304
  end
data/lib/encosion.rb CHANGED
@@ -17,27 +17,27 @@ require 'encosion/playlist'
17
17
  require 'encosion/exceptions'
18
18
 
19
19
  module Encosion
20
-
21
- VERSION = '0.3.0'
20
+
21
+ VERSION = '0.3.1'
22
22
  LOGGER = Logger.new(STDOUT)
23
-
23
+
24
24
  SERVER = 'api.brightcove.com'
25
25
  PORT = 80
26
26
  SECURE = false
27
27
  READ_PATH = '/services/library'
28
28
  WRITE_PATH = '/services/post'
29
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,
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
37
  :write_path => WRITE_PATH }
38
38
  attr_accessor :options
39
-
39
+
40
40
  # make @options available so it can be set externally when using the library
41
41
  extend self
42
-
42
+
43
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: honkster-encosion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Cameron
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-06 00:00:00 -08:00
12
+ date: 2009-11-09 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -61,7 +61,7 @@ files:
61
61
  - test/movie.mov
62
62
  - test/test_helper.rb
63
63
  has_rdoc: true
64
- homepage: http://github.com/cannikin/encosion
64
+ homepage: http://github.com/honkster/encosion
65
65
  licenses: []
66
66
 
67
67
  post_install_message: