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



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(options = {})
  Response.new(Clip, post('clips', options))
end

#create_entitlement_grant_url(options = {}) ⇒ Object



64
65
66
# File 'lib/twitch/client.rb', line 64

def create_entitlement_grant_url(options = {})
  Response.new(EntitlementGrantUrl, post('entitlements/upload', options))
end

#get_bits_leaderboard(options = {}) ⇒ Object



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

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

#get_clips(options = {}) ⇒ Object



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

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

#get_game_analytics(options = {}) ⇒ Object



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

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

#get_games(options = {}) ⇒ Object



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

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

#get_streams(options = {}) ⇒ Object



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

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

#get_streams_metadata(options = {}) ⇒ Object



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

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

#get_top_games(options = {}) ⇒ Object



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

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

#get_users(options = {}) ⇒ Object



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

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

#get_users_follows(options = {}) ⇒ Object



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

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

#get_videos(options = {}) ⇒ Object



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

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

#update_user(options = {}) ⇒ Object



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

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