streamelements 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 31160ff2ea22fd4ede0596df856320841537e8641f1578b12648ee9c9b918799
4
+ data.tar.gz: d2a0068938a6f62b62e627165945324e4a41fddc8c58a805dbfe64bb474f16a7
5
+ SHA512:
6
+ metadata.gz: f368ed8782747b19f755d31500b2b0c2c1d4e30d6a52fe6ac98fefc427be720ea87afa0fd3d8b02481f75b7093d7da7801ffb93539de61822960d4ff9fa19918
7
+ data.tar.gz: 895c89c0e5f073caa1979d509014ea96146d109c9e443d7307968fb7f4f9449586d03e111576c20dc2ef92e040906474c54895623b3240e0bae99824af672947
data/.env.example ADDED
@@ -0,0 +1,2 @@
1
+ STREAMELEMENTS_JWT_ACCESS_TOKEN=
2
+ STREAMELEMENTS_CHANNEL=
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Dean Perry
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.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # StreamElements
2
+
3
+ This is a Ruby library for the StreamElements API. **Currently in development**
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "streamelements"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Authentication
16
+
17
+ Firstly you'll need to set either a JWT Token or an OAuth Access Token.
18
+
19
+ ```ruby
20
+ # Set a JWT Token
21
+ @client = StreamElements::Client.new(jwt_access_token: "")
22
+
23
+ # Or set an OAuth Access Token
24
+ @client = StreamElements::Client.new(access_token: "")
25
+ ```
26
+
27
+ Most of the API requires the Channel ID to be passed as an argument.
28
+
29
+ ### Activities
30
+
31
+ ```ruby
32
+ # Retrieve a list of Activities
33
+ # https://dev.streamelements.com/docs/api-docs/861a5d5450bbb-channel
34
+ @client.activities.list(channel: "channel-id")
35
+
36
+ # Retrieve the Top Activities
37
+ # https://dev.streamelements.com/docs/api-docs/2ce44d058b16b-channel-top
38
+ @client.actvitities.top(channel: "channel-id")
39
+ ```
40
+
41
+ ### Tips
42
+
43
+ ```ruby
44
+ # Retrieve a list of Tips
45
+ # https://dev.streamelements.com/docs/api-docs/704e5580be2d9-channel
46
+ @client.tips.list(channel: "channel-id")
47
+
48
+ # Retrieve a Tip
49
+ @client.tips.retrieve(channel: "channel-id", id: "tip-id")
50
+
51
+ # Retrieve the Tip Tips
52
+ # https://dev.streamelements.com/docs/api-docs/b404f906817c4-channel-top
53
+ @client.tips.top(channel: "channel-id")
54
+ ```
55
+
56
+ ### Song Requests
57
+
58
+ ```ruby
59
+ # Retrieve the current playing song
60
+ @client.song_requests.playing(channel: "channel-id")
61
+
62
+ # Retrieve a list of songs in the queue
63
+ @client.song_requests.queue(channel: "channel-id")
64
+
65
+ # Retrieve a list of previous songs
66
+ @client.song_requests.queue(channel: "channel-id")
67
+
68
+ # Add a song to the queue
69
+ # video is the YouTube video ID or URL
70
+ @client.song_requests.add(channel: "channel-id", video: "video-id")
71
+
72
+ # Pause the player
73
+ @client.song_requests.pause(channel: "channel-id")
74
+
75
+ # Resume the player
76
+ @client.song_requests.play(channel: "channel-id")
77
+
78
+ # Set the volume of the player
79
+ # volume is a number between 0 and 100
80
+ @client.song_requests.volume(channel: "channel-id", volume: 50)
81
+
82
+ # Skip the current song
83
+ @client.song_requests.skip(channel: "channel-id")
84
+ ```
85
+
86
+ ## Contributing
87
+
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/deanpcmad/streamelements.
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
93
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,48 @@
1
+ module StreamElements
2
+ class Client
3
+ BASE_URL = "https://api.streamelements.com/kappa/v2"
4
+
5
+ attr_reader :jwt_access_token, :access_token, :adapter
6
+
7
+ def initialize(jwt_access_token: nil, access_token: nil, adapter: Faraday.default_adapter)
8
+ @jwt_access_token = jwt_access_token
9
+ @access_token = access_token
10
+ @adapter = adapter
11
+ end
12
+
13
+ def activities
14
+ ActivitiesResource.new(self)
15
+ end
16
+
17
+ def tips
18
+ TipsResource.new(self)
19
+ end
20
+
21
+ def song_requests
22
+ SongRequestsResource.new(self)
23
+ end
24
+
25
+ def connection
26
+ @connection ||= Faraday.new(BASE_URL) do |conn|
27
+ if jwt_access_token
28
+ conn.request :authorization, :Bearer, jwt_access_token
29
+ elsif access_token
30
+ conn.request :authorization, :Bearer, access_token
31
+ else
32
+ raise Error, "You must provide a jwt or access token."
33
+ end
34
+
35
+ conn.headers = {
36
+ "User-Agent" => "streamelements/v#{VERSION} (github.com/deanpcmad/streamelements)"
37
+ }
38
+
39
+ conn.request :json
40
+
41
+ conn.response :json, content_type: "application/json"
42
+
43
+ conn.adapter adapter, @stubs
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,23 @@
1
+ module StreamElements
2
+ class Collection
3
+ attr_reader :data, :total
4
+
5
+ def self.from_response(response, type:, name: nil)
6
+ if name.nil?
7
+ body = response.body
8
+ else
9
+ body = response.body[name]
10
+ end
11
+
12
+ new(
13
+ data: body.map { |attrs| type.new(attrs) },
14
+ total: body.count,
15
+ )
16
+ end
17
+
18
+ def initialize(data:, total:)
19
+ @data = data
20
+ @total = total
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ module StreamElements
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+
3
+ module StreamElements
4
+ class Object < OpenStruct
5
+ def initialize(attributes)
6
+ super to_ostruct(attributes)
7
+ end
8
+
9
+ def to_ostruct(obj)
10
+ if obj.is_a?(Hash)
11
+ OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
12
+ elsif obj.is_a?(Array)
13
+ obj.map { |o| to_ostruct(o) }
14
+ else # Assumed to be a primitive value
15
+ obj
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ module StreamElements
2
+ class Activity < Object
3
+
4
+ def initialize(attributes = {})
5
+ attributes["id"] = attributes.delete("_id") if attributes["_id"]
6
+ super
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module StreamElements
2
+ class SongRequest < Object
3
+
4
+ def initialize(attributes = {})
5
+ attributes["id"] = attributes.delete("_id") if attributes["_id"]
6
+ super
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module StreamElements
2
+ class Tip < Object
3
+
4
+ def initialize(attributes = {})
5
+ attributes["id"] = attributes.delete("_id") if attributes["_id"]
6
+ super
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ module StreamElements
2
+ class TopActivity < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StreamElements
2
+ class TopTip < Object
3
+ end
4
+ end
@@ -0,0 +1,58 @@
1
+ module StreamElements
2
+ class Resource
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ private
10
+
11
+ def get_request(url, params: {}, headers: {})
12
+ handle_response client.connection.get(url, params, headers)
13
+ end
14
+
15
+ def post_request(url, body:, headers: {})
16
+ handle_response client.connection.post(url, body, headers)
17
+ end
18
+
19
+ def patch_request(url, body:, headers: {})
20
+ handle_response client.connection.patch(url, body, headers)
21
+ end
22
+
23
+ def put_request(url, body:, headers: {})
24
+ handle_response client.connection.put(url, body, headers)
25
+ end
26
+
27
+ def delete_request(url, params: {}, headers: {})
28
+ handle_response client.connection.delete(url, params, headers)
29
+ end
30
+
31
+ def handle_response(response)
32
+ case response.status
33
+ when 400
34
+ raise Error, "Error 400: Your request was malformed. '#{response.body["message"]}'"
35
+ when 401
36
+ raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["error"]}'"
37
+ when 403
38
+ raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["error"]}'"
39
+ when 404
40
+ raise Error, "Error 404: No results were found for your request. '#{response.body["error"]}'"
41
+ when 409
42
+ raise Error, "Error 409: Your request was a conflict. '#{response.body["message"]}'"
43
+ when 422
44
+ raise Error, "Error 422: Unprocessable Entity. '#{response.body["message"]}"
45
+ when 429
46
+ raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["error"]}'"
47
+ when 500
48
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["error"]}'"
49
+ when 503
50
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["error"]}'"
51
+ when 204
52
+ return true
53
+ end
54
+
55
+ response
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ module StreamElements
2
+ class ActivitiesResource < Resource
3
+
4
+ def list(channel:, after:, before:, limit:, types:, **params)
5
+ attrs = { after: after, before: before, limit: limit, types: types }.merge(params)
6
+ response = get_request("activities/#{channel}", params: attrs)
7
+ Collection.from_response(response, type: Activity)
8
+ end
9
+
10
+ def top(channel:, period:, offset:, limit:, type:)
11
+ attrs = { period: period, offset: offset, limit: limit, type: type }
12
+ response = get_request("activities/#{channel}/top", params: attrs)
13
+ Collection.from_response(response, type: TopActivity)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ module StreamElements
2
+ class SongRequestsResource < Resource
3
+
4
+ def playing(channel:)
5
+ SongRequest.new get_request("songrequest/#{channel}/playing").body
6
+ end
7
+
8
+ def queue(channel:)
9
+ response = get_request("songrequest/#{channel}/queue")
10
+ Collection.from_response(response, type: SongRequest)
11
+ end
12
+
13
+ def history(channel:, **params)
14
+ response = get_request("songrequest/#{channel}/history", params: params)
15
+ Collection.from_response(response, type: SongRequest, name: "history")
16
+ end
17
+
18
+ def add(channel:, video:)
19
+ SongRequest.new post_request("songrequest/#{channel}/queue", body: { video: video }).body
20
+ end
21
+
22
+ def pause(channel:)
23
+ response = post_request("songrequest/#{channel}/player/pause", body: {})
24
+ response.success?
25
+ end
26
+
27
+ def play(channel:)
28
+ response = post_request("songrequest/#{channel}/player/play", body: {})
29
+ response.success?
30
+ end
31
+
32
+ def volume(channel:, volume:)
33
+ response = post_request("songrequest/#{channel}/player/volume", body: {volume: volume})
34
+ response.success?
35
+ end
36
+
37
+ def skip(channel:)
38
+ response = post_request("songrequest/#{channel}/skip", body: {})
39
+ response.success?
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ module StreamElements
2
+ class TipsResource < Resource
3
+
4
+ def list(channel:, **params)
5
+ response = get_request("tips/#{channel}", params: params)
6
+ Collection.from_response(response, type: Tip, name: "docs")
7
+ end
8
+
9
+ def retrieve(channel:, id:)
10
+ Tip.new get_request("tips/#{channel}/#{id}").body
11
+ end
12
+
13
+ def top(channel:)
14
+ response = get_request("tips/#{channel}/top")
15
+ Collection.from_response(response, type: TopTip)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StreamElements
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+ require "stream_elements/version"
6
+
7
+ module StreamElements
8
+
9
+ autoload :Client, "stream_elements/client"
10
+ autoload :Collection, "stream_elements/collection"
11
+ autoload :Error, "stream_elements/error"
12
+ autoload :Resource, "stream_elements/resource"
13
+ autoload :Object, "stream_elements/object"
14
+
15
+ autoload :ActivitiesResource, "stream_elements/resources/activities"
16
+ autoload :TipsResource, "stream_elements/resources/tips"
17
+ autoload :SongRequestsResource, "stream_elements/resources/song_requests"
18
+
19
+ autoload :Activity, "stream_elements/objects/activity"
20
+ autoload :TopActivity, "stream_elements/objects/top_activity"
21
+ autoload :Tip, "stream_elements/objects/tip"
22
+ autoload :TopTip, "stream_elements/objects/top_tip"
23
+ autoload :SongRequest, "stream_elements/objects/song_request"
24
+
25
+ end
@@ -0,0 +1 @@
1
+ require "stream_elements"
@@ -0,0 +1,4 @@
1
+ module Streamelements
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: streamelements
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dean Perry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ description:
28
+ email:
29
+ - deanperry@fastmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".env.example"
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - lib/stream_elements.rb
39
+ - lib/stream_elements/client.rb
40
+ - lib/stream_elements/collection.rb
41
+ - lib/stream_elements/error.rb
42
+ - lib/stream_elements/object.rb
43
+ - lib/stream_elements/objects/activity.rb
44
+ - lib/stream_elements/objects/song_request.rb
45
+ - lib/stream_elements/objects/tip.rb
46
+ - lib/stream_elements/objects/top_activity.rb
47
+ - lib/stream_elements/objects/top_tip.rb
48
+ - lib/stream_elements/resource.rb
49
+ - lib/stream_elements/resources/activities.rb
50
+ - lib/stream_elements/resources/song_requests.rb
51
+ - lib/stream_elements/resources/tips.rb
52
+ - lib/stream_elements/version.rb
53
+ - lib/streamelements.rb
54
+ - sig/streamelements.rbs
55
+ homepage: https://github.com/deanpcmad/streamelements
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.6.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.4.22
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Ruby library for the StreamElements API
78
+ test_files: []