-
Notifications
You must be signed in to change notification settings - Fork 0
Add base twitch api module #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'twitch/version' | ||
| require_relative 'twitch/client' |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||||||||||||||||||||||||
| # frozen_string_literal: true | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| module Twitch | ||||||||||||||||||||||||||||
| # An error returned by the API. | ||||||||||||||||||||||||||||
| class APIError < StandardError | ||||||||||||||||||||||||||||
| # HTTP status code of the response. | ||||||||||||||||||||||||||||
| attr_reader :status_code | ||||||||||||||||||||||||||||
| # Body content of the response. | ||||||||||||||||||||||||||||
| attr_reader :body | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def initialize(status_code, body) | ||||||||||||||||||||||||||||
| @status_code = status_code | ||||||||||||||||||||||||||||
| @body = body | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| super(self.body['message']) | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
|
Comment on lines
+11
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Guard against missing or non-hash def initialize(status_code, body)
@status_code = status_code
@body = body
- super(self.body['message'])
+ msg = body.is_a?(Hash) ? (body['message'] || body.to_s) : body.to_s
+ super(msg)
end📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabiitai can explain me this in more stupid way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Twitch | ||
| # A determination if AutoMod would allow a message in a particular channel's chat. | ||
| class AutomodMessageStatus | ||
| # ID of the message. Matches the field of the same name passed in the request. | ||
| attr_reader :msg_id | ||
| # Whether the message would meet AutoMod requirements for the channel. | ||
| attr_reader :is_permitted | ||
|
|
||
| def initialize(attributes = {}) | ||
| attributes.each do |key, value| | ||
| instance_variable_set :"@#{key}", value | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Twitch | ||
| # A user that is a leader for bits. | ||
| class BitsLeader | ||
| # ID of the user giving bits. | ||
| attr_reader :user_id | ||
| # Display name of the user giving bits. | ||
| attr_reader :user_name | ||
| # Ranking of the user giving bits. | ||
| # Reflects the parent object's date range. | ||
| attr_reader :rank | ||
| # Number of bits given in the parent object's date range. | ||
| attr_reader :score | ||
|
|
||
| def initialize(attributes = {}) | ||
| @user_id = attributes['user_id'] | ||
| @user_name = attributes['user_name'] | ||
| @rank = attributes['rank'] | ||
| @score = attributes['score'] | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Twitch | ||
| ## Data class for categories | ||
| class Category | ||
| ## A URL to an image of the game’s box art or streaming category. | ||
| attr_reader :box_art_url | ||
|
|
||
| ## The name of the game or category. | ||
| attr_reader :name | ||
|
|
||
| ## An ID that uniquely identifies the game or category. | ||
| attr_reader :id | ||
|
|
||
| ## Not always available: | ||
|
|
||
| ## The ID that IGDB uses to identify this game. | ||
| ## If the IGDB ID is not available to Twitch, this field is set to an empty string. | ||
| attr_reader :igdb_id | ||
|
|
||
| def initialize(attributes = {}) | ||
| attributes.each do |key, value| | ||
| instance_variable_set :"@#{key}", value | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Twitch | ||
| # Data object for Twitch channels | ||
| # https://dev.twitch.tv/docs/api/reference#get-channel-information | ||
| class Channel | ||
| # Twitch User ID of this channel owner. | ||
| attr_reader :broadcaster_id | ||
| # Broadcaster's user login name. | ||
| attr_reader :broadcaster_login | ||
| # Twitch user display name of this channel owner. | ||
| attr_reader :broadcaster_name | ||
| # Language of the channel. | ||
| # A language value is either the ISO 639-1 two-letter code for a supported stream language | ||
| # or "other". | ||
| attr_reader :broadcaster_language | ||
| # Current game ID being played on the channel . | ||
| attr_reader :game_id | ||
| # Name of the game being played on the channel. | ||
| attr_reader :game_name | ||
| # Title of the stream. | ||
| attr_reader :title | ||
| # Stream delay in seconds. | ||
| attr_reader :delay | ||
|
|
||
| # Some endpoints, like "Search Channels", can return different fields | ||
| attr_reader :id | ||
| attr_reader :display_name | ||
| # A Boolean value that determines whether the broadcaster is streaming live. | ||
| # Is true if the broadcaster is streaming live; otherwise, false. | ||
| attr_reader :is_live | ||
| # The tags applied to the channel. | ||
| attr_reader :tags | ||
| # A URL to a thumbnail of the broadcaster’s profile image. | ||
| attr_reader :thumbnail_url | ||
| # The UTC date and time (in RFC3339 format) of when the broadcaster started streaming. | ||
| # The string is empty if the broadcaster is not streaming live. | ||
| attr_reader :started_at | ||
|
|
||
| def initialize(attributes = {}) | ||
| attributes.each do |key, value| | ||
| instance_variable_set :"@#{key}", value | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||||||||||||||||||||||||||||||||||
| # frozen_string_literal: true | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| require_relative 'cheermote_tier' | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Require the
# frozen_string_literal: true
+
+require 'time'
require_relative 'cheermote_tier'📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
| module Twitch | ||||||||||||||||||||||||||||||||||||||||
| ## Data class for Cheermotes, animated emotes that viewers can assign Bits to. | ||||||||||||||||||||||||||||||||||||||||
| class Cheermote | ||||||||||||||||||||||||||||||||||||||||
| ## The name portion of the Cheermote string that you use in chat to cheer Bits. | ||||||||||||||||||||||||||||||||||||||||
| ## The full Cheermote string is the concatenation of {prefix} + {number of Bits}. | ||||||||||||||||||||||||||||||||||||||||
| ## For example, if the prefix is “Cheer” and you want to cheer 100 Bits, | ||||||||||||||||||||||||||||||||||||||||
| ## the full Cheermote string is Cheer100. | ||||||||||||||||||||||||||||||||||||||||
| ## When the Cheermote string is entered in chat, | ||||||||||||||||||||||||||||||||||||||||
| ## Twitch converts it to the image associated with the Bits tier that was cheered. | ||||||||||||||||||||||||||||||||||||||||
| attr_reader :prefix | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ## A list of tier levels that the Cheermote supports. | ||||||||||||||||||||||||||||||||||||||||
| ## Each tier identifies the range of Bits that you can cheer at that tier level | ||||||||||||||||||||||||||||||||||||||||
| ## and an image that graphically identifies the tier level. | ||||||||||||||||||||||||||||||||||||||||
| attr_reader :tiers | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ## The type of Cheermote. Possible values are: | ||||||||||||||||||||||||||||||||||||||||
| ## * global_first_party — A Twitch-defined Cheermote that is shown in the Bits card. | ||||||||||||||||||||||||||||||||||||||||
| ## * global_third_party — A Twitch-defined Cheermote that is not shown in the Bits card. | ||||||||||||||||||||||||||||||||||||||||
| ## * channel_custom — A broadcaster-defined Cheermote. | ||||||||||||||||||||||||||||||||||||||||
| ## * display_only — Do not use; for internal use only. | ||||||||||||||||||||||||||||||||||||||||
| ## * sponsored — A sponsor-defined Cheermote. | ||||||||||||||||||||||||||||||||||||||||
| ## When used, the sponsor adds additional Bits to the amount that the user cheered. | ||||||||||||||||||||||||||||||||||||||||
| ## For example, if the user cheered Terminator100, the broadcaster might receive 110 Bits, | ||||||||||||||||||||||||||||||||||||||||
| ## which includes the sponsor's 10 Bits contribution. | ||||||||||||||||||||||||||||||||||||||||
| attr_reader :type | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ## The order that the Cheermotes are shown in the Bits card. | ||||||||||||||||||||||||||||||||||||||||
| ## The numbers may not be consecutive. For example, the numbers may jump from 1 to 7 to 13. | ||||||||||||||||||||||||||||||||||||||||
| ## The order numbers are unique within a Cheermote type (for example, global_first_party) | ||||||||||||||||||||||||||||||||||||||||
| ## but may not be unique amongst all Cheermotes in the response. | ||||||||||||||||||||||||||||||||||||||||
| attr_reader :order | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ## The date and time when this Cheermote was last updated. | ||||||||||||||||||||||||||||||||||||||||
| attr_reader :last_updated | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ## A Boolean value that indicates whether this Cheermote provides | ||||||||||||||||||||||||||||||||||||||||
| ## a charitable contribution match during charity campaigns. | ||||||||||||||||||||||||||||||||||||||||
| attr_reader :is_charitable | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def initialize(attributes = {}) | ||||||||||||||||||||||||||||||||||||||||
| attributes.each do |key, value| | ||||||||||||||||||||||||||||||||||||||||
| value = value.map { |tier| CheermoteTier.new(tier) } if key == 'tiers' | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| instance_variable_set :"@#{key}", value | ||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+45
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Handle symbol keys & convert
def initialize(attributes = {})
- attributes.each do |key, value|
- value = value.map { |tier| CheermoteTier.new(tier) } if key == 'tiers'
-
- instance_variable_set :"@#{key}", value
- end
+ attributes.each do |key, value|
+ case key.to_sym
+ when :tiers
+ @tiers = value.map { |tier| CheermoteTier.new(tier) }
+ when :last_updated
+ @last_updated = Time.parse(value)
+ else
+ instance_variable_set :"@#{key}", value
+ end
+ end
endThis guarantees type-safe fields and works regardless of key type. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,55 @@ | ||||||||||||||||||||||||||||||||||||
| # frozen_string_literal: true | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| require_relative 'cheermote_tier_images' | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| module Twitch | ||||||||||||||||||||||||||||||||||||
| ## Data class for Cheermote Tier. | ||||||||||||||||||||||||||||||||||||
| ## Each tier identifies the range of Bits that you can cheer at that tier level | ||||||||||||||||||||||||||||||||||||
| ## and an image that graphically identifies the tier level. | ||||||||||||||||||||||||||||||||||||
| class CheermoteTier | ||||||||||||||||||||||||||||||||||||
| ## The minimum number of Bits that you must cheer at this tier level. | ||||||||||||||||||||||||||||||||||||
| ## The maximum number of Bits that you can cheer at this level | ||||||||||||||||||||||||||||||||||||
| ## is determined by the required minimum Bits of the next tier level minus 1. | ||||||||||||||||||||||||||||||||||||
| ## For example, if `min_bits` is 1 and `min_bits` for the next tier is 100, | ||||||||||||||||||||||||||||||||||||
| ## the Bits range for this tier level is 1 through 99. | ||||||||||||||||||||||||||||||||||||
| ## The minimum Bits value of the last tier is the maximum number of Bits you can cheer | ||||||||||||||||||||||||||||||||||||
| ## using this Cheermote. For example, 10000. | ||||||||||||||||||||||||||||||||||||
| attr_reader :min_bits | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| ## The tier level. Possible tiers are: | ||||||||||||||||||||||||||||||||||||
| ## * 1 | ||||||||||||||||||||||||||||||||||||
| ## * 100 | ||||||||||||||||||||||||||||||||||||
| ## * 500 | ||||||||||||||||||||||||||||||||||||
| ## * 1000 | ||||||||||||||||||||||||||||||||||||
| ## * 5000 | ||||||||||||||||||||||||||||||||||||
| ## * 10000 | ||||||||||||||||||||||||||||||||||||
| ## * 100000 | ||||||||||||||||||||||||||||||||||||
| attr_reader :id | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| ## The hex code of the color associated with this tier level (for example, `#979797`). | ||||||||||||||||||||||||||||||||||||
| attr_reader :color | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| ## The animated and static image sets for the Cheermote. | ||||||||||||||||||||||||||||||||||||
| ## The dictionary of images is organized by theme, format, and size. | ||||||||||||||||||||||||||||||||||||
| ## The theme keys are `dark` and `light`. | ||||||||||||||||||||||||||||||||||||
| ## Each theme is a dictionary of formats: `animated` and `static`. | ||||||||||||||||||||||||||||||||||||
| ## Each format is a dictionary of sizes: `1`, `1.5`, `2`, `3`, and `4`. | ||||||||||||||||||||||||||||||||||||
| ## The value of each size contains the URL to the image. | ||||||||||||||||||||||||||||||||||||
| attr_reader :images | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| ## A Boolean value that determines whether users can cheer at this tier level. | ||||||||||||||||||||||||||||||||||||
| attr_reader :can_cheer | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| ## A Boolean value that determines whether this tier level is shown in the Bits card. | ||||||||||||||||||||||||||||||||||||
| ## Is `true` if this tier level is shown in the Bits card. | ||||||||||||||||||||||||||||||||||||
| attr_reader :show_in_bits_card | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def initialize(attributes = {}) | ||||||||||||||||||||||||||||||||||||
| attributes.each do |key, value| | ||||||||||||||||||||||||||||||||||||
| value = CheermoteTierImages.new(value) if key == 'images' | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| instance_variable_set :"@#{key}", value | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Symbol-key support & strong typing for Like attributes.each do |key, value|
- value = CheermoteTierImages.new(value) if key == 'images'
+ if key.to_sym == :images
+ value = CheermoteTierImages.new(value)
+ end
instance_variable_set :"@#{key}", value
endConsider raising if 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Twitch | ||
| ## Data class for Cheermote Tier image, static and animated | ||
| class CheermoteTierImage | ||
| ## Each theme is a dictionary of formats: `animated` and `static`. | ||
| ## Each format is a dictionary of sizes: `1`, `1.5`, `2`, `3`, and `4`. | ||
| ## The value of each size contains the URL to the image. | ||
| attr_reader :animated | ||
| attr_reader :static | ||
|
|
||
| def initialize(attributes = {}) | ||
| attributes.each do |key, value| | ||
| instance_variable_set :"@#{key}", value | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'cheermote_tier_image' | ||
|
|
||
| module Twitch | ||
| ## Data class for Cheermote Tier images by themes | ||
| class CheermoteTierImages | ||
| ## The theme keys are `dark` and `light`. | ||
| ## Each theme is a dictionary of formats: `animated` and `static`. | ||
| ## Each format is a dictionary of sizes: `1`, `1.5`, `2`, `3`, and `4`. | ||
| ## The value of each size contains the URL to the image. | ||
| attr_reader :dark | ||
| attr_reader :light | ||
|
|
||
| def initialize(attributes = {}) | ||
| attributes.each do |key, value| | ||
| instance_variable_set :"@#{key}", CheermoteTierImage.new(value) | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure that the response body includes a 'message' key or provide a default error message to avoid passing nil to StandardError.