Class: Sugilite::Kraken

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

Overview

retrieve channel chatrooms from Twitch Kraken

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*channel_id, client_id, auth) ⇒ Kraken

get things up and running



16
17
18
19
20
21
22
23
# File 'lib/sugiliteKraken.rb', line 16

def initialize(*channel_id, client_id, auth)
  # channel id of Twitch Channel
  @channel_id = channel_id
  # client id of user trying to access Twitch api
  @client_id = client_id
  # OAuth of person trying to access Twitch API
  @auth = "OAuth #{auth}"
end

Instance Attribute Details

#responseObject

direct access to the response from net::http



13
14
15
# File 'lib/sugiliteKraken.rb', line 13

def response
  @response
end

Instance Method Details

#getroomsObject

Get chat rooms for user id specified



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sugiliteKraken.rb', line 43

def getrooms
  uri = URI.parse("https://api.twitch.tv/kraken/chat/#{@channel_id[0]}/rooms")
  request = Net::HTTP::Get.new(uri)
  request['Accept'] = 'application/vnd.twitchtv.v5+json'
  request['Client-Id'] = @client_id
  request['Authorization'] = @auth

  req_options = {
      use_ssl: uri.scheme == 'https'
  }
  @response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  Oj.load(@response.body, mode: :object)
end

#getuserid(username) ⇒ Object

Convert username to userid



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sugiliteKraken.rb', line 26

def getuserid(username)
  uri = URI.parse("https://api.twitch.tv/kraken/users?login=#{username}")
  request = Net::HTTP::Get.new(uri)
  request['Accept'] = 'application/vnd.twitchtv.v5+json'
  request['Client-Id'] = @client_id

  req_options = {
      use_ssl: uri.scheme == 'https'
  }
  @response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  id = Oj.load(@response.body, mode: :object)
  id.fetch('users')[0].fetch('_id')
end