Class: Brightcove::Cmsapi

Inherits:
Object
  • Object
show all
Defined in:
lib/brightcove/cmsapi.rb

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_id:, client_id:, client_secret:) ⇒ Cmsapi

Returns a new instance of Cmsapi.



9
10
11
12
13
14
# File 'lib/brightcove/cmsapi.rb', line 9

def initialize(account_id:, client_id:, client_secret:)
  @base_url = "#{API_ROOT}/#{}"
  @client_id = client_id
  @client_secret = client_secret
  set_token
end

Class Method Details

.default_apiObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/brightcove/cmsapi.rb', line 16

def self.default_api
   = ENV['BRIGHTCOVE_ACCOUNT_ID']
  client_id = ENV['BRIGHTCOVE_CLIENT_ID']
  client_secret = ENV['BRIGHTCOVE_CLIENT_SECRET']

  if [, client_id, client_secret].any? { |c| c.to_s.empty? }
    raise AuthenticationError, 'Missing Brightcove API credentials'
  end

  @default_api ||= new(
    account_id: ,
    client_id: client_id,
    client_secret: client_secret)
end

Instance Method Details

#get(path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brightcove/cmsapi.rb', line 31

def get(path)
  set_token if @token_expires < Time.now
  response = HTTP.auth("Bearer #{@token}").get("#{@base_url}/#{path}")

  case response.code
  when 200 # OK
    response
  when 401 # Unauthorized, token expired
    set_token
    response = HTTP.auth("Bearer #{@token}").get("#{@base_url}/#{path}")

    # if a fresh token still returns 401 the request must be unauthorized
     if response.code == 401

    response
  else
    raise CmsapiError, response.to_s
  end
end

#get_all(path = "", resource) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/brightcove/cmsapi.rb', line 51

def get_all(path="", resource)
  if path.empty?
    count = get("counts/#{resource}s").parse.fetch("count")
    resource_path = "#{resource}s"
  else
    count = get(path).parse.fetch("#{resource}_count")
    resource_path = "#{path}/#{resource}s"
  end
  offset = 0
  resources = []
  while offset < count do
    resources.concat(get("#{resource_path}?limit=100&offset=#{offset}").parse)
    offset = offset + 100
  end
  resources
end