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_clips(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) ⇒ Client
constructor
Initializes a Twitch client.
- #update_user(options = {}) ⇒ Object
Constructor Details
#initialize(client_id: nil, access_token: nil) ⇒ 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.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/twitch/client.rb', line 27 def initialize(client_id: nil, access_token: nil) 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 end |
Instance Method Details
#create_clip(options = {}) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/twitch/client.rb', line 54 def create_clip( = {}) res = post('clips', ) clip = res[:data].map { |c| Clip.new(c) } Response.new(clip, res[:rate_limit_headers]) end |
#create_entitlement_grant_url(options = {}) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/twitch/client.rb', line 61 def create_entitlement_grant_url( = {}) res = post('entitlements/upload', ) entitlement_grant = res[:data].map { |e| EntitlementGrantUrl.new(e) } Response.new(entitlement_grant, res[:rate_limit_headers]) end |
#get_clips(options = {}) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/twitch/client.rb', line 68 def get_clips( = {}) res = get('clips', ) clips = res[:data].map { |c| Clip.new(c) } Response.new(clips, res[:rate_limit_headers]) end |
#get_games(options = {}) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/twitch/client.rb', line 75 def get_games( = {}) res = get('games', ) games = res[:data].map { |g| Game.new(g) } Response.new(games, res[:rate_limit_headers]) end |
#get_streams(options = {}) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/twitch/client.rb', line 89 def get_streams( = {}) res = get('streams', ) streams = res[:data].map { |s| Stream.new(s) } Response.new(streams, res[:rate_limit_headers], res[:pagination]) end |
#get_streams_metadata(options = {}) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/twitch/client.rb', line 96 def ( = {}) res = get('streams/metadata', ) = res[:data].map { |s| StreamMetadata.new(s) } Response.new(, res[:rate_limit_headers], res[:pagination]) end |
#get_top_games(options = {}) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/twitch/client.rb', line 82 def get_top_games( = {}) res = get('games/top', ) games = res[:data].map { |g| Game.new(g) } Response.new(games, res[:rate_limit_headers], res[:pagination]) end |
#get_users(options = {}) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/twitch/client.rb', line 110 def get_users( = {}) res = get('users', ) users = res[:data].map { |u| User.new(u) } Response.new(users, res[:rate_limit_headers]) end |
#get_users_follows(options = {}) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/twitch/client.rb', line 103 def get_users_follows( = {}) res = get('users/follows', ) users = res[:data].map { |u| UserFollow.new(u) } Response.new(users, res[:rate_limit_headers], res[:pagination]) end |