Class: Twitchus::Checker
- Inherits:
-
Object
- Object
- Twitchus::Checker
- Defined in:
- lib/twitchus/checker.rb
Instance Method Summary collapse
- #base_url ⇒ Object
- #check(channel) ⇒ Object
-
#check_all(channels) ⇒ Object
Return a list of online channel names.
- #fetch_all(channels) ⇒ Object
-
#initialize(config = nil) ⇒ Checker
constructor
A new instance of Checker.
- #online?(channel) ⇒ Boolean
- #url_for(channel) ⇒ Object
Constructor Details
#initialize(config = nil) ⇒ Checker
Returns a new instance of Checker.
7 8 9 |
# File 'lib/twitchus/checker.rb', line 7 def initialize(config = nil) @config = config end |
Instance Method Details
#base_url ⇒ Object
53 54 55 |
# File 'lib/twitchus/checker.rb', line 53 def base_url "https://api.twitch.tv/kraken/streams/" end |
#check(channel) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/twitchus/checker.rb', line 20 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 |
#check_all(channels) ⇒ Object
Return a list of online channel names
16 17 18 |
# File 'lib/twitchus/checker.rb', line 16 def check_all(channels) channels.select { |channel| online?(channel) } end |
#fetch_all(channels) ⇒ Object
11 12 13 |
# File 'lib/twitchus/checker.rb', line 11 def fetch_all(channels) channels.map { |channel| check(channel) }.select { |c| !c.nil? } end |
#online?(channel) ⇒ Boolean
43 44 45 |
# File 'lib/twitchus/checker.rb', line 43 def online?(channel) !check(channel).nil? end |
#url_for(channel) ⇒ Object
47 48 49 50 51 |
# File 'lib/twitchus/checker.rb', line 47 def url_for(channel) url = base_url + channel url += "?client_key=#{@config.client_key}" if @config url end |