twitchrb 0.1.0 → 0.1.1

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: e6d24ae98033340c3a32007d15326393e2eb630d9bf617a22dcd86c3bd96205b
4
- data.tar.gz: 4099ce7790604f9d77dfb4e7e5c22252573ef6adcfde1806b99ea8b966d984cf
3
+ metadata.gz: 5f783023c0544d6ff69ae55aef0c2af9b1b179d438e435b1195dfe0895b686eb
4
+ data.tar.gz: 2072fb3ec4460387493e2ad33745bcd0028770a9a1f91a0cbe2576c75b519822
5
5
  SHA512:
6
- metadata.gz: 63a0c047cd6803daf0e094bdf88e0fb6ac73fa1959beb84f43c38634190bb5a53ce2e2242993ead65b8361ca4673a601a608a7769eac4026c320b83f9e57a3cc
7
- data.tar.gz: c04701b41259897d3a871df91037b801fb6c36f3e15f8bb764863dc96435b1da5671a97611570e11f5244d94dc851275f1f7cf0b4a69dcd929822548d2061157
6
+ metadata.gz: 8dce1c0ff0bcd6dd7fa506f7bb0c090a883966e9d13a050593bed74b309dfb89dd9ddc6aff2f5ce80aa9c86fb1e5b01b739b7468ec1ea7ba9976212d59fa8b06
7
+ data.tar.gz: e64c64c6a844131c553a71721dfd540dcb4cb0a40adf4b8f0f58a93543164587651f6d914540647fea5a2997ea8ae6198db16815ac7cb853ce2b33c4641f26a4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twitchrb (0.1.0)
4
+ twitchrb (0.1.1)
5
5
  httparty (~> 0.18.1)
6
6
 
7
7
  GEM
@@ -12,7 +12,7 @@ GEM
12
12
  multi_xml (>= 0.5.2)
13
13
  mime-types (3.3.1)
14
14
  mime-types-data (~> 3.2015)
15
- mime-types-data (3.2020.0512)
15
+ mime-types-data (3.2021.0225)
16
16
  minitest (5.14.1)
17
17
  multi_xml (0.6.0)
18
18
  rake (12.3.3)
data/lib/twitch.rb CHANGED
@@ -7,6 +7,12 @@ require "twitch/version"
7
7
  require "twitch/client"
8
8
  require "twitch/initializable"
9
9
 
10
+ # Helix API
11
+ require "twitch/channels"
12
+ require "twitch/emotes"
13
+ require "twitch/badges"
14
+ require "twitch/games"
15
+
10
16
  # Kraken (v5) API
11
17
  require "twitch/kraken/user"
12
18
  require "twitch/kraken/users"
@@ -18,8 +24,8 @@ module Twitch
18
24
  class << self
19
25
  attr_reader :client
20
26
 
21
- def access_details(client_id)
22
- @client = Client.new(client_id)
27
+ def access_details(client_id, access_token=nil)
28
+ @client = Client.new(client_id, access_token)
23
29
  # @client.access_token = access_token if access_token
24
30
  end
25
31
  end
@@ -0,0 +1,39 @@
1
+ module Twitch
2
+ class Badges
3
+
4
+ include Initializable
5
+
6
+ attr_accessor :id, :image_url_1x, :image_url_2x, :image_url_4x
7
+
8
+ class << self
9
+
10
+ # Gets Badges for a channel ID
11
+ def get_channel(id)
12
+ response = Twitch.client.get(:helix, "chat/badges?broadcaster_id=#{id}")
13
+
14
+ badge_array(response["data"])
15
+ end
16
+
17
+ # Gets Global Badges
18
+ def get_global
19
+ response = Twitch.client.get(:helix, "chat/badges/global")
20
+
21
+ badge_array(response["data"])
22
+ end
23
+
24
+ private
25
+
26
+ def badge_array(data)
27
+ badges = []
28
+
29
+ data.each do |e|
30
+ badges << {set_id: e["set_id"], versions: e["versions"].map {|v| new(v)} }
31
+ end
32
+
33
+ badges
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,25 @@
1
+ module Twitch
2
+ class Channels
3
+
4
+ include Initializable
5
+
6
+ attr_accessor :broadcaster_id, :broadcaster_name, :game_name, :game_id, :broadcaster_language, :title, :delay
7
+
8
+ class << self
9
+
10
+ # Gets a specified channel object
11
+ def get(id)
12
+ response = Twitch.client.get(:helix, "channels?broadcaster_id=#{id}")
13
+
14
+ new(response["data"][0])
15
+ end
16
+
17
+ # Update a channel
18
+ def update(id, params={})
19
+ Twitch.client.patch(:helix, "channels?broadcaster_id=#{id}", params)
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
data/lib/twitch/client.rb CHANGED
@@ -4,10 +4,11 @@ module Twitch
4
4
  class Client
5
5
 
6
6
  # def initialize(client_id, client_secret, access_token=nil)
7
- def initialize(client_id)
7
+ def initialize(client_id, access_token=nil)
8
8
  puts "initialize"
9
9
 
10
10
  @client_id = client_id
11
+ @access_token = access_token
11
12
 
12
13
  # if client_id && client_secret
13
14
  # @client_id = client_id
@@ -16,28 +17,61 @@ module Twitch
16
17
  # else
17
18
  # raise Twitch::ClientError.new('Client ID or Client Secret not set')
18
19
  # end
19
-
20
20
  end
21
21
 
22
-
23
- def get(kind, url, params={})
24
- # puts url
25
- # puts params
26
- # puts @client_id
27
- response = HTTParty.get("https://api.twitch.tv/#{kind}/#{url}", {
28
- headers: {
22
+ def headers(kind)
23
+ if kind == :helix
24
+ {
25
+ "Client-ID" => @client_id,
26
+ "Accept" => "application/json",
27
+ "Authorization" => "Bearer #{@access_token}"
28
+ }
29
+ else
30
+ {
29
31
  "Client-ID" => @client_id,
30
32
  "Accept" => "application/vnd.twitchtv.v5+json",
31
33
  # "Authorization" => "OAuth #{@access_token}"
32
34
  }
35
+ end
36
+ end
37
+
38
+
39
+ def get(kind, url)
40
+ response = HTTParty.get("https://api.twitch.tv/#{kind}/#{url}", {
41
+ headers: headers(kind)
33
42
  })
34
- # puts response
35
43
 
36
- # JSON.parse(response.body)
44
+ # Force encoding as the reponse may have emojis
45
+ body = response.body.force_encoding('UTF-8')
37
46
 
38
47
  success = case response.code
39
48
  when 200
40
- JSON.parse(response.body)
49
+ JSON.parse(body)
50
+ when 400
51
+ json = JSON.parse(body)
52
+ raise Twitch::Errors::NotFound, json['error']
53
+ when 503
54
+ raise Twitch::Errors::ServiceUnavailable
55
+ when 401, 403
56
+ puts body.inspect
57
+ raise Twitch::Errors::AccessDenied, "Access Denied for '#{@client_id}'"
58
+ when 400
59
+ json = JSON.parse(body)
60
+ raise Twitch::Errors::ValidationError, json['errors'].to_s
61
+ else
62
+ raise Twitch::Errors::CommunicationError, body
63
+ end
64
+ end
65
+
66
+ def patch(kind, url, params={})
67
+ response = HTTParty.patch("https://api.twitch.tv/#{kind}/#{url}", {
68
+ headers: headers(kind),
69
+ body: params
70
+ })
71
+
72
+ success = case response.code
73
+ when 204
74
+ return true
41
75
  when 400
42
76
  json = JSON.parse(response.body)
43
77
  raise Twitch::Errors::NotFound, json['error']
@@ -52,38 +86,7 @@ module Twitch
52
86
  else
53
87
  raise Twitch::Errors::CommunicationError, response.body
54
88
  end
55
-
56
-
57
- # if response.code == 200
58
- # JSON.parse(response.body)
59
- # elsif response.code == 404
60
- # Twitch::NotFoundError.new(response.body)
61
- # elsif response.code == 500
62
- # Twitch::ServerError.new(response.body)
63
- # end
64
-
65
-
66
-
67
- # when Net::HTTPClientError
68
- # json = JSON.parse(http_result.body)
69
- # raise Twitch::Errors::ValidationError, json['errors'].to_s
70
- # else
71
- # raise Twitch::Errors::CommunicationError, http_result.body
72
- # end
73
-
74
-
75
89
  end
76
90
 
77
-
78
- # private
79
-
80
- # def request(method, path, options={})
81
- # # if @access_token
82
-
83
-
84
-
85
- # end
86
-
87
-
88
91
  end
89
92
  end
@@ -0,0 +1,58 @@
1
+ module Twitch
2
+ class Emotes
3
+
4
+ include Initializable
5
+
6
+ attr_accessor :id, :name, :images, :tier, :emote_type, :emote_set_id, :owner_id
7
+
8
+ def url_1x
9
+ images["url_1x"]
10
+ end
11
+
12
+ def url_2x
13
+ images["url_2x"]
14
+ end
15
+
16
+ def url_4x
17
+ images["url_4x"]
18
+ end
19
+
20
+ class << self
21
+
22
+ # Gets Emotes for a channel ID
23
+ def get_channel(id)
24
+ response = Twitch.client.get(:helix, "chat/emotes?broadcaster_id=#{id}")
25
+
26
+ emote_array(response["data"])
27
+ end
28
+
29
+ # Gets Global Emotes
30
+ def get_global
31
+ response = Twitch.client.get(:helix, "chat/emotes/global")
32
+
33
+ emote_array(response["data"])
34
+ end
35
+
36
+ # Gets Emotes for an Emote Set ID
37
+ def get_emote_set(id)
38
+ response = Twitch.client.get(:helix, "chat/emotes/set?emote_set_id=#{id}")
39
+
40
+ emote_array(response["data"])
41
+ end
42
+
43
+ private
44
+
45
+ def emote_array(data)
46
+ emotes = []
47
+
48
+ data.each do |e|
49
+ emotes << new(e)
50
+ end
51
+
52
+ emotes
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,49 @@
1
+ module Twitch
2
+ class Games
3
+
4
+ include Initializable
5
+
6
+ attr_accessor :id, :name, :box_art_url
7
+
8
+ def box_art_380
9
+ box_art_url.gsub("{width}", "285").gsub("{height}", "380")
10
+ end
11
+
12
+ class << self
13
+
14
+ # Gets Badges for a channel ID
15
+ def get_by_name(name)
16
+ response = Twitch.client.get(:helix, "games?name=#{name}")
17
+
18
+ new(response["data"][0])
19
+ end
20
+
21
+ def get_by_id(id)
22
+ response = Twitch.client.get(:helix, "games?id=#{id}")
23
+
24
+ new(response["data"][0])
25
+ end
26
+
27
+ # Gets Top Games
28
+ def get_top
29
+ response = Twitch.client.get(:helix, "games/top")
30
+
31
+ game_array(response["data"])
32
+ end
33
+
34
+ private
35
+
36
+ def game_array(data)
37
+ games = []
38
+
39
+ data.each do |g|
40
+ games << new(g)
41
+ end
42
+
43
+ games
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module Twitch
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitchrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dean Perry
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-30 00:00:00.000000000 Z
11
+ date: 2021-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -41,7 +41,11 @@ files:
41
41
  - bin/console
42
42
  - bin/setup
43
43
  - lib/twitch.rb
44
+ - lib/twitch/badges.rb
45
+ - lib/twitch/channels.rb
44
46
  - lib/twitch/client.rb
47
+ - lib/twitch/emotes.rb
48
+ - lib/twitch/games.rb
45
49
  - lib/twitch/initializable.rb
46
50
  - lib/twitch/kraken/channels.rb
47
51
  - lib/twitch/kraken/clips.rb
@@ -70,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
74
  - !ruby/object:Gem::Version
71
75
  version: '0'
72
76
  requirements: []
73
- rubygems_version: 3.1.2
77
+ rubygems_version: 3.1.4
74
78
  signing_key:
75
79
  specification_version: 4
76
80
  summary: A Ruby library for interacting with the Twitch API