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 =
'http://api.brightcove.com/services/library'
WRITE_API_URL =
'http://api.brightcove.com/services/post'
VERSION =
'1.0.13'.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.



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_timeoutObject

RestClient POST timeout for opening connection



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

def open_timeout
  @open_timeout
end

#read_api_urlObject

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

#timeoutObject

RestClient POST timeout for reading conection



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

def timeout
  @timeout
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

#write_api_urlObject

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.

Parameters:

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

    Defaults to $stderr.



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.

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.



74
75
76
# File 'lib/brightcove-api.rb', line 74

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.



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.

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.



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.

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.



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.

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.



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.

Parameters:

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

    Updated HTTP headers.



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.

Parameters:

  • timeout (int)

    HTTP timeout value.



64
65
66
# File 'lib/brightcove-api.rb', line 64

def set_timeout(timeout)
  default_timeout(timeout)
end