module NotifyTwitch

Public Instance Methods

api_call(location,params) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/notify_twitch.rb, line 36
def get_user(id)
        api_call("/users",{:id => id})["data"].first
end
get_user_id(username) click to toggle source
# 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) click to toggle source
# File lib/notify_twitch.rb, line 60
def notify(username,offline=false)
        status = !offline ? "online" : "offline"
        %x[notify-send "#{username} is #{status}"] 
end