ooyala-v2-api 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94f0be80c1ca47dcf0a5785c8205e82b571638a6
4
+ data.tar.gz: faec16a436e61baaffd4079e43bab48b03abeb75
5
+ SHA512:
6
+ metadata.gz: 95736c69ccc411c25916db5bed2bc5e6c1626a7c03821c59946763ee42d0caf5b13e686d5256338d56fd4d2f50d3ec5aa211f59f0afa638ba1e5376e27a4682d
7
+ data.tar.gz: 18290b84e9f84685178357087780bf4592194e90e8a2c259af73d249d52872f7abe29de6bc0e04e3b7c485503fc7f97beac94e5da5f09fd611c171216c6a1511
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+
9
+ gem "rdoc"
10
+ gem "rake"
11
+ gem "rest-client", "~> 1.6.6"
12
+ gem "json"
13
+
14
+ group :development do
15
+ gem "shoulda", ">= 0"
16
+ gem "rdoc"
17
+ gem "bundler"
18
+ gem "jeweler"
19
+ gem "simplecov", ">= 0"
20
+ gem "rr"
21
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.13)
5
+ i18n (= 0.6.1)
6
+ multi_json (~> 1.0)
7
+ git (1.2.5)
8
+ i18n (0.6.1)
9
+ jeweler (1.8.4)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.8.0)
15
+ mime-types (1.23)
16
+ multi_json (1.7.3)
17
+ rake (10.0.4)
18
+ rdoc (4.0.1)
19
+ json (~> 1.4)
20
+ rest-client (1.6.7)
21
+ mime-types (>= 1.16)
22
+ rr (1.1.0)
23
+ shoulda (3.5.0)
24
+ shoulda-context (~> 1.0, >= 1.0.1)
25
+ shoulda-matchers (>= 1.4.1, < 3.0)
26
+ shoulda-context (1.1.2)
27
+ shoulda-matchers (2.1.0)
28
+ activesupport (>= 3.0.0)
29
+ simplecov (0.7.1)
30
+ multi_json (~> 1.0)
31
+ simplecov-html (~> 0.7.1)
32
+ simplecov-html (0.7.1)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler
39
+ jeweler
40
+ json
41
+ rake
42
+ rdoc
43
+ rest-client (~> 1.6.6)
44
+ rr
45
+ shoulda
46
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,14 @@
1
+ Copyright 2011 © Ooyala, Inc. All rights reserved.
2
+
3
+ Ooyala, Inc. ("Ooyala") hereby grants permission, free of charge, to any person or entity obtaining a copy of the software code provided in source code format via this webpage and direct links contained within this webpage and any associated documentation (collectively, the "Software"), to use, copy, modify, merge, and/or publish the Software and, subject to pass-through of all terms and conditions hereof, permission to transfer, distribute and sublicense the Software; all of the foregoing subject to the following terms and conditions:
4
+
5
+ 1. The above copyright notice and this permission notice shall be included in all copies or portions of the Software.
6
+
7
+ 2. For purposes of clarity, the Software does not include any APIs, but instead consists of code that may be used in conjunction with APIs that may be provided by Ooyala pursuant to a separate written agreement subject to fees.
8
+
9
+ 3. Ooyala may in its sole discretion maintain and/or update the Software. However, the Software is provided without any promise or obligation of support, maintenance or update.
10
+
11
+ 4. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, RELATING TO, ARISING FROM, IN CONNECTION WITH, OR INCIDENTAL TO THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
+
13
+ 5. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, (i) IN NO EVENT SHALL OOYALA BE LIABLE FOR ANY CONSEQUENTIAL, INCIDENTAL, INDIRECT, SPECIAL, PUNITIVE, OR OTHER DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) RELATING TO, ARISING FROM, IN CONNECTION WITH, OR INCIDENTAL TO THE SOFTWARE OR THE USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF OOYALA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, AND (ii) OOYALA’S TOTAL AGGREGATE LIABILITY RELATING TO, ARISING FROM, IN CONNECTION WITH, OR INCIDENTAL TO THE SOFTWARE SHALL BE LIMITED TO THE ACTUAL DIRECT DAMAGES INCURRED UP TO MAXIMUM AMOUNT OF FIFTY DOLLARS ($50).
14
+
data/README.rdoc ADDED
@@ -0,0 +1,24 @@
1
+ = ooyala-v2-api
2
+
3
+ The Ruby SDK is a client class for our V2 API. The approach is very simple. It
4
+ allows you to do GET, POST, PUT, PATCH and DELETE requests to our API by simply specifying the path to the API you want to hit and depending of the call, a Hash with parameters and Hash containing the body of the request.
5
+
6
+ By specifying a Hash object to represent the JSON data you want to send, you can make calls very fast and easily. First you need to create an Ooyala::API object by passing your V2 API keys like this:
7
+
8
+ api = Ooyala::API.new("<api key>", "<secret key>"})
9
+ Now lets get all the assets under the "Funny dogs" label:
10
+
11
+ parameters = {:where => "labels INCLUDES 'Funny dogs'"}
12
+
13
+ assets = api.get("assets", parameters)["items"]
14
+ Now that we have our results on the assets ArrayList, lets print them out to the console.
15
+
16
+ puts "Printing assets in the 'Funny dogs' label..."
17
+ assets.each do |asset|
18
+ puts "#{asset["embed_code"]} - #{asset["name"]}"
19
+ end
20
+ It's that easy to work with this SDK!
21
+
22
+ == Copyright
23
+ See LICENSE.txt for further details.
24
+
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "ooyala-v2-api"
18
+ gem.homepage = "https://github.com/ooyala/api-sdks"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Ooyala's API SDK}
21
+ gem.description = %Q{Contains the necessary mehtods to communicate with the Ooyala's API.}
22
+ gem.email = "jose@ooyala.com"
23
+ gem.authors = ["Ooyala"]
24
+ # dependencies defined in Gemfile
25
+ # dependencies defined in Gemfile
26
+ gem.add_dependency('json')
27
+ gem.add_dependency('rest-client', "~> 1.6.6")
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ task :default => :test
39
+
40
+ require 'rdoc/task'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "ooyala-sdk #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,305 @@
1
+ # Copyright 2011 © Ooyala, Inc. All rights reserved.
2
+ #
3
+ # Ooyala, Inc. (“Ooyala”) hereby grants permission, free of charge, to any
4
+ # person or entity obtaining a copy of the software code provided in source
5
+ # code format via this webpage and direct links contained within this webpage
6
+ # and any associated documentation (collectively, the "Software"), to use,
7
+ # copy, modify, merge, and/or publish the Software and, subject to
8
+ # pass-through of all terms and conditions hereof, permission to transfer,
9
+ # distribute and sublicense the Software; all of the foregoing subject to the
10
+ # following terms and conditions:
11
+ #
12
+ # 1. The above copyright notice and this permission notice shall be included
13
+ # in all copies or portions of the Software.
14
+ #
15
+ # 2. For purposes of clarity, the Software does not include any APIs, but
16
+ # instead consists of code that may be used in conjunction with APIs that
17
+ # may be provided by Ooyala pursuant to a separate written agreement
18
+ # subject to fees.
19
+ #
20
+ # 3. Ooyala may in its sole discretion maintain and/or update the Software.
21
+ # However, the Software is provided without any promise or obligation of
22
+ # support, maintenance or update.
23
+ #
24
+ # 4. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
27
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
+ # OF CONTRACT, TORT OR OTHERWISE, RELATING TO, ARISING FROM, IN
30
+ # CONNECTION WITH, OR INCIDENTAL TO THE SOFTWARE OR THE USE OR OTHER
31
+ # DEALINGS IN THE SOFTWARE.
32
+ #
33
+ # 5. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, (i) IN NO EVENT
34
+ # SHALL OOYALA BE LIABLE FOR ANY CONSEQUENTIAL, INCIDENTAL, INDIRECT,
35
+ # SPECIAL, PUNITIVE, OR OTHER DAMAGES WHATSOEVER (INCLUDING, WITHOUT
36
+ # LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS
37
+ # INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS)
38
+ # RELATING TO, ARISING FROM, IN CONNECTION WITH, OR INCIDENTAL TO THE
39
+ # SOFTWARE OR THE USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF OOYALA
40
+ # HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, AND (ii) OOYALA’S
41
+ # TOTAL AGGREGATE LIABILITY RELATING TO, ARISING FROM, IN CONNECTION
42
+ # WITH, OR INCIDENTAL TO THE SOFTWARE SHALL BE LIMITED TO THE ACTUAL
43
+ # DIRECT DAMAGES INCURRED UP TO MAXIMUM AMOUNT OF FIFTY DOLLARS ($50).
44
+ #
45
+ require 'rubygems'
46
+ require 'rest-client'
47
+ require 'base64'
48
+ require 'digest/sha2'
49
+ require 'cgi'
50
+ require 'json'
51
+
52
+ # Contains the necessary mehtods to communicate with the Ooyala's API.
53
+ module Ooyala
54
+ class API
55
+ SUPPORTED_HTTP_METHODS = %w{GET POST PUT PATCH DELETE}
56
+ DEFAULT_BASE_URL = 'https://api.ooyala.com'
57
+ DEFAULT_CACHE_BASE_URL = 'https://cdn.api.ooyala.com'
58
+ DEFAULT_EXPIRATION_WINDOW = 15
59
+ ROUND_UP_TIME = 300
60
+
61
+ # Sets the String base URL (default: "https://api.ooyala.com").
62
+ attr_writer :base_url
63
+
64
+ # Sets the String cache base URL (default: "https://cdn.api.ooyala.com").
65
+ attr_writer :cache_base_url
66
+
67
+ # Sets the Number with the expiration window. This is added to the current
68
+ # time. It should be in seconds, and represents the time that a request is
69
+ # valid.
70
+ attr_writer :expiration_window
71
+
72
+ # Gets/Sets the String secret key. This can be found in Backlot's developers
73
+ # tab (http://ooyala.com/backlot/web).
74
+ attr_accessor :secret_key
75
+
76
+ # Gets/Sets the API key. This can be found in Backlot's developers tab
77
+ # (http://ooyala.com/backlot/web).
78
+ attr_accessor :api_key
79
+
80
+ # Gets the base URL. If no base URL is specified it will return the default
81
+ # one.
82
+ #
83
+ # Returns the String base URL.
84
+ def base_url
85
+ @base_url || DEFAULT_BASE_URL
86
+ end
87
+
88
+ # Gets the cache base URL. If no base URL is specified it will return the
89
+ # default one.
90
+ #
91
+ # Returns the String base URL.
92
+ def cache_base_url
93
+ @cache_base_url || DEFAULT_CACHE_BASE_URL
94
+ end
95
+
96
+ # Gets the default expiration window. If not specified, the default one.
97
+ #
98
+ # Returns the Number expiration window.
99
+ def expiration_window
100
+ @expiration_window || DEFAULT_EXPIRATION_WINDOW
101
+ end
102
+
103
+ # Initialize an API. Takes the secret and api keys. If these are not
104
+ # specified the class properties will be used to make requests,
105
+ # generate signatrues, etc.
106
+ #
107
+ # secret_key - The String secret key.
108
+ # api_key - The String API key.
109
+ # options - The Hash options to specify extra settings for this API
110
+ # instance (default: {}):
111
+ # :cache_base_url - The String cache base URL to override
112
+ # the default one.
113
+ # :base_url - The String base URL to override the
114
+ # default one.
115
+ # :expiration_window - The Number in seconds to override the
116
+ # default value (optional).
117
+ def initialize(api_key, secret_key, options = {})
118
+ self.secret_key = secret_key
119
+ self.api_key = api_key
120
+ self.base_url = options[:base_url] || options['base_url']
121
+ self.cache_base_url = options[:cache_base_url] ||
122
+ options['cache_base_url']
123
+ self.expiration_window = options[:expiration_window] ||
124
+ options['expiration_window']
125
+ end
126
+
127
+ # Makes a GET request.
128
+ #
129
+ # path - The String relative path for the request.
130
+ # query_params - A Hash with GET parameters for the request (default: {}).
131
+ #
132
+ # Returns a the JSON parsed response. Could be either a Hash or Array.
133
+ def get(path, query_params = {})
134
+ send_request('GET', path, query_params)
135
+ end
136
+
137
+ # Makes a POST request.
138
+ #
139
+ # path - The String relatice path for the request.
140
+ # body - An optional Object. If a Hash or Array, its contents will
141
+ # be JSON enconded. If a File, it will be read, or anything
142
+ # that responds to to_s (default: nil).
143
+ # query_params - A Hash with GET paramaters fo the request (default: {}).
144
+ #
145
+ # Examples
146
+ #
147
+ # api.post('assets', File.open('my_video.avi'))
148
+ # api.post('labels', {:name => "Test"})
149
+ #
150
+ # Returns the body of the response.
151
+ def post(path, body = nil, query_params = {})
152
+ if Hash === body || Array === body
153
+ body = body.to_json
154
+ elsif File === body
155
+ body = body.read
156
+ end
157
+ send_request('POST', path, query_params, body.to_s)
158
+ end
159
+
160
+ # Makes a PUT request.
161
+ #
162
+ # path - The String relative path for the request.
163
+ # body - An optional Object. If a Hash or Array, its contents will
164
+ # be JSON enconded. If a File, it will be read, or anything
165
+ # that responds to to_s (default: nil).
166
+ # query_params - A Hash with GET paramaters fo the request (default: {}).
167
+ #
168
+ # Returns the body of the response.
169
+ def put(path, body = nil, query_params = {})
170
+ if Hash === body || Array === body
171
+ body = body.to_json
172
+ elsif File === body
173
+ body = body.read
174
+ end
175
+ send_request('PUT', path, query_params, body.to_s)
176
+ end
177
+
178
+ # Makes a PATCH request.
179
+ #
180
+ # path - The String relative path for the request.
181
+ # body - The Hash with the properties to update (default: {}).
182
+ # query_params - A Hash with GET paramaters fo the request (default: {}).
183
+ #
184
+ # Returns the body of the response.
185
+ def patch(path, body = {}, query_params = {})
186
+ send_request('PATCH', path, query_params, body.to_json)
187
+ end
188
+
189
+ # Makes a DELETE request.
190
+ #
191
+ # path - The String relative path for the request.
192
+ # query_params - A Hash with GET paramaters fo the request (default: {}).
193
+ #
194
+ # Returns the body of the response.
195
+ def delete(path, query_params = {})
196
+ send_request('DELETE', path, query_params)
197
+ end
198
+
199
+ # Generates the signature for a request, using a body in the request.
200
+ # If the method is a GET, then it does not need the body. On the other hand
201
+ # if it is a POST, PUT or PATCH, the body is a string with the parameters
202
+ # that are going to be modified, or assigned to the resource. This should be
203
+ # later added to the query parameters, as the signature parameter of the
204
+ # desired requested URI.
205
+ #
206
+ # http_method - The String HTTP method. It could be either GET, DELETE,
207
+ # POST, PUT or PATCH.
208
+ # request_path - The String with the path of the resourcce of the request.
209
+ # query_params - The Hash that contains GET query paramaters (default: {}).
210
+ # request_body - An String that contains the POST request body
211
+ # (default: "").
212
+ #
213
+ # Returns the signature that should be added as query parameter to the URI
214
+ # of the request.
215
+ def generate_signature(http_method, request_path, query_params = {},
216
+ request_body = '')
217
+ string_to_sign = secret_key
218
+ string_to_sign += http_method.to_s.upcase + request_path
219
+ sorted_params = query_params.sort { |a, b| a[0].to_s <=> b[0].to_s }
220
+ string_to_sign += sorted_params.map { |param| param.join('=') }.join
221
+ string_to_sign += request_body.to_s
222
+
223
+ signature = Base64::encode64(Digest::SHA256.digest(string_to_sign))[0..42]
224
+ CGI.escape(signature)
225
+ end
226
+
227
+ # Creates a request to a given path using the indicated HTTP Method.
228
+ #
229
+ # http_method - The String HTTP method that contains either GET, DELETE,
230
+ # POST, PUT or PATCH.
231
+ # query_params - The Hash that contains GET query parameters (default: {}).
232
+ # request_path - The String with the relative path of the request.
233
+ # request_body - An String that contains the POST request body
234
+ # (default: "").
235
+ #
236
+ # Returns the Array or Hash with the JSON parsed response if it was success.
237
+ # Raises Ooyala::MethodNotSupportedException if the HTTP method is not
238
+ # supported.
239
+ # Raises Ooyala::RequestErrorException if there was an error sending the
240
+ # request.
241
+ def send_request(http_method, request_path, query_params = {},
242
+ request_body = nil)
243
+ http_method = http_method.to_s.upcase
244
+ unless SUPPORTED_HTTP_METHODS.include? http_method
245
+ raise Ooyala::MethodNotSupportedException
246
+ end
247
+
248
+ request_path = '/v2/' + request_path unless request_path[0..3] == '/v2/'
249
+ original_params = Hash[*query_params.map { |k,v| [k.to_sym, v] }.flatten]
250
+ query_params = sanitize_and_add_needed_parameters(query_params)
251
+ query_params[:signature] ||= generate_signature(http_method,
252
+ request_path,
253
+ query_params.merge(original_params),
254
+ request_body)
255
+
256
+ request = RestClient::Request.new\
257
+ :method => http_method.to_s.downcase.to_sym,
258
+ :url => build_url(http_method, request_path, query_params),
259
+ :payload => request_body
260
+
261
+ response = request.execute
262
+
263
+ return [] if response.body.empty?
264
+ JSON.parse(response.body)
265
+ rescue RestClient::Exception => ex
266
+ raise Ooyala::RequestErrorException.new(ex.inspect)
267
+ end
268
+
269
+ # Builds the URL for a request. Adds the query parameters, the signature
270
+ # and expiration.
271
+ #
272
+ # http_method - The String request HTTP method. It could be 'GET',
273
+ # 'POST', 'PUT', 'PATCH' or 'DELETE'.
274
+ # request_path - The String absolute path for this URL.
275
+ # query_params - The Hash with the parameters to be added to the URL
276
+ # (default: {}).
277
+ #
278
+ # Returns a String with the built URL.
279
+ def build_url(http_method, request_path, query_params = {})
280
+ url = http_method == 'GET' ? cache_base_url : base_url
281
+ url += request_path + '?'
282
+ url + query_params.sort { |a, b| a[0].to_s <=> b[0].to_s }.map do |param|
283
+ param.join("=")
284
+ end.join("&")
285
+ end
286
+
287
+ private
288
+ def sanitize_and_add_needed_parameters(params)
289
+ params = Hash[*params.map { |k,v| [k.to_sym, CGI.escape(v.to_s)] }.flatten]
290
+
291
+ params[:expires] ||= begin
292
+ expiration = Time.now.to_i + expiration_window
293
+ expiration + ROUND_UP_TIME - (expiration%ROUND_UP_TIME)
294
+ end
295
+ params[:api_key] ||= api_key
296
+ params
297
+ end
298
+ end
299
+
300
+ class RequestErrorException < StandardError
301
+ end
302
+
303
+ class MethodNotSupportedException < StandardError
304
+ end
305
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'simplecov'
4
+
5
+ SimpleCov.start do
6
+ add_filter 'vendor'
7
+ end if ENV["COVERAGE"]
8
+
9
+ begin
10
+ Bundler.setup(:default, :development)
11
+ rescue Bundler::BundlerError => e
12
+ $stderr.puts e.message
13
+ $stderr.puts "Run `bundle install` to install missing gems"
14
+ exit e.status_code
15
+ end
16
+ require 'test/unit'
17
+ require 'shoulda'
18
+ require 'rr'
19
+
20
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
21
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
22
+ require 'ooyala-v2-api'
23
+
24
+ class Test::Unit::TestCase
25
+ end
@@ -0,0 +1,239 @@
1
+ require 'helper'
2
+
3
+ require File.dirname(__FILE__) + '/helper'
4
+
5
+ class TestOoyalaV2Api < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @api_key = '7ab06'
9
+ @secret_key = '329b5b204d0f11e0a2d060334bfffe90ab18xqh5'
10
+ @ooyala_api = Ooyala::API.new(@api_key, @secret_key)
11
+ @request = {:method => "GET", :url => "http://example.com"}
12
+ end
13
+
14
+ def test_initialization
15
+ assert_equal @api_key, @ooyala_api.api_key
16
+ assert_equal @secret_key, @ooyala_api.secret_key
17
+ assert_equal Ooyala::API::DEFAULT_BASE_URL, @ooyala_api.base_url
18
+ assert_equal Ooyala::API::DEFAULT_EXPIRATION_WINDOW,
19
+ @ooyala_api.expiration_window
20
+ end
21
+
22
+ def test_initialization_with_options
23
+ ooyala_api = Ooyala::API.new(@api_key, @secret_key,
24
+ :base_url => 'http://api.ooyala.com',
25
+ :cache_base_url => 'http://api.ooyala.com',
26
+ :expiration_window => 1)
27
+ assert_equal 1, ooyala_api.expiration_window
28
+ assert_equal 'http://api.ooyala.com', ooyala_api.base_url
29
+ assert_equal 'http://api.ooyala.com', ooyala_api.cache_base_url
30
+ end
31
+
32
+ def test_generate_signature
33
+ assert_equal 'OugvH8gjMEqhq8nyoJQeBtSI57nMbIOp%2B7KGaxx9v8I',
34
+ @ooyala_api.generate_signature('get', '/v2/players/HbxJKM')
35
+ end
36
+
37
+ def test_generate_signature_with_query_params
38
+ params = {:api_key => @api_key, 'expires' => '1299991855'}
39
+ assert_equal 'p9DG%2F%2BummS0YcTNOYHtykdjw5N2n5s81OigJfdgHPTA',
40
+ @ooyala_api.generate_signature('GET', '/v2/players/HbxJKM', params)
41
+ params = {'api_key' => @api_key, :expires => '1299991855'}
42
+ assert_equal 'p9DG%2F%2BummS0YcTNOYHtykdjw5N2n5s81OigJfdgHPTA',
43
+ @ooyala_api.generate_signature('GET', '/v2/players/HbxJKM', params)
44
+ end
45
+
46
+ def test_generate_signature_with_payload
47
+ params = {'api_key' => @api_key, :expires => '1299991855'}
48
+ body = 'test'
49
+ path = '/v2/players/HbxJKM'
50
+ assert_equal @ooyala_api.generate_signature('post', path, params, body),
51
+ 'fJrWCcIqeRBZUqa61OV%2B6XOWfpkab6RdW5hJZmZh1CI'
52
+ end
53
+
54
+ def test_build_url
55
+ url = @ooyala_api.build_url 'GET', '/v2/players/HbxJKM'
56
+ assert url.include?('https://cdn.api.ooyala.com/')
57
+ assert url.include?('/v2/players/HbxJKM')
58
+ url = @ooyala_api.build_url 'POST', '/v2/players/HbxJKM',
59
+ {:test => true, 'a' => 'b'}
60
+ assert url.include?('https://api.ooyala.com/')
61
+ assert url.include?('test=true')
62
+ assert url.include?('a=b')
63
+ end
64
+
65
+ def test_build_url_overriding_base_url
66
+ @ooyala_api.base_url = 'http://example.com'
67
+ url = @ooyala_api.build_url 'POST', '/v2/players/HbxJKM'
68
+ assert url.include?('http://example.com')
69
+ @ooyala_api.cache_base_url = 'http://example.com'
70
+ url = @ooyala_api.build_url 'GET', '/v2/players/HbxJKM'
71
+ assert url.include?('http://example.com')
72
+ end
73
+
74
+ def test_send_request_with_method_not_supported
75
+ assert_raise(Ooyala::MethodNotSupportedException) do
76
+ @ooyala_api.send_request(:invalid, '/')
77
+ end
78
+ end
79
+
80
+ def test_send_request_with_a_request_error
81
+ req = RestClient::Request.new(@request)
82
+ mock(RestClient::Request).new.with_any_args { req }
83
+ mock(req).execute { raise RestClient::Exception.new }
84
+ assert_raise(Ooyala::RequestErrorException) do
85
+ @ooyala_api.send_request 'get', '/v2/players/HbxJKM'
86
+ end
87
+ end
88
+
89
+ def test_send_request
90
+ req = RestClient::Request.new(@request)
91
+ res = RestClient::Response
92
+ mock(RestClient::Request).new.with_any_args { req }
93
+ mock(req).execute { res }
94
+ mock(res).body.twice { '{"test":true}' }
95
+ response = @ooyala_api.send_request 'get', '/v2/players/HbxJKM'
96
+ assert response['test']
97
+ end
98
+
99
+ def test_send_request_should_escape_query_parameters
100
+ req = RestClient::Request.new(@request)
101
+ res = RestClient::Response
102
+ mock(RestClient::Request).new.with_any_args do |*args|
103
+ assert args.first[:url].include?(
104
+ "https://cdn.api.ooyala.com/v2/players/HbxJKM")
105
+ assert args.first[:url].include?("test=%27tr+ue%27")
106
+ assert args.first[:url].include?("other=1")
107
+ req
108
+ end
109
+ mock(req).execute { res }
110
+ mock(res).body.twice { '{"test":true}' }
111
+ @ooyala_api.send_request 'get', '/v2/players/HbxJKM',
112
+ {:test => "'tr ue'", :other => 1}
113
+ end
114
+
115
+ def test_send_request_should_complete_the_route
116
+ req = RestClient::Request.new(@request)
117
+ res = RestClient::Response
118
+ mock(RestClient::Request).new.with_any_args do |*args|
119
+ assert args.first[:url].include?(
120
+ "https://cdn.api.ooyala.com/v2/players/HbxJKM")
121
+ req
122
+ end
123
+ mock(req).execute { res }
124
+ mock(res).body.twice { '{"test":true}' }
125
+ @ooyala_api.send_request :get, 'players/HbxJKM'
126
+ end
127
+
128
+ def test_send_request_should_add_needed_params
129
+ req = RestClient::Request.new(@request)
130
+ res = RestClient::Response
131
+ mock(RestClient::Request).new.with_any_args do |*args|
132
+ assert args.first[:url].include?("api_key=#{@api_key}")
133
+ assert args.first[:url].include?("signature=")
134
+ req
135
+ end
136
+ mock(req).execute { res }
137
+ mock(res).body.twice { '{"test":true}' }
138
+ @ooyala_api.send_request 'GET', 'players/HbxJKM'
139
+ end
140
+
141
+ def test_send_request_with_payload
142
+ req = RestClient::Request.new(@request)
143
+ res = RestClient::Response
144
+ mock(RestClient::Request).new.with_any_args do |*args|
145
+ assert_equal "payload", args.first[:payload]
146
+ req
147
+ end
148
+ mock(req).execute { res }
149
+ mock(res).body.twice { '{"test":true}' }
150
+ @ooyala_api.send_request :post, 'players/HbxJKM', {}, 'payload'
151
+ end
152
+
153
+ def test_get
154
+ mock(@ooyala_api).send_request('GET', 'test', {}) { true }
155
+ assert @ooyala_api.get('test')
156
+ end
157
+
158
+ def test_get_with_params
159
+ mock(@ooyala_api).send_request('GET', 'test', {:test => true, 'a' => 1}) do
160
+ true
161
+ end
162
+ assert @ooyala_api.get('test', :test => true, 'a' => 1)
163
+ end
164
+
165
+ def test_post
166
+ mock(@ooyala_api).send_request('POST', 'test', {}, '{"test":true}') { true }
167
+ assert @ooyala_api.post('test', {:test => true})
168
+ mock(@ooyala_api).send_request('POST', 'test', {}, '[1,2,3]') { true }
169
+ assert @ooyala_api.post('test', [1,2,3])
170
+ end
171
+
172
+ def test_post_with_params
173
+ mock(@ooyala_api).send_request('POST', 'test', {:test => true},
174
+ '{"test":true}') { true }
175
+ assert @ooyala_api.post('test', {:test => true}, :test => true)
176
+ end
177
+
178
+ def test_post_with_a_file
179
+ file = File.new(__FILE__)
180
+ mock(file).read { "payload" }
181
+ mock(@ooyala_api).send_request('POST', 'test', {}, 'payload') { true }
182
+ assert @ooyala_api.post('test', file)
183
+ end
184
+
185
+ def test_post_with_a_string
186
+ mock(@ooyala_api).send_request('POST', 'test', {}, 'payload') { true }
187
+ assert @ooyala_api.post('test', 'payload')
188
+ end
189
+
190
+ def test_put
191
+ mock(@ooyala_api).send_request('PUT', 'test', {}, '{"test":true}') { true }
192
+ assert @ooyala_api.put('test', {:test => true})
193
+ mock(@ooyala_api).send_request('PUT', 'test', {}, '[1,2,3]') { true }
194
+ assert @ooyala_api.put('test', [1,2,3])
195
+ end
196
+
197
+ def test_put_with_params
198
+ mock(@ooyala_api).send_request('PUT', 'test', {:test => true},
199
+ '{"test":true}') { true }
200
+ assert @ooyala_api.put('test', {:test => true}, :test => true)
201
+ end
202
+
203
+ def test_put_with_a_file
204
+ file = File.new(__FILE__)
205
+ mock(file).read { "payload" }
206
+ mock(@ooyala_api).send_request('PUT', 'test', {}, 'payload') { true }
207
+ assert @ooyala_api.put('test', file)
208
+ end
209
+
210
+ def test_put_with_a_string
211
+ mock(@ooyala_api).send_request('PUT', 'test', {}, 'payload') { true }
212
+ assert @ooyala_api.put('test', 'payload')
213
+ end
214
+
215
+ def test_patch
216
+ mock(@ooyala_api).send_request('PATCH', 'test', {}, '{"test":true}') { true }
217
+ assert @ooyala_api.patch('test', {:test => true})
218
+ mock(@ooyala_api).send_request('PATCH', 'test', {}, '[1,2,3]') { true }
219
+ assert @ooyala_api.patch('test', [1,2,3])
220
+ end
221
+
222
+ def test_patch_with_params
223
+ mock(@ooyala_api).send_request('PATCH', 'test', {:test => true},
224
+ '{"test":true}') { true }
225
+ assert @ooyala_api.patch('test', {:test => true}, :test => true)
226
+ end
227
+
228
+ def test_delete
229
+ mock(@ooyala_api).send_request('DELETE', 'test', {}) { true }
230
+ assert @ooyala_api.delete('test')
231
+ end
232
+
233
+ def test_delete_with_params
234
+ mock(@ooyala_api).send_request('DELETE', 'test', {:test => true, 'a' => 1}) do
235
+ true
236
+ end
237
+ assert @ooyala_api.delete('test', :test => true, 'a' => 1)
238
+ end
239
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ooyala-v2-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jose Gasca
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rdoc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.6.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: jeweler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 1.6.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 1.6.4
97
+ - !ruby/object:Gem::Dependency
98
+ name: tomdoc
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: json
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rest-client
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ~>
130
+ - !ruby/object:Gem::Version
131
+ version: 1.6.6
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ version: 1.6.6
139
+ description: This gem allows the interaction with the Ooyala's API v2.
140
+ email: jose@ooyala.com
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files:
144
+ - LICENSE.txt
145
+ - README.rdoc
146
+ files:
147
+ - .document
148
+ - Gemfile
149
+ - Gemfile.lock
150
+ - LICENSE.txt
151
+ - README.rdoc
152
+ - Rakefile
153
+ - VERSION
154
+ - lib/ooyala-v2-api.rb
155
+ - test/helper.rb
156
+ - test/test_ooyala-v2-api.rb
157
+ homepage: http://api.ooyala.com/docs/v2/api_libraries
158
+ licenses:
159
+ - See license.txt
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ! '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ! '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubyforge_project:
177
+ rubygems_version: 2.0.3
178
+ signing_key:
179
+ specification_version: 3
180
+ summary: Ooyala's API v2 Ruby SDK
181
+ test_files: []