brightcove-cmsapi 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30e2e95b6fa46f8dc97092182f230b0c7a176002
4
- data.tar.gz: 7f6b5104c142e2ccfc4758b0a9f893edf8549706
3
+ metadata.gz: 4de1317ce6ac141a262964cb793af11ab2a4322e
4
+ data.tar.gz: 9190e766267735108922655ec629585b3c99ee13
5
5
  SHA512:
6
- metadata.gz: 15222de1bbcd9ed0e009a42dd0c3fc7ef4aeb14cc06cd3fbcdda697c700525c4f10e682874d0459e0427bf30f0426c3b33d25ad926a110d770c3da683fc46366
7
- data.tar.gz: 4b6fc1e5201125727f4a03d07c607e98149f8d9f9b1f632bdd506a2417744683ed82d656d08309fb898bdbacd6f4f6b4ad0eeae4c0093e5a08e995f58a724c5d
6
+ metadata.gz: 47562d46b41b59815aa55d4c2432bc3a3839d4556e6846ee1432b5d80aa27748b78c94ad079e524bfbae6a32f4d9af44cf995914d87bafe18e6a421f8ce96ca8
7
+ data.tar.gz: 2841f6d29b1c5f6e79058f82c2fb42347417b23db8ef975bd82d001aa2f2a407e25fa9049752806e4233423ccd2ea20eabf9d58a033e155f9f7c9f21046553e3
@@ -4,12 +4,12 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "brightcove-cmsapi"
7
- spec.version = "0.1.0"
7
+ spec.version = "0.2.0"
8
8
  spec.authors = ["Daniel King"]
9
9
  spec.email = ["daniel.king5@nhs.net"]
10
10
 
11
11
  spec.summary = %q{A simple wrapper around Brightcove's CMS API}
12
- spec.homepage = "https://nhs.uk"
12
+ spec.homepage = "https://github.com/nhsuk/brightcove-cmsapi"
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
@@ -1,4 +1,5 @@
1
1
  require "http"
2
+ require_relative "errors"
2
3
 
3
4
  module Brightcove
4
5
  class Cmsapi
@@ -13,21 +14,37 @@ module Brightcove
13
14
  end
14
15
 
15
16
  def self.default_api
17
+ account_id = ENV['BRIGHTCOVE_ACCOUNT_ID']
18
+ client_id = ENV['BRIGHTCOVE_CLIENT_ID']
19
+ client_secret = ENV['BRIGHTCOVE_CLIENT_SECRET']
20
+
21
+ if [account_id, client_id, client_secret].any? { |c| c.to_s.empty? }
22
+ raise AuthenticationError, 'Missing Brightcove API credentials'
23
+ end
24
+
16
25
  @default_api ||= new(
17
- account_id: ENV['BRIGHTCOVE_ACCOUNT_ID'],
18
- client_id: ENV['BRIGHTCOVE_CLIENT_ID'],
19
- client_secret: ENV['BRIGHTCOVE_CLIENT_SECRET'])
26
+ account_id: account_id,
27
+ client_id: client_id,
28
+ client_secret: client_secret)
20
29
  end
21
30
 
22
31
  def get(path)
23
32
  set_token if @token_expires < Time.now
24
33
  response = HTTP.auth("Bearer #{@token}").get("#{@base_url}/#{path}")
25
34
 
26
- if response.code == 401 # Unauthorized, token expired
35
+ case response.code
36
+ when 200 # OK
37
+ response
38
+ when 401 # Unauthorized, token expired
27
39
  set_token
28
- HTTP.auth("Bearer #{@token}").get("#{@base_url}/#{path}")
29
- else
40
+ response = HTTP.auth("Bearer #{@token}").get("#{@base_url}/#{path}")
41
+
42
+ # if a fresh token still returns 401 the request must be unauthorized
43
+ raise_account_error if response.code == 401
44
+
30
45
  response
46
+ else
47
+ raise CmsapiError, response.to_s
31
48
  end
32
49
  end
33
50
 
@@ -53,9 +70,19 @@ module Brightcove
53
70
  def set_token
54
71
  response = HTTP.basic_auth(user: @client_id, pass: @client_secret)
55
72
  .post(OAUTH_ENDPOINT,
56
- form: { grant_type: "client_credentials" }).parse
57
- @token = response.fetch("access_token")
58
- @token_expires = Time.now + response.fetch("expires_in")
73
+ form: { grant_type: "client_credentials" })
74
+ token_response = response.parse
75
+
76
+ if response.status == 200
77
+ @token = token_response.fetch("access_token")
78
+ @token_expires = Time.now + token_response.fetch("expires_in")
79
+ else
80
+ raise AuthenticationError, token_response.fetch("error_description")
81
+ end
82
+ end
83
+
84
+ def raise_account_error
85
+ raise AuthenticationError, 'Token valid but not for the given account_id'
59
86
  end
60
87
  end
61
88
  end
@@ -0,0 +1,8 @@
1
+ module Brightcove
2
+ class CmsapiError < StandardError
3
+ end
4
+
5
+ # AuthenticationError is raised when the Oauth authentication fails
6
+ class AuthenticationError < CmsapiError
7
+ end
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brightcove-cmsapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel King
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-04 00:00:00.000000000 Z
11
+ date: 2018-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,7 +56,8 @@ files:
56
56
  - bin/setup
57
57
  - brightcove-cmsapi.gemspec
58
58
  - lib/brightcove/cmsapi.rb
59
- homepage: https://nhs.uk
59
+ - lib/brightcove/errors.rb
60
+ homepage: https://github.com/nhsuk/brightcove-cmsapi
60
61
  licenses:
61
62
  - MIT
62
63
  metadata: {}
@@ -76,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
77
  version: '0'
77
78
  requirements: []
78
79
  rubyforge_project:
79
- rubygems_version: 2.5.1
80
+ rubygems_version: 2.5.2
80
81
  signing_key:
81
82
  specification_version: 4
82
83
  summary: A simple wrapper around Brightcove's CMS API