Class: RubyTube::Cipher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(js) ⇒ Cipher

Returns a new instance of Cipher.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubytube/cipher.rb', line 5

def initialize(js)
  self.transform_plan = get_transform_plan(js)

  var_regex = %r{^\$*\w+\W}
  var_match = @transform_plan[0].match(var_regex)

  if var_match.nil?
    raise "RegexMatchError, caller: __init__, pattern: #{var_regex.source}"
  end

  var = var_match[0][0..-2]

  self.transform_map = get_transform_map(js, var)

  self.js_func_patterns = [
    %r{\w+\.(\w+)\(\w,(\d+)\)},
    %r{\w+\[("\w+")\]\(\w,(\d+)\)}
  ]

  self.throttling_array = get_throttling_function_array(js)
  self.throttling_plan = get_throttling_plan(js)
end

Instance Attribute Details

#calculation_nObject

Returns the value of attribute calculation_n.



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

def calculation_n
  @calculation_n
end

#calculation_planObject

Returns the value of attribute calculation_plan.



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

def calculation_plan
  @calculation_plan
end

#js_func_patternsObject

Returns the value of attribute js_func_patterns.



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

def js_func_patterns
  @js_func_patterns
end

#throttling_arrayObject

Returns the value of attribute throttling_array.



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

def throttling_array
  @throttling_array
end

#throttling_planObject

Returns the value of attribute throttling_plan.



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

def throttling_plan
  @throttling_plan
end

#transform_mapObject

Returns the value of attribute transform_map.



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

def transform_map
  @transform_map
end

#transform_planObject

Returns the value of attribute transform_plan.



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

def transform_plan
  @transform_plan
end

Instance Method Details

#calculate_n(initial_n) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubytube/cipher.rb', line 28

def calculate_n(initial_n)
  throttling_array.map! do |item|
    (item == "b") ? initial_n : item
  end

  throttling_plan.each do |step|
    curr_func = throttling_array[step[0].to_i]

    unless curr_func.respond_to?(:call)
      raise ExtractError.new("calculate_n", "curr_func")
    end

    first_arg = throttling_array[step[1].to_i]

    case step.length
    when 2
      curr_func.call(first_arg)
    when 3
      second_arg = throttling_array[step[2].to_i]
      curr_func.call(first_arg, second_arg)
    end
  end

  initial_n.join
end

#get_signature(ciphered_signature) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/rubytube/cipher.rb', line 54

def get_signature(ciphered_signature)
  signature = ciphered_signature.chars

  transform_plan.each do |js_func|
    name, argument = parse_function(js_func)
    signature = transform_map[name].call(signature, argument)
  end

  signature.join("")
end