Class: Twitch::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/twitch/client.rb

Constant Summary collapse

API_ENDPOINT =

Helix API endpoint.

"https://api.twitch.tv/helix".freeze

Instance Method Summary collapse

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



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
59
# 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

Instance Method Details

#create_clip(options = {}) ⇒ Object



61
62
63
# File 'lib/twitch/client.rb', line 61

def create_clip(options = {})
  Response.new(Clip, post('clips', options))
end

#create_entitlement_grant_url(options = {}) ⇒ Object



65
66
67
# 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 = {}) ⇒ Object



69
70
71
# File 'lib/twitch/client.rb', line 69

def create_stream_marker(options = {})
  Response.new(StreamMarker, post('streams/markers', options))
end

#get_bits_leaderboard(options = {}) ⇒ Object



77
78
79
# File 'lib/twitch/client.rb', line 77

def get_bits_leaderboard(options = {})
  Response.new(BitsLeader, get('bits/leaderboard', options))
end

#get_clips(options = {}) ⇒ Object



73
74
75
# File 'lib/twitch/client.rb', line 73

def get_clips(options = {})
  Response.new(Clip, get('clips', options))
end

#get_game_analytics(options = {}) ⇒ Object



89
90
91
# File 'lib/twitch/client.rb', line 89

def get_game_analytics(options = {})
  Response.new(GameAnalytic, get('analytics/games', options))
end

#get_games(options = {}) ⇒ Object



81
82
83
# File 'lib/twitch/client.rb', line 81

def get_games(options = {})
  Response.new(Game, get('games', options))
end

#get_stream_markers(options = {}) ⇒ Object



93
94
95
# File 'lib/twitch/client.rb', line 93

def get_stream_markers(options = {})
  Response.new(StreamMarkerResponse, get('streams/markers', options))
end

#get_streams(options = {}) ⇒ Object



97
98
99
# File 'lib/twitch/client.rb', line 97

def get_streams(options = {})
  Response.new(Stream, get('streams', options))
end

#get_streams_metadata(options = {}) ⇒ Object



101
102
103
# File 'lib/twitch/client.rb', line 101

def (options = {})
  Response.new(StreamMetadata, get('streams/metadata', options))
end

#get_top_games(options = {}) ⇒ Object



85
86
87
# File 'lib/twitch/client.rb', line 85

def get_top_games(options = {})
  Response.new(Game, get('games/top', options))
end

#get_users(options = {}) ⇒ Object



109
110
111
# File 'lib/twitch/client.rb', line 109

def get_users(options = {})
  Response.new(User, get('users', options))
end

#get_users_follows(options = {}) ⇒ Object



105
106
107
# File 'lib/twitch/client.rb', line 105

def get_users_follows(options = {})
  Response.new(UserFollow, get('users/follows', options))
end

#get_videos(options = {}) ⇒ Object



117
118
119
# File 'lib/twitch/client.rb', line 117

def get_videos(options = {})
  Response.new(Video, get('videos', options))
end

#update_user(options = {}) ⇒ Object



113
114
115
# File 'lib/twitch/client.rb', line 113

def update_user(options = {})
  Response.new(User, put('users', options))
end