twitch-api 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 496abaceb2ef84dbd33eee192a05daf8270d9c77c373ba6beadaf915bb1cf426
4
- data.tar.gz: 66a591c5e4c176fab22c198b907020a31dffd2499c7d95cada41e0cd53feb7f1
3
+ metadata.gz: 13fa554e6376acacd400d0aad2e651d295c613297df4476ba7c1736b6e7640aa
4
+ data.tar.gz: 6e180eab8e7061535821922cbc6ad4c4b35e867096314a7ddf564704ea32882c
5
5
  SHA512:
6
- metadata.gz: c9a8bc3775456383bfa0162aff51fc62d35374dcd8fe355b46ec3fb0fe5733fe0164319a3fc1fd0487df2043aae49a8d5e9976f01bddc59572ea6a3e24028c9f
7
- data.tar.gz: b5498b3112dfb9d61a64f04780a46d3aa895ba827b38074739f1b7c4dbdc31d9f4b1687c1c277e3b131a556f9848df65ff47193d7582f08af13fcf0f8e25b34b
6
+ metadata.gz: 7b45c4f07f653c77e3058579dac055efbcb2b1664fe1c68f63caf3715bfd8471a318bacc2d6ec49f072d0fee7686b7d72313c939e26c0e8a8bbd888137611e1a
7
+ data.tar.gz: c29bf47c2020a2b2617d1f6a54b7fda59b0544c165c5787fcbba92963dc11eeaeee2149640e8bbcaa86a4b1eca7551050d82d40ca1d6657543c8647f511f4a40
@@ -1,5 +1,9 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.3
4
+ - 2.3.7
5
+ - 2.4.4
6
+ - 2.5.1
7
+ env:
8
+ - TWITCH_CLIENT_ID=test
5
9
  before_install: gem install bundler -v 1.16.0
data/README.md CHANGED
@@ -1,3 +1,7 @@
1
+ [![Gem](https://img.shields.io/gem/v/twitch-api.svg)](https://rubygems.org/gems/twitch-api)
2
+ [![Downloads](https://img.shields.io/gem/dt/twitch-api.svg)](https://rubygems.org/gems/twitch-api)
3
+ [![Travis](https://img.shields.io/travis/mauricew/ruby-twitch-api.svg)](https://travis-ci.org/mauricew/ruby-twitch-api)
4
+ [![License](https://img.shields.io/github/license/mauricew/ruby-twitch-api.svg)]()
1
5
  # Ruby Twitch API
2
6
 
3
7
  This library is a Ruby implementation of the [Twitch Helix API](https://dev.twitch.tv/docs/api).
@@ -9,6 +13,7 @@ Guaranteed supported APIs include:
9
13
  * Helix Webhooks (coming soon)
10
14
 
11
15
  The future may bring:
16
+ * Authentication
12
17
  * PubSub
13
18
  * TMI/chat
14
19
 
@@ -39,14 +44,17 @@ Or install it yourself as:
39
44
  ## Usage
40
45
  A client must be initialized with your client ID or bearer access token.
41
46
  ```
42
- client = Twitch::Client.new client_id: "YOUR_CLIENT_ID"
47
+ client = Twitch::Client.new(client_id: "YOUR_CLIENT_ID")
43
48
  # or
44
- client = Twitch::Client.new access_token: "YOUR_ACCESS_TOKEN"
49
+ client = Twitch::Client.new(access_token: "YOUR_ACCESS_TOKEN")
45
50
  ```
46
- The retrieval methods take in a hash equal to the parameters of the API endpoint, and return a response object containing the data and other associated request information:
51
+ Because data may change for certain endpoints, there is also a keyword parameter called `with_raw` that returns the raw response for any request called.
52
+
53
+ Retrieval methods take in a hash equal to the parameters of the API endpoint, and return a response object containing the data and other associated request information:
47
54
  * **data** is the data you would get back. Even if it's an array of one object, it remains the same as what comes from the API.
48
55
  * **rate_limit** and associated fields contain API request limit information. Clip creation counts for a second limit (duration currently unknown).
49
- * **pagination** is an hash that appears when data can be traversed, and contains one member (*cursor*) which lets you pagniate through certain requests.
56
+ * **pagination** is a hash that appears when data can be traversed, and contains one member (*cursor*) which lets you pagniate through certain requests.
57
+ * **raw** is the raw HTTP response data when `with_raw` is true in the client.
50
58
  ```
51
59
  # Get top live streams
52
60
  client.get_streams.data
@@ -72,7 +80,3 @@ To install this gem onto your local machine, run `bundle exec rake install`.
72
80
  ## Contributing
73
81
 
74
82
  Bug reports and pull requests are welcome on GitHub at https://github.com/mauricew/ruby-twitch-api.
75
-
76
- ## License
77
-
78
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,18 @@
1
+ module Twitch
2
+ # A user that is a leader for bits.
3
+ class BitsLeader
4
+ # ID of the user giving bits.
5
+ attr_reader :user_id
6
+ # Ranking of the user giving bits.
7
+ # Reflects the parent object's date range.
8
+ attr_reader :rank
9
+ # Number of bits given in the parent object's date range.
10
+ attr_reader :score
11
+
12
+ def initialize(attributes = {})
13
+ @user_id = attributes['user_id']
14
+ @rank = attributes['rank']
15
+ @score = attributes['score']
16
+ end
17
+ end
18
+ end
@@ -3,9 +3,11 @@ require "faraday_middleware"
3
3
 
4
4
  require "twitch/response"
5
5
  require "twitch/api_error"
6
+ require "twitch/bits_leader"
6
7
  require "twitch/clip"
7
8
  require "twitch/entitlement_grant_url"
8
9
  require "twitch/game"
10
+ require "twitch/game_analytic"
9
11
  require "twitch/stream"
10
12
  require "twitch/stream_metadata"
11
13
  require "twitch/user"
@@ -24,7 +26,9 @@ module Twitch
24
26
  # - access_token [String] An access token.
25
27
  # Used as the Authorization header in a request.
26
28
  # Any "Bearer " prefix will be stripped.
27
- def initialize(client_id: nil, access_token: nil)
29
+ # - with_raw [Boolean] Whether to include raw HTTP response
30
+ # Intended for testing/checking API results
31
+ def initialize(client_id: nil, access_token: nil, with_raw: false)
28
32
  if client_id.nil? && access_token.nil?
29
33
  raise "An identifier token (client ID or bearer token) is required"
30
34
  elsif !!client_id && !!access_token
@@ -49,83 +53,60 @@ Unpredictable behavior may follow.})
49
53
  faraday.response :json
50
54
  faraday.adapter Faraday.default_adapter
51
55
  end
56
+
57
+ @with_raw = with_raw
52
58
  end
53
59
 
54
60
  def create_clip(options = {})
55
- res = post('clips', options)
56
-
57
- clip = res[:data].map { |c| Clip.new(c) }
58
- Response.new(clip, res[:rate_limit_headers])
61
+ Response.new(Clip, post('clips', options))
59
62
  end
60
63
 
61
64
  def create_entitlement_grant_url(options = {})
62
- res = post('entitlements/upload', options)
63
-
64
- entitlement_grant = res[:data].map { |e| EntitlementGrantUrl.new(e) }
65
- Response.new(entitlement_grant, res[:rate_limit_headers])
65
+ Response.new(EntitlementGrantUrl, post('entitlements/upload', options))
66
66
  end
67
67
 
68
68
  def get_clips(options = {})
69
- res = get('clips', options)
69
+ Response.new(Clip, get('clips', options))
70
+ end
70
71
 
71
- clips = res[:data].map { |c| Clip.new(c) }
72
- Response.new(clips, res[:rate_limit_headers])
72
+ def get_bits_leaderboard(options = {})
73
+ Response.new(BitsLeader, get('bits/leaderboard', options))
73
74
  end
74
75
 
75
76
  def get_games(options = {})
76
- res = get('games', options)
77
-
78
- games = res[:data].map { |g| Game.new(g) }
79
- Response.new(games, res[:rate_limit_headers])
77
+ Response.new(Game, get('games', options))
80
78
  end
81
79
 
82
80
  def get_top_games(options = {})
83
- res = get('games/top', options)
81
+ Response.new(Game, get('games/top', options))
82
+ end
84
83
 
85
- games = res[:data].map { |g| Game.new(g) }
86
- Response.new(games, res[:rate_limit_headers], res[:pagination])
84
+ def get_game_analytics(options = {})
85
+ Response.new(GameAnalytic, get('analytics/games', options))
87
86
  end
88
87
 
89
88
  def get_streams(options = {})
90
- res = get('streams', options)
91
-
92
- streams = res[:data].map { |s| Stream.new(s) }
93
- Response.new(streams, res[:rate_limit_headers], res[:pagination])
89
+ Response.new(Stream, get('streams', options))
94
90
  end
95
91
 
96
92
  def get_streams_metadata(options = {})
97
- res = get('streams/metadata', options)
98
-
99
- stream_metadata = res[:data].map { |s| StreamMetadata.new(s) }
100
- Response.new(stream_metadata, res[:rate_limit_headers], res[:pagination])
93
+ Response.new(StreamMetadata, get('streams/metadata', options))
101
94
  end
102
95
 
103
96
  def get_users_follows(options = {})
104
- res = get('users/follows', options)
105
-
106
- users = res[:data].map { |u| UserFollow.new(u) }
107
- Response.new(users, res[:rate_limit_headers], res[:pagination])
97
+ Response.new(UserFollow, get('users/follows', options))
108
98
  end
109
99
 
110
100
  def get_users(options = {})
111
- res = get('users', options)
112
-
113
- users = res[:data].map { |u| User.new(u) }
114
- Response.new(users, res[:rate_limit_headers])
101
+ Response.new(User, get('users', options))
115
102
  end
116
103
 
117
104
  def update_user(options = {})
118
- res = put('users', options)
119
-
120
- user = res[:data].map { |u| User.new(u) }
121
- Response.new(user, res[:rate_limit_headers])
105
+ Response.new(User, put('users', options))
122
106
  end
123
107
 
124
108
  def get_videos(options = {})
125
- res = get('videos', options)
126
-
127
- videos = res[:data].map { |v| Video.new(v) }
128
- Response.new(videos, res[:rate_limit_headers], res[:pagination])
109
+ Response.new(Video, get('videos', options))
129
110
  end
130
111
 
131
112
  private
@@ -150,18 +131,9 @@ Unpredictable behavior may follow.})
150
131
  raise ApiError.new(http_res.status, http_res.body)
151
132
  end
152
133
 
153
- rate_limit_headers = Hash[http_res.headers.select do |k,v|
154
- k.to_s.downcase.match(/^ratelimit/)
155
- end.map { |k,v| [k.gsub('ratelimit-', '').to_sym, v] }]
156
-
157
- if http_res.body.key?('pagination')
158
- pagination = http_res.body['pagination']
159
- end
160
-
161
- {
162
- data: http_res.body['data'],
163
- pagination: pagination,
164
- rate_limit_headers: rate_limit_headers
134
+ {
135
+ http_res: http_res,
136
+ with_raw: @with_raw
165
137
  }
166
138
  end
167
139
  end
@@ -0,0 +1,14 @@
1
+ module Twitch
2
+ # A
3
+ class GameAnalytic
4
+ # ID of the game requested.
5
+ attr_reader :game_id
6
+ # A link to a (CSV format) data report.
7
+ attr_reader :url
8
+
9
+ def initialize(attributes = {})
10
+ @game_id = attributes['game_id']
11
+ @url = attributes['URL']
12
+ end
13
+ end
14
+ end
@@ -3,9 +3,17 @@ module Twitch
3
3
  class Response
4
4
  # The requested data.
5
5
  attr_reader :data
6
- # A hash containing a pagination token.
6
+ # A total amount of entities.
7
+ # Applies to select endpoints.
8
+ attr_reader :total
9
+ # A range of dates in which data is effective.
10
+ # Usually contains the keys start_date and end_date.
11
+ # Applies to select endpoints.
12
+ attr_reader :date_range
13
+ # A hash containing a pagination token.
7
14
  # Access it with
8
15
  # pagination['cursor']
16
+ # Applies to select endpoints.
9
17
  attr_reader :pagination
10
18
  # The total amount of calls that can be used in
11
19
  # the rate limit period (one minute by default).
@@ -20,9 +28,18 @@ module Twitch
20
28
  # The remaining amount of clips that can be created in
21
29
  # the clip rate limit period.
22
30
  attr_reader :clip_rate_limit_remaining
31
+ # The HTTP raw response
32
+ attr_reader :raw
23
33
 
24
- def initialize(data, rate_limit_headers, pagination = nil)
25
- @data = data
34
+ def initialize(data_class, res)
35
+ http_res = res[:http_res]
36
+ @raw = http_res if res[:with_raw]
37
+
38
+ @data = http_res.body['data'].map { |d| data_class.new(d) }
39
+
40
+ rate_limit_headers = Hash[http_res.headers.select do |k,v|
41
+ k.to_s.downcase.match(/^ratelimit/)
42
+ end.map { |k,v| [k.gsub('ratelimit-', '').to_sym, v] }]
26
43
 
27
44
  @rate_limit = rate_limit_headers[:limit].to_i
28
45
  @rate_limit_remaining = rate_limit_headers[:remaining].to_i
@@ -33,7 +50,9 @@ module Twitch
33
50
  @clip_rate_limit_remaining = rate_limit_headers[:'helixclipscreation-remaining']
34
51
  end
35
52
 
36
- @pagination = pagination
53
+ @pagination = http_res.body['pagination']
54
+ @total = http_res.body['total']
55
+ @date_range = http_res.body['date_range']
37
56
  end
38
57
  end
39
58
  end
@@ -1,4 +1,4 @@
1
1
  module Twitch
2
2
  # Library version.
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_dependency "faraday", "~> 0.13"
24
+ spec.add_dependency "faraday", ">= 0.12.2"
25
25
  spec.add_dependency "faraday_middleware", "~> 0.12"
26
26
 
27
27
  spec.add_development_dependency "bundler", "~> 1.16"
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitch-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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: 2018-01-06 00:00:00.000000000 Z
11
+ date: 2018-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0.13'
19
+ version: 0.12.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0.13'
26
+ version: 0.12.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: faraday_middleware
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -126,10 +126,12 @@ files:
126
126
  - bin/setup
127
127
  - lib/twitch-api.rb
128
128
  - lib/twitch/api_error.rb
129
+ - lib/twitch/bits_leader.rb
129
130
  - lib/twitch/client.rb
130
131
  - lib/twitch/clip.rb
131
132
  - lib/twitch/entitlement_grant_url.rb
132
133
  - lib/twitch/game.rb
134
+ - lib/twitch/game_analytic.rb
133
135
  - lib/twitch/response.rb
134
136
  - lib/twitch/stream.rb
135
137
  - lib/twitch/stream_metadata.rb