Sha256: 9cb368d72a2ee9e24b604d636810d9d10ba3699173b73ad839c5d81cb92742b9

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# encoding: utf-8
require "rest-client"
require "json"

module Twitchus
  class Checker
    def initialize(config = nil)
      @config = config
    end

    def fetch_all(channels)
      channels.map { |channel| check(channel) }.select { |c| !c.nil? }
    end

    # Return a list of online channel names
    def check_all(channels)
      channels.select { |channel| online?(channel) }
    end

    def check(channel)
      raise ArgumentError, "Channel is required." unless channel
      response = RestClient.get(base_url + channel)
      response = JSON.parse(response)

      response["stream"]
    rescue RestClient::BadRequest => e
      $stderr.puts "Request failed due to rate limit, channel: #{channel}, #{e}"
      nil
    rescue RestClient::ResourceNotFound => e
      $stderr.puts "Stream #{channel} is not available anymore"
      nil
    rescue RestClient::RequestTimeout => e
      $stderr.puts "Request to a channel #{channel} timed out"
      nil
    rescue RestClient::UnprocessableEntity => e
      $stderr.puts "API responded with 422 error #{e}"
      nil
    end

    def online?(channel)
      !check(channel).nil?
    end

    def url_for(channel)
      url = base_url + channel
      url += "?client_key=#{@config.client_key}" if @config
      url
    end

    def base_url
      "https://api.twitch.tv/kraken/streams/"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitchus-0.1.7 lib/twitchus/checker.rb