diff --git a/.gitignore b/.gitignore index ce69a02..820d634 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,9 @@ Gemfile.lock .bundle /zencoder*.gem /tmp -/.rbx \ No newline at end of file +/.rbx +.zenflow-log +.ruby-gemset +.ruby-version +/.byebug-history +/.tool-versions diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6b483f1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: ruby +rvm: + - 2.5 + - 2.4 + - 2.3 + - 2.2 + - jruby diff --git a/.zenflow b/.zenflow new file mode 100644 index 0000000..945c90d --- /dev/null +++ b/.zenflow @@ -0,0 +1,11 @@ +--- +project: zencoder-rb +development_branch: master +staging_branch: false +qa_branch: false +release_branch: false +merge_strategy: merge +remote: origin +backup_remote: false +confirm_staging: false +confirm_review: true diff --git a/Gemfile b/Gemfile index 588d1cf..fa75df1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,16 +1,3 @@ -source :rubygems +source 'https://rubygems.org' -gem "multi_json" - -group :test do - gem "shoulda" - gem "mocha" - gem "webmock", "~>1.6.0" -end - -group :development do - gem "jruby-openssl", :platforms => :jruby - gem "ruby-debug", :platforms => :mri_18 - gem "ruby-debug19", :platforms => :mri_19 - gem "typhoeus", :platforms => [:mri_18, :mri_19] -end +gemspec diff --git a/README.markdown b/README.markdown index 5d8dfcb..29da5e3 100644 --- a/README.markdown +++ b/README.markdown @@ -1,18 +1,26 @@ # Zencoder +[![Build Status](https://travis-ci.org/zencoder/zencoder-rb.svg?branch=master)](https://travis-ci.org/zencoder/zencoder-rb) + The gem for interacting with the API on [Zencoder](http://zencoder.com). -See [http://zencoder.com/docs/api](http://zencoder.com/docs/api) for more details on the API. +See [https://support.brightcove.com/zencoder](https://support.brightcove.com/zencoder) for more details on the API. Tested on the following versions of Ruby: -* Ruby 1.8.6-p420 -* Ruby 1.8.7-p249 -* Ruby 1.8.7-p352 -* Ruby 1.9.2-p290 -* Ruby 1.9.3-p0 -* Rubinius 2.0.0dev -* jRuby 1.6.5 +* Ruby 2.2 +* Ruby 2.3 +* Ruby 2.4 +* Ruby 2.5 +* Latest jRuby + +## 2.5 WARNING!!! + +Version 2.5 brings a single, significant change to the gem which you should be aware of: + +* __The Zencoder SSL CA chain is no longer bundled.__ Our intermediate SSL cert is expiring and the necessary file may change in the future. You can now specify the CA file or CA path along with the request. + +[We recommend installing a CA bundle (probably cURL's) in your OS and have ruby use that.](http://mislav.uniqpath.com/2013/07/ruby-openssl/) ## v2.4 WARNING!!! @@ -35,8 +43,11 @@ With the release of version two of the Zencoder API, there are some new methods * Zencoder::Input.progress(input\_id) * Zencoder::Output.details(output\_id) * Zencoder::Report.minutes(:from => "2011-01-01", :to => "2011-03-01") +* Zencoder::Report.all(:from => "2011-01-01", :to => "2011-03-01") +* Zencoder::Report.live(:from => "2011-01-01", :to => "2011-03-01") +* Zencoder::Report.vod(:from => "2011-01-01", :to => "2011-03-01") -These new methods will not work with older versions of the API. Please see the [Zencoder documentation](https://app.zencoder.com/docs) and our [blog post on the subject](http://blog.zencoder.com/2012/01/05/announcing-zencoder-api-v2/) for more information on APIv2. +These new methods will not work with older versions of the API. Please see the [Zencoder documentation](https://support.brightcove.com/zencoder) for more information on APIv2. If you'd like to use the new version of the library but continue using APIv1 until you work through any integration troubles, you can do the following: @@ -46,6 +57,12 @@ Zencoder.base_url = "https://app.zencoder.com/api/v1" ## Getting Started +To install the gem on Rails, simply add it to your Gemfile: + +```ruby +gem "zencoder", "~> 2.0" +``` + The first thing you'll need to interact with the Zencoder API is your API key. You can use your API key in one of three ways. The first and easiest is to set it and forget it on the Zencoder module like so: ```ruby @@ -73,6 +90,7 @@ response.code # => 200 response.body # => the JSON-parsed body or raw body if unparseable response.raw_body # => the body pre-JSON-parsing response.raw_response # => the raw Net::HTTP or Typhoeus response (see below for how to use Typhoeus) +response.request # => the request object, you can inspect it if you need details on the request to debug it ``` ### Parameters @@ -95,7 +113,7 @@ There's more you can do on jobs than anything else in the API. The following met ### create -The hash you pass to the `create` method should be encodable to the [JSON you would pass to the Job creation API call on Zencoder](http://zencoder.com/docs/api/#encoding-job). We'll auto-populate your API key if you've set it already. +The hash you pass to the `create` method should be encodable to the [JSON you would pass to the Job creation API call on Zencoder](https://docs.brightcove.com/zencoder-api/v2/doc/index.html#api-Jobs-Create_a_Job). We'll auto-populate your API key if you've set it already. ```ruby Zencoder::Job.create({:input => 's3://bucket/key.mp4'}) @@ -227,7 +245,7 @@ Zencoder::Notification.list(:api_key => 'abcd1234') ### create -The hash you pass to the `create` method should be encodable to the [JSON you would pass to the Account creation API call on Zencoder](http://zencoder.com/docs/api/#accounts). No API key is required for this call, of course. +The hash you pass to the `create` method should be encodable to the [JSON you would pass to the Account creation API call on Zencoder](https://docs.brightcove.com/zencoder-api/v2/doc/index.html#api-Accounts-Create_an_Account). No API key is required for this call, of course. ```ruby Zencoder::Account.create({:terms_of_service => 1, @@ -235,6 +253,7 @@ Zencoder::Account.create({:terms_of_service => 1, Zencoder::Account.create({:terms_of_service => 1, :email => 'bob@example.com', :password => 'abcd1234', + :password_confirmation => 'abcd1234', :affiliate_code => 'abcd1234'}) ``` @@ -273,6 +292,30 @@ This will list the minutes used for your account within a certain, configurable Zencoder::Report.minutes(:from => "2011-10-30", :to => "2011-11-24") ``` +### all + +This will list all usage, including VOD and Live for your account within a certain, configurable range. + +```ruby +Zencoder::Report.all(:from => "2011-10-30", :to => "2011-11-24") +``` + +### vod + +This will list just VOD usage for your account within a certain, configurable range. + +```ruby +Zencoder::Report.vod(:from => "2011-10-30", :to => "2011-11-24") +``` + +### live + +This will list just Live usage for your account within a certain, configurable range. + +```ruby +Zencoder::Report.live(:from => "2011-10-30", :to => "2011-11-24") +``` + ## Advanced HTTP ### Alternate HTTP Libraries @@ -300,7 +343,25 @@ Zencoder::Job.create({:input => 's3://bucket/key.mp4'}, {:timeout => 1000}) ### SSL Verification -We will use our bundled SSL CA chain for SSL peer verification which should almost always work without a hitch. However, if you'd like to skip SSL verification you can pass an option in the secondary options hash. +SSL verification using the default Net::HTTP backend requires that your ruby be appropriately configured with up to date path to a cert bundle on your system or by specifying the a CA file or CA path when sending requests. + +```ruby +Zencoder::Job.create({:input => 's3://bucket/key.mp4'}, {:ca_path => "/path/to/certs/"}) +# or +Zencoder::Job.create({:input => 's3://bucket/key.mp4'}, {:ca_file => "/path/to/certs/zen.crt"}) +``` + +Alternatively you can add it to the default options. + +```ruby +Zencoder::HTTP.default_options.merge!(:ca_path => "/path/to/certs/") +# or +Zencoder::HTTP.default_options.merge!(:ca_file => "/path/to/certs/zen.crt") +``` + +You can get a CA bundle from [the curl website](http://curl.haxx.se/docs/caextract.html), but it is recommended that you use your system's package manager to install these certs and keep them up to date. + +However, if you'd like to skip SSL verification you can pass an option in the secondary options hash. **NOTE: WE HIGHLY DISCOURAGE THIS! THIS WILL LEAVE YOU VULNERABLE TO MAN-IN-THE-MIDDLE ATTACKS!** diff --git a/Rakefile b/Rakefile index 406fe18..77f8a53 100644 --- a/Rakefile +++ b/Rakefile @@ -1,36 +1,8 @@ require 'rubygems' -require 'rake' -require 'rake/testtask' - -Rake::TestTask.new(:test) do |test| - test.libs << 'lib' << 'test' - test.pattern = 'test/**/*_test.rb' - test.verbose = true -end - -task :default => :test - -require 'rdoc/task' - -Rake::RDocTask.new do |rdoc| - version = File.exist?('VERSION') ? File.read('VERSION') : "" - - rdoc.rdoc_dir = 'rdoc' - rdoc.title = "zencoder-rb #{version}" - rdoc.rdoc_files.include('README*') - rdoc.rdoc_files.include('lib/**/*.rb') -end begin - require 'rcov/rcovtask' - Rcov::RcovTask.new do |test| - test.libs << 'test' - test.pattern = 'test/**/*_test.rb' - test.verbose = true - test.rcov_opts << '--exclude "gems/*"' - end + require 'rspec/core/rake_task' + RSpec::Core::RakeTask.new(:spec) + task :default => :spec rescue LoadError - task :rcov do - abort "RCov is not available. In order to run rcov, you must: sudo gem install rcov" - end end diff --git a/lib/zencoder/http.rb b/lib/zencoder/http.rb index 8de528d..e3007e4 100644 --- a/lib/zencoder/http.rb +++ b/lib/zencoder/http.rb @@ -1,8 +1,6 @@ module Zencoder class HTTP - CA_CHAIN_PATH = File.expand_path(File.join(File.dirname(__FILE__), "http", "resources", "zencoder_ca_chain.crt")) - include Zencoder::Serializer attr_accessor :url, :options, :method @@ -75,11 +73,15 @@ def default_options self.class.default_options end + def inspect + "#{method.to_s.upcase} #{url}\nOptions: " + options.inspect + end protected def process(http_response) response = Response.new + response.request = self response.code = http_response.code begin diff --git a/lib/zencoder/http/net_http.rb b/lib/zencoder/http/net_http.rb index d8555bf..df4d421 100644 --- a/lib/zencoder/http/net_http.rb +++ b/lib/zencoder/http/net_http.rb @@ -2,17 +2,19 @@ module Zencoder class HTTP class NetHTTP - attr_accessor :method, :url, :uri, :body, :params, :headers, :timeout, :skip_ssl_verify, :options + attr_accessor :method, :url, :uri, :body, :params, :headers, :timeout, :skip_ssl_verify, :options, :ca_file, :ca_path def initialize(method, url, options) @method = method @url = url - @body = options.delete(:body) - @params = options.delete(:params) - @headers = options.delete(:headers) - @timeout = options.delete(:timeout) - @skip_ssl_verify = options.delete(:skip_ssl_verify) - @options = options + @options = options.dup + @body = @options.delete(:body) + @params = @options.delete(:params) + @headers = @options.delete(:headers) + @timeout = @options.delete(:timeout) + @skip_ssl_verify = @options.delete(:skip_ssl_verify) + @ca_file = @options.delete(:ca_file) + @ca_path = @options.delete(:ca_path) end def self.post(url, options={}) @@ -59,9 +61,17 @@ def http if skip_ssl_verify http.verify_mode = OpenSSL::SSL::VERIFY_NONE else - http.ca_file = Zencoder::HTTP::CA_CHAIN_PATH http.verify_mode = OpenSSL::SSL::VERIFY_PEER - http.verify_depth = 5 + + http.cert_store = OpenSSL::X509::Store.new + http.cert_store.set_default_paths + + if defined?(OpenSSL::X509::V_FLAG_CRL_CHECK_ALL) + http.cert_store.flags = OpenSSL::X509::V_FLAG_CRL_CHECK_ALL + end + + http.cert_store.add_file(ca_file) if ca_file + http.cert_store.add_path(ca_path) if ca_path end end diff --git a/lib/zencoder/http/resources/zencoder_ca_chain.crt b/lib/zencoder/http/resources/zencoder_ca_chain.crt deleted file mode 100644 index e0de7e6..0000000 --- a/lib/zencoder/http/resources/zencoder_ca_chain.crt +++ /dev/null @@ -1,62 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIDJzCCApCgAwIBAgIBATANBgkqhkiG9w0BAQQFADCBzjELMAkGA1UEBhMCWkEx -FTATBgNVBAgTDFdlc3Rlcm4gQ2FwZTESMBAGA1UEBxMJQ2FwZSBUb3duMR0wGwYD -VQQKExRUaGF3dGUgQ29uc3VsdGluZyBjYzEoMCYGA1UECxMfQ2VydGlmaWNhdGlv -biBTZXJ2aWNlcyBEaXZpc2lvbjEhMB8GA1UEAxMYVGhhd3RlIFByZW1pdW0gU2Vy -dmVyIENBMSgwJgYJKoZIhvcNAQkBFhlwcmVtaXVtLXNlcnZlckB0aGF3dGUuY29t -MB4XDTk2MDgwMTAwMDAwMFoXDTIwMTIzMTIzNTk1OVowgc4xCzAJBgNVBAYTAlpB -MRUwEwYDVQQIEwxXZXN0ZXJuIENhcGUxEjAQBgNVBAcTCUNhcGUgVG93bjEdMBsG -A1UEChMUVGhhd3RlIENvbnN1bHRpbmcgY2MxKDAmBgNVBAsTH0NlcnRpZmljYXRp -b24gU2VydmljZXMgRGl2aXNpb24xITAfBgNVBAMTGFRoYXd0ZSBQcmVtaXVtIFNl -cnZlciBDQTEoMCYGCSqGSIb3DQEJARYZcHJlbWl1bS1zZXJ2ZXJAdGhhd3RlLmNv -bTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA0jY2aovXwlue2oFBYo847kkE -VdbQ7xwblRZH7xhINTpS9CtqBo87L+pW46+GjZ4X9560ZXUCTe/LCaIhUdib0GfQ -ug2SBhRz1JPLlyoAnFxODLz6FVL88kRu2hFKbgifLy3j+ao6hnO2RlNYyIkFvYMR -uHM/qgeN9EJN50CdHDcCAwEAAaMTMBEwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG -9w0BAQQFAAOBgQAmSCwWwlj66BZ0DKqqX1Q/8tfJeGBeXm43YyJ3Nn6yF8Q0ufUI -hfzJATj/Tb7yFkJD57taRvvBxhEf8UqwKEbJw8RCfbz6q1lu1bdRiBHjpIUZa4JM -pAwSremkrj/xw0llmozFyD4lt5SZu5IycQfwhl7tUCemDaYj+bvLpgcUQg== ------END CERTIFICATE----- ------BEGIN CERTIFICATE----- -MIIE2DCCBEGgAwIBAgIEN0rSQzANBgkqhkiG9w0BAQUFADCBwzELMAkGA1UEBhMC -VVMxFDASBgNVBAoTC0VudHJ1c3QubmV0MTswOQYDVQQLEzJ3d3cuZW50cnVzdC5u -ZXQvQ1BTIGluY29ycC4gYnkgcmVmLiAobGltaXRzIGxpYWIuKTElMCMGA1UECxMc -KGMpIDE5OTkgRW50cnVzdC5uZXQgTGltaXRlZDE6MDgGA1UEAxMxRW50cnVzdC5u -ZXQgU2VjdXJlIFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05OTA1 -MjUxNjA5NDBaFw0xOTA1MjUxNjM5NDBaMIHDMQswCQYDVQQGEwJVUzEUMBIGA1UE -ChMLRW50cnVzdC5uZXQxOzA5BgNVBAsTMnd3dy5lbnRydXN0Lm5ldC9DUFMgaW5j -b3JwLiBieSByZWYuIChsaW1pdHMgbGlhYi4pMSUwIwYDVQQLExwoYykgMTk5OSBF -bnRydXN0Lm5ldCBMaW1pdGVkMTowOAYDVQQDEzFFbnRydXN0Lm5ldCBTZWN1cmUg -U2VydmVyIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MIGdMA0GCSqGSIb3DQEBAQUA -A4GLADCBhwKBgQDNKIM0VBuJ8w+vN5Ex/68xYMmo6LIQaO2f55M28Qpku0f1BBc/ -I0dNxScZgSYMVHINiC3ZH5oSn7yzcdOAGT9HZnuMNSjSuQrfJNqc1lB5gXpa0zf3 -wkrYKZImZNHkmGw6AIr1NJtl+O3jEP/9uElY3KDegjlrgbEWGWG5VLbmQwIBA6OC -AdcwggHTMBEGCWCGSAGG+EIBAQQEAwIABzCCARkGA1UdHwSCARAwggEMMIHeoIHb -oIHYpIHVMIHSMQswCQYDVQQGEwJVUzEUMBIGA1UEChMLRW50cnVzdC5uZXQxOzA5 -BgNVBAsTMnd3dy5lbnRydXN0Lm5ldC9DUFMgaW5jb3JwLiBieSByZWYuIChsaW1p -dHMgbGlhYi4pMSUwIwYDVQQLExwoYykgMTk5OSBFbnRydXN0Lm5ldCBMaW1pdGVk -MTowOAYDVQQDEzFFbnRydXN0Lm5ldCBTZWN1cmUgU2VydmVyIENlcnRpZmljYXRp -b24gQXV0aG9yaXR5MQ0wCwYDVQQDEwRDUkwxMCmgJ6AlhiNodHRwOi8vd3d3LmVu -dHJ1c3QubmV0L0NSTC9uZXQxLmNybDArBgNVHRAEJDAigA8xOTk5MDUyNTE2MDk0 -MFqBDzIwMTkwNTI1MTYwOTQwWjALBgNVHQ8EBAMCAQYwHwYDVR0jBBgwFoAU8Bdi -E1U9s/8KAGv7UISX8+1i0BowHQYDVR0OBBYEFPAXYhNVPbP/CgBr+1CEl/PtYtAa -MAwGA1UdEwQFMAMBAf8wGQYJKoZIhvZ9B0EABAwwChsEVjQuMAMCBJAwDQYJKoZI -hvcNAQEFBQADgYEAkNwwAvpkdMKnCqV8IY00F6j7Rw7/JXyNEwr75Ji174z4xRAN -95K+8cPV1ZVqBLssziY2ZcgxxufuP+NXdYR6Ee9GTxj005i7qIcyunL2POI9n9cd -2cNgQ4xYDiKWL2KjLB+6rQXvqzJ4h6BUcxm1XAX5Uj5tLUUL9wqT6u0G+bI= ------END CERTIFICATE----- ------BEGIN CERTIFICATE----- -MIICWjCCAcMCAgGlMA0GCSqGSIb3DQEBBAUAMHUxCzAJBgNVBAYTAlVTMRgwFgYD -VQQKEw9HVEUgQ29ycG9yYXRpb24xJzAlBgNVBAsTHkdURSBDeWJlclRydXN0IFNv -bHV0aW9ucywgSW5jLjEjMCEGA1UEAxMaR1RFIEN5YmVyVHJ1c3QgR2xvYmFsIFJv -b3QwHhcNOTgwODEzMDAyOTAwWhcNMTgwODEzMjM1OTAwWjB1MQswCQYDVQQGEwJV -UzEYMBYGA1UEChMPR1RFIENvcnBvcmF0aW9uMScwJQYDVQQLEx5HVEUgQ3liZXJU -cnVzdCBTb2x1dGlvbnMsIEluYy4xIzAhBgNVBAMTGkdURSBDeWJlclRydXN0IEds -b2JhbCBSb290MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVD6C28FCc6HrH -iM3dFw4usJTQGz0O9pTAipTHBsiQl8i4ZBp6fmw8U+E3KHNgf7KXUwefU/ltWJTS -r41tiGeA5u2ylc9yMcqlHHK6XALnZELn+aks1joNrI1CqiQBOeacPwGFVw1Yh0X4 -04Wqk2kmhXBIgD8SFcd5tB8FLztimQIDAQABMA0GCSqGSIb3DQEBBAUAA4GBAG3r -GwnpXtlR22ciYaQqPEh346B8pt5zohQDhT37qw4wxYMWM4ETCJ57NE7fQMh017l9 -3PR2VX2bY1QY6fDq81yx2YtCHrnAlU66+tXifPVoYb+O7AWXX1uw16OFNMQkpw0P -lZPvy5TYnh+dXIVtx6quTx8itc2VrbqnzPmrC3p/ ------END CERTIFICATE----- \ No newline at end of file diff --git a/lib/zencoder/http/typhoeus.rb b/lib/zencoder/http/typhoeus.rb index 2ae4ae4..667687e 100644 --- a/lib/zencoder/http/typhoeus.rb +++ b/lib/zencoder/http/typhoeus.rb @@ -19,10 +19,19 @@ def self.delete(url, options={}) end def self.perform(method, url, options={}) + options = options.dup if options.delete(:skip_ssl_verify) options[:disable_ssl_peer_verification] = true end + if ca_file = options.delete(:ca_file) + options[:sslcert] = ca_file + end + + if ca_path = options.delete(:ca_path) + options[:capath] = ca_path + end + ::Typhoeus::Request.send(method, url, options) end diff --git a/lib/zencoder/job.rb b/lib/zencoder/job.rb index c72169d..43076b8 100644 --- a/lib/zencoder/job.rb +++ b/lib/zencoder/job.rb @@ -30,5 +30,9 @@ def self.cancel(job_id, options={}) put("/jobs/#{job_id}/cancel", nil, options) end + def self.finish(job_id, options={}) + put("/jobs/#{job_id}/finish", nil, options) + end + end end diff --git a/lib/zencoder/report.rb b/lib/zencoder/report.rb index b10eabf..233dad8 100644 --- a/lib/zencoder/report.rb +++ b/lib/zencoder/report.rb @@ -2,7 +2,33 @@ module Zencoder class Report < Resource def self.minutes(options={}) - get("/reports/minutes", options) + get("/reports/minutes", merge_params(*extract_params(options))) + end + + def self.all(options={}) + get("/reports/all", merge_params(*extract_params(options))) + end + + def self.live(options={}) + get("/reports/live", merge_params(*extract_params(options))) + end + + def self.vod(options={}) + get("/reports/vod", merge_params(*extract_params(options))) + end + + + protected + + def self.extract_params(options={}) + options = options.dup + params = { + :from => options.delete(:from), + :to => options.delete(:to), + :grouping => options.delete(:grouping) + } + + return options, params.delete_if { |k, v| v.nil? } end end diff --git a/lib/zencoder/response.rb b/lib/zencoder/response.rb index 9edbe99..6c7ef04 100644 --- a/lib/zencoder/response.rb +++ b/lib/zencoder/response.rb @@ -1,7 +1,7 @@ module Zencoder class Response - attr_accessor :code, :body, :raw_body, :raw_response + attr_accessor :request, :code, :body, :raw_body, :raw_response def initialize(options={}) options.each do |k, v| diff --git a/lib/zencoder/version.rb b/lib/zencoder/version.rb index 8e73603..b0ca1e5 100644 --- a/lib/zencoder/version.rb +++ b/lib/zencoder/version.rb @@ -1,3 +1,3 @@ module Zencoder - GEM_VERSION = '2.4.4' + GEM_VERSION = '2.5.1' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..7dc92cf --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,54 @@ +require "rubygems" +require "bundler" +Bundler.setup +Bundler.require(:default, :test, :development) +require 'test/unit/assertions' +require 'webmock/rspec' + +RSpec.configure do |config| + config.include Test::Unit::Assertions + + def assert_same_elements(a1, a2, msg = nil) + [:select, :inject, :size].each do |m| + [a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") } + end + + assert a1h = a1.inject({}) { |h,e| h[e] ||= a1.select { |i| i == e }.size; h } + assert a2h = a2.inject({}) { |h,e| h[e] ||= a2.select { |i| i == e }.size; h } + + assert_equal(a1h, a2h, msg) + end + + config.after(:example) do + ENV["ZENCODER_API_KEY"] = nil + Zencoder.api_key = nil + end + + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.shared_context_metadata_behavior = :apply_to_host_groups + + config.filter_run_when_matching :focus + + # config.example_status_persistence_file_path = "spec/examples.txt" + + config.disable_monkey_patching! + + config.warnings = true + + if config.files_to_run.one? + config.default_formatter = "doc" + end + + # config.profile_examples = 10 + + config.order = :random + + Kernel.srand config.seed +end diff --git a/spec/zencoder/account_spec.rb b/spec/zencoder/account_spec.rb new file mode 100644 index 0000000..b68e22a --- /dev/null +++ b/spec/zencoder/account_spec.rb @@ -0,0 +1,53 @@ +require "spec_helper" + +RSpec.describe Zencoder::Account do + before do + @api_key = 'abc123' + end + + describe ".create" do + before do + @url = "#{Zencoder.base_url}/account" + @params = {:api_key => @api_key} + @params_as_json = Zencoder::Serializer.encode(@params) + end + + it "POSTs to the correct url and return a response" do + expect(Zencoder::HTTP).to receive(:post).with(@url, @params_as_json, {}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Account.create(@params).class + end + end + + describe ".details" do + before do + @url = "#{Zencoder.base_url}/account" + end + + it "GETs the correct url and return a response" do + expect(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Account.details(:api_key => @api_key).class + end + end + + describe ".integration" do + before do + @url = "#{Zencoder.base_url}/account/integration" + end + + it "PUTs the correct url and return a response" do + expect(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Account.integration(:api_key => @api_key).class + end + end + + describe ".live" do + before do + @url = "#{Zencoder.base_url}/account/live" + end + + it "PUTs the correct url and return a response" do + expect(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Account.live(:api_key => @api_key).class + end + end +end diff --git a/spec/zencoder/http/net_http_spec.rb b/spec/zencoder/http/net_http_spec.rb new file mode 100644 index 0000000..fcebcbf --- /dev/null +++ b/spec/zencoder/http/net_http_spec.rb @@ -0,0 +1,121 @@ +require "spec_helper" + +RSpec.describe Zencoder::HTTP::NetHTTP do + + describe "call options" do + it "requests with timeout" do + stub_request(:post, "https://example.com") + expect(Timeout).to receive(:timeout).with(0.001) + Zencoder::HTTP::NetHTTP.post('https://example.com', :timeout => 1) + end + + it "requests without timeout" do + stub_request(:post, "https://example.com") + allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) + assert_nothing_raised do + Zencoder::HTTP::NetHTTP.post('https://example.com', :timeout => nil) + end + end + + it "adds params to the query string if passed" do + stub_request(:post, "https://example.com/path?some=param") + Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:params => {:some => 'param'}}) + end + + it "adds params to the existing query string if passed" do + stub_request(:post,'https://example.com/path?original=param&some=param') + Zencoder::HTTP::NetHTTP.post('https://example.com/path?original=param', {:params => {:some => 'param'}}) + end + + it "adds headers" do + stub_request(:post,'https://example.com/path').with(:headers => {'some' => 'header'}) + Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:headers => {:some => 'header'}}) + end + + it "adds the body to the request" do + stub_request(:post, 'https://example.com/path').with(:body => '{"some": "body"}') + Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:body => '{"some": "body"}'}) + end + end + + describe "SSL verification" do + before do + @cert_store = double(:add_file => true, :add_path => true, :flags= => true, :set_default_paths => true).as_null_object + @http_stub = double(:use_ssl= => true, :request => true, :verify_mode= => true, :cert_store= => true, :cert_store => @cert_store).as_null_object + expect(::Net::HTTP).to receive(:new).and_return(@http_stub) + end + + describe "when set to skip ssl verification" do + it "nots verify" do + expect(@http_stub).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) + Zencoder::HTTP::NetHTTP.post('https://example.com/path', :skip_ssl_verify => true) + end + + it "nots setup a custom cert store" do + expect(@http_stub).to_not receive(:cert_store=) + Zencoder::HTTP::NetHTTP.post('https://example.com/path', :skip_ssl_verify => true) + end + end + + describe "when set to do ssl verification" do + it "setups a custom cert store" do + expect(@http_stub).to receive(:cert_store=) + Zencoder::HTTP::NetHTTP.post('https://example.com/path') + end + + it "sets the default paths on the custom cert store" do + expect(@cert_store).to receive(:set_default_paths) + Zencoder::HTTP::NetHTTP.post('https://example.com/path') + end + + it "sets the ca_file when it is passed in" do + expect(@cert_store).to receive(:add_file).with("/foo/bar/baz.crt") + Zencoder::HTTP::NetHTTP.post('https://example.com/path', :ca_file => "/foo/bar/baz.crt") + end + + it "sets the ca_path when it is passed in" do + expect(@cert_store).to receive(:add_path).with("/foo/bar/") + Zencoder::HTTP::NetHTTP.post('https://example.com/path', :ca_path => "/foo/bar/") + end + end + + end + + describe ".post" do + it "POSTs to specified body to the specified path" do + stub_request(:post, 'https://example.com').with(:body => '{}') + Zencoder::HTTP::NetHTTP.post('https://example.com', :body => '{}') + end + + it "POSTs with an empty body if none is provided" do + stub_request(:post, 'https://example.com').with(:body => '') + Zencoder::HTTP::NetHTTP.post('https://example.com') + end + end + + describe ".put" do + it "PUTs to specified body to the specified path" do + stub_request(:put, 'https://example.com').with(:body => '{}') + Zencoder::HTTP::NetHTTP.put('https://example.com', :body => '{}') + end + + it "PUTs with an empty body if none is provided" do + stub_request(:put, 'https://example.com').with(:body => '') + Zencoder::HTTP::NetHTTP.put('https://example.com') + end + end + + describe ".get" do + it "GETs to specified body to the specified path" do + stub_request(:get, 'https://example.com') + Zencoder::HTTP::NetHTTP.get('https://example.com') + end + end + + describe ".delete" do + it "DELETEs to specified body to the specified path" do + stub_request(:delete, 'https://example.com') + Zencoder::HTTP::NetHTTP.delete('https://example.com') + end + end +end diff --git a/spec/zencoder/http/typhoeus_spec.rb b/spec/zencoder/http/typhoeus_spec.rb new file mode 100644 index 0000000..ad0882f --- /dev/null +++ b/spec/zencoder/http/typhoeus_spec.rb @@ -0,0 +1,68 @@ +require "spec_helper" + +# Most useless tests ever, but who knows, right? + +if !defined?(Typhoeus) + module ::Typhoeus + module Request + extend self + + def get(*) + end + + def put(*) + end + + def post(*) + end + + def delete(*) + end + end + end +end + +RSpec.describe Zencoder::HTTP::Typhoeus do + describe ".post" do + it "POSTs using Typhoeus" do + expect(Typhoeus::Request).to receive(:post).with('https://example.com', {:some => 'options'}) + Zencoder::HTTP::Typhoeus.post('https://example.com', {:some => 'options'}) + end + end + + describe ".put" do + it "PUTs using Typhoeus" do + expect(Typhoeus::Request).to receive(:put).with('https://example.com', {:some => 'options'}) + Zencoder::HTTP::Typhoeus.put('https://example.com', {:some => 'options'}) + end + end + + describe ".get" do + it "GETs using Typhoeus" do + expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:some => 'options'}) + Zencoder::HTTP::Typhoeus.get('https://example.com', {:some => 'options'}) + end + end + + describe ".delete" do + it "DELETEs using Typhoeus" do + expect(Typhoeus::Request).to receive(:delete).with('https://example.com', {:some => 'options'}) + Zencoder::HTTP::Typhoeus.delete('https://example.com', {:some => 'options'}) + end + end + + it "skips ssl verification" do + expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:disable_ssl_peer_verification => true}) + Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true}) + end + + it "uses the path to the cert file" do + expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:disable_ssl_peer_verification => true, :sslcert => "/foo/bar/baz.crt"}) + Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true, :ca_file => "/foo/bar/baz.crt"}) + end + + it "uses the path to the certs directory" do + expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:disable_ssl_peer_verification => true, :capath => "/foo/bar/baz.crt"}) + Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true, :ca_path => "/foo/bar/baz.crt"}) + end +end diff --git a/spec/zencoder/http_spec.rb b/spec/zencoder/http_spec.rb new file mode 100644 index 0000000..46ce002 --- /dev/null +++ b/spec/zencoder/http_spec.rb @@ -0,0 +1,117 @@ +require "spec_helper" + +RSpec.describe Zencoder::HTTP do + it "haves a default_options hash" do + assert Zencoder::HTTP.default_options.is_a?(Hash) + end + + it "haves a default HTTP backend" do + assert Zencoder::HTTP.http_backend + end + + it "allows the changing of the HTTP backend" do + assert_not_equal Zencoder::HTTP.http_backend, Zencoder::HTTP::Typhoeus + + assert_nothing_raised do + Zencoder::HTTP.http_backend = Zencoder::HTTP::Typhoeus + end + + assert_equal Zencoder::HTTP.http_backend, Zencoder::HTTP::Typhoeus + end + + it "raises a Zencoder::HTTPError when there is an HTTP error" do + expect(Zencoder::HTTP.http_backend).to receive(:get). + with('https://example.com', Zencoder::HTTP.default_options). + at_least(:once). + and_raise(Errno::ECONNREFUSED) + + assert_raises Zencoder::HTTPError do + Zencoder::HTTP.get('https://example.com') + end + + begin + Zencoder::HTTP.get('https://example.com') + rescue Zencoder::HTTPError => e + assert_no_match(/perform_method/, e.backtrace.first) + end + end + + it "returns a Zencoder::Response" do + allow(Zencoder::HTTP.http_backend).to receive(:post).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) + allow(Zencoder::HTTP.http_backend).to receive(:put).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) + allow(Zencoder::HTTP.http_backend).to receive(:get).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) + allow(Zencoder::HTTP.http_backend).to receive(:delete).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) + + assert Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').is_a?(Zencoder::Response) + assert Zencoder::HTTP.put('https://example.com', '{"some": "hash"}').is_a?(Zencoder::Response) + assert Zencoder::HTTP.get('https://example.com').is_a?(Zencoder::Response) + assert Zencoder::HTTP.delete('https://example.com').is_a?(Zencoder::Response) + end + + it "stores the raw response" do + post_stub = double(:code => 200, :body => '{"some": "hash"}').as_null_object + allow(Zencoder::HTTP.http_backend).to receive(:post).and_return(post_stub) + assert_equal post_stub, Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').raw_response + end + + it "stores the raw response body" do + allow(Zencoder::HTTP.http_backend).to receive(:post).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) + assert_equal '{"some": "hash"}', Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').raw_body + end + + it "stores the response code" do + allow(Zencoder::HTTP.http_backend).to receive(:post).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) + assert_equal 200, Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').code + end + + it "JSONs parse the response body" do + allow(Zencoder::HTTP.http_backend).to receive(:put).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) + assert_equal({'some' => 'hash'}, Zencoder::HTTP.put('https://example.com', '{"some": "hash"}').body) + end + + it "stores the raw body if the body fails to be JSON parsed" do + allow(Zencoder::HTTP.http_backend).to receive(:put).and_return(double(:code => 200, :body => '{"some": bad json').as_null_object) + assert_equal '{"some": bad json', Zencoder::HTTP.put('https://example.com', '{"some": "hash"}').body + end + + describe ".post" do + it "calls post on the http_backend" do + expect(Zencoder::HTTP.http_backend).to receive(:post). + with('https://example.com', Zencoder::HTTP.default_options.merge(:body => '{}')). + and_return(Zencoder::Response.new) + + Zencoder::HTTP.post('https://example.com', '{}') + end + end + + describe ".put" do + it "calls put on the http_backend" do + expect(Zencoder::HTTP.http_backend).to receive(:put). + with('https://example.com', Zencoder::HTTP.default_options.merge(:body => '{}')). + and_return(Zencoder::Response.new) + + Zencoder::HTTP.put('https://example.com', '{}') + end + end + + describe ".get" do + it "calls post on the http_backend" do + expect(Zencoder::HTTP.http_backend).to receive(:get). + with('https://example.com', Zencoder::HTTP.default_options). + and_return(Zencoder::Response.new) + + Zencoder::HTTP.get('https://example.com') + end + end + + describe ".delete" do + it "calls post on the http_backend" do + expect(Zencoder::HTTP.http_backend).to receive(:delete). + with('https://example.com', Zencoder::HTTP.default_options). + and_return(Zencoder::Response.new) + + Zencoder::HTTP.delete('https://example.com') + end + end +end + diff --git a/spec/zencoder/input_spec.rb b/spec/zencoder/input_spec.rb new file mode 100644 index 0000000..0cf055a --- /dev/null +++ b/spec/zencoder/input_spec.rb @@ -0,0 +1,31 @@ +require "spec_helper" + +RSpec.describe Zencoder::Input do + before do + @api_key = 'abc123' + end + + describe ".details" do + before do + @input_id = 1 + @url = "#{Zencoder.base_url}/inputs/#{@input_id}" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Input.details(@input_id, :api_key => @api_key).class + end + end + + describe ".progress" do + before do + @input_id = 1 + @url = "#{Zencoder.base_url}/inputs/#{@input_id}/progress" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Input.progress(@input_id, :api_key => @api_key).class + end + end +end diff --git a/spec/zencoder/job_spec.rb b/spec/zencoder/job_spec.rb new file mode 100644 index 0000000..bfeb9c5 --- /dev/null +++ b/spec/zencoder/job_spec.rb @@ -0,0 +1,115 @@ +require "spec_helper" + +RSpec.describe Zencoder::Job do + before do + @api_key = 'abc123' + end + + describe ".create" do + before do + @url = "#{Zencoder.base_url}/jobs" + @params = {:api_key => @api_key, + :input => "s3://bucket-name/file-name.avi"} + @params_as_json = Zencoder::Serializer.encode(@params) + end + + it "POSTs to the correct url and return a response" do + expect(Zencoder::HTTP).to receive(:post).with(@url, @params_as_json, {}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Job.create(@params).class + end + + it "applies the global API key as a header" do + Zencoder.api_key = 'asdfasdf' + expect(Zencoder::HTTP).to receive(:post). + with(instance_of(String), + instance_of(String), + headers: { "Zencoder-Api-Key" => Zencoder.api_key }). + and_return(Zencoder::Response.new) + Zencoder::Job.create(:input => @params[:input]) + end + end + + describe ".list" do + before do + @url = "#{Zencoder.base_url}/jobs" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :page => 1, + :per_page => 50, + :state => nil}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Job.list(:api_key => @api_key).class + end + + it "merges params well" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => {:page => 1, + :per_page => 50, + :some => 'param', + :state => nil}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Job.list(:api_key => @api_key, :params => {:some => 'param'}).class + end + end + + describe ".details" do + before do + @job_id = 1 + @url = "#{Zencoder.base_url}/jobs/#{@job_id}" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Job.details(1, :api_key => @api_key).class + end + end + + describe ".progress" do + before do + @job_id = 1 + @url = "#{Zencoder.base_url}/jobs/#{@job_id}/progress" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Job.progress(1, :api_key => @api_key).class + end + end + + describe ".resubmit" do + before do + @job_id = 1 + @url = "#{Zencoder.base_url}/jobs/#{@job_id}/resubmit" + end + + it "PUTs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => {"Zencoder-Api-Key" => @api_key}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Job.resubmit(1, :api_key => @api_key).class + end + end + + describe ".cancel" do + before do + @job_id = 1 + @url = "#{Zencoder.base_url}/jobs/#{@job_id}/cancel" + end + + it "PUTs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => {"Zencoder-Api-Key" => @api_key}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Job.cancel(1, :api_key => @api_key).class + end + end + + describe ".finish" do + before do + @job_id = 1 + @url = "#{Zencoder.base_url}/jobs/#{@job_id}/finish" + end + + it "PUTs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => {"Zencoder-Api-Key" => @api_key}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Job.finish(1, :api_key => @api_key).class + end + end + +end diff --git a/spec/zencoder/notification_spec.rb b/spec/zencoder/notification_spec.rb new file mode 100644 index 0000000..07645f1 --- /dev/null +++ b/spec/zencoder/notification_spec.rb @@ -0,0 +1,17 @@ +require "spec_helper" + +RSpec.describe Zencoder::Notification do + describe ".list" do + before do + @url = "#{Zencoder.base_url}/notifications" + @api_key = 'asdfasdf' + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :page => 1, + :per_page => 50}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Notification.list(:api_key => @api_key).class + end + end +end diff --git a/spec/zencoder/output_spec.rb b/spec/zencoder/output_spec.rb new file mode 100644 index 0000000..cb8ee8c --- /dev/null +++ b/spec/zencoder/output_spec.rb @@ -0,0 +1,31 @@ +require "spec_helper" + +RSpec.describe Zencoder::Output do + before do + @api_key = 'abc123' + end + + describe ".details" do + before do + @output_id = 1 + @url = "#{Zencoder.base_url}/outputs/#{@output_id}" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Output.details(@output_id, :api_key => @api_key).class + end + end + + describe ".progress" do + before do + @output_id = 1 + @url = "#{Zencoder.base_url}/outputs/#{@output_id}/progress" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Output.progress(@output_id, :api_key => @api_key).class + end + end +end diff --git a/spec/zencoder/report_spec.rb b/spec/zencoder/report_spec.rb new file mode 100644 index 0000000..bbbc707 --- /dev/null +++ b/spec/zencoder/report_spec.rb @@ -0,0 +1,101 @@ +require "spec_helper" + +RSpec.describe Zencoder::Report do + + describe ".minutes" do + before do + @api_key = "abcd123" + @url = "#{Zencoder.base_url}/reports/minutes" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", + :to => "2011-06-01", + :grouping => "foo"}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Report.minutes(:api_key => @api_key, :from => "2011-01-01", + :to => "2011-06-01", + :grouping => "foo").class + end + + it "merges params well" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", + :to => "2011-06-01"}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Report.minutes(:api_key => @api_key, :from => "2011-01-01", :to => "2011-06-01").class + end + end + + describe ".all" do + before do + @api_key = "abcd123" + @url = "#{Zencoder.base_url}/reports/all" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", + :to => "2011-06-01", + :grouping => "foo"}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Report.all(:api_key => @api_key, :from => "2011-01-01", + :to => "2011-06-01", + :grouping => "foo").class + end + + it "merges params well" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", + :to => "2011-06-01"}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Report.all(:api_key => @api_key, :from => "2011-01-01", :to => "2011-06-01").class + end + end + + describe ".vod" do + before do + @api_key = "abcd123" + @url = "#{Zencoder.base_url}/reports/vod" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", + :to => "2011-06-01", + :grouping => "foo"}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Report.vod(:api_key => @api_key, :from => "2011-01-01", + :to => "2011-06-01", + :grouping => "foo").class + end + + it "merges params well" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", + :to => "2011-06-01"}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Report.vod(:api_key => @api_key, :from => "2011-01-01", :to => "2011-06-01").class + end + end + + describe ".live" do + before do + @api_key = "abcd123" + @url = "#{Zencoder.base_url}/reports/live" + end + + it "GETs the correct url and return a response" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", + :to => "2011-06-01", + :grouping => "foo"}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Report.live(:api_key => @api_key, :from => "2011-01-01", + :to => "2011-06-01", + :grouping => "foo").class + end + + it "merges params well" do + allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", + :to => "2011-06-01"}, + :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) + assert_equal Zencoder::Response, Zencoder::Report.live(:api_key => @api_key, :from => "2011-01-01", :to => "2011-06-01").class + end + end + +end diff --git a/spec/zencoder/response_spec.rb b/spec/zencoder/response_spec.rb new file mode 100644 index 0000000..b6acd0d --- /dev/null +++ b/spec/zencoder/response_spec.rb @@ -0,0 +1,45 @@ +require "spec_helper" + +RSpec.describe Zencoder::Response do + describe "#success?" do + it "returns true when code is between 200 and 299" do + assert Zencoder::Response.new(:code => 200).success? + assert Zencoder::Response.new(:code => 299).success? + assert Zencoder::Response.new(:code => 250).success? + end + + it "returns false when code it less than 200 or greater than 299" do + assert !Zencoder::Response.new(:code => 300).success? + assert !Zencoder::Response.new(:code => 199).success? + end + end + + describe "#errors" do + it "returns an empty array when body is not a Hash" do + assert_equal [], Zencoder::Response.new(:body => 1).errors + assert_equal [], Zencoder::Response.new(:body => "something").errors + assert_equal [], Zencoder::Response.new(:body => [1]).errors + end + + it "returns the value of the key 'errors' as a compacted array when body is a Hash" do + assert_same_elements ['must be awesome'], Zencoder::Response.new(:body => {'errors' => ['must be awesome']}).errors + assert_same_elements ['must be awesome'], Zencoder::Response.new(:body => {'errors' => 'must be awesome'}).errors + assert_same_elements ['must be awesome'], Zencoder::Response.new(:body => {'errors' => ['must be awesome', nil]}).errors + assert_same_elements [], Zencoder::Response.new(:body => {}).errors + end + end + + describe "#body_without_wrapper" do + it "returns the body when the body is a string" do + assert_equal "some text", Zencoder::Response.new(:body => "some text").body_without_wrapper + end + + it "returns the body when the body is not wrapped in api_response and is a hash" do + assert_equal({'some' => 'hash'}, Zencoder::Response.new(:body => {'some' => 'hash'}).body_without_wrapper) + end + + it "returns body['api_response'] when body is a hash and body['api_response'] exists" do + assert_equal({'some' => 'hash'}, Zencoder::Response.new(:body => {'api_response' => {'some' => 'hash'}}).body_without_wrapper) + end + end +end diff --git a/spec/zencoder/zencoder_spec.rb b/spec/zencoder/zencoder_spec.rb new file mode 100644 index 0000000..5613c00 --- /dev/null +++ b/spec/zencoder/zencoder_spec.rb @@ -0,0 +1,37 @@ +require "spec_helper" + +RSpec.describe Zencoder do + + it "allows user to set an api key" do + Zencoder.api_key = "123" + assert_equal Zencoder::Job.api_key, "123" + end + + it "allows ENV variable to set an api key" do + ENV['ZENCODER_API_KEY'] = "321" + assert_equal Zencoder::Job.api_key, "321" + end + + it "takes user-supplie api key over ENV-supplied key" do + Zencoder.api_key = "123" + ENV['ZENCODER_API_KEY'] = "321" + assert_equal Zencoder::Job.api_key, "123" + end + + it "encodes to json" do + assert_match(/"api_request"/, Zencoder::Serializer.encode({:api_request => {:input => 'https://example.com'}})) + end + + it "nots encode when the content is a String" do + assert_match(/^api_request$/, Zencoder::Serializer.encode("api_request")) + end + + it "decodes from json" do + assert Zencoder::Serializer.decode(%@{"api_request": {"input": "https://example.com"}}@)['api_request']['input'] + end + + it "does not decode when content is not a String" do + assert_equal 1, Zencoder::Serializer.decode(1) + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index de6ce0c..0000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'rubygems' -require 'bundler' -Bundler.setup -Bundler.require(:default, :test) -require 'mocha/integration/test_unit' # Bundler load-order hax - -$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) -$LOAD_PATH.unshift(File.dirname(__FILE__)) - -require 'zencoder' - -begin - require 'typhoeus' -rescue LoadError # doesn't work for all ruby versions - puts - puts 'Typhoeus not loaded. If your ruby version supports native extensions' - puts 'then consider installing it.' - puts - puts ' gem install typhoeus' - puts -end - -class Test::Unit::TestCase - include WebMock::API -end diff --git a/test/zencoder/account_test.rb b/test/zencoder/account_test.rb deleted file mode 100644 index f1e4ac9..0000000 --- a/test/zencoder/account_test.rb +++ /dev/null @@ -1,56 +0,0 @@ -require 'test_helper' - -class Zencoder::AccountTest < Test::Unit::TestCase - - context Zencoder::Account do - setup do - @api_key = 'abc123' - end - - context ".create" do - setup do - @url = "#{Zencoder.base_url}/account" - @params = {:api_key => @api_key} - @params_as_json = Zencoder::Serializer.encode(@params) - end - - should "POST to the correct url and return a response" do - Zencoder::HTTP.stubs(:post).with(@url, @params_as_json, {}).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Account.create(@params).class - end - end - - context ".details" do - setup do - @url = "#{Zencoder.base_url}/account" - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Account.details(:api_key => @api_key).class - end - end - - context ".integration" do - setup do - @url = "#{Zencoder.base_url}/account/integration" - end - - should "PUT the correct url and return a response" do - Zencoder::HTTP.stubs(:put).with(@url, nil, :headers => { "Zencoder-Api-Key" => @api_key }).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Account.integration(:api_key => @api_key).class - end - end - - context ".live" do - setup do - @url = "#{Zencoder.base_url}/account/live" - end - - should "PUT the correct url and return a response" do - Zencoder::HTTP.stubs(:put).with(@url, nil, :headers => { "Zencoder-Api-Key" => @api_key }).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Account.live(:api_key => @api_key).class - end - end - end -end diff --git a/test/zencoder/http/net_http_test.rb b/test/zencoder/http/net_http_test.rb deleted file mode 100644 index 2edc230..0000000 --- a/test/zencoder/http/net_http_test.rb +++ /dev/null @@ -1,91 +0,0 @@ -require 'test_helper' - -class Zencoder::HTTP::NetHTTPTest < Test::Unit::TestCase - - context Zencoder::HTTP::NetHTTP do - - context "call options" do - should "request with timeout" do - stub_request(:post, "https://example.com") - Timeout.expects(:timeout).with(0.001) - Zencoder::HTTP::NetHTTP.post('https://example.com', :timeout => 1) - end - - should "request without timeout" do - stub_request(:post, "https://example.com") - Timeout.stubs(:timeout).raises(Exception) - assert_nothing_raised do - Zencoder::HTTP::NetHTTP.post('https://example.com', :timeout => nil) - end - end - - should "add params to the query string if passed" do - stub_request(:post, "https://example.com/path?some=param") - Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:params => {:some => 'param'}}) - end - - should "add params to the existing query string if passed" do - stub_request(:post,'https://example.com/path?original=param&some=param') - Zencoder::HTTP::NetHTTP.post('https://example.com/path?original=param', {:params => {:some => 'param'}}) - end - - should "add headers" do - stub_request(:post,'https://example.com/path').with(:headers => {'some' => 'header'}) - Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:headers => {:some => 'header'}}) - end - - should "add the body to the request" do - stub_request(:post, 'https://example.com/path').with(:body => '{"some": "body"}') - Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:body => '{"some": "body"}'}) - end - end - - context "SSL verification" do - should "not verify when set to skip ssl verification" do - http_stub = stub(:use_ssl= => true, :request => true) - http_stub.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) - ::Net::HTTP.expects(:new).returns(http_stub) - Zencoder::HTTP::NetHTTP.post('https://example.com/path', :skip_ssl_verify => true) - end - end - - context ".post" do - should "POST to specified body to the specified path" do - stub_request(:post, 'https://example.com').with(:body => '{}') - Zencoder::HTTP::NetHTTP.post('https://example.com', :body => '{}') - end - - should "POST with an empty body if none is provided" do - stub_request(:post, 'https://example.com').with(:body => '') - Zencoder::HTTP::NetHTTP.post('https://example.com') - end - end - - context ".put" do - should "PUT to specified body to the specified path" do - stub_request(:put, 'https://example.com').with(:body => '{}') - Zencoder::HTTP::NetHTTP.put('https://example.com', :body => '{}') - end - - should "PUT with an empty body if none is provided" do - stub_request(:put, 'https://example.com').with(:body => '') - Zencoder::HTTP::NetHTTP.put('https://example.com') - end - end - - context ".get" do - should "GET to specified body to the specified path" do - stub_request(:get, 'https://example.com') - Zencoder::HTTP::NetHTTP.get('https://example.com') - end - end - - context ".delete" do - should "DELETE to specified body to the specified path" do - stub_request(:delete, 'https://example.com') - Zencoder::HTTP::NetHTTP.delete('https://example.com') - end - end - end - -end diff --git a/test/zencoder/http/typhoeus_test.rb b/test/zencoder/http/typhoeus_test.rb deleted file mode 100644 index 9fa9005..0000000 --- a/test/zencoder/http/typhoeus_test.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'test_helper' - -class Zencoder::HTTP::TyphoeusTest < Test::Unit::TestCase - - # Most useless tests ever, but who knows, right? - - if !defined?(Typhoeus) - module ::Typhoeus - module Request - end - end - end - - context Zencoder::HTTP::Typhoeus do - context ".post" do - should "POST using Typhoeus" do - Typhoeus::Request.expects(:post).with('https://example.com', {:some => 'options'}) - Zencoder::HTTP::Typhoeus.post('https://example.com', {:some => 'options'}) - end - end - - context ".put" do - should "PUT using Typhoeus" do - Typhoeus::Request.expects(:put).with('https://example.com', {:some => 'options'}) - Zencoder::HTTP::Typhoeus.put('https://example.com', {:some => 'options'}) - end - end - - context ".get" do - should "GET using Typhoeus" do - Typhoeus::Request.expects(:get).with('https://example.com', {:some => 'options'}) - Zencoder::HTTP::Typhoeus.get('https://example.com', {:some => 'options'}) - end - end - - context ".delete" do - should "DELETE using Typhoeus" do - Typhoeus::Request.expects(:delete).with('https://example.com', {:some => 'options'}) - Zencoder::HTTP::Typhoeus.delete('https://example.com', {:some => 'options'}) - end - end - - should "skip ssl verification" do - Typhoeus::Request.expects(:get).with('https://example.com', {:disable_ssl_peer_verification => true}) - Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true}) - end - end - -end diff --git a/test/zencoder/http_test.rb b/test/zencoder/http_test.rb deleted file mode 100644 index bbb1d63..0000000 --- a/test/zencoder/http_test.rb +++ /dev/null @@ -1,120 +0,0 @@ -require 'test_helper' - -class Zencoder::HTTPTest < Test::Unit::TestCase - - context Zencoder::HTTP do - should "have a default_options hash" do - assert Zencoder::HTTP.default_options.is_a?(Hash) - end - - should "have a default HTTP backend" do - assert Zencoder::HTTP.http_backend - end - - should "allow the changing of the HTTP backend" do - assert_not_equal Zencoder::HTTP.http_backend, Zencoder::HTTP::Typhoeus - - assert_nothing_raised do - Zencoder::HTTP.http_backend = Zencoder::HTTP::Typhoeus - end - - assert_equal Zencoder::HTTP.http_backend, Zencoder::HTTP::Typhoeus - end - - should "raise a Zencoder::HTTPError when there is an HTTP error" do - Zencoder::HTTP.http_backend.expects(:get). - with('https://example.com', Zencoder::HTTP.default_options). - at_least_once. - raises(Errno::ECONNREFUSED) - - assert_raises Zencoder::HTTPError do - Zencoder::HTTP.get('https://example.com') - end - - begin - Zencoder::HTTP.get('https://example.com') - rescue Zencoder::HTTPError => e - assert_no_match /perform_method/, e.backtrace.first - end - end - - should "return a Zencoder::Response" do - Zencoder::HTTP.http_backend.stubs(:post).returns(stub(:code => 200, :body => '{"some": "hash"}')) - Zencoder::HTTP.http_backend.stubs(:put).returns(stub(:code => 200, :body => '{"some": "hash"}')) - Zencoder::HTTP.http_backend.stubs(:get).returns(stub(:code => 200, :body => '{"some": "hash"}')) - Zencoder::HTTP.http_backend.stubs(:delete).returns(stub(:code => 200, :body => '{"some": "hash"}')) - - assert Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').is_a?(Zencoder::Response) - assert Zencoder::HTTP.put('https://example.com', '{"some": "hash"}').is_a?(Zencoder::Response) - assert Zencoder::HTTP.get('https://example.com').is_a?(Zencoder::Response) - assert Zencoder::HTTP.delete('https://example.com').is_a?(Zencoder::Response) - end - - should "store the raw response" do - post_stub = stub(:code => 200, :body => '{"some": "hash"}') - Zencoder::HTTP.http_backend.stubs(:post).returns(post_stub) - assert_equal post_stub, Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').raw_response - end - - should "store the raw response body" do - Zencoder::HTTP.http_backend.stubs(:post).returns(stub(:code => 200, :body => '{"some": "hash"}')) - assert_equal '{"some": "hash"}', Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').raw_body - end - - should "store the response code" do - Zencoder::HTTP.http_backend.stubs(:post).returns(stub(:code => 200, :body => '{"some": "hash"}')) - assert_equal 200, Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').code - end - - should "JSON parse the response body" do - Zencoder::HTTP.http_backend.stubs(:put).returns(stub(:code => 200, :body => '{"some": "hash"}')) - assert_equal({'some' => 'hash'}, Zencoder::HTTP.put('https://example.com', '{"some": "hash"}').body) - end - - should "store the raw body if the body fails to be JSON parsed" do - Zencoder::HTTP.http_backend.stubs(:put).returns(stub(:code => 200, :body => '{"some": bad json')) - assert_equal '{"some": bad json', Zencoder::HTTP.put('https://example.com', '{"some": "hash"}').body - end - - context ".post" do - should "call post on the http_backend" do - Zencoder::HTTP.http_backend.expects(:post). - with('https://example.com', Zencoder::HTTP.default_options.merge(:body => '{}')). - returns(Zencoder::Response.new) - - Zencoder::HTTP.post('https://example.com', '{}') - end - end - - context ".put" do - should "call put on the http_backend" do - Zencoder::HTTP.http_backend.expects(:put). - with('https://example.com', Zencoder::HTTP.default_options.merge(:body => '{}')). - returns(Zencoder::Response.new) - - Zencoder::HTTP.put('https://example.com', '{}') - end - end - - context ".get" do - should "call post on the http_backend" do - Zencoder::HTTP.http_backend.expects(:get). - with('https://example.com', Zencoder::HTTP.default_options). - returns(Zencoder::Response.new) - - Zencoder::HTTP.get('https://example.com') - end - end - - context ".delete" do - should "call post on the http_backend" do - Zencoder::HTTP.http_backend.expects(:delete). - with('https://example.com', Zencoder::HTTP.default_options). - returns(Zencoder::Response.new) - - Zencoder::HTTP.delete('https://example.com') - end - end - end - -end diff --git a/test/zencoder/input_test.rb b/test/zencoder/input_test.rb deleted file mode 100644 index efa73bb..0000000 --- a/test/zencoder/input_test.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'test_helper' - -class Zencoder::InputTest < Test::Unit::TestCase - - context Zencoder::Input do - setup do - @api_key = 'abc123' - end - - context ".details" do - setup do - @input_id = 1 - @url = "#{Zencoder.base_url}/inputs/#{@input_id}" - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Input.details(@input_id, :api_key => @api_key).class - end - end - - context ".progress" do - setup do - @input_id = 1 - @url = "#{Zencoder.base_url}/inputs/#{@input_id}/progress" - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Input.progress(@input_id, :api_key => @api_key).class - end - end - end -end diff --git a/test/zencoder/job_test.rb b/test/zencoder/job_test.rb deleted file mode 100644 index f323c83..0000000 --- a/test/zencoder/job_test.rb +++ /dev/null @@ -1,105 +0,0 @@ -require 'test_helper' - -class Zencoder::JobTest < Test::Unit::TestCase - - context Zencoder::Job do - setup do - @api_key = 'abc123' - end - - context ".create" do - setup do - @url = "#{Zencoder.base_url}/jobs" - @params = {:api_key => @api_key, - :input => "s3://bucket-name/file-name.avi"} - @params_as_json = Zencoder::Serializer.encode(@params) - end - - should "POST to the correct url and return a response" do - Zencoder::HTTP.expects(:post).with(@url, @params_as_json, {}).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Job.create(@params).class - end - - should "apply the global API key as a header" do - Zencoder.api_key = 'asdfasdf' - Zencoder::HTTP.expects(:post).with do |url, params, options| - options[:headers]["Zencoder-Api-Key"] == Zencoder.api_key - end.returns(Zencoder::Response.new) - Zencoder::Job.create(:input => @params[:input]) - Zencoder.api_key = nil - end - end - - context ".list" do - setup do - @url = "#{Zencoder.base_url}/jobs" - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, {:params => { :page => 1, - :per_page => 50, - :state => nil}, - :headers => { "Zencoder-Api-Key" => @api_key }}).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Job.list(:api_key => @api_key).class - end - - should "merge params well" do - Zencoder::HTTP.stubs(:get).with(@url, {:params => {:page => 1, - :per_page => 50, - :some => 'param', - :state => nil}, - :headers => { "Zencoder-Api-Key" => @api_key }}).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Job.list(:api_key => @api_key, :params => {:some => 'param'}).class - end - end - - context ".details" do - setup do - @job_id = 1 - @url = "#{Zencoder.base_url}/jobs/#{@job_id}" - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Job.details(1, :api_key => @api_key).class - end - end - - context ".progress" do - setup do - @job_id = 1 - @url = "#{Zencoder.base_url}/jobs/#{@job_id}/progress" - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Job.progress(1, :api_key => @api_key).class - end - end - - context ".resubmit" do - setup do - @job_id = 1 - @url = "#{Zencoder.base_url}/jobs/#{@job_id}/resubmit" - end - - should "PUT the correct url and return a response" do - Zencoder::HTTP.stubs(:put).with(@url, nil, :headers => {"Zencoder-Api-Key" => @api_key}).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Job.resubmit(1, :api_key => @api_key).class - end - end - - context ".cancel" do - setup do - @job_id = 1 - @url = "#{Zencoder.base_url}/jobs/#{@job_id}/cancel" - end - - should "PUT the correct url and return a response" do - Zencoder::HTTP.stubs(:put).with(@url, nil, :headers => {"Zencoder-Api-Key" => @api_key}).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Job.cancel(1, :api_key => @api_key).class - end - end - - end -end diff --git a/test/zencoder/notification_test.rb b/test/zencoder/notification_test.rb deleted file mode 100644 index ebdd830..0000000 --- a/test/zencoder/notification_test.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'test_helper' - -class Zencoder::NotificationTest < Test::Unit::TestCase - - context Zencoder::Notification do - context ".list" do - setup do - @url = "#{Zencoder.base_url}/notifications" - @api_key = 'asdfasdf' - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, {:params => { :page => 1, - :per_page => 50}, - :headers => { "Zencoder-Api-Key" => @api_key }}).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Notification.list(:api_key => @api_key).class - end - end - end - -end diff --git a/test/zencoder/output_test.rb b/test/zencoder/output_test.rb deleted file mode 100644 index 7da0cbc..0000000 --- a/test/zencoder/output_test.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'test_helper' - -class Zencoder::OutputTest < Test::Unit::TestCase - - context Zencoder::Output do - setup do - @api_key = 'abc123' - end - - context ".details" do - setup do - @output_id = 1 - @url = "#{Zencoder.base_url}/outputs/#{@output_id}" - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Output.details(@output_id, :api_key => @api_key).class - end - end - - context ".progress" do - setup do - @output_id = 1 - @url = "#{Zencoder.base_url}/outputs/#{@output_id}/progress" - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Output.progress(@output_id, :api_key => @api_key).class - end - end - end -end diff --git a/test/zencoder/reports_test.rb b/test/zencoder/reports_test.rb deleted file mode 100644 index 991bd86..0000000 --- a/test/zencoder/reports_test.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'test_helper' - -class Zencoder::ReportTest < Test::Unit::TestCase - - context ".minutes" do - setup do - @api_key = "abcd123" - @url = "#{Zencoder.base_url}/reports/minutes" - end - - should "GET the correct url and return a response" do - Zencoder::HTTP.stubs(:get).with(@url, {:params => { :from => "2011-01-01", - :to => "2011-06-01", - :grouping => "foo"}, - :headers => { "Zencoder-Api-Key" => @api_key }}).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Report.minutes(:api_key => @api_key, :params => { :from => "2011-01-01", - :to => "2011-06-01", - :grouping => "foo" }).class - end - - should "merge params well" do - Zencoder::HTTP.stubs(:get).with(@url, {:params => { :from => "2011-01-01", - :to => "2011-06-01"}, - :headers => { "Zencoder-Api-Key" => @api_key }}).returns(Zencoder::Response.new) - assert_equal Zencoder::Response, Zencoder::Report.minutes(:api_key => @api_key, :params => {:from => "2011-01-01", :to => "2011-06-01"}).class - end - end - -end diff --git a/test/zencoder/response_test.rb b/test/zencoder/response_test.rb deleted file mode 100644 index 56e9282..0000000 --- a/test/zencoder/response_test.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'test_helper' - -class Zencoder::ResponseTest < Test::Unit::TestCase - - context Zencoder::Response do - context "#success?" do - should "return true when code is between 200 and 299" do - assert Zencoder::Response.new(:code => 200).success? - assert Zencoder::Response.new(:code => 299).success? - assert Zencoder::Response.new(:code => 250).success? - end - - should "return false when code it less than 200 or greater than 299" do - assert !Zencoder::Response.new(:code => 300).success? - assert !Zencoder::Response.new(:code => 199).success? - end - end - - context "#errors" do - should "return an empty array when body is not a Hash" do - assert_equal [], Zencoder::Response.new(:body => 1).errors - assert_equal [], Zencoder::Response.new(:body => "something").errors - assert_equal [], Zencoder::Response.new(:body => [1]).errors - end - - should "return the value of the key 'errors' as a compacted array when body is a Hash" do - assert_same_elements ['must be awesome'], Zencoder::Response.new(:body => {'errors' => ['must be awesome']}).errors - assert_same_elements ['must be awesome'], Zencoder::Response.new(:body => {'errors' => 'must be awesome'}).errors - assert_same_elements ['must be awesome'], Zencoder::Response.new(:body => {'errors' => ['must be awesome', nil]}).errors - assert_same_elements [], Zencoder::Response.new(:body => {}).errors - end - end - - context "#body_without_wrapper" do - should "return the body when the body is a string" do - assert_equal "some text", Zencoder::Response.new(:body => "some text").body_without_wrapper - end - - should "return the body when the body is not wrapped in api_response and is a hash" do - assert_equal({'some' => 'hash'}, Zencoder::Response.new(:body => {'some' => 'hash'}).body_without_wrapper) - end - - should "return body['api_response'] when body is a hash and body['api_response'] exists" do - assert_equal({'some' => 'hash'}, Zencoder::Response.new(:body => {'api_response' => {'some' => 'hash'}}).body_without_wrapper) - end - end - end - -end diff --git a/test/zencoder/zencoder_test.rb b/test/zencoder/zencoder_test.rb deleted file mode 100644 index 5bf9b8f..0000000 --- a/test/zencoder/zencoder_test.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'test_helper' - -class ZencoderTest < Test::Unit::TestCase - - should "allow user to set an api key" do - Zencoder.api_key = "123" - assert_equal Zencoder::Job.api_key, "123" - end - - should "allow ENV variable to set an api key" do - Zencoder.api_key = nil - ENV['ZENCODER_API_KEY'] = "321" - assert_equal Zencoder::Job.api_key, "321" - end - - should "take user-supplie api key over ENV-supplied key" do - Zencoder.api_key = "123" - ENV['ZENCODER_API_KEY'] = "321" - assert_equal Zencoder::Job.api_key, "123" - end - - should "encode to json" do - assert_match /"api_request"/, Zencoder::Serializer.encode({:api_request => {:input => 'https://example.com'}}) - end - - should "not encode when the content is a String" do - assert_match /^api_request$/, Zencoder::Serializer.encode("api_request") - end - - should "decode from json" do - assert Zencoder::Serializer.decode(%@{"api_request": {"input": "https://example.com"}}@)['api_request']['input'] - end - - should "not decode when content is not a String" do - assert_equal 1, Zencoder::Serializer.decode(1) - end - -end diff --git a/zencoder.gemspec b/zencoder.gemspec index 703fb29..aba19d1 100644 --- a/zencoder.gemspec +++ b/zencoder.gemspec @@ -1,4 +1,3 @@ -# -*- encoding: utf-8 -*- lib = File.expand_path('../lib/', __FILE__) $:.unshift lib unless $:.include?(lib) @@ -15,9 +14,17 @@ Gem::Specification.new do |s| s.description = "Zencoder integration library." s.rubyforge_project = "zencoder" s.add_dependency "multi_json" - s.add_development_dependency "shoulda" - s.add_development_dependency "mocha" - s.add_development_dependency "webmock" + if RUBY_PLATFORM =~ /java/ + s.platform = "java" + s.add_development_dependency "jruby-openssl" + else + s.add_development_dependency "typhoeus" + s.add_development_dependency "byebug" + end + s.add_development_dependency "rspec", "~> 3.8.0" + s.add_development_dependency "webmock", "~> 3.4.2" + s.add_development_dependency "rake" + s.add_development_dependency "test-unit" s.files = Dir.glob("bin/**/*") + Dir.glob("lib/**/*") + %w(LICENSE README.markdown Rakefile) s.require_path = "lib" end