Class: JserrorRails::JserrorTemplate

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/jserror-rails/jserror_template.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_mime_typeObject



7
8
9
# File 'lib/jserror-rails/jserror_template.rb', line 7

def self.default_mime_type
  'application/javascript'
end

Instance Method Details

#evaluate(scope, locals, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jserror-rails/jserror_template.rb', line 14

def evaluate(scope, locals, &block)
  data_copy = data
  scanning_data = data
  functions = []
  capture = true
  while capture
    function = (/(^.*= function.*$)/).match(scanning_data)
    if function 
      if function.to_s.count('{') != function.to_s.count('}')
        old_lines = []
        new_code_lines = []

        scanning_data = function.post_match
        method_line = function.captures.first
      
        tabs = method_line.partition(method_line.strip[0]).first
        name = method_line.match(/(.*) = function/).captures.first.strip
      
        arguments = method_line.gsub(tabs, '').gsub(name, '').gsub(/\s*=\s*function\s*\(/, '').gsub(/\)\s*{.*$/, '').strip.split(', ').map {|a| "{#{a}: #{a}}"}

        new_code_lines << method_line
        old_lines << method_line
      
        new_code_lines << "\n#{tabs}\ttry {"

        bracket_count = 1 #start scanning until we find the ending bracket
        scanning_data.each_line do |line|
          line.chars do |char|
            bracket_count += 1 if char == '{'
            bracket_count -= 1 if char == '}'
          end
          new_code_lines << "\t" + line
          old_lines << line
          break if bracket_count <= 0
        end
      
        new_code_lines.pop #pop of the last }; to add catch block before closing bracket in function

        console_line = "javascript_error({name: '#{name.gsub('\'', '"')}',  
                                    error_message: e.message, 
                                    error_name: e.name, 
                                    code_block: '#{old_lines.join('').gsub("\n", '[::n::]').gsub('\'', '')}', 
                                    arguments: [#{arguments.join(', ')}]
                        });"
      
        new_code_lines << "#{tabs}\t} catch (e) { \n#{tabs}\t\t#{console_line}\n#{tabs}\t}\n"
        new_code_lines << "#{tabs}};\n"
        functions << {:replace => old_lines.join(''), :new => new_code_lines.join('')}
      else
        scanning_data = function.post_match
      end
    else
      capture = false
    end
  end
  
  scanning_data = data_copy.clone
  capture = true
  while capture
    function = (/(.*\(.+function\(.*\).*\{.*$)/).match(scanning_data)
    if function
      if function.to_s.match(/setTimeout/).nil?
        old_lines = []
        new_code_lines = []

        scanning_data = function.post_match
        method_line = function.captures.first
      
        tabs = method_line.partition(method_line.strip[0]).first
      
        name = method_line
        arguments = (/.*\(.+function\((.*)\).*\{.*$/).match(method_line).captures.first.split(', ').map {|a| "{#{a}: #{a}}"}

        new_code_lines << method_line
        old_lines << method_line
      
        new_code_lines << "\n#{tabs}\ttry {"

        bracket_count = 1 #start scanning until we find the ending bracket
        scanning_data.each_line do |line|
          line.chars do |char|
            bracket_count += 1 if char == '{'
            bracket_count -= 1 if char == '}'
          end
          new_code_lines << "\t" + line
          old_lines << line
          break if bracket_count <= 0
        end
      
        new_code_lines.pop #pop of the last }; to add catch block before closing bracket in function

        console_line = "javascript_error({name: '#{name.gsub('\'', '"')}',  
                                    error_message: e.message, 
                                    error_name: e.name, 
                                    code_block: '#{old_lines.join('').gsub("\n", '[::n::]').gsub('\'', '')}', 
                                    arguments: [#{arguments.join(', ')}]
                        });"
      
        new_code_lines << "#{tabs}\t} catch (e) { \n#{tabs}\t\t#{console_line}\n#{tabs}\t}\n"
        new_code_lines << "#{tabs}});\n"
        functions << {:replace => old_lines.join(''), :new => new_code_lines.join('')}
      else
        scanning_data = function.post_match
      end
    else
      capture = false
    end
  end
  

  functions.each do |s|
    data_copy.gsub!(s[:replace], s[:new])
  end
  
  output = "try {"
  output << data_copy
  output << "} catch (e) { javascript_error({name: 'unfound javascript error', error_message: e.message, error_name: e.name, code_block: '#{scope.logical_path.to_s}', arguments: []}); }"
  output
end

#prepareObject



11
12
# File 'lib/jserror-rails/jserror_template.rb', line 11

def prepare
end