Module: PushRoutes

Defined in:
lib/push_routes.rb,
lib/push_routes/engine.rb,
lib/push_routes/helpers.rb,
lib/push_routes/version.rb,
lib/push_routes/push_route_url.rb,
app/helpers/push_routes/application_helper.rb

Overview

TODO: setup the add_trigger method to take a block and do the rails magic which will take functions defined with regular names and attach them That rails magic might not be possible…

Defined Under Namespace

Modules: ApplicationHelper, Helpers Classes: Engine, PushRouteUrl

Constant Summary collapse

VERSION =
"0.0.8"
@@redis =
nil
@@controllers =
Set.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.NODE_URLObject

Returns the value of attribute NODE_URL.



16
17
18
# File 'lib/push_routes.rb', line 16

def NODE_URL
  @NODE_URL
end

Class Method Details

.active_routesObject



86
87
88
# File 'lib/push_routes.rb', line 86

def self.active_routes
  PushRoutes.controllers.flat_map {|c| c.push_routes.map {|k,v| v.source}}
end

.class_prefixes(string) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/push_routes.rb', line 56

def self.class_prefixes(string)
  res = []
  pos = 0
  while(string.index("::",pos))
    pos = string.index("::",pos)
    res << string[0..pos-1]
    pos += 2
  end
  res << string[0..-1]
end

.controllersObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/push_routes.rb', line 67

def self.controllers
  Dir[Rails.root.join('app','controllers','**','*.rb')].map do |file|
    #Note: This may break if the directory structure has /app/controllers multiple times.
    #We want to match everything after the first app/controllers after the project root
    Object.const_get(/^.*?\/app\/controllers\/(.*?).rb$/.match(file)[1].camelize)
  end
  # Dir[Rails.root.join('app','controllers','**','*.rb')].each do |file|
  #   #Note: This may break if the directory structure has /app/controllers multiple times.
  #   #We want to match everything after the first app/controllers after the project root

  #   controller_name = /^.*?\/app\/controllers\/(.*?).rb$/.match(file)[1].camelize
  #   class_prefixes(controller_name)[0..-2].each do |name|
  #     Object.const_get(name)
  #   end
  # end
end

.newGetRoutesObject



21
22
23
24
25
26
27
28
29
# File 'lib/push_routes.rb', line 21

def self.newGetRoutes
  hash = {}
  PushRoutes.controllers.each do |c|
    c.push_routes.each do |k,v|
      hash[v.source] = c.controller_path+"#"+k.to_s if v.isNew
    end
  end
  return hash
end

.redisObject



52
53
54
# File 'lib/push_routes.rb', line 52

def self.redis
  @@redis
end

.REDIS_URLObject



31
32
33
# File 'lib/push_routes.rb', line 31

def self.REDIS_URL
  @@redis.client.options[:url] if @@redis
end

.REDIS_URL=(x) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/push_routes.rb', line 44

def self.REDIS_URL= (x)
  begin
    @@redis = Redis.new(:url => x);
  rescue
    warn "Failed to connect to redis server. Push Routes will not work"
  end
end

.trigger(route, type: '', data: 'placeholder') ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/push_routes.rb', line 35

def self.trigger(route, type: '', data: 'placeholder')
  return warn "PushRoutes: Redis not configured properly" if PushRoutes.redis.nil?
  begin
    PushRoutes.redis.publish 'rt-change', {channel: route, type: type, data: data}.to_json
  rescue Redis::CannotConnectError => e
    warn "PushRoutes cannot reach the redis server"
  end
end