twitch-api 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -7
- data/lib/twitch/api_error.rb +13 -0
- data/lib/twitch/client.rb +48 -17
- data/lib/twitch/game.rb +3 -3
- data/lib/twitch/response.rb +15 -0
- data/lib/twitch/stream_metadata.rb +19 -0
- data/lib/twitch/user_follow.rb +11 -0
- data/lib/twitch/version.rb +1 -1
- data/lib/twitch/video.rb +2 -1
- data/twitch-api.gemspec +2 -0
- metadata +34 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb54134c100870a945316675e056f7c32c0cd67ea354d19bfba277d7a1660fb1
|
4
|
+
data.tar.gz: 4fa7aff219b7993eb679d7444e7ebd35d309923d3ffcc52daa5d3130ec2ce6b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a8429635b999c142b06996ed11ff36d35b10dac4ed68d255bd6eefd9341e3360f13e3d8c1b8ce8ac303efccde1ff48fdba9f613396971ffeef7129b28af8680
|
7
|
+
data.tar.gz: 51b75f09e9df2d67045e0c9b8e32f707425156641066789b6544e8384cc2b20cc532a274c698a30b5e2ae5e49f2269f5b920b67e58676642fdbf6705db9a2488
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
This library is a Ruby implementation of the [Twitch Helix API](https://dev.twitch.tv/docs/api).
|
4
4
|
|
5
5
|
## Installation
|
6
|
+
Ruby 2.1 or later is required.
|
6
7
|
|
7
8
|
Add this line to your application's Gemfile:
|
8
9
|
|
@@ -19,21 +20,22 @@ Or install it yourself as:
|
|
19
20
|
$ gem install twitch-api
|
20
21
|
|
21
22
|
## Usage
|
22
|
-
NOTE: **These procedures are subject to change** while both the API and this library are in an early state.
|
23
|
-
|
24
23
|
A client must be initialized with your client ID, and optionally an OAuth access token.
|
25
24
|
```
|
26
25
|
client = Twitch::Client.new client_id: "YOUR_CLIENT_ID"
|
27
26
|
```
|
28
|
-
The retrieval methods take in a hash equal to the GET parameters of the API endpoint, and return
|
27
|
+
The retrieval methods take in a hash equal to the GET parameters of the API endpoint, and return a response object containing the data and other associated request information:
|
28
|
+
* **data** is the data you would get back
|
29
|
+
* **rate_limit** contains the number of API requests that can be made in total within a 60-second window. See **rate_limit_remaining** and **rate_limit_resets_at** to determine the current allowed rate.
|
30
|
+
* **pagination** (optional) is an hash that usually contains one member (*cursor*) which lets you pagniate through certain requests.
|
29
31
|
```
|
30
32
|
# Get top live streams
|
31
|
-
client.get_streams
|
33
|
+
client.get_streams.data
|
32
34
|
# Get a single user
|
33
|
-
client.get_users({login: "disguisedtoasths"}).first
|
35
|
+
client.get_users({login: "disguisedtoasths"}).data.first
|
34
36
|
# Find some games
|
35
37
|
# (use an array for up to 100 of most queryable resources)
|
36
|
-
client.get_games({name: ["Heroes of the Storm", "Super Mario Odyssey"]})
|
38
|
+
client.get_games({name: ["Heroes of the Storm", "Super Mario Odyssey"]}).data
|
37
39
|
```
|
38
40
|
|
39
41
|
## Development
|
@@ -44,7 +46,7 @@ To install this gem onto your local machine, run `bundle exec rake install`.
|
|
44
46
|
|
45
47
|
## Contributing
|
46
48
|
|
47
|
-
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mauricew/ruby-twitch-api.
|
48
50
|
|
49
51
|
## License
|
50
52
|
|
data/lib/twitch/client.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
require "faraday"
|
2
2
|
require "faraday_middleware"
|
3
3
|
|
4
|
+
require "twitch/response"
|
5
|
+
require "twitch/api_error"
|
4
6
|
require "twitch/stream"
|
7
|
+
require "twitch/stream_metadata"
|
5
8
|
require "twitch/user"
|
9
|
+
require "twitch/user_follow"
|
6
10
|
require "twitch/game"
|
7
11
|
require "twitch/video"
|
8
12
|
|
@@ -10,7 +14,9 @@ module Twitch
|
|
10
14
|
class Client
|
11
15
|
API_ENDPOINT = "https://api.twitch.tv/helix".freeze
|
12
16
|
|
13
|
-
def initialize(client_id
|
17
|
+
def initialize(client_id: nil, access_token: nil)
|
18
|
+
raise "Client ID is required" if client_id.nil?
|
19
|
+
|
14
20
|
headers = {
|
15
21
|
"Client-ID": client_id,
|
16
22
|
"User-Agent": "twitch-api ruby client #{Twitch::VERSION}"
|
@@ -25,44 +31,69 @@ module Twitch
|
|
25
31
|
end
|
26
32
|
end
|
27
33
|
|
34
|
+
|
35
|
+
def get_games(options = {})
|
36
|
+
res = get('games', options)
|
37
|
+
|
38
|
+
games = res[:data].map { |g| Game.new(g) }
|
39
|
+
Response.new(games, res[:rate_limit_headers])
|
40
|
+
end
|
41
|
+
|
28
42
|
def get_streams(options = {})
|
29
43
|
res = get('streams', options)
|
30
44
|
|
31
|
-
streams = res
|
45
|
+
streams = res[:data].map { |s| Stream.new(s) }
|
46
|
+
Response.new(streams, res[:rate_limit_headers], res[:pagination])
|
32
47
|
end
|
33
48
|
|
34
|
-
def
|
35
|
-
res = get('
|
49
|
+
def get_streams_metadata(options = {})
|
50
|
+
res = get('streams/metadata', options)
|
36
51
|
|
37
|
-
|
52
|
+
stream_metadata = res[:data].map { |s| StreamMetadata.new(s) }
|
53
|
+
Response.new(stream_metadata, res[:rate_limit_headers], res[:pagination])
|
38
54
|
end
|
39
55
|
|
40
|
-
def
|
41
|
-
res = get('
|
56
|
+
def get_users_follows(options = {})
|
57
|
+
res = get('users/follows', options)
|
42
58
|
|
43
|
-
|
59
|
+
users = res[:data].map { |u| UserFollow.new(u) }
|
60
|
+
Response.new(users, res[:rate_limit_headers], res[:pagination])
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_users(options = {})
|
64
|
+
res = get('users', options)
|
65
|
+
|
66
|
+
users = res[:data].map { |u| User.new(u) }
|
67
|
+
Response.new(users, res[:rate_limit_headers])
|
44
68
|
end
|
45
69
|
|
46
70
|
def get_videos(options = {})
|
47
71
|
res = get('videos', options)
|
48
72
|
|
49
|
-
videos = res
|
73
|
+
videos = res[:data].map { |v| Video.new(v) }
|
74
|
+
Response.new(videos, res[:rate_limit_headers], res[:pagination])
|
50
75
|
end
|
51
76
|
|
52
77
|
private
|
53
78
|
|
54
79
|
def get(resource, params)
|
55
|
-
|
56
|
-
|
57
|
-
unless res.status == 200
|
58
|
-
msg= %Q{The server returned an error.
|
59
|
-
#{res.body["error"]}: #{res.body["message"]}
|
60
|
-
Status: #{res.body["status"]}}
|
80
|
+
http_res = @conn.get(resource, params)
|
61
81
|
|
62
|
-
|
82
|
+
unless http_res.success?
|
83
|
+
raise ApiError.new(http_res.status, http_res.body)
|
63
84
|
end
|
64
85
|
|
65
|
-
|
86
|
+
rate_limit_headers = Hash[http_res.headers.select { |k,v|
|
87
|
+
k.to_s.downcase.match(/^ratelimit/)
|
88
|
+
}.map { |k,v| [k.gsub('ratelimit-', '').to_sym, v] }]
|
89
|
+
|
90
|
+
pagination = http_res.body['pagination'] if http_res.body.key?('pagination')
|
91
|
+
|
92
|
+
{
|
93
|
+
data: http_res.body['data'],
|
94
|
+
pagination: pagination,
|
95
|
+
rate_limit_headers: rate_limit_headers
|
96
|
+
}
|
66
97
|
end
|
67
98
|
end
|
68
99
|
end
|
data/lib/twitch/game.rb
CHANGED
@@ -3,9 +3,9 @@ module Twitch
|
|
3
3
|
attr_reader :id, :name, :box_art_url
|
4
4
|
|
5
5
|
def initialize(attributes = {})
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
@id = attributes['id']
|
7
|
+
@name = attributes['name']
|
8
|
+
@box_art_url = attributes['name']
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Twitch
|
2
|
+
class Response
|
3
|
+
attr_reader :data, :pagination, :rate_limit, :rate_limit_remaining, :rate_limit_resets_at
|
4
|
+
|
5
|
+
def initialize(data, rate_limit_headers, pagination = nil)
|
6
|
+
@data = data
|
7
|
+
|
8
|
+
@rate_limit = rate_limit_headers[:limit].to_i
|
9
|
+
@rate_limit_remaining = rate_limit_headers[:remaining].to_i
|
10
|
+
@rate_limit_resets_at = Time.at(rate_limit_headers[:reset].to_i)
|
11
|
+
|
12
|
+
@pagination = pagination
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Twitch
|
2
|
+
class StreamMetadata
|
3
|
+
attr_reader :user_id, :game_id
|
4
|
+
|
5
|
+
def initialize(attributes = {})
|
6
|
+
@user_id = attributes['user_id']
|
7
|
+
@game_id = attributes['game_id']
|
8
|
+
|
9
|
+
# Since more games can be supported in the future, this will ensure
|
10
|
+
# they will all be available.
|
11
|
+
attributes.each do |k, v|
|
12
|
+
unless instance_variables.include?("@#{k}".to_sym)
|
13
|
+
self.class.send(:attr_reader, k.to_sym)
|
14
|
+
instance_variable_set("@#{k}", v)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/twitch/version.rb
CHANGED
data/lib/twitch/video.rb
CHANGED
@@ -4,7 +4,8 @@ module Twitch
|
|
4
4
|
class Video
|
5
5
|
DATE_ATTRIBUTES = [:created_at, :published_at]
|
6
6
|
|
7
|
-
attr_reader :id, :title, :description, :language, :view_count, :created_at,
|
7
|
+
attr_reader :id, :title, :description, :language, :view_count, :created_at,
|
8
|
+
:published_at, :thumbnail_url, :type, :url, :user_id, :viewable, :duration
|
8
9
|
|
9
10
|
def initialize(attributes = {})
|
10
11
|
attributes.each do |k, v|
|
data/twitch-api.gemspec
CHANGED
@@ -27,4 +27,6 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_development_dependency "bundler", "~> 1.16"
|
28
28
|
spec.add_development_dependency "rake", "~> 10.0"
|
29
29
|
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
spec.add_development_dependency "webmock", "~> 3.1"
|
31
|
+
spec.add_development_dependency "vcr", "~> 3.0"
|
30
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twitch-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maurice Wahba
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -80,6 +80,34 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: vcr
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
83
111
|
description:
|
84
112
|
email:
|
85
113
|
- maurice.wahba@gmail.com
|
@@ -97,10 +125,14 @@ files:
|
|
97
125
|
- bin/console
|
98
126
|
- bin/setup
|
99
127
|
- lib/twitch-api.rb
|
128
|
+
- lib/twitch/api_error.rb
|
100
129
|
- lib/twitch/client.rb
|
101
130
|
- lib/twitch/game.rb
|
131
|
+
- lib/twitch/response.rb
|
102
132
|
- lib/twitch/stream.rb
|
133
|
+
- lib/twitch/stream_metadata.rb
|
103
134
|
- lib/twitch/user.rb
|
135
|
+
- lib/twitch/user_follow.rb
|
104
136
|
- lib/twitch/version.rb
|
105
137
|
- lib/twitch/video.rb
|
106
138
|
- twitch-api.gemspec
|