Class: Twitch::Client
- Inherits:
-
Object
- Object
- Twitch::Client
- Defined in:
- lib/twitch/client.rb
Constant Summary collapse
- API_ENDPOINT =
Helix API endpoint.
"https://api.twitch.tv/helix".freeze
Instance Method Summary collapse
- #create_clip(options = {}) ⇒ Object
- #create_entitlement_grant_url(options = {}) ⇒ Object
- #get_bits_leaderboard(options = {}) ⇒ Object
- #get_clips(options = {}) ⇒ Object
- #get_game_analytics(options = {}) ⇒ Object
- #get_games(options = {}) ⇒ Object
- #get_streams(options = {}) ⇒ Object
- #get_streams_metadata(options = {}) ⇒ Object
- #get_top_games(options = {}) ⇒ Object
- #get_users(options = {}) ⇒ Object
- #get_users_follows(options = {}) ⇒ Object
- #get_videos(options = {}) ⇒ Object
-
#initialize(client_id: nil, access_token: nil, with_raw: false) ⇒ Client
constructor
Initializes a Twitch client.
- #update_user(options = {}) ⇒ Object
Constructor Details
#initialize(client_id: nil, access_token: nil, with_raw: false) ⇒ Client
Initializes a Twitch client.
-
client_id [String] The client ID.
Used as the Client-ID header in a request.
-
access_token [String] An access token.
Used as the Authorization header in a request. Any “Bearer ” prefix will be stripped.
-
with_raw [Boolean] Whether to include raw HTTP response
Intended for testing/checking API results
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/twitch/client.rb', line 31 def initialize(client_id: nil, access_token: nil, with_raw: false) if client_id.nil? && access_token.nil? raise "An identifier token (client ID or bearer token) is required" elsif !!client_id && !!access_token warn(%{WARNING: It is recommended that only one identifier token is specified. Unpredictable behavior may follow.}) end headers = { "User-Agent": "twitch-api ruby client #{Twitch::VERSION}" } unless client_id.nil? headers["Client-ID"] = client_id end unless access_token.nil? access_token = access_token.gsub(/^Bearer /, "") headers["Authorization"] = "Bearer #{access_token}" end @conn = Faraday.new(API_ENDPOINT, { headers: headers }) do |faraday| faraday.request :json faraday.response :json faraday.adapter Faraday.default_adapter end @with_raw = with_raw end |
Instance Method Details
#create_clip(options = {}) ⇒ Object
60 61 62 |
# File 'lib/twitch/client.rb', line 60 def create_clip( = {}) Response.new(Clip, post('clips', )) end |
#create_entitlement_grant_url(options = {}) ⇒ Object
64 65 66 |
# File 'lib/twitch/client.rb', line 64 def create_entitlement_grant_url( = {}) Response.new(EntitlementGrantUrl, post('entitlements/upload', )) end |
#get_bits_leaderboard(options = {}) ⇒ Object
72 73 74 |
# File 'lib/twitch/client.rb', line 72 def get_bits_leaderboard( = {}) Response.new(BitsLeader, get('bits/leaderboard', )) end |
#get_clips(options = {}) ⇒ Object
68 69 70 |
# File 'lib/twitch/client.rb', line 68 def get_clips( = {}) Response.new(Clip, get('clips', )) end |
#get_game_analytics(options = {}) ⇒ Object
84 85 86 |
# File 'lib/twitch/client.rb', line 84 def get_game_analytics( = {}) Response.new(GameAnalytic, get('analytics/games', )) end |
#get_games(options = {}) ⇒ Object
76 77 78 |
# File 'lib/twitch/client.rb', line 76 def get_games( = {}) Response.new(Game, get('games', )) end |
#get_streams(options = {}) ⇒ Object
88 89 90 |
# File 'lib/twitch/client.rb', line 88 def get_streams( = {}) Response.new(Stream, get('streams', )) end |
#get_streams_metadata(options = {}) ⇒ Object
92 93 94 |
# File 'lib/twitch/client.rb', line 92 def ( = {}) Response.new(StreamMetadata, get('streams/metadata', )) end |
#get_top_games(options = {}) ⇒ Object
80 81 82 |
# File 'lib/twitch/client.rb', line 80 def get_top_games( = {}) Response.new(Game, get('games/top', )) end |
#get_users(options = {}) ⇒ Object
100 101 102 |
# File 'lib/twitch/client.rb', line 100 def get_users( = {}) Response.new(User, get('users', )) end |
#get_users_follows(options = {}) ⇒ Object
96 97 98 |
# File 'lib/twitch/client.rb', line 96 def get_users_follows( = {}) Response.new(UserFollow, get('users/follows', )) end |