├── .autotest ├── .gitignore ├── .rspec ├── .travis.yml ├── .zenflow ├── Gemfile ├── LICENSE ├── README.markdown ├── Rakefile ├── lib ├── zencoder.rb └── zencoder │ ├── account.rb │ ├── errors.rb │ ├── http.rb │ ├── http │ ├── net_http.rb │ └── typhoeus.rb │ ├── input.rb │ ├── job.rb │ ├── notification.rb │ ├── output.rb │ ├── report.rb │ ├── resource.rb │ ├── response.rb │ ├── serializer.rb │ ├── version.rb │ └── zencoder.rb ├── spec ├── spec_helper.rb └── zencoder │ ├── account_spec.rb │ ├── http │ ├── net_http_spec.rb │ └── typhoeus_spec.rb │ ├── http_spec.rb │ ├── input_spec.rb │ ├── job_spec.rb │ ├── notification_spec.rb │ ├── output_spec.rb │ ├── report_spec.rb │ ├── response_spec.rb │ └── zencoder_spec.rb └── zencoder.gemspec /.autotest: -------------------------------------------------------------------------------- 1 | Autotest.add_hook :initialize do |at| 2 | at.clear_mappings 3 | 4 | # Run any test that changes 5 | at.add_mapping(%r{^test/.*_test\.rb$}) do |f, _| 6 | [f] 7 | end 8 | 9 | # Run tests for any file that changes in lib 10 | at.add_mapping(%r{^lib((/[^/]+)+)\.rb$}) do |_, m| 11 | ["test#{m[1]}_test.rb"] 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | coverage 3 | rdoc 4 | Gemfile.lock 5 | .bundle 6 | /zencoder*.gem 7 | /tmp 8 | /.rbx 9 | .zenflow-log 10 | .ruby-gemset 11 | .ruby-version 12 | /.byebug-history 13 | /.tool-versions 14 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.5 4 | - 2.4 5 | - 2.3 6 | - 2.2 7 | - jruby 8 | -------------------------------------------------------------------------------- /.zenflow: -------------------------------------------------------------------------------- 1 | --- 2 | project: zencoder-rb 3 | development_branch: master 4 | staging_branch: false 5 | qa_branch: false 6 | release_branch: false 7 | merge_strategy: merge 8 | remote: origin 9 | backup_remote: false 10 | confirm_staging: false 11 | confirm_review: true 12 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 Zencoder 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # Zencoder 2 | 3 | [![Build Status](https://travis-ci.org/zencoder/zencoder-rb.svg?branch=master)](https://travis-ci.org/zencoder/zencoder-rb) 4 | 5 | The gem for interacting with the API on [Zencoder](http://zencoder.com). 6 | 7 | See [https://support.brightcove.com/zencoder](https://support.brightcove.com/zencoder) for more details on the API. 8 | 9 | Tested on the following versions of Ruby: 10 | 11 | * Ruby 2.2 12 | * Ruby 2.3 13 | * Ruby 2.4 14 | * Ruby 2.5 15 | * Latest jRuby 16 | 17 | ## 2.5 WARNING!!! 18 | 19 | Version 2.5 brings a single, significant change to the gem which you should be aware of: 20 | 21 | * __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. 22 | 23 | [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/) 24 | 25 | ## v2.4 WARNING!!! 26 | 27 | Version 2.4 brings some significant changes to the gem, ones which you should be aware of: 28 | 29 | * __Removed dependency on `activesupport`.__ This means that the keys of your hashes should all be symbols except in the case of HTTP headers. 30 | * __Added dependency on `multi_json`__. This gem allows encoding and decoding to and from JSON without all the baggage of `activesupport`. 31 | * __Removed support for XML requests.__ Since we're doing all the encoding and decoding in the gem, it didn't make sense to support it any longer. You could still conceivably do this with the gem, but you'd need to encode and decode to and from XML yourself and pass appropriate headers. Let us know if this is a problem for you. 32 | * __Using header authentication by default.__ Zencoder has always allowed the passing of the API key as an HTTP header (`Zencoder-Api-Key`), but in this library we've traditionally merged it in with your requests. In at least one case this would result in messy deserialization and serialization of parameters. Using this alternative authentication method clears up this problem. 33 | * __Some actions only work on future versions of the API.__ See the section titled `APIv2` below. 34 | * __Now defaults to API v2.__ If you'd like to continue using API v1, you should change the base_url as outlined in the section titled `APIv2` below. 35 | * __The Zencoder SSL CA chain is now bundled.__ Previously when you used the default HTTP backend (Net::HTTP), we would try to detect the CA path on your system and use it. This led to some frustration for some users and was generally unreliable. We now bundle our SSL CA chain in the library which should make integration easier. Please note that if you were using `Zencoder::HTTP::NetHTTP.root_cert_paths` or `Zencoder::HTTP::NetHTTP.skip_setting_root_cert_path`, they have been removed. 36 | 37 | ## APIv2 38 | 39 | With the release of version two of the Zencoder API, there are some new methods available to you. 40 | 41 | * Zencoder::Job.progress(job\_id) 42 | * Zencoder::Input.details(input\_id) 43 | * Zencoder::Input.progress(input\_id) 44 | * Zencoder::Output.details(output\_id) 45 | * Zencoder::Report.minutes(:from => "2011-01-01", :to => "2011-03-01") 46 | * Zencoder::Report.all(:from => "2011-01-01", :to => "2011-03-01") 47 | * Zencoder::Report.live(:from => "2011-01-01", :to => "2011-03-01") 48 | * Zencoder::Report.vod(:from => "2011-01-01", :to => "2011-03-01") 49 | 50 | 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. 51 | 52 | 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: 53 | 54 | ```ruby 55 | Zencoder.base_url = "https://app.zencoder.com/api/v1" 56 | ``` 57 | 58 | ## Getting Started 59 | 60 | To install the gem on Rails, simply add it to your Gemfile: 61 | 62 | ```ruby 63 | gem "zencoder", "~> 2.0" 64 | ``` 65 | 66 | 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: 67 | 68 | ```ruby 69 | Zencoder.api_key = 'abcd1234' 70 | ``` 71 | 72 | Alternatively, you can use an environment variable: 73 | 74 | ```ruby 75 | ENV['ZENCODER_API_KEY'] = 'abcd1234' 76 | ``` 77 | 78 | You can also pass your API key in every request, but who wants to do that? 79 | 80 | ## Responses 81 | 82 | All calls in the Zencoder library either raise Zencoder::HTTPError or return a Zencoder::Response. 83 | 84 | A Zencoder::Response can be used as follows: 85 | 86 | ```ruby 87 | response = Zencoder::Job.list 88 | response.success? # => true if the response code was 200 through 299 89 | response.code # => 200 90 | response.body # => the JSON-parsed body or raw body if unparseable 91 | response.raw_body # => the body pre-JSON-parsing 92 | response.raw_response # => the raw Net::HTTP or Typhoeus response (see below for how to use Typhoeus) 93 | response.request # => the request object, you can inspect it if you need details on the request to debug it 94 | ``` 95 | 96 | ### Parameters 97 | 98 | When sending API request parameters you can specify them as a non-string object, which we'll then serialize to JSON: 99 | 100 | ```ruby 101 | Zencoder::Job.create({:input => 's3://bucket/key.mp4'}) 102 | ``` 103 | 104 | Or you can specify them as a string, which we'll just pass along as the request body: 105 | 106 | ```ruby 107 | Zencoder::Job.create('{"input": "s3://bucket/key.mp4"}') 108 | ``` 109 | 110 | ## Jobs 111 | 112 | There's more you can do on jobs than anything else in the API. The following methods are available: `list`, `create`, `details`, `resubmit`, `cancel`, `delete`. 113 | 114 | ### create 115 | 116 | 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. 117 | 118 | ```ruby 119 | Zencoder::Job.create({:input => 's3://bucket/key.mp4'}) 120 | Zencoder::Job.create({:input => 's3://bucket/key.mp4', 121 | :outputs => [{:label => 'vp8 for the web', 122 | :url => 's3://bucket/key_output.webm'}]}) 123 | Zencoder::Job.create({:input => 's3://bucket/key.mp4', :api_key => 'abcd1234'}) 124 | ``` 125 | 126 | This returns a Zencoder::Response object. The body includes a Job ID, and one or more Output IDs (one for every output file created). 127 | 128 | ```ruby 129 | response = Zencoder::Job.create({:input => 's3://bucket/key.mp4'}) 130 | response.code # => 201 131 | response.body['id'] # => 12345 132 | ``` 133 | 134 | ### list 135 | 136 | By default the jobs listing is paginated with 50 jobs per page and sorted by ID in descending order. You can pass two parameters to control the paging: `page` and `per_page`. 137 | 138 | ```ruby 139 | Zencoder::Job.list 140 | Zencoder::Job.list(:per_page => 10) 141 | Zencoder::Job.list(:per_page => 10, :page => 2) 142 | Zencoder::Job.list(:per_page => 10, :page => 2, :api_key => 'abcd1234') 143 | ``` 144 | 145 | ### details 146 | 147 | The number passed to `details` is the ID of a Zencoder job. 148 | 149 | ```ruby 150 | Zencoder::Job.details(1) 151 | Zencoder::Job.details(1, :api_key => 'abcd1234') 152 | ``` 153 | 154 | ### progress 155 | 156 | The number passed to `progress` is the ID of a Zencoder job. 157 | 158 | ```ruby 159 | Zencoder::Job.progress(1) 160 | Zencoder::Job.progress(1, :api_key => 'abcd1234') 161 | ``` 162 | 163 | ### resubmit 164 | 165 | The number passed to `resubmit` is the ID of a Zencoder job. 166 | 167 | ```ruby 168 | Zencoder::Job.resubmit(1) 169 | Zencoder::Job.resubmit(1, :api_key => 'abcd1234') 170 | ``` 171 | 172 | ### cancel 173 | 174 | The number passed to `cancel` is the ID of a Zencoder job. 175 | 176 | ```ruby 177 | Zencoder::Job.cancel(1) 178 | Zencoder::Job.cancel(1, :api_key => 'abcd1234') 179 | ``` 180 | 181 | ### delete 182 | 183 | The number passed to `delete` is the ID of a Zencoder job. 184 | 185 | ```ruby 186 | Zencoder::Job.delete(1) 187 | Zencoder::Job.delete(1, :api_key => 'abcd1234') 188 | ``` 189 | 190 | ## Inputs 191 | 192 | ### details 193 | 194 | The number passed to `details` is the ID of a Zencoder input. 195 | 196 | ```ruby 197 | Zencoder::Input.details(1) 198 | Zencoder::Input.details(1, :api_key => 'abcd1234') 199 | ``` 200 | 201 | ### progress 202 | 203 | The number passed to `progress` is the ID of a Zencoder input. 204 | 205 | ```ruby 206 | Zencoder::Input.progress(1) 207 | Zencoder::Input.progress(1, :api_key => 'abcd1234') 208 | ``` 209 | 210 | ## Outputs 211 | 212 | ### details 213 | 214 | The number passed to `details` is the ID of a Zencoder output. 215 | 216 | ```ruby 217 | Zencoder::Output.details(1) 218 | Zencoder::Output.details(1, :api_key => 'abcd1234') 219 | ``` 220 | 221 | ### progress 222 | 223 | *Important:* the number passed to `progress` is the output file ID, not the Job ID. 224 | 225 | ```ruby 226 | Zencoder::Output.progress(1) 227 | Zencoder::Output.progress(1, :api_key => 'abcd1234') 228 | ``` 229 | 230 | ## Notifications 231 | 232 | ### list 233 | 234 | By default the jobs listing is paginated with 50 jobs per page and sorted by ID in descending order. You can pass three parameters to control the paging: `page`, `per_page`, and `since_id`. Passing `since_id` will return notifications for jobs created after the job with the given ID. 235 | 236 | ```ruby 237 | Zencoder::Notification.list 238 | Zencoder::Notification.list(:per_page => 10) 239 | Zencoder::Notification.list(:per_page => 10, :page => 2) 240 | Zencoder::Notification.list(:per_page => 10, :page => 2, :since_id => 20) 241 | Zencoder::Notification.list(:api_key => 'abcd1234') 242 | ``` 243 | 244 | ## Accounts 245 | 246 | ### create 247 | 248 | 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. 249 | 250 | ```ruby 251 | Zencoder::Account.create({:terms_of_service => 1, 252 | :email => 'bob@example.com'}) 253 | Zencoder::Account.create({:terms_of_service => 1, 254 | :email => 'bob@example.com', 255 | :password => 'abcd1234', 256 | :password_confirmation => 'abcd1234', 257 | :affiliate_code => 'abcd1234'}) 258 | ``` 259 | 260 | ### details 261 | 262 | ```ruby 263 | Zencoder::Account.details 264 | Zencoder::Account.details(:api_key => 'abcd1234') 265 | ``` 266 | 267 | ### integration 268 | 269 | This will put your account into integration mode (site-wide). 270 | 271 | ```ruby 272 | Zencoder::Account.integration 273 | Zencoder::Account.integration(:api_key => 'abcd1234') 274 | ``` 275 | 276 | ### live 277 | 278 | This will put your account into live mode (site-wide). 279 | 280 | ```ruby 281 | Zencoder::Account.live 282 | Zencoder::Account.live(:api_key => 'abcd1234') 283 | ``` 284 | 285 | ## Reports 286 | 287 | ### minutes 288 | 289 | This will list the minutes used for your account within a certain, configurable range. 290 | 291 | ```ruby 292 | Zencoder::Report.minutes(:from => "2011-10-30", :to => "2011-11-24") 293 | ``` 294 | 295 | ### all 296 | 297 | This will list all usage, including VOD and Live for your account within a certain, configurable range. 298 | 299 | ```ruby 300 | Zencoder::Report.all(:from => "2011-10-30", :to => "2011-11-24") 301 | ``` 302 | 303 | ### vod 304 | 305 | This will list just VOD usage for your account within a certain, configurable range. 306 | 307 | ```ruby 308 | Zencoder::Report.vod(:from => "2011-10-30", :to => "2011-11-24") 309 | ``` 310 | 311 | ### live 312 | 313 | This will list just Live usage for your account within a certain, configurable range. 314 | 315 | ```ruby 316 | Zencoder::Report.live(:from => "2011-10-30", :to => "2011-11-24") 317 | ``` 318 | 319 | ## Advanced HTTP 320 | 321 | ### Alternate HTTP Libraries 322 | 323 | By default this library will use Net::HTTP to make all API calls. You can change the backend or add your own: 324 | 325 | ```ruby 326 | require 'typhoeus' 327 | Zencoder::HTTP.http_backend = Zencoder::HTTP::Typhoeus 328 | 329 | require 'my_favorite_http_library' 330 | Zencoder::HTTP.http_backend = MyFavoriteHTTPBackend 331 | ``` 332 | 333 | See the HTTP backends in this library to get started on your own. 334 | 335 | ### Advanced Options 336 | 337 | A secondary options hash can be passed to any method call which will then be passed on to the HTTP backend. You can pass `timeout` (in milliseconds), `headers`, and `params` (will be added to the query string) to any of the backends. If you are using Typhoeus, see their documentation for further options. In the following example the timeout is set to one second: 338 | 339 | ```ruby 340 | Zencoder::Job.create({:input => 's3://bucket/key.mp4'}, {:timeout => 1000}) 341 | ``` 342 | 343 | 344 | ### SSL Verification 345 | 346 | 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. 347 | 348 | ```ruby 349 | Zencoder::Job.create({:input => 's3://bucket/key.mp4'}, {:ca_path => "/path/to/certs/"}) 350 | # or 351 | Zencoder::Job.create({:input => 's3://bucket/key.mp4'}, {:ca_file => "/path/to/certs/zen.crt"}) 352 | ``` 353 | 354 | Alternatively you can add it to the default options. 355 | 356 | ```ruby 357 | Zencoder::HTTP.default_options.merge!(:ca_path => "/path/to/certs/") 358 | # or 359 | Zencoder::HTTP.default_options.merge!(:ca_file => "/path/to/certs/zen.crt") 360 | ``` 361 | 362 | 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. 363 | 364 | However, if you'd like to skip SSL verification you can pass an option in the secondary options hash. 365 | 366 | **NOTE: WE HIGHLY DISCOURAGE THIS! THIS WILL LEAVE YOU VULNERABLE TO MAN-IN-THE-MIDDLE ATTACKS!** 367 | 368 | ```ruby 369 | Zencoder::Job.create({:input => 's3://bucket/key.mp4'}, {:skip_ssl_verify => true}) 370 | ``` 371 | 372 | Alternatively you can add it to the default options. 373 | 374 | ```ruby 375 | Zencoder::HTTP.default_options.merge!(:skip_ssl_verify => true) 376 | ``` 377 | 378 | ### Default Options 379 | 380 | Default options are passed to the HTTP backend. These can be retrieved and modified. 381 | 382 | ```ruby 383 | Zencoder::HTTP.default_options = {:timeout => 3000, 384 | :headers => {'Accept' => 'application/json', 385 | 'Content-Type' => 'application/json'}} 386 | ``` 387 | 388 | ## Advanced JSON 389 | 390 | ### Alternate JSON Libraries 391 | 392 | This library uses the `multi_json` gem to encode and decode JSON. This fantastic gem lets you swap out the JSON backend at will and includes a working JSON encoder/decoder. You can check the [MultiJson](https://github.com/intridea/multi_json) project for more information on how to accomplish this. 393 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | 3 | begin 4 | require 'rspec/core/rake_task' 5 | RSpec::Core::RakeTask.new(:spec) 6 | task :default => :spec 7 | rescue LoadError 8 | end 9 | -------------------------------------------------------------------------------- /lib/zencoder.rb: -------------------------------------------------------------------------------- 1 | # Standard Library 2 | require 'cgi' 3 | require 'net/https' 4 | require 'timeout' 5 | 6 | # JSON serialization 7 | require 'multi_json' 8 | 9 | # Zencoder 10 | require 'zencoder/version' 11 | require 'zencoder/serializer' 12 | require 'zencoder/zencoder' 13 | require 'zencoder/resource' 14 | require 'zencoder/http/net_http' 15 | require 'zencoder/http/typhoeus' 16 | require 'zencoder/http' 17 | require 'zencoder/errors' 18 | require 'zencoder/report' 19 | require 'zencoder/job' 20 | require 'zencoder/input' 21 | require 'zencoder/output' 22 | require 'zencoder/account' 23 | require 'zencoder/notification' 24 | require 'zencoder/response' 25 | require 'zencoder/version' 26 | -------------------------------------------------------------------------------- /lib/zencoder/account.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class Account < Resource 3 | 4 | def self.create(params={}, options={}) 5 | post("/account", params, options) 6 | end 7 | 8 | def self.details(options={}) 9 | get("/account", options) 10 | end 11 | 12 | def self.integration(options={}) 13 | put("/account/integration", nil, options) 14 | end 15 | 16 | def self.live(options={}) 17 | put("/account/live", nil, options) 18 | end 19 | 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /lib/zencoder/errors.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class Error < StandardError 3 | 4 | def initialize(error_or_message) 5 | if error_or_message.is_a?(Exception) 6 | @error = error_or_message 7 | else 8 | @message = error_or_message 9 | end 10 | end 11 | 12 | def message 13 | @message || "#{@error.class} (wrapped in a #{self.class}) - #{@error.message}" 14 | end 15 | 16 | def backtrace 17 | if @error 18 | error_backtrace = ["--- Backtrace from #{@error.class} ---"] + (@error.backtrace || []) 19 | wrapper_backtrace = ["--- Backtrace from #{self.class} ---"] + (super || []) 20 | error_backtrace + wrapper_backtrace 21 | else 22 | super 23 | end 24 | end 25 | 26 | def inspect 27 | if @error 28 | "#{@error.inspect} (wrapped in a #{self.class})" 29 | else 30 | super 31 | end 32 | end 33 | 34 | def to_s 35 | if @error 36 | "#{@error.class} (wrapped in a #{self.class}) - #{@error}" 37 | else 38 | super 39 | end 40 | end 41 | end 42 | 43 | class HTTPError < Error; end 44 | 45 | end 46 | -------------------------------------------------------------------------------- /lib/zencoder/http.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class HTTP 3 | 4 | include Zencoder::Serializer 5 | 6 | attr_accessor :url, :options, :method 7 | 8 | class << self 9 | attr_accessor :default_options, :http_backend 10 | end 11 | 12 | self.http_backend = NetHTTP 13 | 14 | self.default_options = {:timeout => 60000, 15 | :headers => {'Accept' => 'application/json', 16 | 'Content-Type' => 'application/json', 17 | 'User-Agent' => "Zencoder-rb v#{Zencoder::GEM_VERSION}"}} 18 | 19 | def initialize(method, url, options={}) 20 | self.method = method 21 | self.url = url 22 | self.options = options 23 | end 24 | 25 | def self.post(url, body, options={}) 26 | new(:post, url, options.merge(:body => body)).perform_method 27 | end 28 | 29 | def self.put(url, body, options={}) 30 | new(:put, url, options.merge(:body => body)).perform_method 31 | end 32 | 33 | def self.get(url, options={}) 34 | new(:get, url, options).perform_method 35 | end 36 | 37 | def self.delete(url, options={}) 38 | new(:delete, url, options).perform_method 39 | end 40 | 41 | def perform_method 42 | process(http_backend.send(method, url, options)) 43 | rescue StandardError => e 44 | raise HTTPError.new(e) 45 | end 46 | 47 | def options=(value) 48 | value ||= {} 49 | 50 | # Hacky, deeper hash merge 51 | default_options.keys.each do |key| 52 | if value.has_key?(key) 53 | if value[key].is_a?(Hash) && default_options[key].is_a?(Hash) 54 | value[key] = default_options[key].merge(value[key]) 55 | end 56 | else 57 | value[key] = default_options[key] 58 | end 59 | end 60 | 61 | @options = value 62 | end 63 | 64 | def options 65 | @options || self.options = default_options 66 | end 67 | 68 | def http_backend 69 | self.class.http_backend 70 | end 71 | 72 | def default_options 73 | self.class.default_options 74 | end 75 | 76 | def inspect 77 | "#{method.to_s.upcase} #{url}\nOptions: " + options.inspect 78 | end 79 | 80 | protected 81 | 82 | def process(http_response) 83 | response = Response.new 84 | response.request = self 85 | response.code = http_response.code 86 | 87 | begin 88 | response.body = decode(http_response.body.to_s) 89 | rescue StandardError # Hack! Returns different exceptions depending on the decoding engine 90 | response.body = http_response.body 91 | end 92 | 93 | response.raw_body = http_response.body 94 | response.raw_response = http_response 95 | response 96 | end 97 | 98 | end 99 | end 100 | -------------------------------------------------------------------------------- /lib/zencoder/http/net_http.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class HTTP 3 | class NetHTTP 4 | 5 | attr_accessor :method, :url, :uri, :body, :params, :headers, :timeout, :skip_ssl_verify, :options, :ca_file, :ca_path 6 | 7 | def initialize(method, url, options) 8 | @method = method 9 | @url = url 10 | @options = options.dup 11 | @body = @options.delete(:body) 12 | @params = @options.delete(:params) 13 | @headers = @options.delete(:headers) 14 | @timeout = @options.delete(:timeout) 15 | @skip_ssl_verify = @options.delete(:skip_ssl_verify) 16 | @ca_file = @options.delete(:ca_file) 17 | @ca_path = @options.delete(:ca_path) 18 | end 19 | 20 | def self.post(url, options={}) 21 | new(:post, url, options).perform 22 | end 23 | 24 | def self.put(url, options={}) 25 | new(:put, url, options).perform 26 | end 27 | 28 | def self.get(url, options={}) 29 | new(:get, url, options).perform 30 | end 31 | 32 | def self.delete(url, options={}) 33 | new(:delete, url, options).perform 34 | end 35 | 36 | def perform 37 | deliver(http, request) 38 | end 39 | 40 | 41 | protected 42 | 43 | def deliver(http, request) 44 | if timeout 45 | Timeout.timeout(timeout / 1000.0) do 46 | http.request(request) 47 | end 48 | else 49 | http.request(request) 50 | end 51 | end 52 | 53 | def http 54 | u = uri 55 | 56 | http = Net::HTTP.new(u.host, u.port) 57 | 58 | if u.scheme == 'https' 59 | http.use_ssl = true 60 | 61 | if skip_ssl_verify 62 | http.verify_mode = OpenSSL::SSL::VERIFY_NONE 63 | else 64 | http.verify_mode = OpenSSL::SSL::VERIFY_PEER 65 | 66 | http.cert_store = OpenSSL::X509::Store.new 67 | http.cert_store.set_default_paths 68 | 69 | if defined?(OpenSSL::X509::V_FLAG_CRL_CHECK_ALL) 70 | http.cert_store.flags = OpenSSL::X509::V_FLAG_CRL_CHECK_ALL 71 | end 72 | 73 | http.cert_store.add_file(ca_file) if ca_file 74 | http.cert_store.add_path(ca_path) if ca_path 75 | end 76 | end 77 | 78 | http 79 | end 80 | 81 | def request 82 | r = request_class.new(path) 83 | if body 84 | r.body = body 85 | elsif [:post, :put].include?(@method) 86 | r.body = "" 87 | end 88 | 89 | if headers 90 | headers.each do |header, value| 91 | r.add_field(header.to_s, value.to_s) 92 | end 93 | end 94 | r 95 | end 96 | 97 | def uri 98 | u = URI.parse(url) 99 | 100 | if params 101 | params_as_query = params.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&') 102 | if u.query.to_s.empty? 103 | u.query = params_as_query 104 | else 105 | u.query = [u.query.to_s, params_as_query].join('&') 106 | end 107 | end 108 | 109 | u 110 | end 111 | 112 | def path 113 | u = uri 114 | 115 | if u.path.empty? 116 | u.path = '/' 117 | end 118 | 119 | if u.query.to_s.empty? 120 | u.path 121 | else 122 | u.path + '?' + u.query.to_s 123 | end 124 | end 125 | 126 | def request_class 127 | Net::HTTP.const_get(method.to_s.capitalize) 128 | end 129 | 130 | end 131 | end 132 | end 133 | -------------------------------------------------------------------------------- /lib/zencoder/http/typhoeus.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class HTTP 3 | class Typhoeus 4 | 5 | def self.post(url, options={}) 6 | perform(:post, url, options) 7 | end 8 | 9 | def self.put(url, options={}) 10 | perform(:put, url, options) 11 | end 12 | 13 | def self.get(url, options={}) 14 | perform(:get, url, options) 15 | end 16 | 17 | def self.delete(url, options={}) 18 | perform(:delete, url, options) 19 | end 20 | 21 | def self.perform(method, url, options={}) 22 | options = options.dup 23 | if options.delete(:skip_ssl_verify) 24 | options[:disable_ssl_peer_verification] = true 25 | end 26 | 27 | if ca_file = options.delete(:ca_file) 28 | options[:sslcert] = ca_file 29 | end 30 | 31 | if ca_path = options.delete(:ca_path) 32 | options[:capath] = ca_path 33 | end 34 | 35 | ::Typhoeus::Request.send(method, url, options) 36 | end 37 | 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/zencoder/input.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class Input < Resource 3 | 4 | def self.details(input_id, options={}) 5 | get("/inputs/#{input_id}", options) 6 | end 7 | 8 | def self.progress(input_id, options={}) 9 | get("/inputs/#{input_id}/progress", options) 10 | end 11 | 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/zencoder/job.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class Job < Resource 3 | 4 | def self.create(params={}, options={}) 5 | post("/jobs", params, options) 6 | end 7 | 8 | def self.list(options={}) 9 | options = options.dup 10 | params = { :page => options.delete(:page) || 1, 11 | :per_page => options.delete(:per_page) || 50, 12 | :state => options.delete(:state) } 13 | 14 | get("/jobs", merge_params(options, params)) 15 | end 16 | 17 | def self.details(job_id, options={}) 18 | get("/jobs/#{job_id}", options) 19 | end 20 | 21 | def self.progress(job_id, options={}) 22 | get("/jobs/#{job_id}/progress", options) 23 | end 24 | 25 | def self.resubmit(job_id, options={}) 26 | put("/jobs/#{job_id}/resubmit", nil, options) 27 | end 28 | 29 | def self.cancel(job_id, options={}) 30 | put("/jobs/#{job_id}/cancel", nil, options) 31 | end 32 | 33 | def self.finish(job_id, options={}) 34 | put("/jobs/#{job_id}/finish", nil, options) 35 | end 36 | 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/zencoder/notification.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class Notification < Resource 3 | 4 | def self.list(options={}) 5 | options = options.dup 6 | params = {:page => options.delete(:page) || 1, 7 | :per_page => options.delete(:per_page) || 50 } 8 | 9 | get("/notifications", merge_params(options, params)) 10 | end 11 | 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/zencoder/output.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class Output < Resource 3 | 4 | def self.details(output_id, options={}) 5 | get("/outputs/#{output_id}", options) 6 | end 7 | 8 | def self.progress(output_id, options={}) 9 | get("/outputs/#{output_id}/progress", options) 10 | end 11 | 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/zencoder/report.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class Report < Resource 3 | 4 | def self.minutes(options={}) 5 | get("/reports/minutes", merge_params(*extract_params(options))) 6 | end 7 | 8 | def self.all(options={}) 9 | get("/reports/all", merge_params(*extract_params(options))) 10 | end 11 | 12 | def self.live(options={}) 13 | get("/reports/live", merge_params(*extract_params(options))) 14 | end 15 | 16 | def self.vod(options={}) 17 | get("/reports/vod", merge_params(*extract_params(options))) 18 | end 19 | 20 | 21 | protected 22 | 23 | def self.extract_params(options={}) 24 | options = options.dup 25 | params = { 26 | :from => options.delete(:from), 27 | :to => options.delete(:to), 28 | :grouping => options.delete(:grouping) 29 | } 30 | 31 | return options, params.delete_if { |k, v| v.nil? } 32 | end 33 | 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/zencoder/resource.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class Resource 3 | 4 | include Zencoder::Serializer 5 | 6 | def self.api_key 7 | Zencoder.api_key 8 | end 9 | 10 | def self.base_url 11 | Zencoder.base_url 12 | end 13 | 14 | def self.post(path, params={}, options={}) 15 | options = options.dup 16 | url = url_for(path, options) 17 | body = encode(params) 18 | options = add_api_key_header(options) 19 | HTTP.post(url, body, options) 20 | end 21 | 22 | def self.put(path, params={}, options={}) 23 | options = options.dup 24 | url = url_for(path, options) 25 | body = encode(params) 26 | options = add_api_key_header(options) 27 | HTTP.put(url, body, options) 28 | end 29 | 30 | def self.get(path, options={}) 31 | options = options.dup 32 | url = url_for(path, options) 33 | options = add_api_key_header(options) 34 | HTTP.get(url, options) 35 | end 36 | 37 | def self.delete(path, options={}) 38 | options = options.dup 39 | url = url_for(path) 40 | options = add_api_key_header(options) 41 | HTTP.delete(url, options) 42 | end 43 | 44 | 45 | protected 46 | 47 | def self.url_for(path, options={}) 48 | File.join((options[:base_url] || base_url).to_s, path.to_s) 49 | end 50 | 51 | def self.add_api_key_header(options) 52 | effective_api_key = options.delete(:api_key) || api_key 53 | 54 | if effective_api_key 55 | if options[:headers] 56 | options[:headers] = options[:headers].dup 57 | else 58 | options[:headers] = {} 59 | end 60 | options[:headers]["Zencoder-Api-Key"] = effective_api_key 61 | end 62 | 63 | options 64 | end 65 | 66 | def self.merge_params(options, params) 67 | if options[:params] 68 | options[:params] = options[:params].merge(params) 69 | options 70 | else 71 | options.merge(:params => params) 72 | end 73 | end 74 | 75 | end 76 | end 77 | -------------------------------------------------------------------------------- /lib/zencoder/response.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | class Response 3 | 4 | attr_accessor :request, :code, :body, :raw_body, :raw_response 5 | 6 | def initialize(options={}) 7 | options.each do |k, v| 8 | send("#{k}=", v) if respond_to?("#{k}=") 9 | end 10 | end 11 | 12 | def success? 13 | code.to_i > 199 && code.to_i < 300 14 | end 15 | 16 | def errors 17 | if body.is_a?(Hash) 18 | Array(body['errors']).compact 19 | else 20 | [] 21 | end 22 | end 23 | 24 | def body_without_wrapper 25 | if body.is_a?(Hash) && body['api_response'] 26 | body['api_response'] 27 | else 28 | body 29 | end 30 | end 31 | 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /lib/zencoder/serializer.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | module Serializer 3 | 4 | extend self 5 | 6 | def self.included(klass) 7 | klass.extend(self) 8 | end 9 | 10 | def encode(content) 11 | if content.is_a?(String) || content.nil? 12 | content 13 | else 14 | if MultiJson.respond_to?(:dump) 15 | MultiJson.dump(content) 16 | else 17 | MultiJson.encode(content) 18 | end 19 | end 20 | end 21 | 22 | def decode(content) 23 | if content.is_a?(String) 24 | if MultiJson.respond_to?(:dump) 25 | MultiJson.load(content) 26 | else 27 | MultiJson.decode(content) 28 | end 29 | else 30 | content 31 | end 32 | end 33 | 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/zencoder/version.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | GEM_VERSION = '2.5.1' 3 | end 4 | -------------------------------------------------------------------------------- /lib/zencoder/zencoder.rb: -------------------------------------------------------------------------------- 1 | module Zencoder 2 | 3 | class << self 4 | attr_accessor :api_key, :base_url 5 | end 6 | 7 | self.api_key = nil 8 | self.base_url = 'https://app.zencoder.com/api/v2' 9 | 10 | def self.api_key 11 | @api_key || ENV['ZENCODER_API_KEY'] 12 | end 13 | 14 | def self.base_url(env=nil) 15 | @base_url 16 | end 17 | 18 | end 19 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require "rubygems" 2 | require "bundler" 3 | Bundler.setup 4 | Bundler.require(:default, :test, :development) 5 | require 'test/unit/assertions' 6 | require 'webmock/rspec' 7 | 8 | RSpec.configure do |config| 9 | config.include Test::Unit::Assertions 10 | 11 | def assert_same_elements(a1, a2, msg = nil) 12 | [:select, :inject, :size].each do |m| 13 | [a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") } 14 | end 15 | 16 | assert a1h = a1.inject({}) { |h,e| h[e] ||= a1.select { |i| i == e }.size; h } 17 | assert a2h = a2.inject({}) { |h,e| h[e] ||= a2.select { |i| i == e }.size; h } 18 | 19 | assert_equal(a1h, a2h, msg) 20 | end 21 | 22 | config.after(:example) do 23 | ENV["ZENCODER_API_KEY"] = nil 24 | Zencoder.api_key = nil 25 | end 26 | 27 | config.expect_with :rspec do |expectations| 28 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 29 | end 30 | 31 | config.mock_with :rspec do |mocks| 32 | mocks.verify_partial_doubles = true 33 | end 34 | 35 | config.shared_context_metadata_behavior = :apply_to_host_groups 36 | 37 | config.filter_run_when_matching :focus 38 | 39 | # config.example_status_persistence_file_path = "spec/examples.txt" 40 | 41 | config.disable_monkey_patching! 42 | 43 | config.warnings = true 44 | 45 | if config.files_to_run.one? 46 | config.default_formatter = "doc" 47 | end 48 | 49 | # config.profile_examples = 10 50 | 51 | config.order = :random 52 | 53 | Kernel.srand config.seed 54 | end 55 | -------------------------------------------------------------------------------- /spec/zencoder/account_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder::Account do 4 | before do 5 | @api_key = 'abc123' 6 | end 7 | 8 | describe ".create" do 9 | before do 10 | @url = "#{Zencoder.base_url}/account" 11 | @params = {:api_key => @api_key} 12 | @params_as_json = Zencoder::Serializer.encode(@params) 13 | end 14 | 15 | it "POSTs to the correct url and return a response" do 16 | expect(Zencoder::HTTP).to receive(:post).with(@url, @params_as_json, {}).and_return(Zencoder::Response.new) 17 | assert_equal Zencoder::Response, Zencoder::Account.create(@params).class 18 | end 19 | end 20 | 21 | describe ".details" do 22 | before do 23 | @url = "#{Zencoder.base_url}/account" 24 | end 25 | 26 | it "GETs the correct url and return a response" do 27 | expect(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) 28 | assert_equal Zencoder::Response, Zencoder::Account.details(:api_key => @api_key).class 29 | end 30 | end 31 | 32 | describe ".integration" do 33 | before do 34 | @url = "#{Zencoder.base_url}/account/integration" 35 | end 36 | 37 | it "PUTs the correct url and return a response" do 38 | expect(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) 39 | assert_equal Zencoder::Response, Zencoder::Account.integration(:api_key => @api_key).class 40 | end 41 | end 42 | 43 | describe ".live" do 44 | before do 45 | @url = "#{Zencoder.base_url}/account/live" 46 | end 47 | 48 | it "PUTs the correct url and return a response" do 49 | expect(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) 50 | assert_equal Zencoder::Response, Zencoder::Account.live(:api_key => @api_key).class 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /spec/zencoder/http/net_http_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder::HTTP::NetHTTP do 4 | 5 | describe "call options" do 6 | it "requests with timeout" do 7 | stub_request(:post, "https://example.com") 8 | expect(Timeout).to receive(:timeout).with(0.001) 9 | Zencoder::HTTP::NetHTTP.post('https://example.com', :timeout => 1) 10 | end 11 | 12 | it "requests without timeout" do 13 | stub_request(:post, "https://example.com") 14 | allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) 15 | assert_nothing_raised do 16 | Zencoder::HTTP::NetHTTP.post('https://example.com', :timeout => nil) 17 | end 18 | end 19 | 20 | it "adds params to the query string if passed" do 21 | stub_request(:post, "https://example.com/path?some=param") 22 | Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:params => {:some => 'param'}}) 23 | end 24 | 25 | it "adds params to the existing query string if passed" do 26 | stub_request(:post,'https://example.com/path?original=param&some=param') 27 | Zencoder::HTTP::NetHTTP.post('https://example.com/path?original=param', {:params => {:some => 'param'}}) 28 | end 29 | 30 | it "adds headers" do 31 | stub_request(:post,'https://example.com/path').with(:headers => {'some' => 'header'}) 32 | Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:headers => {:some => 'header'}}) 33 | end 34 | 35 | it "adds the body to the request" do 36 | stub_request(:post, 'https://example.com/path').with(:body => '{"some": "body"}') 37 | Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:body => '{"some": "body"}'}) 38 | end 39 | end 40 | 41 | describe "SSL verification" do 42 | before do 43 | @cert_store = double(:add_file => true, :add_path => true, :flags= => true, :set_default_paths => true).as_null_object 44 | @http_stub = double(:use_ssl= => true, :request => true, :verify_mode= => true, :cert_store= => true, :cert_store => @cert_store).as_null_object 45 | expect(::Net::HTTP).to receive(:new).and_return(@http_stub) 46 | end 47 | 48 | describe "when set to skip ssl verification" do 49 | it "nots verify" do 50 | expect(@http_stub).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) 51 | Zencoder::HTTP::NetHTTP.post('https://example.com/path', :skip_ssl_verify => true) 52 | end 53 | 54 | it "nots setup a custom cert store" do 55 | expect(@http_stub).to_not receive(:cert_store=) 56 | Zencoder::HTTP::NetHTTP.post('https://example.com/path', :skip_ssl_verify => true) 57 | end 58 | end 59 | 60 | describe "when set to do ssl verification" do 61 | it "setups a custom cert store" do 62 | expect(@http_stub).to receive(:cert_store=) 63 | Zencoder::HTTP::NetHTTP.post('https://example.com/path') 64 | end 65 | 66 | it "sets the default paths on the custom cert store" do 67 | expect(@cert_store).to receive(:set_default_paths) 68 | Zencoder::HTTP::NetHTTP.post('https://example.com/path') 69 | end 70 | 71 | it "sets the ca_file when it is passed in" do 72 | expect(@cert_store).to receive(:add_file).with("/foo/bar/baz.crt") 73 | Zencoder::HTTP::NetHTTP.post('https://example.com/path', :ca_file => "/foo/bar/baz.crt") 74 | end 75 | 76 | it "sets the ca_path when it is passed in" do 77 | expect(@cert_store).to receive(:add_path).with("/foo/bar/") 78 | Zencoder::HTTP::NetHTTP.post('https://example.com/path', :ca_path => "/foo/bar/") 79 | end 80 | end 81 | 82 | end 83 | 84 | describe ".post" do 85 | it "POSTs to specified body to the specified path" do 86 | stub_request(:post, 'https://example.com').with(:body => '{}') 87 | Zencoder::HTTP::NetHTTP.post('https://example.com', :body => '{}') 88 | end 89 | 90 | it "POSTs with an empty body if none is provided" do 91 | stub_request(:post, 'https://example.com').with(:body => '') 92 | Zencoder::HTTP::NetHTTP.post('https://example.com') 93 | end 94 | end 95 | 96 | describe ".put" do 97 | it "PUTs to specified body to the specified path" do 98 | stub_request(:put, 'https://example.com').with(:body => '{}') 99 | Zencoder::HTTP::NetHTTP.put('https://example.com', :body => '{}') 100 | end 101 | 102 | it "PUTs with an empty body if none is provided" do 103 | stub_request(:put, 'https://example.com').with(:body => '') 104 | Zencoder::HTTP::NetHTTP.put('https://example.com') 105 | end 106 | end 107 | 108 | describe ".get" do 109 | it "GETs to specified body to the specified path" do 110 | stub_request(:get, 'https://example.com') 111 | Zencoder::HTTP::NetHTTP.get('https://example.com') 112 | end 113 | end 114 | 115 | describe ".delete" do 116 | it "DELETEs to specified body to the specified path" do 117 | stub_request(:delete, 'https://example.com') 118 | Zencoder::HTTP::NetHTTP.delete('https://example.com') 119 | end 120 | end 121 | end 122 | -------------------------------------------------------------------------------- /spec/zencoder/http/typhoeus_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | # Most useless tests ever, but who knows, right? 4 | 5 | if !defined?(Typhoeus) 6 | module ::Typhoeus 7 | module Request 8 | extend self 9 | 10 | def get(*) 11 | end 12 | 13 | def put(*) 14 | end 15 | 16 | def post(*) 17 | end 18 | 19 | def delete(*) 20 | end 21 | end 22 | end 23 | end 24 | 25 | RSpec.describe Zencoder::HTTP::Typhoeus do 26 | describe ".post" do 27 | it "POSTs using Typhoeus" do 28 | expect(Typhoeus::Request).to receive(:post).with('https://example.com', {:some => 'options'}) 29 | Zencoder::HTTP::Typhoeus.post('https://example.com', {:some => 'options'}) 30 | end 31 | end 32 | 33 | describe ".put" do 34 | it "PUTs using Typhoeus" do 35 | expect(Typhoeus::Request).to receive(:put).with('https://example.com', {:some => 'options'}) 36 | Zencoder::HTTP::Typhoeus.put('https://example.com', {:some => 'options'}) 37 | end 38 | end 39 | 40 | describe ".get" do 41 | it "GETs using Typhoeus" do 42 | expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:some => 'options'}) 43 | Zencoder::HTTP::Typhoeus.get('https://example.com', {:some => 'options'}) 44 | end 45 | end 46 | 47 | describe ".delete" do 48 | it "DELETEs using Typhoeus" do 49 | expect(Typhoeus::Request).to receive(:delete).with('https://example.com', {:some => 'options'}) 50 | Zencoder::HTTP::Typhoeus.delete('https://example.com', {:some => 'options'}) 51 | end 52 | end 53 | 54 | it "skips ssl verification" do 55 | expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:disable_ssl_peer_verification => true}) 56 | Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true}) 57 | end 58 | 59 | it "uses the path to the cert file" do 60 | expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:disable_ssl_peer_verification => true, :sslcert => "/foo/bar/baz.crt"}) 61 | Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true, :ca_file => "/foo/bar/baz.crt"}) 62 | end 63 | 64 | it "uses the path to the certs directory" do 65 | expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:disable_ssl_peer_verification => true, :capath => "/foo/bar/baz.crt"}) 66 | Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true, :ca_path => "/foo/bar/baz.crt"}) 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /spec/zencoder/http_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder::HTTP do 4 | it "haves a default_options hash" do 5 | assert Zencoder::HTTP.default_options.is_a?(Hash) 6 | end 7 | 8 | it "haves a default HTTP backend" do 9 | assert Zencoder::HTTP.http_backend 10 | end 11 | 12 | it "allows the changing of the HTTP backend" do 13 | assert_not_equal Zencoder::HTTP.http_backend, Zencoder::HTTP::Typhoeus 14 | 15 | assert_nothing_raised do 16 | Zencoder::HTTP.http_backend = Zencoder::HTTP::Typhoeus 17 | end 18 | 19 | assert_equal Zencoder::HTTP.http_backend, Zencoder::HTTP::Typhoeus 20 | end 21 | 22 | it "raises a Zencoder::HTTPError when there is an HTTP error" do 23 | expect(Zencoder::HTTP.http_backend).to receive(:get). 24 | with('https://example.com', Zencoder::HTTP.default_options). 25 | at_least(:once). 26 | and_raise(Errno::ECONNREFUSED) 27 | 28 | assert_raises Zencoder::HTTPError do 29 | Zencoder::HTTP.get('https://example.com') 30 | end 31 | 32 | begin 33 | Zencoder::HTTP.get('https://example.com') 34 | rescue Zencoder::HTTPError => e 35 | assert_no_match(/perform_method/, e.backtrace.first) 36 | end 37 | end 38 | 39 | it "returns a Zencoder::Response" do 40 | allow(Zencoder::HTTP.http_backend).to receive(:post).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) 41 | allow(Zencoder::HTTP.http_backend).to receive(:put).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) 42 | allow(Zencoder::HTTP.http_backend).to receive(:get).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) 43 | allow(Zencoder::HTTP.http_backend).to receive(:delete).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) 44 | 45 | assert Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').is_a?(Zencoder::Response) 46 | assert Zencoder::HTTP.put('https://example.com', '{"some": "hash"}').is_a?(Zencoder::Response) 47 | assert Zencoder::HTTP.get('https://example.com').is_a?(Zencoder::Response) 48 | assert Zencoder::HTTP.delete('https://example.com').is_a?(Zencoder::Response) 49 | end 50 | 51 | it "stores the raw response" do 52 | post_stub = double(:code => 200, :body => '{"some": "hash"}').as_null_object 53 | allow(Zencoder::HTTP.http_backend).to receive(:post).and_return(post_stub) 54 | assert_equal post_stub, Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').raw_response 55 | end 56 | 57 | it "stores the raw response body" do 58 | allow(Zencoder::HTTP.http_backend).to receive(:post).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) 59 | assert_equal '{"some": "hash"}', Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').raw_body 60 | end 61 | 62 | it "stores the response code" do 63 | allow(Zencoder::HTTP.http_backend).to receive(:post).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) 64 | assert_equal 200, Zencoder::HTTP.post('https://example.com', '{"some": "hash"}').code 65 | end 66 | 67 | it "JSONs parse the response body" do 68 | allow(Zencoder::HTTP.http_backend).to receive(:put).and_return(double(:code => 200, :body => '{"some": "hash"}').as_null_object) 69 | assert_equal({'some' => 'hash'}, Zencoder::HTTP.put('https://example.com', '{"some": "hash"}').body) 70 | end 71 | 72 | it "stores the raw body if the body fails to be JSON parsed" do 73 | allow(Zencoder::HTTP.http_backend).to receive(:put).and_return(double(:code => 200, :body => '{"some": bad json').as_null_object) 74 | assert_equal '{"some": bad json', Zencoder::HTTP.put('https://example.com', '{"some": "hash"}').body 75 | end 76 | 77 | describe ".post" do 78 | it "calls post on the http_backend" do 79 | expect(Zencoder::HTTP.http_backend).to receive(:post). 80 | with('https://example.com', Zencoder::HTTP.default_options.merge(:body => '{}')). 81 | and_return(Zencoder::Response.new) 82 | 83 | Zencoder::HTTP.post('https://example.com', '{}') 84 | end 85 | end 86 | 87 | describe ".put" do 88 | it "calls put on the http_backend" do 89 | expect(Zencoder::HTTP.http_backend).to receive(:put). 90 | with('https://example.com', Zencoder::HTTP.default_options.merge(:body => '{}')). 91 | and_return(Zencoder::Response.new) 92 | 93 | Zencoder::HTTP.put('https://example.com', '{}') 94 | end 95 | end 96 | 97 | describe ".get" do 98 | it "calls post on the http_backend" do 99 | expect(Zencoder::HTTP.http_backend).to receive(:get). 100 | with('https://example.com', Zencoder::HTTP.default_options). 101 | and_return(Zencoder::Response.new) 102 | 103 | Zencoder::HTTP.get('https://example.com') 104 | end 105 | end 106 | 107 | describe ".delete" do 108 | it "calls post on the http_backend" do 109 | expect(Zencoder::HTTP.http_backend).to receive(:delete). 110 | with('https://example.com', Zencoder::HTTP.default_options). 111 | and_return(Zencoder::Response.new) 112 | 113 | Zencoder::HTTP.delete('https://example.com') 114 | end 115 | end 116 | end 117 | 118 | -------------------------------------------------------------------------------- /spec/zencoder/input_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder::Input do 4 | before do 5 | @api_key = 'abc123' 6 | end 7 | 8 | describe ".details" do 9 | before do 10 | @input_id = 1 11 | @url = "#{Zencoder.base_url}/inputs/#{@input_id}" 12 | end 13 | 14 | it "GETs the correct url and return a response" do 15 | allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) 16 | assert_equal Zencoder::Response, Zencoder::Input.details(@input_id, :api_key => @api_key).class 17 | end 18 | end 19 | 20 | describe ".progress" do 21 | before do 22 | @input_id = 1 23 | @url = "#{Zencoder.base_url}/inputs/#{@input_id}/progress" 24 | end 25 | 26 | it "GETs the correct url and return a response" do 27 | allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) 28 | assert_equal Zencoder::Response, Zencoder::Input.progress(@input_id, :api_key => @api_key).class 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /spec/zencoder/job_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder::Job do 4 | before do 5 | @api_key = 'abc123' 6 | end 7 | 8 | describe ".create" do 9 | before do 10 | @url = "#{Zencoder.base_url}/jobs" 11 | @params = {:api_key => @api_key, 12 | :input => "s3://bucket-name/file-name.avi"} 13 | @params_as_json = Zencoder::Serializer.encode(@params) 14 | end 15 | 16 | it "POSTs to the correct url and return a response" do 17 | expect(Zencoder::HTTP).to receive(:post).with(@url, @params_as_json, {}).and_return(Zencoder::Response.new) 18 | assert_equal Zencoder::Response, Zencoder::Job.create(@params).class 19 | end 20 | 21 | it "applies the global API key as a header" do 22 | Zencoder.api_key = 'asdfasdf' 23 | expect(Zencoder::HTTP).to receive(:post). 24 | with(instance_of(String), 25 | instance_of(String), 26 | headers: { "Zencoder-Api-Key" => Zencoder.api_key }). 27 | and_return(Zencoder::Response.new) 28 | Zencoder::Job.create(:input => @params[:input]) 29 | end 30 | end 31 | 32 | describe ".list" do 33 | before do 34 | @url = "#{Zencoder.base_url}/jobs" 35 | end 36 | 37 | it "GETs the correct url and return a response" do 38 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :page => 1, 39 | :per_page => 50, 40 | :state => nil}, 41 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 42 | assert_equal Zencoder::Response, Zencoder::Job.list(:api_key => @api_key).class 43 | end 44 | 45 | it "merges params well" do 46 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => {:page => 1, 47 | :per_page => 50, 48 | :some => 'param', 49 | :state => nil}, 50 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 51 | assert_equal Zencoder::Response, Zencoder::Job.list(:api_key => @api_key, :params => {:some => 'param'}).class 52 | end 53 | end 54 | 55 | describe ".details" do 56 | before do 57 | @job_id = 1 58 | @url = "#{Zencoder.base_url}/jobs/#{@job_id}" 59 | end 60 | 61 | it "GETs the correct url and return a response" do 62 | allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) 63 | assert_equal Zencoder::Response, Zencoder::Job.details(1, :api_key => @api_key).class 64 | end 65 | end 66 | 67 | describe ".progress" do 68 | before do 69 | @job_id = 1 70 | @url = "#{Zencoder.base_url}/jobs/#{@job_id}/progress" 71 | end 72 | 73 | it "GETs the correct url and return a response" do 74 | allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) 75 | assert_equal Zencoder::Response, Zencoder::Job.progress(1, :api_key => @api_key).class 76 | end 77 | end 78 | 79 | describe ".resubmit" do 80 | before do 81 | @job_id = 1 82 | @url = "#{Zencoder.base_url}/jobs/#{@job_id}/resubmit" 83 | end 84 | 85 | it "PUTs the correct url and return a response" do 86 | allow(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => {"Zencoder-Api-Key" => @api_key}).and_return(Zencoder::Response.new) 87 | assert_equal Zencoder::Response, Zencoder::Job.resubmit(1, :api_key => @api_key).class 88 | end 89 | end 90 | 91 | describe ".cancel" do 92 | before do 93 | @job_id = 1 94 | @url = "#{Zencoder.base_url}/jobs/#{@job_id}/cancel" 95 | end 96 | 97 | it "PUTs the correct url and return a response" do 98 | allow(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => {"Zencoder-Api-Key" => @api_key}).and_return(Zencoder::Response.new) 99 | assert_equal Zencoder::Response, Zencoder::Job.cancel(1, :api_key => @api_key).class 100 | end 101 | end 102 | 103 | describe ".finish" do 104 | before do 105 | @job_id = 1 106 | @url = "#{Zencoder.base_url}/jobs/#{@job_id}/finish" 107 | end 108 | 109 | it "PUTs the correct url and return a response" do 110 | allow(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => {"Zencoder-Api-Key" => @api_key}).and_return(Zencoder::Response.new) 111 | assert_equal Zencoder::Response, Zencoder::Job.finish(1, :api_key => @api_key).class 112 | end 113 | end 114 | 115 | end 116 | -------------------------------------------------------------------------------- /spec/zencoder/notification_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder::Notification do 4 | describe ".list" do 5 | before do 6 | @url = "#{Zencoder.base_url}/notifications" 7 | @api_key = 'asdfasdf' 8 | end 9 | 10 | it "GETs the correct url and return a response" do 11 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :page => 1, 12 | :per_page => 50}, 13 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 14 | assert_equal Zencoder::Response, Zencoder::Notification.list(:api_key => @api_key).class 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /spec/zencoder/output_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder::Output do 4 | before do 5 | @api_key = 'abc123' 6 | end 7 | 8 | describe ".details" do 9 | before do 10 | @output_id = 1 11 | @url = "#{Zencoder.base_url}/outputs/#{@output_id}" 12 | end 13 | 14 | it "GETs the correct url and return a response" do 15 | allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) 16 | assert_equal Zencoder::Response, Zencoder::Output.details(@output_id, :api_key => @api_key).class 17 | end 18 | end 19 | 20 | describe ".progress" do 21 | before do 22 | @output_id = 1 23 | @url = "#{Zencoder.base_url}/outputs/#{@output_id}/progress" 24 | end 25 | 26 | it "GETs the correct url and return a response" do 27 | allow(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new) 28 | assert_equal Zencoder::Response, Zencoder::Output.progress(@output_id, :api_key => @api_key).class 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /spec/zencoder/report_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder::Report do 4 | 5 | describe ".minutes" do 6 | before do 7 | @api_key = "abcd123" 8 | @url = "#{Zencoder.base_url}/reports/minutes" 9 | end 10 | 11 | it "GETs the correct url and return a response" do 12 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", 13 | :to => "2011-06-01", 14 | :grouping => "foo"}, 15 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 16 | assert_equal Zencoder::Response, Zencoder::Report.minutes(:api_key => @api_key, :from => "2011-01-01", 17 | :to => "2011-06-01", 18 | :grouping => "foo").class 19 | end 20 | 21 | it "merges params well" do 22 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", 23 | :to => "2011-06-01"}, 24 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 25 | assert_equal Zencoder::Response, Zencoder::Report.minutes(:api_key => @api_key, :from => "2011-01-01", :to => "2011-06-01").class 26 | end 27 | end 28 | 29 | describe ".all" do 30 | before do 31 | @api_key = "abcd123" 32 | @url = "#{Zencoder.base_url}/reports/all" 33 | end 34 | 35 | it "GETs the correct url and return a response" do 36 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", 37 | :to => "2011-06-01", 38 | :grouping => "foo"}, 39 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 40 | assert_equal Zencoder::Response, Zencoder::Report.all(:api_key => @api_key, :from => "2011-01-01", 41 | :to => "2011-06-01", 42 | :grouping => "foo").class 43 | end 44 | 45 | it "merges params well" do 46 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", 47 | :to => "2011-06-01"}, 48 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 49 | assert_equal Zencoder::Response, Zencoder::Report.all(:api_key => @api_key, :from => "2011-01-01", :to => "2011-06-01").class 50 | end 51 | end 52 | 53 | describe ".vod" do 54 | before do 55 | @api_key = "abcd123" 56 | @url = "#{Zencoder.base_url}/reports/vod" 57 | end 58 | 59 | it "GETs the correct url and return a response" do 60 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", 61 | :to => "2011-06-01", 62 | :grouping => "foo"}, 63 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 64 | assert_equal Zencoder::Response, Zencoder::Report.vod(:api_key => @api_key, :from => "2011-01-01", 65 | :to => "2011-06-01", 66 | :grouping => "foo").class 67 | end 68 | 69 | it "merges params well" do 70 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", 71 | :to => "2011-06-01"}, 72 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 73 | assert_equal Zencoder::Response, Zencoder::Report.vod(:api_key => @api_key, :from => "2011-01-01", :to => "2011-06-01").class 74 | end 75 | end 76 | 77 | describe ".live" do 78 | before do 79 | @api_key = "abcd123" 80 | @url = "#{Zencoder.base_url}/reports/live" 81 | end 82 | 83 | it "GETs the correct url and return a response" do 84 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", 85 | :to => "2011-06-01", 86 | :grouping => "foo"}, 87 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 88 | assert_equal Zencoder::Response, Zencoder::Report.live(:api_key => @api_key, :from => "2011-01-01", 89 | :to => "2011-06-01", 90 | :grouping => "foo").class 91 | end 92 | 93 | it "merges params well" do 94 | allow(Zencoder::HTTP).to receive(:get).with(@url, {:params => { :from => "2011-01-01", 95 | :to => "2011-06-01"}, 96 | :headers => { "Zencoder-Api-Key" => @api_key }}).and_return(Zencoder::Response.new) 97 | assert_equal Zencoder::Response, Zencoder::Report.live(:api_key => @api_key, :from => "2011-01-01", :to => "2011-06-01").class 98 | end 99 | end 100 | 101 | end 102 | -------------------------------------------------------------------------------- /spec/zencoder/response_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder::Response do 4 | describe "#success?" do 5 | it "returns true when code is between 200 and 299" do 6 | assert Zencoder::Response.new(:code => 200).success? 7 | assert Zencoder::Response.new(:code => 299).success? 8 | assert Zencoder::Response.new(:code => 250).success? 9 | end 10 | 11 | it "returns false when code it less than 200 or greater than 299" do 12 | assert !Zencoder::Response.new(:code => 300).success? 13 | assert !Zencoder::Response.new(:code => 199).success? 14 | end 15 | end 16 | 17 | describe "#errors" do 18 | it "returns an empty array when body is not a Hash" do 19 | assert_equal [], Zencoder::Response.new(:body => 1).errors 20 | assert_equal [], Zencoder::Response.new(:body => "something").errors 21 | assert_equal [], Zencoder::Response.new(:body => [1]).errors 22 | end 23 | 24 | it "returns the value of the key 'errors' as a compacted array when body is a Hash" do 25 | assert_same_elements ['must be awesome'], Zencoder::Response.new(:body => {'errors' => ['must be awesome']}).errors 26 | assert_same_elements ['must be awesome'], Zencoder::Response.new(:body => {'errors' => 'must be awesome'}).errors 27 | assert_same_elements ['must be awesome'], Zencoder::Response.new(:body => {'errors' => ['must be awesome', nil]}).errors 28 | assert_same_elements [], Zencoder::Response.new(:body => {}).errors 29 | end 30 | end 31 | 32 | describe "#body_without_wrapper" do 33 | it "returns the body when the body is a string" do 34 | assert_equal "some text", Zencoder::Response.new(:body => "some text").body_without_wrapper 35 | end 36 | 37 | it "returns the body when the body is not wrapped in api_response and is a hash" do 38 | assert_equal({'some' => 'hash'}, Zencoder::Response.new(:body => {'some' => 'hash'}).body_without_wrapper) 39 | end 40 | 41 | it "returns body['api_response'] when body is a hash and body['api_response'] exists" do 42 | assert_equal({'some' => 'hash'}, Zencoder::Response.new(:body => {'api_response' => {'some' => 'hash'}}).body_without_wrapper) 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /spec/zencoder/zencoder_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | RSpec.describe Zencoder do 4 | 5 | it "allows user to set an api key" do 6 | Zencoder.api_key = "123" 7 | assert_equal Zencoder::Job.api_key, "123" 8 | end 9 | 10 | it "allows ENV variable to set an api key" do 11 | ENV['ZENCODER_API_KEY'] = "321" 12 | assert_equal Zencoder::Job.api_key, "321" 13 | end 14 | 15 | it "takes user-supplie api key over ENV-supplied key" do 16 | Zencoder.api_key = "123" 17 | ENV['ZENCODER_API_KEY'] = "321" 18 | assert_equal Zencoder::Job.api_key, "123" 19 | end 20 | 21 | it "encodes to json" do 22 | assert_match(/"api_request"/, Zencoder::Serializer.encode({:api_request => {:input => 'https://example.com'}})) 23 | end 24 | 25 | it "nots encode when the content is a String" do 26 | assert_match(/^api_request$/, Zencoder::Serializer.encode("api_request")) 27 | end 28 | 29 | it "decodes from json" do 30 | assert Zencoder::Serializer.decode(%@{"api_request": {"input": "https://example.com"}}@)['api_request']['input'] 31 | end 32 | 33 | it "does not decode when content is not a String" do 34 | assert_equal 1, Zencoder::Serializer.decode(1) 35 | end 36 | 37 | end 38 | -------------------------------------------------------------------------------- /zencoder.gemspec: -------------------------------------------------------------------------------- 1 | lib = File.expand_path('../lib/', __FILE__) 2 | $:.unshift lib unless $:.include?(lib) 3 | 4 | require 'zencoder/version' 5 | 6 | Gem::Specification.new do |s| 7 | s.name = "zencoder" 8 | s.version = Zencoder::GEM_VERSION 9 | s.platform = Gem::Platform::RUBY 10 | s.authors = ["Nathan Sutton", "Brandon Arbini"] 11 | s.email = "info@zencoder.com" 12 | s.homepage = "http://github.com/zencoder/zencoder-rb" 13 | s.summary = "Zencoder integration library." 14 | s.description = "Zencoder integration library." 15 | s.rubyforge_project = "zencoder" 16 | s.add_dependency "multi_json" 17 | if RUBY_PLATFORM =~ /java/ 18 | s.platform = "java" 19 | s.add_development_dependency "jruby-openssl" 20 | else 21 | s.add_development_dependency "typhoeus" 22 | s.add_development_dependency "byebug" 23 | end 24 | s.add_development_dependency "rspec", "~> 3.8.0" 25 | s.add_development_dependency "webmock", "~> 3.4.2" 26 | s.add_development_dependency "rake" 27 | s.add_development_dependency "test-unit" 28 | s.files = Dir.glob("bin/**/*") + Dir.glob("lib/**/*") + %w(LICENSE README.markdown Rakefile) 29 | s.require_path = "lib" 30 | end 31 | --------------------------------------------------------------------------------