Class: Pinkman::Broadcaster
- Inherits:
-
Object
- Object
- Pinkman::Broadcaster
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/pinkman/broadcaster.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
-
#record ⇒ Object
Returns the value of attribute record.
-
#scope ⇒ Object
Returns the value of attribute scope.
Class Method Summary collapse
- .broadcast(room, scope, action, record) ⇒ Object
- .broadcasters ⇒ Object
- .room_name_from_params(broadcaster, params) ⇒ Object
- .room_name_from_record(room_name, record) ⇒ Object
- .stream(channel, current_allowed_scopes, params) ⇒ Object
Instance Method Summary collapse
- #encrypt(room_name, passphrase) ⇒ Object
- #encrypt_room_name(record) ⇒ Object
- #filter_by(*args) ⇒ Object
- #get_record(params) ⇒ Object
-
#initialize(options) ⇒ Broadcaster
constructor
A new instance of Broadcaster.
- #md5(val) ⇒ Object
- #room(room_name = nil) ⇒ Object
- #room_name(record = nil) ⇒ Object
- #save ⇒ Object
- #set_callbacks ⇒ Object
Constructor Details
#initialize(options) ⇒ Broadcaster
Returns a new instance of Broadcaster.
11 12 13 14 15 |
# File 'lib/pinkman/broadcaster.rb', line 11 def initialize self.scope = [:scope] self.model = [:model] true end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
7 8 9 |
# File 'lib/pinkman/broadcaster.rb', line 7 def model @model end |
#record ⇒ Object
Returns the value of attribute record.
7 8 9 |
# File 'lib/pinkman/broadcaster.rb', line 7 def record @record end |
#scope ⇒ Object
Returns the value of attribute scope.
7 8 9 |
# File 'lib/pinkman/broadcaster.rb', line 7 def scope @scope end |
Class Method Details
.broadcast(room, scope, action, record) ⇒ Object
21 22 23 |
# File 'lib/pinkman/broadcaster.rb', line 21 def self.broadcast room, scope, action, record ActionCable.server.broadcast(room_name_from_record(room,record), {action: action, data: record.json_hash(scope)}) end |
.broadcasters ⇒ Object
17 18 19 |
# File 'lib/pinkman/broadcaster.rb', line 17 def self.broadcasters @broadcasters ||= {} end |
.room_name_from_params(broadcaster, params) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/pinkman/broadcaster.rb', line 39 def self.room_name_from_params broadcaster, params if params[:filter_by].is_a?(String) or params[:filter_by].is_a?(Numeric) broadcaster.encrypt(broadcaster.room,params[:filter_by].to_s) elsif params[:filter_by].is_a? Array broadcaster.encrypt(broadcaster.room, params[:filter_by].map(&:to_s).join(' ')) else broadcaster.room end end |
.room_name_from_record(room_name, record) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/pinkman/broadcaster.rb', line 49 def self.room_name_from_record room_name, record broadcaster = broadcasters[room_name.to_sym] if broadcaster broadcaster.room_name(record) else raise "#{room_name} not found/declared" end end |
.stream(channel, current_allowed_scopes, params) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pinkman/broadcaster.rb', line 25 def self.stream channel, current_allowed_scopes, params if params[:room].present? and params[:scope].present? broadcaster = broadcasters[params[:room].to_sym] raise 'Unknown scope' unless params[:scope].is_a?(String) if broadcaster.present? and params[:scope].to_sym.in?(current_allowed_scopes) channel.stream_from(room_name_from_params(broadcaster,params)) else raise "Insuficient permissions or room '#{params[:room]}' not found." end else raise 'Room or Scope not specified through client.' end end |
Instance Method Details
#encrypt(room_name, passphrase) ⇒ Object
91 92 93 |
# File 'lib/pinkman/broadcaster.rb', line 91 def encrypt room_name, passphrase md5(room_name.to_s + 'pinkroom' + passphrase) end |
#encrypt_room_name(record) ⇒ Object
86 87 88 89 |
# File 'lib/pinkman/broadcaster.rb', line 86 def encrypt_room_name(record) passphrase = filter_by.map { |attribute| record.send(attribute) }.join(' ') encrypt(room,passphrase) end |
#filter_by(*args) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/pinkman/broadcaster.rb', line 66 def filter_by *args if args.any? @filter_by = args else @filter_by ||= [] end end |
#get_record(params) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/pinkman/broadcaster.rb', line 78 def get_record params if filter_by.any? and params[:filter_by].present? where_hash = {} filter_by.each_with_index {|attribute,i| where_hash[attribute] = params[:filter_by][i] } model.where(where_hash).first if model.where(where_hash).any? end end |
#md5(val) ⇒ Object
99 100 101 |
# File 'lib/pinkman/broadcaster.rb', line 99 def md5 val Digest::MD5.hexdigest(val) end |
#room(room_name = nil) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/pinkman/broadcaster.rb', line 58 def room room_name=nil if room_name.present? @room = room_name else @room end end |
#room_name(record = nil) ⇒ Object
95 96 97 |
# File 'lib/pinkman/broadcaster.rb', line 95 def room_name(record=nil) (record.present? and filter_by.any?) ? encrypt_room_name(record) : room end |
#save ⇒ Object
74 75 76 |
# File 'lib/pinkman/broadcaster.rb', line 74 def save valid? and (self.class.broadcasters[room.to_sym] = self) and set_callbacks end |
#set_callbacks ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/pinkman/broadcaster.rb', line 103 def set_callbacks if valid? ['create','update','destroy'].each do |action| current_room = room current_scope = scope method_name = "broadcast_#{action}_to_#{room}".to_sym unless model.instance_methods.include?(method_name) model.define_method method_name do Pinkman::Broadcaster.broadcast(current_room, current_scope, action, self) end model.send("after_#{action}".to_sym, method_name) end end true end end |