Completed
Push — develop ( 48fc97...2aa611 )
by Dylan
03:11
created

js/crawler_file_tester.js   A

Complexity

Total Complexity 10
Complexity/F 2

Size

Lines of Code 61
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
dl 0
loc 61
rs 10
c 1
b 0
f 0
cc 0
nc 1
mnd 4
bc 10
fnc 5
bpm 2
cpm 2
noi 3

4 Functions

Rating   Name   Duplication   Size   Complexity  
A crawler_file_tester.init 0 16 1
A crawler_file_tester.get_file_contents 0 3 1
A 0 3 1
B crawler_file_tester.parse_robots_file 0 22 6
1
const crawler_file_tester = {
2
3
    robot_rules: [],
4
5
    /**
6
     * Parse the content of the robots file
7
     *
8
     * @param {*} result
9
     * @throws {Exception}
10
     */
11
    parse_robots_file: function(result){
12
        var rules   = result.split("\n"),
13
            status  = crawler_painter.create_status('success', 'Robots file loaded');
14
        crawler_painter.add_row('robots_page',[ status, result.replace(/(?:\r\n|\r|\n)/g, '<br />') ]);
15
16
        var agent = '*';
17
        for(var r in rules){
18
            if( rules[r].length < 1 || rules[r].toLowerCase().indexOf('sitemap:') >= 0 ){
19
                continue;
1 ignored issue
show
Unused Code introduced by
This continue has no effect on the loop flow and can be removed.
Loading history...
20
            }else if( rules[r].toLowerCase().indexOf('user-agent:') >= 0 ){
21
                agent = rules[r].replace(/user-agent:/gi, '');
22
            }else if( rules[r].toLowerCase().indexOf('disallow:') >= 0 ){
23
                var rule = rules[r].replace(/disallow:/gi, '');
24
                crawler_file_tester.robot_rules.push({ 'rule': rule, 'agent': agent });
25
            }else{
26
                console.log(rules[r]);
1 ignored issue
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
27
                throw "Found a rule which we don't understand. Report it to the developer";
28
            }
29
        }
30
31
        console.log(crawler_file_tester.robot_rules);
32
    },
33
34
    get_file_contents: function(url, type, callback, failed_callback){
35
        $.ajax({ 'url' : url, 'dataType' : type }).done(callback).fail(failed_callback);
36
    },
37
38
    init: function(){
39
        // Robots
40
        crawler.regiser_test('robots_page', 'ROBOTS PAGE', ['Status', 'Content'], false);
41
        crawler.regiser_test('blocked_pages', 'BLOCKED PAGES', ['URL'], this.test_blocked_pages);
42
        crawler_file_tester.get_file_contents(
43
            crawler.robots_url,
44
            'text',
45
            crawler_file_tester.parse_robots_file,
46
            function(){
47
                var status = crawler_painter.create_status('error', 'Failed to load robots file');
48
                crawler_painter.add_row('robots_page', [ status ]);
49
            }
50
        );
51
52
        // Sitemap
53
    }
54
};
55
56
/**
57
 * Start up the file tester
58
 */
59
(function($){
0 ignored issues
show
Unused Code introduced by
The parameter $ is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
60
    crawler.on('BEFORE_INIT', crawler_file_tester.init);
61
}(jQuery));
62