module TwitchAPI

Public Class Methods

get_follow_data(user) click to toggle source
# File lib/twitchy/twitch_api.rb, line 8
def self.get_follow_data(user)
    catch_exception(OpenURI::HTTPError) do
        get_as_struct("users/#{user}/follows/channels").follows
    end
end
get_stream_data(streamer) click to toggle source
# File lib/twitchy/twitch_api.rb, line 23
def self.get_stream_data(streamer)
    catch_exception(OpenURI::HTTPError) do
        get_as_struct("streams/#{streamer}").stream
    end
end
get_stream_status(streamers) click to toggle source
# File lib/twitchy/twitch_api.rb, line 14
def self.get_stream_status(streamers)
    query = "streams?channel=#{streamers.join(",")}"
    catch_exception(OpenURI::HTTPError, default: {}) do
        get_as_struct(query).streams.map do |s|
            [s.channel.name, s]
        end.to_h
    end
end
get_streamers_for_game(game, limit: 10, offset: 0) click to toggle source
# File lib/twitchy/twitch_api.rb, line 40
def self.get_streamers_for_game(game, limit: 10, offset: 0)
    catch_exception(OpenURI::HTTPError) do
        query = "streams?game=#{URI.encode(game)}&limit=#{limit}&offset=#{offset}"
        get_as_struct(query).streams.map{|s| s.channel.name}
    end
end
get_videos(streamer, limit: 10, offset: 0, highlights: false) click to toggle source
# File lib/twitchy/twitch_api.rb, line 29
def self.get_videos(streamer, limit: 10, offset: 0, highlights: false)
    catch_exception(OpenURI::HTTPError) do
        query = "channels/#{streamer}/videos"\
                "?broadcasts=#{!highlights}"\
                "&limit=#{limit}&offset=#{offset}"
        get_as_struct(query).videos.each do |v|
            v.recorded_at = DateTime.parse(v.recorded_at)
        end
    end
end

Private Class Methods

catch_exception(exception, default: nil) { || ... } click to toggle source
# File lib/twitchy/twitch_api.rb, line 48
def self.catch_exception(exception, default: nil, &block)
    if block
        begin
            yield
        rescue exception
            default
        end
    else
        default
    end
end
get(request) click to toggle source
# File lib/twitchy/twitch_api.rb, line 60
def self.get(request)
    open( @@PREFIX + request ).each_line.to_a.join
end
get_as_json(request) click to toggle source
# File lib/twitchy/twitch_api.rb, line 64
def self.get_as_json(request)
    JSON.parse(get(request))
end
get_as_struct(request) click to toggle source
# File lib/twitchy/twitch_api.rb, line 68
def self.get_as_struct(request)
    DeepStruct.new(get_as_json(request))
end