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.



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

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



15
16
17
18
19
20
# File 'lib/brightcove/cmsapi.rb', line 15

def self.default_api
  @default_api ||= new(
    account_id: ENV['BRIGHTCOVE_ACCOUNT_ID'],
    client_id: ENV['BRIGHTCOVE_CLIENT_ID'],
    client_secret: ENV['BRIGHTCOVE_CLIENT_SECRET'])
end

Instance Method Details

#get(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/brightcove/cmsapi.rb', line 22

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

  if response.code == 401 # Unauthorized, token expired
    set_token
    HTTP.auth("Bearer #{@token}").get("#{@base_url}/#{path}")
  else
    response
  end
end

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



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

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