Class: Brightcove::API

Inherits:
Object
  • Object
show all
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 =
'https://api.brightcove.com/services/library'
WRITE_API_URL =
'https://api.brightcove.com/services/post'
VERSION =
'1.0.18'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • token (String)

    Brightcove token which can be a read-only, write or read-write token.

  • read_api_url (String) (defaults to: READ_API_URL)

    Read API URL or the default of Brightcove::API::READ_API_URL.

  • write_api_url (String) (defaults to: WRITE_API_URL)

    Write API URL or the default of Brightcove::API::WRITE_API_URL.



38
39
40
41
42
43
44
# File 'lib/brightcove-api.rb', line 38

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_timeoutObject

RestClient POST timeout for opening connection



31
32
33
# File 'lib/brightcove-api.rb', line 31

def open_timeout
  @open_timeout
end

#read_api_urlObject

Returns the value of attribute read_api_url.



24
25
26
# File 'lib/brightcove-api.rb', line 24

def read_api_url
  @read_api_url
end

#timeoutObject

RestClient POST timeout for reading conection



29
30
31
# File 'lib/brightcove-api.rb', line 29

def timeout
  @timeout
end

#tokenObject

Returns the value of attribute token.



26
27
28
# File 'lib/brightcove-api.rb', line 26

def token
  @token
end

#write_api_urlObject

Returns the value of attribute write_api_url.



25
26
27
# File 'lib/brightcove-api.rb', line 25

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.

Parameters:

  • location (Object) (defaults to: $stderr)

    Defaults to $stderr.



49
50
51
# File 'lib/brightcove-api.rb', line 49

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.

Parameters:

  • api_method (String)

    Brightcove API method.

  • options (Hash) (defaults to: {})

    Optional hash containing parameter names and values. The options parameter can be either a query string or a hash. If a query string, it will be normalized to a hash via CGI.parse.



76
77
78
# File 'lib/brightcove-api.rb', line 76

def get(api_method, options = {})
  self.class.get(@read_api_url, build_query_from_options(api_method, options))
end

#post(api_method, parameters = {}) ⇒ Object

Make an HTTP POST call to the Brightcove API for a particular API method.

Parameters:

  • api_method (String)

    Brightcove API method.

  • parameters (Hash) (defaults to: {})

    Optional hash containing parameter names and values.



84
85
86
87
88
89
90
91
92
# File 'lib/brightcove-api.rb', line 84

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.

Parameters:

  • api_method (String)

    Brightcove API method.

  • file (String)

    Full path of file to be uploaded.

  • parameters (Hash) (defaults to: {})

    Optional hash containing parameter names and values.



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
131
132
# File 'lib/brightcove-api.rb', line 99

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.

Parameters:

  • api_method (String)

    Brightcove API method.

  • upload_file (String)

    Full path of file to be uploaded.

  • parameters (Hash)

    Optional hash containing parameter names and values.



139
140
141
# File 'lib/brightcove-api.rb', line 139

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.

Parameters:

  • api_method (String)

    Brightcove API method.

  • file (File handle)

    File handle of file to be uploaded.

  • parameters (Hash)

    Optional hash containing parameter names and values.



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
177
178
179
180
181
182
183
184
# File 'lib/brightcove-api.rb', line 148

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

  if file.is_a?(UploadIO)
    payload[:file] = file
  else
    filename = file.respond_to?(:base_uri) ? File.basename(file.base_uri.to_s) : File.basename(file.path) rescue nil
    payload[:file] = UploadIO.new(file, content_type, filename)
  end

  request = Net::HTTP::Post::Multipart.new(url.path, payload)

  response = Net::HTTP.start(url.host, url.port, :use_ssl => true) 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.

Parameters:

  • http_headers (Hash) (defaults to: {})

    Updated HTTP headers.



58
59
60
61
# File 'lib/brightcove-api.rb', line 58

def set_http_headers(http_headers = {})
  http_headers.merge!(DEFAULT_HEADERS)
  self.class.headers(http_headers)
end

#set_timeout(timeout) ⇒ Object

Set a timeout for HTTP requests.

Parameters:

  • timeout (int)

    HTTP timeout value.



66
67
68
# File 'lib/brightcove-api.rb', line 66

def set_timeout(timeout)
  self.class.default_timeout(timeout)
end