class Twitch::Client

Constants

API_ENDPOINT

Helix API endpoint.

Public Class Methods

new(client_id: nil, access_token: nil, with_raw: false) click to toggle source

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

# File lib/twitch/client.rb, line 32
    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

Public Instance Methods

create_clip(options = {}) click to toggle source
# File lib/twitch/client.rb, line 61
def create_clip(options = {})
  Response.new(Clip, post('clips', options))
end
create_entitlement_grant_url(options = {}) click to toggle source
# File lib/twitch/client.rb, line 65
def create_entitlement_grant_url(options = {})
  Response.new(EntitlementGrantUrl, post('entitlements/upload', options))
end
create_stream_marker(options = {}) click to toggle source
# File lib/twitch/client.rb, line 69
def create_stream_marker(options = {})
  Response.new(StreamMarker, post('streams/markers', options))
end
get_bits_leaderboard(options = {}) click to toggle source
# File lib/twitch/client.rb, line 77
def get_bits_leaderboard(options = {})
  Response.new(BitsLeader, get('bits/leaderboard', options))
end
get_clips(options = {}) click to toggle source
# File lib/twitch/client.rb, line 73
def get_clips(options = {})
  Response.new(Clip, get('clips', options))
end
get_game_analytics(options = {}) click to toggle source
# File lib/twitch/client.rb, line 89
def get_game_analytics(options = {})
  Response.new(GameAnalytic, get('analytics/games', options))
end
get_games(options = {}) click to toggle source
# File lib/twitch/client.rb, line 81
def get_games(options = {})
  Response.new(Game, get('games', options))
end
get_stream_markers(options = {}) click to toggle source
# File lib/twitch/client.rb, line 93
def get_stream_markers(options = {})
  Response.new(StreamMarkerResponse, get('streams/markers', options))
end
get_streams(options = {}) click to toggle source
# File lib/twitch/client.rb, line 97
def get_streams(options = {})
  Response.new(Stream, get('streams', options))
end
get_streams_metadata(options = {}) click to toggle source
# File lib/twitch/client.rb, line 101
def get_streams_metadata(options = {})
  Response.new(StreamMetadata, get('streams/metadata', options))
end
get_top_games(options = {}) click to toggle source
# File lib/twitch/client.rb, line 85
def get_top_games(options = {})
  Response.new(Game, get('games/top', options))
end
get_users(options = {}) click to toggle source
# File lib/twitch/client.rb, line 109
def get_users(options = {})
  Response.new(User, get('users', options))
end
get_users_follows(options = {}) click to toggle source
# File lib/twitch/client.rb, line 105
def get_users_follows(options = {})
  Response.new(UserFollow, get('users/follows', options))
end
get_videos(options = {}) click to toggle source
# File lib/twitch/client.rb, line 117
def get_videos(options = {})
  Response.new(Video, get('videos', options))
end
update_user(options = {}) click to toggle source
# File lib/twitch/client.rb, line 113
def update_user(options = {})
  Response.new(User, put('users', options))
end

Private Instance Methods

finish(http_res) click to toggle source
# File lib/twitch/client.rb, line 138
def finish(http_res)
  unless http_res.success?
    raise ApiError.new(http_res.status, http_res.body)
  end

  {
      http_res: http_res,
      with_raw: @with_raw
  }
end
get(resource, params) click to toggle source
# File lib/twitch/client.rb, line 123
def get(resource, params)
  http_res = @conn.get(resource, params)
  finish(http_res)
end
post(resource, params) click to toggle source
# File lib/twitch/client.rb, line 128
def post(resource, params)
  http_res = @conn.post(resource, params)
  finish(http_res)
end
put(resource, params) click to toggle source
# File lib/twitch/client.rb, line 133
def put(resource, params)
  http_res = @conn.put(resource, params)
  finish(http_res)
end