Class: RubyTube::StreamQuery

Inherits:
Array
  • Object
show all
Defined in:
lib/rubytube/stream_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fmt_streams) ⇒ StreamQuery

Returns a new instance of StreamQuery.



5
6
7
8
# File 'lib/rubytube/stream_query.rb', line 5

def initialize(fmt_streams)
  super
  @streams = fmt_streams
end

Instance Attribute Details

#streamsObject (readonly)

Returns the value of attribute streams.



3
4
5
# File 'lib/rubytube/stream_query.rb', line 3

def streams
  @streams
end

Instance Method Details

#filter(file_extension: nil, only_audio: false, only_video: false, resolution: nil, progressive: false, adaptive: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubytube/stream_query.rb', line 10

def filter(file_extension: nil, only_audio: false, only_video: false, resolution: nil, progressive: false, adaptive: false)
  filters = []

  filters << ->(stream) { stream.subtype == file_extension } if file_extension
  filters << ->(stream) { stream.is_audio? } if only_audio
  filters << ->(stream) { stream.is_video? } if only_video
  filters << ->(stream) { stream.resolution == resolution } if resolution
  filters << ->(stream) { stream.is_progressive? } if progressive
  filters << ->(stream) { stream.is_adaptive? } if adaptive

  r = streams
  filters.each do |f|
    r = r.select(&f)
  end

  StreamQuery.new(r)
end

#get_by_itag(itag) ⇒ Object



28
29
30
# File 'lib/rubytube/stream_query.rb', line 28

def get_by_itag(itag)
  streams.find { |s| s.itag == itag }
end

#get_by_resolution(resolution) ⇒ Object



32
33
34
# File 'lib/rubytube/stream_query.rb', line 32

def get_by_resolution(resolution)
  streams.find { |s| s.resolution == resolution }
end

#get_highest_resolutionObject



36
37
38
# File 'lib/rubytube/stream_query.rb', line 36

def get_highest_resolution
  order(resolution: :desc).first
end

#order(arg) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubytube/stream_query.rb', line 40

def order(arg)
  case arg
  when Symbol
    field = arg
    dir = :asc
  when Hash
    field = arg.keys.first
    dir = (arg[field] == :desc) ? :desc : :asc
  end

  allowed_fields = [:file_size, :itag, :resolution]
  raise InvalidArgumentError unless allowed_fields.include? field

  r = streams
  r.sort! { |a, b| a.send(field).to_i <=> b.send(field).to_i }

  r.reverse! if dir == :desc

  StreamQuery.new(r)
end