cannikin-encosion 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,3 @@
1
- tokens.txt
2
1
  .DS_Store
3
2
  test/random.rb
3
+ pkg
data/README.rdoc CHANGED
@@ -8,6 +8,17 @@ To get the gem:
8
8
 
9
9
  = Usage
10
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
+
11
22
  Encosion has several class methods that are similar to those found in ActiveRecord
12
23
  for finding records. You'll pass a hash of options which correspond to those expected
13
24
  by Brightcove (see http://docs.brightcove.com/en/media/). All calls will require you
@@ -16,25 +27,27 @@ read or write methods.
16
27
 
17
28
  == Read Methods
18
29
 
19
- require 'rubygems'
20
- require 'encosion'
21
-
22
30
  # find all videos, 25 at a time, and return the first page
23
- videos = Encosion::Video.find(:all, :token => '123abc', :page_size => 25, :page_number => 1)
31
+ videos = Encosion::Video.find(:all, :page_size => 25, :page_number => 1)
24
32
 
25
33
  # find a single video by Brightcove video id
26
- video = Encosion::Video.find(12345, :token => '123abc')
34
+ video = Encosion::Video.find(12345)
27
35
 
28
36
  # find several videos by their Brightcove video ids
29
- videos = Encosion::Video.find(12345, 67890, 24680, :token => '123abc')
37
+ videos = Encosion::Video.find(12345, 67890, 24680)
30
38
 
31
39
  # find videos by your own reference id
32
- video = Encosion::Video.find_by_reference_id('our_internal_id', :token => '123abc')
40
+ video = Encosion::Video.find_by_reference_id('our_internal_id')
33
41
 
34
- # check the status of a video (technically this uses the write API so you need to provide your write token)
35
- status = Encosion::Video.status(12345, :token => '123abc') # finds by Brightcove video_id
36
- status = Encosion::Video.status('our_internal_id', :token => '123abc') # finds by reference_id (string instead of integer)
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)
37
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
+
38
51
  See Encosion::Video for all the available find methods and their variants.
39
52
 
40
53
  == Write Methods
@@ -42,13 +55,15 @@ See Encosion::Video for all the available find methods and their variants.
42
55
  To write a video to Brightcove you will instantiate Encosion::Video, set a few required fields
43
56
  and then save it:
44
57
 
45
- require 'rubygems'
46
- require 'encosion'
47
-
48
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'])
49
- brightcove_id = new_video.save(:token => '123abc')
59
+ brightcove_id = new_video.save
50
60
 
51
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')
52
67
 
53
68
  Brightcove requires a name and short description before it will let you save a video. The fields
54
69
  that you can set are those that Brightcove considers writable:
@@ -62,10 +77,9 @@ that you can set are those that Brightcove considers writable:
62
77
  * reference_id (string)
63
78
  * economics (enumerable, either :free or :ad_supported)
64
79
 
65
- And of course the video file itself.
80
+ And of course the video file itself. Just give :file a File object.
66
81
 
67
82
  = To Do
68
83
 
69
84
  * Implement the remaining Video write methods: update_video, delete_video, share_video, add_image
70
85
  * Implement the Playlist API read/write methods
71
- * Create a persistent config store so you can set the tokens once instead of on each method call
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
data/encosion.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{encosion}
5
- s.version = "0.2.2"
5
+ s.version = "0.3.0"
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-03}
9
+ s.date = %q{2009-08-04}
10
10
  s.email = %q{cannikinn@gmail.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
data/lib/encosion.rb CHANGED
@@ -17,38 +17,26 @@ require 'encosion/exceptions'
17
17
 
18
18
  module Encosion
19
19
 
20
- VERSION = '0.1.3'
20
+ VERSION = '0.3.0'
21
21
  LOGGER = Logger.new(STDOUT)
22
22
 
23
- def self.new(*args)
24
- Engine.new(*args)
25
- end
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 }
26
29
 
27
- class Engine
28
-
29
- SERVER = 'api.brightcove.com'
30
- PORT = 80
31
- PATH = '/services/library'
32
- SECURE = false
33
- DEFAULT_ARGS = { :read_token => nil, :write_token => nil }
34
- DEFAULT_OPTIONS = { :debug => false }
35
-
36
- attr_reader :read_token, :write_token
37
-
38
- def initialize(options={})
39
- @options = DEFAULT_OPTIONS.merge(options)
40
- @options[:read_token] = @options[:token] if @options[:token] # if there's only one token, assume it's a read
41
- @read_token = @options[:read_token]
42
- @write_token = @options[:write_token]
43
- if @read_token.nil? && @write_token.nil?
44
- raise EncosionError::TokenMissing, 'You must provide either a read or write token to use the Brightcove API'
45
- end
46
-
47
- #@http = Net::HTTP.new(SERVER, PORT)
48
- #@http.use_ssl = SECURE
49
- #@http.set_debug_output $stdout if @options[:debug]
50
- end
51
-
52
- end
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
53
41
 
54
42
  end
data/lib/encosion/base.rb CHANGED
@@ -44,14 +44,16 @@ 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
53
+
52
54
 
53
55
  # Performs an HTTP GET
54
- def get(server,port,path,secure,command,options)
56
+ def get(server,port,secure,path,command,options)
55
57
  http = HTTPClient.new
56
58
  url = secure ? 'https://' : 'http://'
57
59
  url += "#{server}:#{port}#{path}"
@@ -71,14 +73,15 @@ module Encosion
71
73
  return body
72
74
  end
73
75
 
76
+
74
77
  # Performs an HTTP POST
75
- def post(server,port,path,secure,command,options,instance)
78
+ def post(server,port,secure,path,command,options,instance)
76
79
  http = HTTPClient.new
77
80
  url = secure ? 'https://' : 'http://'
78
81
  url += "#{server}:#{port}#{path}"
79
-
82
+
80
83
  content = { 'json' => { 'method' => command, 'params' => options }.to_json } # package up the variables as a JSON-RPC string
81
- content.merge!({ 'file' => instance.file }) if instance.respond_to?('file') # and add a file if there is one
84
+ content.merge!({ 'file' => instance.file }) if instance.respond_to?('file') # and add a file if there is one
82
85
 
83
86
  response = http.post(url, content)
84
87
  # get the header and body for error checking
@@ -90,6 +93,7 @@ module Encosion
90
93
  return body
91
94
  end
92
95
 
96
+
93
97
  # Checks the HTTP response and handles any errors
94
98
  def error_check(header,body)
95
99
  if header.status_code == 200
@@ -1,12 +1,10 @@
1
1
  module Encosion
2
2
 
3
+ # Raised if you try to set an invalid economics value
4
+ class InvalidEconomicsValue < StandardError; end;
5
+
3
6
  class Video < Base
4
7
 
5
- SERVER = 'api.brightcove.com'
6
- PORT = 80
7
- SECURE = false
8
- READ_PATH = '/services/library'
9
- WRITE_PATH = '/services/post'
10
8
  ENUMS = { :economics => { :free => 'FREE', :ad_supported => 'AD_SUPPORTED'}}
11
9
 
12
10
  attr_accessor(:name,
@@ -37,8 +35,8 @@ module Encosion
37
35
 
38
36
  # Find a video by reference_id. Invokes Brightcove Media API command 'find_video_by_reference_id' or
39
37
  # 'find_videos_by_reference_ids' depending on whether you call one or multiple ids
40
- # Encosion::Video.find_by_reference_id('mycompany_1',:token => 'asdf')
41
- # Encosion::Video.find_by_reference_id('mycompany_1','mycompany_2','mycompany_3',:token => 'asdf')
38
+ # Encosion::Video.find_by_reference_id('mycompany_1')
39
+ # Encosion::Video.find_by_reference_id('mycompany_1','mycompany_2','mycompany_3')
42
40
 
43
41
  def find_by_reference_id(*args)
44
42
  options = extract_options(args)
@@ -49,24 +47,24 @@ module Encosion
49
47
  raise AssetNotFound, "Couldn't find #{self.class} without a reference ID"
50
48
  when 1
51
49
  options.merge!({:reference_id => ids.first})
52
- response = get(SERVER,PORT,READ_PATH,SECURE,'find_video_by_reference_id',options)
50
+ response = read('find_video_by_reference_id',options)
53
51
  return self.parse(response)
54
52
  else
55
53
  options.merge!({:reference_ids => ids.join(',')})
56
- response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_reference_ids',options)
54
+ response = read('find_videos_by_reference_ids',options)
57
55
  return response['items'].collect { |item| self.parse(item) }
58
56
  end
59
57
  end
60
58
 
61
59
  # Find a video by text search. Invokes Brightcove Media API command 'find_videos_by_text'
62
- # Encosion::Video.find_by_text('funny videos',:token => 'asdf')
60
+ # Encosion::Video.find_by_text('funny videos')
63
61
 
64
62
  def find_by_text(*args)
65
63
  options = extract_options(args)
66
64
  text = args.flatten.compact.uniq
67
65
  raise AssetNotFound, "Couldn't find #{self.class} without text" if text == ''
68
66
  options.merge!({:text => text.first})
69
- if response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_text',options)
67
+ if response = read('find_videos_by_text',options)
70
68
  return response['items'].collect { |item| self.parse(item) }
71
69
  else
72
70
  nil
@@ -75,12 +73,12 @@ module Encosion
75
73
 
76
74
 
77
75
  # Find videos related to the given video_id. Invokes Brightcove Media API command 'find_related_videos'
78
- # Encosion::Video.find_related(123456,:token => 'asdf')
76
+ # Encosion::Video.find_related(123456)
79
77
 
80
78
  def find_related(*args)
81
79
  options = extract_options(args)
82
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?
83
- if response = get(SERVER,PORT,READ_PATH,SECURE,'find_related_videos',options)
81
+ if response = read('find_related_videos',options)
84
82
  return response['items'].collect { |item| self.parse(item) }
85
83
  else
86
84
  return nil
@@ -89,7 +87,7 @@ module Encosion
89
87
 
90
88
 
91
89
  # Find a video by tag search. Invokes Brightcove Media API command 'find_videos_by_tags'
92
- # Encosion::Video.find_by_tags('bloopers','gagreel','funny',:token => 'asdf')
90
+ # Encosion::Video.find_by_tags('bloopers','gagreel','funny')
93
91
 
94
92
  def find_by_tags(*args)
95
93
  options = extract_options(args)
@@ -100,7 +98,7 @@ module Encosion
100
98
  raise AssetNotFound, "Couldn't find #{self.class} without tags"
101
99
  else
102
100
  options.merge!({:and_tags => tags.join(',')})
103
- if response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_tags',options)
101
+ if response = read('find_videos_by_tags',options)
104
102
  return response['items'].collect { |item| self.parse(item) }
105
103
  else
106
104
  return nil
@@ -124,18 +122,45 @@ module Encosion
124
122
  options.merge!({:video_id => id})
125
123
  end
126
124
 
127
- if response = Video.post(SERVER,PORT,WRITE_PATH,SECURE,'get_upload_status',options,self)
125
+ if response = write('get_upload_status',options)
128
126
  return response['result'].downcase.to_sym
129
127
  else
130
128
  return nil
131
129
  end
132
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
133
158
 
134
159
  protected
135
160
  # Find some videos by ids
136
161
  def find_one(id, options)
137
162
  options.merge!({:video_id => id})
138
- response = get(SERVER,PORT,READ_PATH,SECURE,'find_video_by_id',options)
163
+ response = read('find_video_by_id',options)
139
164
  return self.parse(response)
140
165
  end
141
166
 
@@ -143,17 +168,17 @@ module Encosion
143
168
  # Find mutliple videos by id
144
169
  def find_some(ids, options)
145
170
  options.merge!({:video_ids => ids.join(',')})
146
- response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_ids',options)
171
+ response = read('find_videos_by_ids',options)
147
172
  return response['items'].collect { |item| self.parse(item) }
148
173
  end
149
174
 
150
175
 
151
176
  # Find all videos
152
177
  def find_all(options)
153
- response = get(SERVER,PORT,READ_PATH,SECURE,'find_all_videos',options)
178
+ response = read('find_all_videos', options)
154
179
  return response['items'].collect { |item| self.parse(item) }
155
180
  end
156
-
181
+
157
182
 
158
183
  # Creates a new Video object from a Ruby hash (used to create a video from a parsed API call)
159
184
  def parse(obj)
@@ -201,7 +226,7 @@ module Encosion
201
226
  @thumbnail_url = args[:thumbnail_url]
202
227
  @reference_id = args[:reference_id]
203
228
  @length = args[:length]
204
- @economics = args[:economics]
229
+ @economics = self.economics = args[:economics]
205
230
  @plays_total = args[:plays_total]
206
231
  @plays_trailing_week = args[:plays_trailing_week]
207
232
  @file = args[:file]
@@ -216,7 +241,14 @@ module Encosion
216
241
  # check to make sure we have everything needed for a create_video call
217
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?
218
243
  options = args.merge({ 'video' => self.to_brightcove }) # take the parameters of this video and make them a valid video object for upload
219
- response = Video.post(SERVER,PORT,WRITE_PATH,SECURE,'create_video',options,self)
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)
220
252
  return response['result'] # returns the Brightcove ID of the video that was just uploaded
221
253
  end
222
254
 
@@ -242,6 +274,7 @@ module Encosion
242
274
  :plays_trailing_week => @plays_trailing_week }.to_json
243
275
  end
244
276
 
277
+
245
278
  # Outputs the video object into Brightcove's expected format
246
279
  def to_brightcove
247
280
  { 'name' => @name,
@@ -256,11 +289,13 @@ module Encosion
256
289
 
257
290
 
258
291
  # Makes sure that the economics set on this video is one of a predetermined list
259
- def encosion=(sym)
260
- if ENUMS[:economics].has_key?(sym)
261
- raise EncosionError::InvalidEconomicsValue, "A video's economics value must be one of #{ENUMS[:economics].collect { |key,value| e.key }.join(',')}"
262
- else
263
- @economics = sym
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
264
299
  end
265
300
  end
266
301
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cannikin-encosion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
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-08-03 00:00:00 -07:00
12
+ date: 2009-08-04 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency