Class: BrightcoveService::Api

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/brightcove_service.rb

Constant Summary collapse

OAUTH_ENDPOINT =
'https://oauth.brightcove.com/v4/access_token'
API_ROOT =
'https://cms.api.brightcove.com/v1/accounts'
PER_PAGE =
100

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#create_video(params) ⇒ Object



26
27
28
# 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) ⇒ Object



30
31
32
# File 'lib/brightcove_service.rb', line 30

def get_s3_url(video_id, filename)
  perform_action('get', "videos/#{video_id}/upload-urls/#{filename}")
end

#httpObject



57
58
59
# 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) ⇒ Object



34
35
36
# 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_requestObject



48
49
50
51
52
53
54
55
# 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 = {}) ⇒ Object

Raises:

  • (StandardError)


38
39
40
41
42
43
44
45
46
# 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