Class: Brightcove::API
- Inherits:
-
Object
- Object
- Brightcove::API
- Includes:
- HTTParty
- Defined in:
- lib/brightcove-api.rb,
lib/brightcove-api/version.rb
Constant Summary collapse
- DEFAULT_HEADERS =
{ 'User-Agent' => "brightcove-api gem #{VERSION}" }
- READ_API_URL =
'http://api.brightcove.com/services/library'
- WRITE_API_URL =
'http://api.brightcove.com/services/post'
- VERSION =
'1.0.13'.freeze
Instance Attribute Summary collapse
-
#open_timeout ⇒ Object
RestClient POST timeout for opening connection.
-
#read_api_url ⇒ Object
Returns the value of attribute read_api_url.
-
#timeout ⇒ Object
RestClient POST timeout for reading conection.
-
#token ⇒ Object
Returns the value of attribute token.
-
#write_api_url ⇒ Object
Returns the value of attribute write_api_url.
Instance Method Summary collapse
-
#debug(location = $stderr) ⇒ Object
Set a location for debug HTTP output to be written to.
-
#get(api_method, options = {}) ⇒ Object
Make an HTTP GET call to the Brightcove API for a particular API method.
-
#initialize(token, read_api_url = READ_API_URL, write_api_url = WRITE_API_URL) ⇒ API
constructor
Initialize a new instance of the
Brightcove::API
using a Brightcove token. -
#post(api_method, parameters = {}) ⇒ Object
Make an HTTP POST call to the Brightcove API for a particular API method.
-
#post_file(api_method, file, parameters = {}) ⇒ Object
Post a file to the Brightcove API, e.g.
-
#post_file_streaming(api_method, upload_file, content_type, parameters) ⇒ Object
Post a file via HTTP streaming to the Brightcove API, e.g.
-
#post_io_streaming(api_method, file, content_type, parameters) ⇒ Object
Post a file IO object via HTTP streaming to the Brightcove API, e.g.
-
#set_http_headers(http_headers = {}) ⇒ Object
Set HTTP headers that should be used in API requests.
-
#set_timeout(timeout) ⇒ Object
Set a timeout for HTTP requests.
Constructor Details
#initialize(token, read_api_url = READ_API_URL, write_api_url = WRITE_API_URL) ⇒ API
Initialize a new instance of the Brightcove::API
using a Brightcove token.
36 37 38 39 40 41 42 |
# File 'lib/brightcove-api.rb', line 36 def initialize(token, read_api_url = READ_API_URL, write_api_url = WRITE_API_URL) @token = token @read_api_url = read_api_url @write_api_url = write_api_url @timeout = nil @open_timeout = nil end |
Instance Attribute Details
#open_timeout ⇒ Object
RestClient POST timeout for opening connection
29 30 31 |
# File 'lib/brightcove-api.rb', line 29 def open_timeout @open_timeout end |
#read_api_url ⇒ Object
Returns the value of attribute read_api_url.
22 23 24 |
# File 'lib/brightcove-api.rb', line 22 def read_api_url @read_api_url end |
#timeout ⇒ Object
RestClient POST timeout for reading conection
27 28 29 |
# File 'lib/brightcove-api.rb', line 27 def timeout @timeout end |
#token ⇒ Object
Returns the value of attribute token.
24 25 26 |
# File 'lib/brightcove-api.rb', line 24 def token @token end |
#write_api_url ⇒ Object
Returns the value of attribute write_api_url.
23 24 25 |
# File 'lib/brightcove-api.rb', line 23 def write_api_url @write_api_url end |
Instance Method Details
#debug(location = $stderr) ⇒ Object
Set a location for debug HTTP output to be written to.
47 48 49 |
# File 'lib/brightcove-api.rb', line 47 def debug(location = $stderr) self.class.debug_output(location) end |
#get(api_method, options = {}) ⇒ Object
Make an HTTP GET call to the Brightcove API for a particular API method.
74 75 76 |
# File 'lib/brightcove-api.rb', line 74 def get(api_method, = {}) self.class.get(@read_api_url, (api_method, )) end |
#post(api_method, parameters = {}) ⇒ Object
Make an HTTP POST call to the Brightcove API for a particular API method.
82 83 84 85 86 87 88 89 90 |
# File 'lib/brightcove-api.rb', line 82 def post(api_method, parameters = {}) parameters.merge!({"token" => @token}) body = {} body.merge!({:method => api_method}) body.merge!({:params => parameters}) self.class.post(@write_api_url, {:body => {:json => JSON.generate(body)}}) end |
#post_file(api_method, file, parameters = {}) ⇒ Object
Post a file to the Brightcove API, e.g. uploading video.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/brightcove-api.rb', line 97 def post_file(api_method, file, parameters = {}) parameters.merge!({"token" => @token}) body = {} body.merge!({:method => api_method}) body.merge!({:params => parameters}) # Brightcove requires that the JSON-RPC call absolutely # be the first part of a multi-part POST like create_video. if RUBY_VERSION >= '1.9' payload = {} else payload = OrderedHash.new end payload[:json] = body.to_json payload[:file] = File.new(file, 'rb') execution_payload = { :method => :post, :url => @write_api_url, :payload => payload, :content_type => :json, :accept => :json, :multipart => true } execution_payload[:timeout] = @timeout if @timeout execution_payload[:open_timeout] = @open_timeout if @open_timeout response = RestClient::Request.execute(execution_payload) JSON.parse(response) end |
#post_file_streaming(api_method, upload_file, content_type, parameters) ⇒ Object
Post a file via HTTP streaming to the Brightcove API, e.g. uploading video.
137 138 139 |
# File 'lib/brightcove-api.rb', line 137 def post_file_streaming(api_method, upload_file, content_type, parameters) File.open(upload_file) { |file| post_io_streaming(api_method, file, content_type, parameters) } end |
#post_io_streaming(api_method, file, content_type, parameters) ⇒ Object
Post a file IO object via HTTP streaming to the Brightcove API, e.g. uploading video.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/brightcove-api.rb', line 146 def post_io_streaming(api_method, file, content_type, parameters) parameters.merge!({"token" => @token}) body = {} body.merge!({:method => api_method}) body.merge!({:params => parameters}) # Brightcove requires that the JSON-RPC call absolutely # be the first part of a multi-part POST like create_video. if RUBY_VERSION >= '1.9' payload = {} else payload = OrderedHash.new end url = URI.parse(@write_api_url) response = nil payload[:json] = body.to_json payload[:file] = file.is_a?(UploadIO) ? file : UploadIO.new(file, content_type) request = Net::HTTP::Post::Multipart.new(url.path, payload) response = Net::HTTP.start(url.host, url.port) do |http| http.read_timeout = @timeout if @timeout http.open_timeout = @open_timeout if @open_timeout http.request(request) end JSON.parse(response.body) end |
#set_http_headers(http_headers = {}) ⇒ Object
Set HTTP headers that should be used in API requests. The Brightcove::API::DEFAULT_HEADERS
will be merged into the passed-in hash.
56 57 58 59 |
# File 'lib/brightcove-api.rb', line 56 def set_http_headers(http_headers = {}) http_headers.merge!(DEFAULT_HEADERS) headers(http_headers) end |
#set_timeout(timeout) ⇒ Object
Set a timeout for HTTP requests.
64 65 66 |
# File 'lib/brightcove-api.rb', line 64 def set_timeout(timeout) default_timeout(timeout) end |