~tim-alwaysreformed/reformedchurcheslocator/rcl-backbone-test_for_directory_type

« back to all changes in this revision

Viewing changes to changes_listeners.js

  • Committer: Tim Black
  • Date: 2012-12-03 23:10:37 UTC
  • Revision ID: tim@alwaysreformed.com-20121203231037-z6n84gp99y9piuxh
Documented new attributes in model.Directory, develop code to get state page

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
// It monitors when requests are submitted to:
6
6
//      (when configuring a directory's settings) get a url in general
7
7
//      (when a directory is already configured) download all cong data for a directory
8
 
// TODO: Could we use jquery.couch.js here instead of cradle, in order to use our 
9
 
//      CouchAppObject model here?
 
8
// TODO: Could we use backbone-couch.js here instead of cradle, in order to use our 
 
9
//      Backbone model here?
10
10
 
11
11
var buffer = '',
12
12
        http = require('http'),
20
20
        var longjohn = require('./rcl/node_modules/longjohn')
21
21
 
22
22
//stdin.setEncoding('utf8');
 
23
        
 
24
// Declare utility functions
 
25
function get_url(doc, from_url, to_html, status_flag){
 
26
    http.get(doc[from_url], function(res){
 
27
        var pageData = ''
 
28
        res.on('data', function(chunk){
 
29
            pageData += chunk
 
30
        })
 
31
        res.on('end', function(){
 
32
            // TODO: Check to see if we got a 404 response
 
33
            // Write the contents of the html variable back to the database
 
34
            doc[to_html] = pageData
 
35
            doc[status_flag] = 'gotten'
 
36
            // TODO: Use Backbone here instead of cradle
 
37
            db.save(doc._id, doc._rev, doc, function(err, res){
 
38
                // TODO: Do anything more that needs to be done here
 
39
            });
 
40
        })
 
41
    });
 
42
}
 
43
function get_url_set(doc, from_urls, to_html, status_flag){
 
44
    for (var i=0; i<doc[from_urls].length; i++){
 
45
        doc[status_flag] = 'getting'
 
46
        http.get(doc[from_urls][i], function(res){
 
47
            var pageData = ''
 
48
            res.on('data', function(chunk){
 
49
                pageData += chunk
 
50
            })
 
51
            res.on('end', function(){
 
52
                // TODO: Check to see if we got a 404 response
 
53
                // Write the contents of the html variable back to the database
 
54
                if (!doc[to_html] || doc[to_html] == ''){
 
55
                    doc[to_html] = new Array() // declare as array
 
56
                }
 
57
                var temp_items = doc[to_html]
 
58
                var num_items = temp_items.push(pageData)
 
59
                doc[to_html] = temp_items
 
60
                // TODO: Start here
 
61
                if (num_items==doc[from_urls].length){
 
62
                    // We've gotten all the items' html!
 
63
                    doc[status_flag] = 'gotten'
 
64
                }
 
65
                // TODO: Use Backbone here instead of cradle
 
66
                db.save(doc._id, doc._rev, doc, function(err, res){
 
67
                    // TODO: Do anything more that needs to be done here
 
68
                });
 
69
            })
 
70
        });
 
71
    }
 
72
}
23
73
 
 
74
// Handle all changes
24
75
process.on('message', function(doc){
25
 
        // Handle all changes
26
76
 
27
77
        // Watch for requests to get the contents of a URL for a church directory
28
78
        // TODO: Check to see if the URL is valid
29
 
        if (doc.collection == 'directory' && doc.get_url_contents==true && doc.url){
 
79
        if (doc.collection == 'directory' && doc.get_url_html=='requested' && doc.url){
30
80
                // E.g., when a user enters "opc.org/locator.html" into the church directory configuration page,
31
81
                //      then go get the contents of that URL.
32
 
                http.get(doc.url, function(res){
33
 
                        var pageData = ''
34
 
                        res.on('data', function(chunk){
35
 
                                pageData += chunk
36
 
                        })
37
 
                        res.on('end', function(){
38
 
                                // TODO: Check to see if we got a 404 response
39
 
                                // Write the contents of the html variable back to the database
40
 
                                doc.url_html = pageData
41
 
                                doc.get_url_contents = false
42
 
                        // TODO: Use Backbone here instead of cradle
43
 
                                db.save(doc._id, doc._rev, doc, function(err, res){
44
 
                                        // TODO: Do anything more that needs to be done here
45
 
                                });
46
 
                        })
47
 
                });
 
82
        get_url(doc, 'url', 'url_html', 'get_url_html')
48
83
        }
 
84
        // Watch for requests to get the contents of a state page URL
 
85
    if (doc.collection == 'directory' && doc.get_state_url_html=='requested' && doc.state_url){
 
86
            get_url(doc, 'state_page_values', 'state_url_html', 'get_state_url_html')
 
87
    }
49
88
});