class BrightcoveService::Api

Constants

API_ROOT
OAUTH_ENDPOINT
PER_PAGE

Public Class Methods

new() click to toggle source
# File lib/brightcove_service.rb, line 16
def initialize
  @client_id = ENV['BRIGHTCOVE_CLIENT_ID']
  @client_secret = ENV['BRIGHTCOVE_CLIENT_SECRET']
  @base_url = "#{API_ROOT}/#{ENV['BRIGHTCOVE_ACCOUNT_ID']}"
  if [@client_id, @client_secret].any? { |c| c.to_s.empty? }
    raise AuthenticationError, 'Missing Brightcove API credentials'
  end
  set_token
end

Public Instance Methods

create_video(params) click to toggle source
# File lib/brightcove_service.rb, line 26
def create_video(params)
  perform_action('post', 'videos', params.to_json)
end
get_s3_url(video_id, filename) click to toggle source
# File lib/brightcove_service.rb, line 30
def get_s3_url(video_id, filename)
  perform_action('get', "videos/#{video_id}/upload-urls/#{filename}")
end
http() click to toggle source
# File lib/brightcove_service.rb, line 57
def http
  HTTP.headers(Authorization: "Bearer #{@token}", 'Content-Type': 'application/json')
end
ingest_video_and_assets(video_id, params) click to toggle source
# File lib/brightcove_service.rb, line 34
def ingest_video_and_assets(video_id, params)
  perform_action('post', "videos/#{video_id}/ingest-requests", params)
end
make_request() click to toggle source
# File lib/brightcove_service.rb, line 48
def make_request
  case @request_type
  when 'post'
    http.post(@url, body: @params)
  when 'get'
    http.get(@url)
  end
end
perform_action(request_type, url, params = {}) click to toggle source
# File lib/brightcove_service.rb, line 38
def perform_action(request_type, url, params = {})
  set_token if @token_expires < Time.now
  @request_type = request_type
  @url = "#{@base_url}/#{url}"
  @params = params
  response = make_request
  return response.parse if response.code.in? [200, 201]
  raise StandardError, response.to_s
end

Private Instance Methods

auth_request() click to toggle source
# File lib/brightcove_service.rb, line 75
def auth_request
  HTTP.basic_auth(user: @client_id, pass: @client_secret)
      .post(OAUTH_ENDPOINT,
            form: { grant_type: 'client_credentials' })
end
raise_account_error() click to toggle source
# File lib/brightcove_service.rb, line 81
def raise_account_error
  raise AuthenticationError, 'Token valid but not for the given account_id'
end
set_token() click to toggle source
# File lib/brightcove_service.rb, line 63
def set_token
  response = auth_request
  token_response = auth_request.parse
  return update_token(token_response) if response.status == 200
  raise AuthenticationError, token_response.fetch('error_description')
end
update_token(token_response) click to toggle source
# File lib/brightcove_service.rb, line 70
def update_token(token_response)
  @token = token_response.fetch('access_token')
  @token_expires = Time.now + token_response.fetch('expires_in')
end