Sha256: 9b579a61bb75d1d112e0bca80dba248f84648d0d7f3f64167ff0447ecfab7042

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 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
    rescue RestClient::ServiceUnavailable => e
      $stderr.puts "API service is not available #{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.8 lib/twitchus/checker.rb