Class: Twitch::V2::Videos

Inherits:
Object
  • Object
show all
Defined in:
lib/kappa/video.rb

Overview

Query class for finding videos.

See Also:

Instance Method Summary collapse

Instance Method Details

#for_channel(channel, options = {}) {|video| ... } ⇒ Array<Video>?

Get the videos for a channel, most recently created first.

Examples:

v = Twitch.videos.for_channel('dreamhacktv')
v = Twitch.videos.for_channel('dreamhacktv', :type => :highlights, :limit => 10)
Twitch.videos.for_channel('dreamhacktv') do |video|
  next if video.view_count < 10000
  puts video.url
end

Parameters:

  • options (Hash) (defaults to: {})

    Filter criteria.

Options Hash (options):

  • :type (Symbol) — default: :highlights

    The type of videos to return. Valid values are :broadcasts, :highlights.

  • :limit (Fixnum) — default: nil

    Limit on the number of results returned.

  • :offset (Fixnum) — default: 0

    Offset into the result set to begin enumeration.

Yields:

  • Optional. If a block is given, each video is yielded.

Yield Parameters:

  • video (Video)

    Current video.

Returns:

  • (Array<Video>)

    Videos for the channel, if no block is given.

  • (nil)

    If a block is given.

Raises:

  • (ArgumentError)

    If :type is not one of :broadcasts or :highlights.

See Also:



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/kappa/video.rb', line 200

def for_channel(channel, options = {})
  if channel.respond_to?(:name)
    channel_name = channel.name
  else
    channel_name = channel.to_s
  end

  params = {}

  type = options[:type] || :highlights
  if !type.nil?
    if ![:broadcasts, :highlights].include?(type)
      raise ArgumentError, 'type'
    end

    params[:broadcasts] = (type == :broadcasts)
  end

  name = CGI.escape(channel_name)
  return @query.connection.accumulate(
    :path => "channels/#{name}/videos",
    :params => params,
    :json => 'videos',
    :create => -> hash { Video.new(hash, @query) },
    :limit => options[:limit],
    :offset => options[:offset]
  )
end

#get(id) ⇒ Video

Get a video by ID.

Examples:

Twitch.videos.get('a396294648')

Parameters:

  • id (String)

    The ID of the video to get.

Returns:

  • (Video)

    A valid Video object if the video exists, nil otherwise.

Raises:

  • (ArgumentError)

    If id is nil or empty.



120
121
122
123
124
125
126
127
128
# File 'lib/kappa/video.rb', line 120

def get(id)
  raise ArgumentError, 'id' if !id || id.strip.empty?

  id = CGI.escape(id)
  Twitch::Status.map(404 => nil) do
    json = @query.connection.get("videos/#{id}")
    Video.new(json, @query)
  end
end

#top(options = {}) {|video| ... } ⇒ Array<Video>?

Note:

The number of videos returned is potentially very large, so it's recommended that you specify a :limit.

Get the list of most popular videos based on view count.

Examples:

Twitch.videos.top
Twitch.videos.top(:period => :month, :game => 'Super Meat Boy')
Twitch.videos.top(:period => :all, :limit => 10)
Twitch.videos.top(:period => :all) do |video|
  next if video.view_count < 10000
  puts video.url
end

Parameters:

  • options (Hash) (defaults to: {})

    Filter criteria.

Options Hash (options):

  • :period (Symbol) — default: :week

    Return videos only in this time period. Valid values are :week, :month, :all.

  • :game (String) — default: nil

    Return videos only for this game.

  • :limit (Fixnum) — default: nil

    Limit on the number of results returned.

  • :offset (Fixnum) — default: 0

    Offset into the result set to begin enumeration.

Yields:

  • Optional. If a block is given, each top video is yielded.

Yield Parameters:

  • video (Video)

    Current video.

Returns:

  • (Array<Video>)

    Top videos, if no block is given.

  • (nil)

    If a block is given.

Raises:

  • (ArgumentError)

    If :period is not one of :week, :month, or :all.

See Also:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/kappa/video.rb', line 154

def top(options = {}, &block)
  params = {}

  if options[:game]
    params[:game] = options[:game]
  end

  period = options[:period] || :week
  if ![:week, :month, :all].include?(period)
    raise ArgumentError, 'period'
  end

  params[:period] = period.to_s

  return @query.connection.accumulate(
    :path => 'videos/top',
    :params => params,
    :json => 'videos',
    :create => -> hash { Video.new(hash, @query) },
    :limit => options[:limit],
    :offset => options[:offset],
    &block
  )
end