brightcove-cmsapi 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30e2e95b6fa46f8dc97092182f230b0c7a176002
4
+ data.tar.gz: 7f6b5104c142e2ccfc4758b0a9f893edf8549706
5
+ SHA512:
6
+ metadata.gz: 15222de1bbcd9ed0e009a42dd0c3fc7ef4aeb14cc06cd3fbcdda697c700525c4f10e682874d0459e0427bf30f0426c3b33d25ad926a110d770c3da683fc46366
7
+ data.tar.gz: 4b6fc1e5201125727f4a03d07c607e98149f8d9f9b1f632bdd506a2417744683ed82d656d08309fb898bdbacd6f4f6b4ad0eeae4c0093e5a08e995f58a724c5d
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in brightcove-cmsapi.gemspec
6
+ gemspec
7
+
8
+ group :development, :test do
9
+ gem "rake", "~> 10.0"
10
+ end
11
+
12
+ group :test do
13
+ gem "rspec", "~> 3.0"
14
+ gem "webmock", "~> 3.0.1"
15
+ gem "timecop", "~> 0.9.1"
16
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Daniel King
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,95 @@
1
+ # Brightcove::Cmsapi
2
+
3
+ [![Build Status](https://travis-ci.org/nhsuk/brightcove-cmsapi.svg?branch=master)](https://travis-ci.org/nhsuk/brightcove-cmsapi)
4
+
5
+ This gem wraps Brightcove's [CMS API](https://brightcovelearning.github.io/Brightcove-API-References/cms-api/v1/doc/index.html).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'brightcove-cmsapi'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install brightcove-cmsapi
22
+
23
+ ## Setup
24
+
25
+ To use this library you will require a client ID and secret, as well as your brightcove account's ID.
26
+ You can find instructions of obtaining these credentials in the [Brightcove docs](https://support.brightcove.com/managing-api-authentication-credentials).
27
+
28
+ ** Setup a basic client **
29
+
30
+ ```ruby
31
+ @client = Brightcove::Cmsapi.new(
32
+ account_id: "my_account_id",
33
+ client_id: "my_client_id",
34
+ client_secret: "my_client_secret")
35
+ ```
36
+
37
+ One alternative to this setup that can save you time and boilerplate code would be to call the `.default_api` method.
38
+ This assumes you have your credentials and account ID set as environment variables `BRIGHTCOVE_ACCOUNT_ID` `BRIGHTCOVE_CLIENT_ID` `BRIGHTCOVE_CLIENT_SECRET`.
39
+
40
+ ** Alternatively: don't setup the client each time (RECOMMENDED USAGE) **
41
+
42
+ ```ruby
43
+ Brightcove::Cmsapi.default_api.get('videos')
44
+ ```
45
+
46
+ ## Usage examples
47
+
48
+ ** Get the available videos in account (by default the first 20) **
49
+
50
+ ```ruby
51
+ @client.get('videos')
52
+
53
+ # or use the default_api setup:
54
+ Brightcove::Cmsapi.default_api.get('videos')
55
+ ```
56
+
57
+ Calling `.get` uses the default API settings and simply appends the argument to the API call like this:
58
+
59
+ `https://cms.api.brightcove.com/v1/accounts/:account_id/videos`
60
+
61
+ The result will be a `HTTP::Response` object (the gem uses the [http.rb gem](https://github.com/httprb/http)).
62
+ To parse the JSON in this response into a Ruby hash call `.parse`.
63
+
64
+ The API defaults are described in [Brightcove's documentation](https://brightcovelearning.github.io/Brightcove-API-References/cms-api/v1/doc/index.html).
65
+ To get more than the default 20 results you could call `.get('videos?limit=100')`, however there is a hard limit at 100 results that you can not exceed.
66
+ To get around this limit you can use the `.get_all` method as displayed below, this will paginate through the results and return a parsed set of all
67
+ a particular resource.
68
+
69
+ ** Get all available videos **
70
+
71
+ ```ruby
72
+ Brightcove::Cmsapi.default_api.get_all('video')
73
+ ```
74
+
75
+ ** Get all available videos in a folder **
76
+
77
+ ```ruby
78
+ Brightcove::Cmsapi.default_api.get_all('folder/:folder_id', 'video')
79
+ ```
80
+
81
+ ** Get all available videos in a folder as an array of ruby hashes **
82
+
83
+ ```ruby
84
+ Brightcove::Cmsapi.default_api.get_all('folder/:folder_id', 'video').parse
85
+ ```
86
+
87
+ ## Development
88
+
89
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
90
+
91
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "brightcove/cmsapi"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "brightcove-cmsapi"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Daniel King"]
9
+ spec.email = ["daniel.king5@nhs.net"]
10
+
11
+ spec.summary = %q{A simple wrapper around Brightcove's CMS API}
12
+ spec.homepage = "https://nhs.uk"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ spec.required_ruby_version = '~> 2.3.1'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.15"
24
+ spec.add_runtime_dependency "http", "~> 2.2.2"
25
+ end
@@ -0,0 +1,61 @@
1
+ require "http"
2
+
3
+ module Brightcove
4
+ class Cmsapi
5
+ OAUTH_ENDPOINT = "https://oauth.brightcove.com/v4/access_token"
6
+ API_ROOT = "https://cms.api.brightcove.com/v1/accounts"
7
+
8
+ def initialize(account_id:, client_id:, client_secret:)
9
+ @base_url = "#{API_ROOT}/#{account_id}"
10
+ @client_id = client_id
11
+ @client_secret = client_secret
12
+ set_token
13
+ end
14
+
15
+ def self.default_api
16
+ @default_api ||= new(
17
+ account_id: ENV['BRIGHTCOVE_ACCOUNT_ID'],
18
+ client_id: ENV['BRIGHTCOVE_CLIENT_ID'],
19
+ client_secret: ENV['BRIGHTCOVE_CLIENT_SECRET'])
20
+ end
21
+
22
+ def get(path)
23
+ set_token if @token_expires < Time.now
24
+ response = HTTP.auth("Bearer #{@token}").get("#{@base_url}/#{path}")
25
+
26
+ if response.code == 401 # Unauthorized, token expired
27
+ set_token
28
+ HTTP.auth("Bearer #{@token}").get("#{@base_url}/#{path}")
29
+ else
30
+ response
31
+ end
32
+ end
33
+
34
+ def get_all(path="", resource)
35
+ if path.empty?
36
+ count = get("counts/#{resource}s").parse.fetch("count")
37
+ resource_path = "#{resource}s"
38
+ else
39
+ count = get(path).parse.fetch("#{resource}_count")
40
+ resource_path = "#{path}/#{resource}s"
41
+ end
42
+ offset = 0
43
+ resources = []
44
+ while offset < count do
45
+ resources.concat(get("#{resource_path}?limit=100&offset=#{offset}").parse)
46
+ offset = offset + 100
47
+ end
48
+ resources
49
+ end
50
+
51
+ private
52
+
53
+ def set_token
54
+ response = HTTP.basic_auth(user: @client_id, pass: @client_secret)
55
+ .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")
59
+ end
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brightcove-cmsapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel King
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.2
41
+ description:
42
+ email:
43
+ - daniel.king5@nhs.net
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - brightcove-cmsapi.gemspec
58
+ - lib/brightcove/cmsapi.rb
59
+ homepage: https://nhs.uk
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: 2.3.1
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.5.1
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: A simple wrapper around Brightcove's CMS API
83
+ test_files: []