Class: Twitch::Client

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, access_token: nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/twitch/client.rb', line 13

def initialize(client_id:, access_token: nil)
  headers = {
    "Client-ID": client_id,
    "User-Agent": "twitch-api ruby client #{Twitch::VERSION}"
  }
  unless access_token.nil?
    headers["Authorization"] = "Bearer #{access_token}"
  end
  
  @conn = Faraday.new(API_ENDPOINT, { headers: headers }) do |faraday|
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

Instance Method Details

#get_games(options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/twitch/client.rb', line 40

def get_games(options = {})
  res = get('games', options)

  games = res.body['data'].map { |g| Game.new(g) }
end

#get_streams(options = {}) ⇒ Object



28
29
30
31
32
# File 'lib/twitch/client.rb', line 28

def get_streams(options = {})
  res = get('streams', options)

  streams = res.body['data'].map { |s| Stream.new(s) }
end

#get_users(options = {}) ⇒ Object



34
35
36
37
38
# File 'lib/twitch/client.rb', line 34

def get_users(options = {})
  res = get('users', options)

  users = res.body['data'].map { |u| User.new(u) }
end

#get_videos(options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/twitch/client.rb', line 46

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

  videos = res.body['data'].map { |v| Video.new(v) }
end