Module: MyPusherModule

Defined in:
lib/lib/mypusher.rb

Overview

Pusherサービスへのアクセスを提供する https://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 "<!DOCTYPE html>\n<head>\n<title>PusherTail</title>\n<script src=\"http://js.pusherapp.com/1.8/pusher.min.js\"></script>\n<script>\n  // Enable pusher logging - don't include this in production\n  Pusher.log = function(message) {\n    if (window.console && window.console.log) window.console.log(message);\n  };\n\n  // Flash fallback logging - don't include this in production\n  WEB_SOCKET_DEBUG = true;\n  var pusher = new Pusher('\#{ @c['key']}');\n  var channel = pusher.subscribe('\#{ @c['channel']}');\n  channel.bind('\#{ @c['event']}', function(data) {\n     hoge = document.getElementById(\"main\").innerHTML;\n     document.getElementById(\"main\").innerHTML = data['\#{ @c['app_name']}'] + \"<hr>\" + hoge;\n  });\n</script>\n</head>\n<body>\nwork at Chrome\n<div id=\"main\">&nbsp;</div>\n</body>\n"
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