Module: MyPusherModule

Defined in:
lib/lib/mypusher.rb

Overview

Pusherサービスへのアクセスを提供する app.pusherapp.com/apps/7449/api_access?welcome=true

@pusher_app_id = 'your pusher app id'
@pusher_key = 'pusher key'
@pusher_secret = 'pusher secret'
@config
~/config.ymlに
mypushermodule:
  app_id: xxx
  key: xxxxxxxxxxxxx
  secret: xxxxxxxxxx
  event: my_event
  channel: test_channel

使い方など class ThisDo

include MyPusherModule

して o = ThisDo.new o.push_pusher(‘test_app’,‘test’) とかでok

Instance Method Summary collapse

Instance Method Details

#get_puhser_htmlObject



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

def get_puhser_html
  return <<-"EOT"
<!DOCTYPE html>
<head>
<title>PusherTail</title>
<script src="http://js.pusherapp.com/1.8/pusher.min.js"></script>
<script>
  // Enable pusher logging - don't include this in production
  Pusher.log = function(message) {
    if (window.console && window.console.log) window.console.log(message);
  };

  // Flash fallback logging - don't include this in production
  WEB_SOCKET_DEBUG = true;
  var pusher = new Pusher('#{ @c['key']}');
  var channel = pusher.subscribe('#{ @c['channel']}');
  channel.bind('#{ @c['event']}', function(data) {
     hoge = document.getElementById("main").innerHTML;
     document.getElementById("main").innerHTML = data['#{ @c['app_name']}'] + "<hr>" + hoge;
  });
</script>
</head>
<body>
work at Chrome
<div id="main">&nbsp;</div>
</body>
EOT
end

#push_pusher(app_name = 'test_app', data = 'test') ⇒ Object

Pusherにデータをpushする

args
app_name : string アプリの名前
data     : string データ


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lib/mypusher.rb', line 43

def push_pusher(app_name='test_app',data='test')
	if @pusherconnected == nil
    set_my_pusher
    @pusherconnected = true
	end
	begin
 Pusher[@pusher_channel].trigger!(
      @pusher_event, { app_name => data})
	rescue Pusher::Error => e
 p e  
	end
end

#set_my_pusherObject

Pusherへの接続を設定する



28
29
30
31
32
33
34
35
36
37
# File 'lib/lib/mypusher.rb', line 28

def set_my_pusher
  @c = MyConfig.get['mypushermodule']
	require "pusher"
	Pusher.app_id = @c['app_id']
	Pusher.key = @c['key']
	Pusher.secret = @c['secret']
  @pusher_event = @c['event']
  @pusher_channel = @c['channel']
  p @c
end