openlive 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 1.9.3
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in openlive.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Robert May
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,42 @@
1
+ # Openlive
2
+
3
+ [![Code Climate](https://codeclimate.com/github/piratestudios/openlive/badges/gpa.svg)](https://codeclimate.com/github/piratestudios/openlive)
4
+
5
+ This is an API wrapper for the OpenLIVE API, a music recording service.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'openlive'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install openlive
23
+
24
+ ## Usage
25
+
26
+ TODO: Write usage instructions here
27
+
28
+ ## Development
29
+
30
+ 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.
31
+
32
+ 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).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/openlive.
37
+
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
42
+
@@ -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 "openlive"
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
@@ -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,28 @@
1
+ require "gem_config"
2
+ require "openlive/version"
3
+ require "openlive/oauth"
4
+ require "openlive/base"
5
+
6
+ module Openlive
7
+ include GemConfig::Base
8
+
9
+ with_configuration do
10
+ has :oauth_settings, classes: Hash, default: {
11
+ site: "https://staging-identity.openlive.co",
12
+ authorize_url: "/connect/authorize",
13
+ token_url: "/connect/token",
14
+ scope: "tenant"
15
+ }
16
+
17
+ has :oauth_credentials, classes: Hash, default: {
18
+ client_id: ENV['OPENLIVE_OAUTH_CLIENT_ID'],
19
+ client_secret: ENV['OPENLIVE_OAUTH_CLIENT_SECRET'],
20
+ }
21
+
22
+ has :default_headers, classes: Hash, default: {}
23
+ has :base_uri, classes: String, default: (ENV['OPENLIVE_BASE_URI'] || "https://api.openlive.co/v1")
24
+ end
25
+
26
+ class Error < StandardError; end
27
+ class APIError < Error; end
28
+ end
@@ -0,0 +1,47 @@
1
+ module Openlive
2
+ class Artist < Base
3
+ class << self
4
+ # Find and return an artist record
5
+ #
6
+ # @param id [String]
7
+ # @return [Artist]
8
+ # @raise [APIError] Will raise an error on unsuccessful response
9
+ def find(id)
10
+ response = Request.get("artists/#{id}")
11
+
12
+ handle_response(response, error_class: APIError) do |response|
13
+ new(response.body, response: response)
14
+ end
15
+ end
16
+
17
+ # Create a new artist on Openlive
18
+ #
19
+ # @param [Hash] attributes A hash of attributes to set
20
+ # @option attributes [String] :name
21
+ # @option attributes [String] :userId
22
+ # @return [Artist] the created artist object
23
+ # @raise [APIError] Will raise an error on unsuccessful response
24
+ def create(attributes)
25
+ response = Request.post("artists", attributes)
26
+
27
+ handle_response(response, error_class: APIError) do |response|
28
+ new(response.body, response: response)
29
+ end
30
+ end
31
+
32
+ # Fetch and return a list of all artists
33
+ #
34
+ # @return [Array<Artist>]
35
+ # @raise [APIError] Will raise an error on unsuccessful response
36
+ def all
37
+ response = Request.get("artists")
38
+
39
+ handle_response(response, error_class: APIError) do |response|
40
+ response.body['data'].map do |a|
41
+ new(a, response: response)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,113 @@
1
+ module Openlive
2
+ class Base
3
+ extend Forwardable
4
+
5
+ # @return [Hash] a hash of data returned from the API
6
+ attr_accessor :api_data
7
+
8
+ # Convenience method for accessing the API response data
9
+ attr_accessor :response
10
+
11
+ # Initialize an instance (used by subclasses) with API data
12
+ #
13
+ # @param data [Hash]
14
+ # @return [Hash] the API data
15
+ def initialize(data, response: nil)
16
+ self.api_data = data
17
+ self.response = response
18
+ end
19
+
20
+ # Instance convenience method for the connection
21
+ #
22
+ # @return [Faraday::Connection]
23
+ def connection
24
+ self.class.connection
25
+ end
26
+
27
+ # Instance convenience method for oauth instance
28
+ #
29
+ # @return [Openlive::OAuth]
30
+ def oauth
31
+ self.class.oauth
32
+ end
33
+
34
+ # Refetch data from the API and update existing attributes
35
+ #
36
+ # @return [self]
37
+ def refresh
38
+ new_data = self.class.find(id)
39
+ self.api_data = new_data.api_data
40
+ self.response = new_data.response
41
+ self
42
+ end
43
+
44
+ # Pass method calls through to the API data
45
+ def method_missing(name, *args, &block)
46
+ if api_data.is_a?(Hash)
47
+ api_data[name.to_s]
48
+ end
49
+ end
50
+
51
+ class << self
52
+ # Faraday connection
53
+ #
54
+ # @return [Faraday::Connection]
55
+ def connection
56
+ @connection ||= (
57
+ conn = Faraday.new(url: Openlive.configuration.base_uri) do |faraday|
58
+ faraday.request :url_encoded
59
+ faraday.response :logger
60
+ faraday.adapter Faraday.default_adapter
61
+ end
62
+
63
+ conn.authorization(:Bearer, oauth.token.token)
64
+ conn.url_prefix = Openlive.configuration.base_uri
65
+ conn
66
+ )
67
+ end
68
+
69
+ # OAuth handler
70
+ #
71
+ # @return [Openlive::OAuth]
72
+ def oauth
73
+ @oauth ||= OAuth.new
74
+ end
75
+
76
+ # Raise an exception or execute the following block,
77
+ # used for generic error handling for all routes
78
+ #
79
+ # @param response [Response]
80
+ # @param error_class [OpenliveError]
81
+ # @param message [String] an optional message for the exception if raised
82
+ # @yield [Response] Block called for success condition
83
+ # @raise [OpenliveError] Will raise an error on unsuccessful response
84
+ def handle_response(response, error_class: Openlive::Error, message: nil, &block)
85
+ message = case
86
+ when !message.nil?
87
+ message
88
+ when error_class == Openlive::APIError
89
+ "endpoint returned a #{response.status} status: #{response.body}"
90
+ end
91
+
92
+ case
93
+ when response.success?
94
+ block.call(response)
95
+ when response.status == 404
96
+ nil
97
+ else
98
+ raise error_class, message
99
+ end
100
+
101
+ rescue Exception => ex
102
+ raise error_class, ex.message
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ require "openlive/request"
109
+ require "openlive/response"
110
+ require "openlive/artist"
111
+ require "openlive/booking"
112
+ require "openlive/master_builder"
113
+ require "openlive/user"
@@ -0,0 +1,68 @@
1
+ module Openlive
2
+ class Booking < Base
3
+ # Convenience method for deleting this booking
4
+ #
5
+ # @return [Truthy] whether the record was successfully deleted or not
6
+ # @raise [APIError] Will raise an error on unsuccessful response
7
+ def delete
8
+ self.class.delete(id)
9
+ end
10
+
11
+ class << self
12
+ # Find and return a booking record
13
+ #
14
+ # @param id [String]
15
+ # @return [Booking]
16
+ # @raise [APIError] Will raise an error on unsuccessful response
17
+ def find(id)
18
+ response = Request.get("bookings/#{id}")
19
+
20
+ handle_response(response, error_class: APIError) do |response|
21
+ new(response.body, response: response)
22
+ end
23
+ end
24
+
25
+ # Create a new booking on Openlive
26
+ #
27
+ # @param [Hash] attributes A hash of attributes to set
28
+ # @option attributes [String] :artistId
29
+ # @option attributes [String] :masterbuilderId
30
+ # @option attributes [Time] :start
31
+ # @option attributes [Time] :finish
32
+ # @return [Booking] the created booking object
33
+ # @raise [APIError] Will raise an error on unsuccessful response
34
+ def create(attributes)
35
+ response = Request.post("bookings", format_attributes(attributes))
36
+
37
+ handle_response(response, error_class: APIError) do |response|
38
+ new(response.body, response: response)
39
+ end
40
+ end
41
+
42
+ # Delete an existing booking on Openlive
43
+ #
44
+ # @param [String] id The booking ID
45
+ # @return [Truthy] whether the record was successfully deleted or not
46
+ # @raise [APIError] Will raise an error on unsuccessful response
47
+ def delete(id)
48
+ response = Request.delete("bookings/#{id}")
49
+
50
+ handle_response(response, error_class: APIError) do |response|
51
+ response.response.success?
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def format_attributes(attributes)
58
+ attributes.update(attributes) do |key, val|
59
+ if val.is_a?(Time) || val.is_a?(DateTime)
60
+ val.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
61
+ else
62
+ val
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,32 @@
1
+ module Openlive
2
+ class MasterBuilder < Base
3
+ class << self
4
+ # Find and return a masterbuilder record
5
+ #
6
+ # @param id [String]
7
+ # @return [MasterBuilder]
8
+ # @raise [APIError] Will raise an error on unsuccessful response
9
+ def find(id)
10
+ response = Request.get("masterbuilders/#{id}")
11
+
12
+ handle_response(response, error_class: APIError) do |response|
13
+ new(response.body, response: response)
14
+ end
15
+ end
16
+
17
+ # Fetch and return a list of all master builder units
18
+ #
19
+ # @return [Array<MasterBuilder>]
20
+ # @raise [APIError] Will raise an error on unsuccessful response
21
+ def all
22
+ response = Request.get("masterbuilders")
23
+
24
+ handle_response(response, error_class: Openlive::APIError) do |response|
25
+ response.body['data'].map do |mb|
26
+ new(mb, response: response)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,46 @@
1
+ require "oauth2"
2
+ require "oauth2/access_token"
3
+
4
+ module Openlive
5
+ class OAuth
6
+ # @return [OAuth2::AccessToken] Used to store the existing token
7
+ attr_accessor :current_token
8
+
9
+ # Return or instantiate the OAuth client
10
+ #
11
+ # @return [OAuth2::Client]
12
+
13
+ def client
14
+ @client ||= (
15
+ credentials = Openlive.configuration.oauth_credentials
16
+
17
+ OAuth2::Client.new(
18
+ credentials[:client_id],
19
+ credentials[:client_secret],
20
+ Openlive.configuration.oauth_settings
21
+ )
22
+ )
23
+ end
24
+
25
+ # Return an existing unexpired token for this OAuth instance
26
+ # or requisition a new one from the server.
27
+ #
28
+ # @return [OAuth2::AccessToken]
29
+
30
+ def token
31
+ if current_token.nil? || current_token.expired?
32
+ requisition_token
33
+ else
34
+ current_token
35
+ end
36
+ end
37
+
38
+ # Fetch a new token from the OAuth server
39
+ #
40
+ # @return [OAuth2::AccessToken]
41
+
42
+ def requisition_token
43
+ self.current_token = client.client_credentials.get_token(scope: Openlive.configuration.oauth_settings[:scope])
44
+ end
45
+ end
46
+ end