twitchrb 0.1.0 → 0.2.2
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.
- checksums.yaml +4 -4
- data/.env.example +3 -0
- data/.github/FUNDING.yml +4 -0
- data/.gitignore +1 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +28 -9
- data/README.md +46 -6
- data/bin/console +5 -0
- data/lib/twitch/client.rb +103 -65
- data/lib/twitch/collection.rb +21 -0
- data/lib/twitch/error.rb +4 -0
- data/lib/twitch/object.rb +19 -0
- data/lib/twitch/objects/badge.rb +4 -0
- data/lib/twitch/objects/banned_event.rb +4 -0
- data/lib/twitch/objects/banned_user.rb +4 -0
- data/lib/twitch/objects/blocked_user.rb +4 -0
- data/lib/twitch/objects/channel.rb +4 -0
- data/lib/twitch/objects/channel_editor.rb +4 -0
- data/lib/twitch/objects/clip.rb +4 -0
- data/lib/twitch/objects/custom_reward.rb +4 -0
- data/lib/twitch/objects/custom_reward_redemption.rb +4 -0
- data/lib/twitch/objects/emote.rb +4 -0
- data/lib/twitch/objects/event_sub_subscription.rb +4 -0
- data/lib/twitch/objects/followed_user.rb +4 -0
- data/lib/twitch/objects/game.rb +4 -0
- data/lib/twitch/objects/goal.rb +4 -0
- data/lib/twitch/objects/hype_train_event.rb +4 -0
- data/lib/twitch/objects/moderator.rb +4 -0
- data/lib/twitch/objects/moderator_event.rb +4 -0
- data/lib/twitch/objects/poll.rb +4 -0
- data/lib/twitch/objects/prediction.rb +4 -0
- data/lib/twitch/objects/search_result.rb +4 -0
- data/lib/twitch/objects/stream.rb +4 -0
- data/lib/twitch/objects/stream_marker.rb +4 -0
- data/lib/twitch/objects/stream_schedule.rb +4 -0
- data/lib/twitch/objects/subscription.rb +4 -0
- data/lib/twitch/objects/subscription_count.rb +4 -0
- data/lib/twitch/objects/tag.rb +4 -0
- data/lib/twitch/objects/user.rb +4 -0
- data/lib/twitch/objects/video.rb +4 -0
- data/lib/twitch/resource.rb +57 -0
- data/lib/twitch/resources/badges.rb +15 -0
- data/lib/twitch/resources/banned_events.rb +10 -0
- data/lib/twitch/resources/banned_users.rb +11 -0
- data/lib/twitch/resources/channels.rb +20 -0
- data/lib/twitch/resources/clips.rb +23 -0
- data/lib/twitch/resources/custom_reward_redemptions.rb +22 -0
- data/lib/twitch/resources/custom_rewards.rb +30 -0
- data/lib/twitch/resources/emotes.rb +20 -0
- data/lib/twitch/resources/event_sub_subscriptions.rb +21 -0
- data/lib/twitch/resources/games.rb +20 -0
- data/lib/twitch/resources/goals.rb +12 -0
- data/lib/twitch/resources/hype_train_events.rb +12 -0
- data/lib/twitch/resources/moderator_events.rb +11 -0
- data/lib/twitch/resources/moderators.rb +11 -0
- data/lib/twitch/resources/polls.rb +27 -0
- data/lib/twitch/resources/predictions.rb +32 -0
- data/lib/twitch/resources/search.rb +17 -0
- data/lib/twitch/resources/stream_markers.rb +19 -0
- data/lib/twitch/resources/stream_schedule.rb +47 -0
- data/lib/twitch/resources/streams.rb +19 -0
- data/lib/twitch/resources/subscriptions.rb +32 -0
- data/lib/twitch/resources/tags.rb +20 -0
- data/lib/twitch/resources/users.rb +54 -0
- data/lib/twitch/resources/videos.rb +17 -0
- data/lib/twitch/version.rb +1 -1
- data/lib/twitch.rb +63 -29
- data/twitchrb.gemspec +3 -2
- metadata +79 -13
- data/.travis.yml +0 -6
- data/lib/twitch/initializable.rb +0 -16
- data/lib/twitch/kraken/channels.rb +0 -22
- data/lib/twitch/kraken/clips.rb +0 -22
- data/lib/twitch/kraken/user.rb +0 -23
- data/lib/twitch/kraken/users.rb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30b0eac63e649a017a1c4938b034886f1934da667145ca984f7d2898220d9c91
|
4
|
+
data.tar.gz: 97883516313836adc1a003649904b532c66ce5f14568981d5f82b21b807f489b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5337a58e97212f82d77208f41a0af2b2b0f5b36c3652b7905c51af117e67767affe6a1388e647ed8e4f8ed307488084ee91989cc1a21494265dd5abba9055eea
|
7
|
+
data.tar.gz: 4a95c514a5d1addd4eebd265ba04ba79ae945913042ee4245d07a563e69bf37d365a49d31078130c425a5b545128886058211db96cc3444ff32bd56ebaa31490
|
data/.env.example
ADDED
data/.github/FUNDING.yml
ADDED
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,26 +1,45 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
twitchrb (0.
|
5
|
-
|
4
|
+
twitchrb (0.2.2)
|
5
|
+
faraday (~> 1.7)
|
6
|
+
faraday_middleware (~> 1.1)
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
9
10
|
specs:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
dotenv (2.7.6)
|
12
|
+
faraday (1.8.0)
|
13
|
+
faraday-em_http (~> 1.0)
|
14
|
+
faraday-em_synchrony (~> 1.0)
|
15
|
+
faraday-excon (~> 1.1)
|
16
|
+
faraday-httpclient (~> 1.0.1)
|
17
|
+
faraday-net_http (~> 1.0)
|
18
|
+
faraday-net_http_persistent (~> 1.1)
|
19
|
+
faraday-patron (~> 1.0)
|
20
|
+
faraday-rack (~> 1.0)
|
21
|
+
multipart-post (>= 1.2, < 3)
|
22
|
+
ruby2_keywords (>= 0.0.4)
|
23
|
+
faraday-em_http (1.0.0)
|
24
|
+
faraday-em_synchrony (1.0.0)
|
25
|
+
faraday-excon (1.1.0)
|
26
|
+
faraday-httpclient (1.0.1)
|
27
|
+
faraday-net_http (1.0.1)
|
28
|
+
faraday-net_http_persistent (1.2.0)
|
29
|
+
faraday-patron (1.0.0)
|
30
|
+
faraday-rack (1.0.0)
|
31
|
+
faraday_middleware (1.2.0)
|
32
|
+
faraday (~> 1.0)
|
16
33
|
minitest (5.14.1)
|
17
|
-
|
34
|
+
multipart-post (2.1.1)
|
18
35
|
rake (12.3.3)
|
36
|
+
ruby2_keywords (0.0.5)
|
19
37
|
|
20
38
|
PLATFORMS
|
21
39
|
ruby
|
22
40
|
|
23
41
|
DEPENDENCIES
|
42
|
+
dotenv
|
24
43
|
minitest (~> 5.0)
|
25
44
|
rake (~> 12.0)
|
26
45
|
twitchrb!
|
data/README.md
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
**This library is a work in progress**
|
4
4
|
|
5
|
-
|
5
|
+
TwitchRB is a Ruby library for intereacting with the Twitch API.
|
6
6
|
|
7
|
-
It
|
7
|
+
It only supports the Helix API as v5 is deprecated.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -14,15 +14,55 @@ Add this line to your application's Gemfile:
|
|
14
14
|
gem "twitchrb", require: "twitch"
|
15
15
|
```
|
16
16
|
|
17
|
-
|
17
|
+
## Usage
|
18
18
|
|
19
|
-
|
19
|
+
### Set Client Details
|
20
20
|
|
21
|
+
Firstly you'll need to set a Client ID, Secret Key and an Access Token.
|
21
22
|
|
22
|
-
|
23
|
+
An access token is required because the Helix API requires authentication.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
@client = Twitch::Client.new(client_id: "", secret_key: "", access_token: "")
|
27
|
+
```
|
28
|
+
|
29
|
+
### Users
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
@client.users.get_by_id(user_id: 141981764)
|
33
|
+
@client.users.get_by_username(username: "twitchdev")
|
34
|
+
```
|
35
|
+
|
36
|
+
### Channels
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
@client.channels.get(broadcaster_id: 141981764)
|
40
|
+
```
|
41
|
+
|
42
|
+
### Videos
|
23
43
|
|
24
|
-
|
44
|
+
```ruby
|
45
|
+
@client.videos.get(user_id: 141981764)
|
46
|
+
```
|
47
|
+
|
48
|
+
### Emotes
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
@client.emotes.global
|
52
|
+
@client.emotes.channel(broadcaster_id: 141981764)
|
53
|
+
@client.emotes.sets(emote_set_id: 301590448)
|
54
|
+
```
|
55
|
+
|
56
|
+
### Badges
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
@client.badges.global
|
60
|
+
@client.badges.channel(broadcaster_id: 141981764)
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
25
64
|
|
65
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/twitchrb/twitchrb.
|
26
66
|
|
27
67
|
## License
|
28
68
|
|
data/bin/console
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "twitch"
|
5
5
|
|
6
|
+
# Load environment variables from .env file
|
7
|
+
require 'dotenv/load'
|
8
|
+
|
6
9
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
10
|
# with your gem easier. You can also use a different console, if you like.
|
8
11
|
|
@@ -10,5 +13,7 @@ require "twitch"
|
|
10
13
|
# require "pry"
|
11
14
|
# Pry.start
|
12
15
|
|
16
|
+
@client = Twitch::Client.new(client_id: ENV["TWITCH_CLIENT_ID"], client_secret: ENV["TWITCH_CLIENT_SECRET"], access_token: ENV["TWITCH_ACCESS_TOKEN"])
|
17
|
+
|
13
18
|
require "irb"
|
14
19
|
IRB.start(__FILE__)
|
data/lib/twitch/client.rb
CHANGED
@@ -1,89 +1,127 @@
|
|
1
|
-
require "httparty"
|
2
|
-
|
3
1
|
module Twitch
|
4
2
|
class Client
|
3
|
+
BASE_URL = "https://api.twitch.tv/helix"
|
5
4
|
|
6
|
-
|
7
|
-
def initialize(client_id)
|
8
|
-
puts "initialize"
|
5
|
+
attr_reader :client_id, :client_secret, :access_token, :adapter
|
9
6
|
|
7
|
+
def initialize(client_id:, client_secret:, access_token:, adapter: Faraday.default_adapter, stubs: nil)
|
10
8
|
@client_id = client_id
|
9
|
+
@client_secret = client_secret
|
10
|
+
@access_token = access_token
|
11
|
+
@adapter = adapter
|
11
12
|
|
12
|
-
#
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
raise Twitch::Errors::ValidationError, json['errors'].to_s
|
52
|
-
else
|
53
|
-
raise Twitch::Errors::CommunicationError, response.body
|
54
|
-
end
|
13
|
+
# Test stubs for requests
|
14
|
+
@stubs = stubs
|
15
|
+
end
|
16
|
+
|
17
|
+
def users
|
18
|
+
UsersResource.new(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
def channels
|
22
|
+
ChannelsResource.new(self)
|
23
|
+
end
|
24
|
+
|
25
|
+
def emotes
|
26
|
+
EmotesResource.new(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
def badges
|
30
|
+
BadgesResource.new(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def games
|
34
|
+
GamesResource.new(self)
|
35
|
+
end
|
36
|
+
|
37
|
+
def videos
|
38
|
+
VideosResource.new(self)
|
39
|
+
end
|
40
|
+
|
41
|
+
def clips
|
42
|
+
ClipsResource.new(self)
|
43
|
+
end
|
44
|
+
|
45
|
+
def eventsub_subscriptions
|
46
|
+
EventSubSubscriptionsResource.new(self)
|
47
|
+
end
|
48
|
+
|
49
|
+
def banned_events
|
50
|
+
BannedEventsResource.new(self)
|
51
|
+
end
|
55
52
|
|
53
|
+
def banned_users
|
54
|
+
BannedUsersResource.new(self)
|
55
|
+
end
|
56
|
+
|
57
|
+
def moderators
|
58
|
+
ModeratorsResource.new(self)
|
59
|
+
end
|
60
|
+
|
61
|
+
def moderator_events
|
62
|
+
ModeratorEventsResource.new(self)
|
63
|
+
end
|
56
64
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
65
|
+
def polls
|
66
|
+
PollsResource.new(self)
|
67
|
+
end
|
68
|
+
|
69
|
+
def predictions
|
70
|
+
PredictionsResource.new(self)
|
71
|
+
end
|
72
|
+
|
73
|
+
def stream_schedule
|
74
|
+
StreamScheduleResource.new(self)
|
75
|
+
end
|
64
76
|
|
77
|
+
def search
|
78
|
+
SearchResource.new(self)
|
79
|
+
end
|
65
80
|
|
81
|
+
def streams
|
82
|
+
StreamsResource.new(self)
|
83
|
+
end
|
66
84
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
# else
|
71
|
-
# raise Twitch::Errors::CommunicationError, http_result.body
|
72
|
-
# end
|
85
|
+
def stream_markers
|
86
|
+
StreamMarkersResource.new(self)
|
87
|
+
end
|
73
88
|
|
89
|
+
def subscriptions
|
90
|
+
SubscriptionsResource.new(self)
|
91
|
+
end
|
74
92
|
|
93
|
+
def tags
|
94
|
+
TagsResource.new(self)
|
75
95
|
end
|
76
96
|
|
97
|
+
def custom_rewards
|
98
|
+
CustomRewardsResource.new(self)
|
99
|
+
end
|
77
100
|
|
78
|
-
|
101
|
+
def custom_reward_redemptions
|
102
|
+
CustomRewardRedemptionsResource.new(self)
|
103
|
+
end
|
79
104
|
|
80
|
-
|
81
|
-
|
105
|
+
def goals
|
106
|
+
GoalsResource.new(self)
|
107
|
+
end
|
82
108
|
|
109
|
+
def hype_train_events
|
110
|
+
HypeTrainEventsResource.new(self)
|
111
|
+
end
|
83
112
|
|
113
|
+
def connection
|
114
|
+
@connection ||= Faraday.new(BASE_URL) do |conn|
|
115
|
+
conn.request :authorization, :Bearer, access_token
|
116
|
+
conn.headers = { "Client-ID": client_id }
|
117
|
+
conn.request :json
|
84
118
|
|
85
|
-
|
119
|
+
conn.response :dates
|
120
|
+
conn.response :json, content_type: "application/json"
|
86
121
|
|
122
|
+
conn.adapter adapter, @stubs
|
123
|
+
end
|
124
|
+
end
|
87
125
|
|
88
126
|
end
|
89
127
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Twitch
|
2
|
+
class Collection
|
3
|
+
attr_reader :data, :total, :cursor
|
4
|
+
|
5
|
+
def self.from_response(response, type:)
|
6
|
+
body = response.body
|
7
|
+
|
8
|
+
new(
|
9
|
+
data: body["data"].map { |attrs| type.new(attrs) },
|
10
|
+
total: body["data"].count,
|
11
|
+
cursor: body.dig("pagination", "cursor")
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(data:, total:, cursor:)
|
16
|
+
@data = data
|
17
|
+
@total = total
|
18
|
+
@cursor = cursor.nil? ? nil : cursor
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/twitch/error.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
module Twitch
|
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,57 @@
|
|
1
|
+
module Twitch
|
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 429
|
44
|
+
raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["error"]}'"
|
45
|
+
when 500
|
46
|
+
raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["error"]}'"
|
47
|
+
when 503
|
48
|
+
raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["error"]}'"
|
49
|
+
when 204
|
50
|
+
# 204 is a response for success on Twitch's API
|
51
|
+
return true
|
52
|
+
end
|
53
|
+
|
54
|
+
response
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Twitch
|
2
|
+
class BadgesResource < Resource
|
3
|
+
|
4
|
+
def channel(broadcaster_id:)
|
5
|
+
response = get_request("chat/badges?broadcaster_id=#{broadcaster_id}")
|
6
|
+
Collection.from_response(response, type: Badge)
|
7
|
+
end
|
8
|
+
|
9
|
+
def global
|
10
|
+
response = get_request("chat/badges/global")
|
11
|
+
Collection.from_response(response, type: Badge)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|