Module: NotifyTwitch

Extended by:
NotifyTwitch
Included in:
NotifyTwitch
Defined in:
lib/notify_twitch.rb

Instance Method Summary collapse

Instance Method Details

#api_call(location, params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/notify_twitch.rb', line 7

def api_call(location,params)
	config = Config.new
	base = "#{BASE_API}#{location}?"
	params.each do |k,v|
		if base[-1] == "?"
			base << "#{k}=#{v}"
		elsif k.nil? && base[-1] == "?"
			base << v
		elsif k.nil?
			base << "&#{v}"
		else
			base << "&#{k}=#{v}"
		end
	end
	url = URI.parse(base)
	Net::HTTP.start(url.host,url.port,:use_ssl => url.scheme == "https") do |http|
		request = Net::HTTP::Get.new(url)
		request["Client-ID"] = config.client_id
		response = http.request request
		if response.code.to_i < 400 
			JSON.parse response.body
		else
			raise "Bad status code #{response.code} #{response.body}"
		end
	end
end

#get_followers(id) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/notify_twitch.rb', line 39

def get_followers(id)
	followees_ids = []
	url = URI.parse "#{BASE_API}/users/follows?from_id=#{id}&first=100"
	followees = api_call("/users/follows",{:from_id => id, :first => 100})["data"]
	followees.each do |f|
			followees_ids << f["to_id"]
			sleep 1
	end
	followees_ids
end

#get_live_followers(username) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/notify_twitch.rb', line 49

def get_live_followers(username)
	followers_users = []
	id = get_user_id(username)
	followers = get_followers(id).map {|f| "user_id=#{f}"}.join("&")
	api_call("/streams",{:type => "live", :first => 100, nil => followers})["data"].each do |f|
		id = f["user_id"]
		followers_users << get_user(id)["login"]
		sleep 1
	end
	followers_users
end

#get_user(id) ⇒ Object



36
37
38
# File 'lib/notify_twitch.rb', line 36

def get_user(id)
	api_call("/users",{:id => id})["data"].first
end

#get_user_id(username) ⇒ Object



33
34
35
# File 'lib/notify_twitch.rb', line 33

def get_user_id(username)
	api_call("/users",{:login => username})["data"].first["id"]
end

#notify(username, offline = false) ⇒ Object



60
61
62
63
# File 'lib/notify_twitch.rb', line 60

def notify(username,offline=false)
	status = !offline ? "online" : "offline"
	%x[notify-send "#{username} is #{status}"] 
end