cannikin-encosion 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.2.1
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.1.4"
5
+ s.version = "0.2.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-07-07}
9
+ s.date = %q{2009-07-22}
10
10
  s.email = %q{cannikinn@gmail.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
data/lib/encosion/base.rb CHANGED
@@ -48,95 +48,29 @@ module Encosion
48
48
  # This is an alias for find(:all)
49
49
  def all(*args)
50
50
  find(:all, *args)
51
- end
51
+ end
52
52
 
53
- protected
53
+ # Performs an HTTP GET
54
+ def get(server,port,path,secure,command,options)
55
+ http = HTTPClient.new
56
+ url = secure ? 'https://' : 'http://'
57
+ url += "#{server}:#{port}#{path}"
54
58
 
55
-
56
- # Pulls any Hash off the end of an array of arguments and returns it
57
- def extract_options(opts)
58
- opts.last.is_a?(::Hash) ? opts.pop : {}
59
- end
60
-
61
-
62
- # Find an asset from a single or array of ids
63
- def find_from_ids(ids, options)
64
- expects_array = ids.first.kind_of?(Array)
65
- return ids.first if expects_array && ids.first.empty?
66
-
67
- ids = ids.flatten.compact.uniq
68
-
69
- case ids.size
70
- when 0
71
- raise AssetNotFound, "Couldn't find #{self.class} without an ID"
72
- when 1
73
- result = find_one(ids.first, options)
74
- expects_array ? [ result ] : result
75
- else
76
- find_some(ids, options)
77
- end
78
- end
59
+ options.merge!({'command' => command })
60
+ query_string = options.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
79
61
 
62
+ response = http.get(url, query_string)
80
63
 
81
- # Performs an HTTP GET
82
- def get(server,port,path,secure,command,options)
83
- http = HTTPClient.new
84
- url = secure ? 'https://' : 'http://'
85
- url += "#{server}:#{port}#{path}"
86
-
87
- options.merge!({'command' => command })
88
- query_string = options.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
89
-
90
- response = http.get(url, query_string)
91
-
92
- body = response.body.content.strip == 'null' ? nil : JSON.parse(response.body.content.strip) # if the call returns 'null' then there were no valid results
93
- header = response.header
94
-
95
- error_check(header,body)
96
-
97
- return body
98
- end
99
-
64
+ body = response.body.content.strip == 'null' ? nil : JSON.parse(response.body.content.strip) # if the call returns 'null' then there were no valid results
65
+ header = response.header
100
66
 
101
- # Checks the HTTP response and handles any errors
102
- def error_check(header,body)
103
- if header.status_code == 200
104
- return true if body.nil?
105
- puts body['error']
106
- if body.has_key? 'error' && !body['error'].nil?
107
- message = "Brightcove responded with an error: #{body['error']} (code #{body['code']})"
108
- body['errors'].each do |error|
109
- message += "\n#{error.values.first} (code #{error.values.last})"
110
- end if body.has_key? 'errors'
111
- raise BrightcoveException, message
112
- end
113
- else
114
- # should only happen if the Brightcove API is unavailable (even BC errors return a 200)
115
- raise BrightcoveException, body + " (status code: #{header.status_code})"
116
- end
117
- end
67
+ error_check(header,body)
118
68
 
69
+ # puts "url: #{url}\nquery_string:#{query_string}"
119
70
 
120
- # Turns a hash into a query string and appends the token
121
- def queryize_args(args, type)
122
- case type
123
- when :read
124
- raise MissingToken, 'No read token found' if @read_token.nil?
125
- args.merge!({ :token => @read_token })
126
- when :write
127
- raise MissingToken, 'No write token found' if @write_token.nil?
128
- args.merge!({ :token => @write_token })
129
- end
130
- return args.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
131
- end
71
+ return body
72
+ end
132
73
 
133
- end
134
-
135
-
136
- #
137
- # Instance methods
138
- #
139
- private
140
74
  # Performs an HTTP POST
141
75
  def post(server,port,path,secure,command,options,instance)
142
76
  http = HTTPClient.new
@@ -144,7 +78,7 @@ module Encosion
144
78
  url += "#{server}:#{port}#{path}"
145
79
 
146
80
  content = { 'json' => { 'method' => command, 'params' => options }.to_json } # package up the variables as a JSON-RPC string
147
- content.merge!({ 'file' => instance.file }) if instance.file # and add a file if there is one
81
+ content.merge!({ 'file' => instance.file }) if instance.respond_to?('file') # and add a file if there is one
148
82
 
149
83
  response = http.post(url, content)
150
84
  # get the header and body for error checking
@@ -156,8 +90,6 @@ module Encosion
156
90
  return body
157
91
  end
158
92
 
159
-
160
- # TODO: shouldn't need to duplicate this method here, some way to call the class method
161
93
  # Checks the HTTP response and handles any errors
162
94
  def error_check(header,body)
163
95
  if header.status_code == 200
@@ -175,6 +107,50 @@ module Encosion
175
107
  raise BrightcoveException, body + " (status code: #{header.status_code})"
176
108
  end
177
109
  end
110
+
111
+
112
+ protected
113
+
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
+
120
+ # Find an asset from a single or array of ids
121
+ def find_from_ids(ids, options)
122
+ expects_array = ids.first.kind_of?(Array)
123
+ return ids.first if expects_array && ids.first.empty?
124
+
125
+ ids = ids.flatten.compact.uniq
126
+
127
+ case ids.size
128
+ when 0
129
+ raise AssetNotFound, "Couldn't find #{self.class} without an ID"
130
+ when 1
131
+ result = find_one(ids.first, options)
132
+ expects_array ? [ result ] : result
133
+ else
134
+ find_some(ids, options)
135
+ end
136
+ end
137
+
138
+
139
+ # Turns a hash into a query string and appends the token
140
+ def queryize_args(args, type)
141
+ case type
142
+ when :read
143
+ raise MissingToken, 'No read token found' if @read_token.nil?
144
+ args.merge!({ :token => @read_token })
145
+ when :write
146
+ raise MissingToken, 'No write token found' if @write_token.nil?
147
+ args.merge!({ :token => @write_token })
148
+ end
149
+ return args.collect { |key,value| "#{key.to_s}=#{value.to_s}" }.join('&')
150
+ end
151
+
152
+ end
153
+
178
154
 
179
155
  end
180
156
 
@@ -37,7 +37,6 @@ module Encosion
37
37
 
38
38
  # Find a video by reference_id. Invokes Brightcove Media API command 'find_video_by_reference_id' or
39
39
  # 'find_videos_by_reference_ids' depending on whether you call one or multiple ids
40
- #
41
40
  # Encosion::Video.find_by_reference_id('mycompany_1',:token => 'asdf')
42
41
  # Encosion::Video.find_by_reference_id('mycompany_1','mycompany_2','mycompany_3',:token => 'asdf')
43
42
 
@@ -55,12 +54,11 @@ module Encosion
55
54
  else
56
55
  options.merge!({:reference_ids => ids.join(',')})
57
56
  response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_reference_ids',options)
58
- return self.parse(response)
57
+ return response['items'].collect { |item| self.parse(item) }
59
58
  end
60
59
  end
61
60
 
62
61
  # Find a video by text search. Invokes Brightcove Media API command 'find_videos_by_text'
63
- #
64
62
  # Encosion::Video.find_by_text('funny videos',:token => 'asdf')
65
63
 
66
64
  def find_by_text(*args)
@@ -68,25 +66,29 @@ module Encosion
68
66
  text = args.flatten.compact.uniq
69
67
  raise AssetNotFound, "Couldn't find #{self.class} without text" if text == ''
70
68
  options.merge!({:text => text.first})
71
- response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_text',options)
72
- return response['items'].collect { |item| self.parse(item) }
69
+ if response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_text',options)
70
+ return response['items'].collect { |item| self.parse(item) }
71
+ else
72
+ nil
73
+ end
73
74
  end
74
75
 
75
76
 
76
77
  # Find videos related to the given video_id. Invokes Brightcove Media API command 'find_related_videos'
77
- #
78
78
  # Encosion::Video.find_related(123456,:token => 'asdf')
79
79
 
80
80
  def find_related(*args)
81
81
  options = extract_options(args)
82
82
  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
- response = get(SERVER,PORT,READ_PATH,SECURE,'find_related_videos',options)
84
- return response['items'].collect { |item| self.parse(item) }
83
+ if response = get(SERVER,PORT,READ_PATH,SECURE,'find_related_videos',options)
84
+ return response['items'].collect { |item| self.parse(item) }
85
+ else
86
+ return nil
87
+ end
85
88
  end
86
89
 
87
90
 
88
91
  # Find a video by tag search. Invokes Brightcove Media API command 'find_videos_by_tags'
89
- #
90
92
  # Encosion::Video.find_by_tags('bloopers','gagreel','funny',:token => 'asdf')
91
93
 
92
94
  def find_by_tags(*args)
@@ -98,10 +100,36 @@ module Encosion
98
100
  raise AssetNotFound, "Couldn't find #{self.class} without tags"
99
101
  else
100
102
  options.merge!({:and_tags => tags.join(',')})
101
- response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_tags',options)
102
- return response['items'].collect { |item| self.parse(item) }
103
+ if response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_tags',options)
104
+ return response['items'].collect { |item| self.parse(item) }
105
+ else
106
+ return nil
107
+ end
103
108
  end
104
109
  end
110
+
111
+
112
+ # Returns the status of a video upload (returns one of :uploading | :processing | :complete | :error )
113
+ # Takes either Brightcove's video_id or your own reference_id. If you pass an integer it's assumed to be
114
+ # a video_id, if you pass a string it's assumed to be a reference_id.
115
+ # Encosion::Video.status(12345)
116
+
117
+ def status(*args)
118
+ options = extract_options(args)
119
+ id = args.flatten.compact.uniq.first
120
+
121
+ if id.class == String
122
+ options.merge!({:reference_id => id})
123
+ else
124
+ options.merge!({:video_id => id})
125
+ end
126
+
127
+ if response = Video.post(SERVER,PORT,WRITE_PATH,SECURE,'get_upload_status',options,self)
128
+ return response['result'].downcase.to_sym
129
+ else
130
+ return nil
131
+ end
132
+ end
105
133
 
106
134
  protected
107
135
  # Find some videos by ids
@@ -116,7 +144,6 @@ module Encosion
116
144
  def find_some(ids, options)
117
145
  options.merge!({:video_ids => ids.join(',')})
118
146
  response = get(SERVER,PORT,READ_PATH,SECURE,'find_videos_by_ids',options)
119
- puts response
120
147
  return response['items'].collect { |item| self.parse(item) }
121
148
  end
122
149
 
@@ -130,24 +157,29 @@ module Encosion
130
157
 
131
158
  # Creates a new Video object from a Ruby hash (used to create a video from a parsed API call)
132
159
  def parse(obj)
133
- args = {:id => obj['id'].to_i,
134
- :name => obj['name'],
135
- :short_description => obj['shortDescription'],
136
- :long_description => obj['longDescription'],
137
- :creation_date => Time.at(obj['creationDate'].to_i/1000),
138
- :published_date => Time.at(obj['publishedDate'].to_i/1000),
139
- :last_modified_date => Time.at(obj['lastModifiedDate'].to_i/1000),
140
- :link_url => obj['linkURL'],
141
- :link_text => obj['linkText'],
142
- :tags => obj['tags'],
143
- :video_still_url => obj['videoStillURL'],
144
- :thumbnail_url => obj['thumbnailURL'],
145
- :reference_id => obj['referenceID'],
146
- :length => obj['length'].to_i,
147
- :economics => obj['economics'] ? ENUMS[:economics].find { |key,value| value == obj['economics'] }.first : nil,
148
- :plays_total => obj['playsTotal'].to_i,
149
- :plays_trailing_week => obj['playsTrailingWeek'].to_i } unless obj.nil?
150
- return self.new(args)
160
+ puts obj.inspect
161
+ if obj
162
+ args = {:id => obj['id'].to_i,
163
+ :name => obj['name'],
164
+ :short_description => obj['shortDescription'],
165
+ :long_description => obj['longDescription'],
166
+ :creation_date => Time.at(obj['creationDate'].to_i/1000),
167
+ :published_date => Time.at(obj['publishedDate'].to_i/1000),
168
+ :last_modified_date => Time.at(obj['lastModifiedDate'].to_i/1000),
169
+ :link_url => obj['linkURL'],
170
+ :link_text => obj['linkText'],
171
+ :tags => obj['tags'],
172
+ :video_still_url => obj['videoStillURL'],
173
+ :thumbnail_url => obj['thumbnailURL'],
174
+ :reference_id => obj['referenceID'],
175
+ :length => obj['length'].to_i,
176
+ :economics => obj['economics'] ? ENUMS[:economics].find { |key,value| value == obj['economics'] }.first : nil,
177
+ :plays_total => obj['playsTotal'].to_i,
178
+ :plays_trailing_week => obj['playsTrailingWeek'].to_i } unless obj.nil?
179
+ return self.new(args)
180
+ else
181
+ return nil
182
+ end
151
183
  end
152
184
 
153
185
  end
@@ -185,7 +217,7 @@ module Encosion
185
217
  # check to make sure we have everything needed for a create_video call
186
218
  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?
187
219
  options = args.merge({ 'video' => self.to_brightcove }) # take the parameters of this video and make them a valid video object for upload
188
- response = post(SERVER,PORT,WRITE_PATH,SECURE,'create_video',options,self)
220
+ response = Video.post(SERVER,PORT,WRITE_PATH,SECURE,'create_video',options,self)
189
221
  return response['result'] # returns the Brightcove ID of the video that was just uploaded
190
222
  end
191
223
 
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.1.4
4
+ version: 0.2.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-07-07 00:00:00 -07:00
12
+ date: 2009-07-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency