Class: MongoPubNubPipe::Connect

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

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Connect

Returns a new instance of Connect.



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
# File 'lib/mongopipe.rb', line 33

def initialize(args={})
    @publish_key   = args[:publish_key]   or 'demo'
    @subscribe_key = args[:subscribe_key] or 'demo'
    @database      = args[:db]            or 'test'
    @collection    = args[:collection]    or 'cap_col'
    @server_port   = args[:port]          or 27017
    @server_ip     = args[:ip]            or '0.0.0.0'
    @callback      = args[:callback]      or lambda {}
    @channel       = "#{@database}.#{@collection}"
    @pubnub        = Pubnub.new(
        :publish_key   => @publish_key,
        :subscribe_key => @subscribe_key
    )

    if args[:puts_usage]
        puts " "
        puts " ------------------------------------------------------"
        puts " Step 1:"
        puts " -------"
        puts " Open Your Browser to Show PubNub Pipe Console"
        puts " ------------------------------------------------------"
        puts " "
        puts " > open http://www.pubnub.com/console?channel=#{@channel}"
        puts " "
        puts " ------------------------------------------------------"
        puts " Step 2:"
        puts " -------"
        puts " Open Demo Map on Your Phone"
        puts " ------------------------------------------------------"
        puts " "
        puts " > open http://goo.gl/HAqAv##{@channel}"
        puts " "
        puts " ------------------------------------------------------"
        puts " Step 3:"
        puts " -------"
        puts " Insert Test Data"
        puts " ------------------------------------------------------"
        puts " "
        puts " > ./mongo"
        puts " "
        puts " > use #{@database}"
        puts " > db.#{@collection}.insert({ latlon : [ 1.5, 2.0 ] })"
        puts " "
    end

    ## ---------------
    ## Easter Egg
    ## ---------------
    '''
    for (var i=0;i<10;i++) {
        db.cap_collection.insert({
            latlon : [ Math.random()*40.0, Math.random()*-122.0 ]
        })
    }
    '''
    self.connect()
end

Instance Method Details

#connectObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mongopipe.rb', line 91

def connect()
    @connection = Mongo::Connection.new( @server_ip, @server_port )
    @db         = @connection[@database]
    @table      = @db[@collection]
    @cursor     = Mongo::Cursor.new(
        @table,
        :timeout  => false,
        :tailable => true
    )

    if not @db.collection_names.include?(@collection)
        @db.create_collection( @collection, {
            :capped => true,
            :max    => 1024,
            :size   => 1024
        } )
        @table = @db[@collection]
    end
end

#pipeObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mongopipe.rb', line 111

def pipe()
    if not @started then
        while doc = @cursor.next do end
        @started = true
    end
    while true do
        begin
            doc = @cursor.next
            if @cursor.closed?; self.connect(); next end
            if doc == nil; sleep(1); next end
        rescue
            self.connect()
            self.pipe()
            return
        end

        @callback.call(doc)
        @pubnub.publish(
            :channel  => @channel,
            :message  => doc,
            :callback => lambda {|x|}
        )
    end
end