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) ⇒ 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(options = {})
  res = post('clips', options)

  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(options = {})
  res = post('entitlements/upload', options)

  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(options = {})
  res = get('clips', options)

  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(options = {})
  res = get('games', options)

  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(options = {})
  res = get('streams', options)

  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 (options = {})
  res = get('streams/metadata', options)

   = 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(options = {})
  res = get('games/top', options)

  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(options = {})
  res = get('users', options)

  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(options = {})
  res = get('users/follows', options)

  users = res[:data].map { |u| UserFollow.new(u) }
  Response.new(users, res[:rate_limit_headers], res[:pagination])
end

#get_videos(options = {}) ⇒ Object



124
125
126
127
128
129
# File 'lib/twitch/client.rb', line 124

def get_videos(options = {})
  res = get('videos', options)

  videos = res[:data].map { |v| Video.new(v) }
  Response.new(videos, res[:rate_limit_headers], res[:pagination])
end

#update_user(options = {}) ⇒ Object



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

def update_user(options = {})
  res = put('users', options)

  user = res[:data].map { |u| User.new(u) }
  Response.new(user, res[:rate_limit_headers])
end