mongopipe 1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/mongopipe.rb +136 -0
  2. metadata +78 -0
@@ -0,0 +1,136 @@
1
+ ## ----------------------------------------------------------------------------------
2
+ ## USAGE:
3
+ ## ----------------------------------------------------------------------------------
4
+ ## ruby ./pubnub-mongodb-ruby-pipe.rb
5
+ ## ----------------------------------------------------------------------------------
6
+ ##
7
+ ## See bottom of file for usage coding usage.
8
+ ##
9
+ ## ----------------------------------------------------------------------------------
10
+
11
+ ## ----------------------------------------------------------------------------------
12
+ ## Example Usage
13
+ ## ----------------------------------------------------------------------------------
14
+ # MongoPubNubPipe::Connect.new(
15
+ # :puts_usage => true,
16
+ # :publish_key => 'demo',
17
+ # :subscribe_key => 'demo',
18
+ # :db => 'test',
19
+ # :collection => 'cap_collection',
20
+ # :callback => lambda{ |doc|
21
+ # ## Optional Callback Called on Doc Insert
22
+ # ## Remove :callback if you don't need it.
23
+ # puts(doc)
24
+ # }
25
+ # ).pipe()
26
+
27
+ require 'rubygems'
28
+ require 'pubnub'
29
+ require 'mongo'
30
+
31
+ module MongoPubNubPipe
32
+ class Connect
33
+ def initialize(args={})
34
+ @publish_key = args[:publish_key] or 'demo'
35
+ @subscribe_key = args[:subscribe_key] or 'demo'
36
+ @database = args[:db] or 'test'
37
+ @collection = args[:collection] or 'cap_col'
38
+ @server_port = args[:port] or 27017
39
+ @server_ip = args[:ip] or '0.0.0.0'
40
+ @callback = args[:callback] or lambda {}
41
+ @channel = "#{@database}.#{@collection}"
42
+ @pubnub = Pubnub.new(
43
+ :publish_key => @publish_key,
44
+ :subscribe_key => @subscribe_key
45
+ )
46
+
47
+ if args[:puts_usage]
48
+ puts " "
49
+ puts " ------------------------------------------------------"
50
+ puts " Step 1:"
51
+ puts " -------"
52
+ puts " Open Your Browser to Show PubNub Pipe Console"
53
+ puts " ------------------------------------------------------"
54
+ puts " "
55
+ puts " > open http://www.pubnub.com/console?channel=#{@channel}"
56
+ puts " "
57
+ puts " ------------------------------------------------------"
58
+ puts " Step 2:"
59
+ puts " -------"
60
+ puts " Open Demo Map on Your Phone"
61
+ puts " ------------------------------------------------------"
62
+ puts " "
63
+ puts " > open http://goo.gl/HAqAv##{@channel}"
64
+ puts " "
65
+ puts " ------------------------------------------------------"
66
+ puts " Step 3:"
67
+ puts " -------"
68
+ puts " Insert Test Data"
69
+ puts " ------------------------------------------------------"
70
+ puts " "
71
+ puts " > ./mongo"
72
+ puts " "
73
+ puts " > use #{@database}"
74
+ puts " > db.#{@collection}.insert({ latlon : [ 1.5, 2.0 ] })"
75
+ puts " "
76
+ end
77
+
78
+ ## ---------------
79
+ ## Easter Egg
80
+ ## ---------------
81
+ '''
82
+ for (var i=0;i<10;i++) {
83
+ db.cap_collection.insert({
84
+ latlon : [ Math.random()*40.0, Math.random()*-122.0 ]
85
+ })
86
+ }
87
+ '''
88
+ self.connect()
89
+ end
90
+
91
+ def connect()
92
+ @connection = Mongo::Connection.new( @server_ip, @server_port )
93
+ @db = @connection[@database]
94
+ @table = @db[@collection]
95
+ @cursor = Mongo::Cursor.new(
96
+ @table,
97
+ :timeout => false,
98
+ :tailable => true
99
+ )
100
+
101
+ if not @db.collection_names.include?(@collection)
102
+ @db.create_collection( @collection, {
103
+ :capped => true,
104
+ :max => 1024,
105
+ :size => 1024
106
+ } )
107
+ @table = @db[@collection]
108
+ end
109
+ end
110
+
111
+ def pipe()
112
+ if not @started then
113
+ while doc = @cursor.next do end
114
+ @started = true
115
+ end
116
+ while true do
117
+ begin
118
+ doc = @cursor.next
119
+ if @cursor.closed?; self.connect(); next end
120
+ if doc == nil; sleep(1); next end
121
+ rescue
122
+ self.connect()
123
+ self.pipe()
124
+ return
125
+ end
126
+
127
+ @callback.call(doc)
128
+ @pubnub.publish(
129
+ :channel => @channel,
130
+ :message => doc,
131
+ :callback => lambda {|x|}
132
+ )
133
+ end
134
+ end
135
+ end
136
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongopipe
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.3'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - PubNub
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pubnub
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mongo
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: MongoDB Pipe powered by PubNub.
47
+ email: help@pubnub.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - mongopipe.rb
53
+ homepage: https://github.com/stephenlb/pubnub-mongo-pipe
54
+ licenses:
55
+ - MIT
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.24
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Pipe MongoDB Document Data directly to your Mobile App.
78
+ test_files: []