Class: PusherFake::Server::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/pusher-fake/server/application.rb

Constant Summary collapse

CHANNEL_FILTER_ERROR =
"user_count may only be requested for presence channels - " +
"please supply filter_by_prefix begining with presence-".freeze
CHANNEL_USER_COUNT_ERROR =
"Cannot retrieve the user count unless the channel is a presence channel".freeze
PRESENCE_PREFIX_MATCHER =
/\Apresence-/.freeze

Class Method Summary collapse

Class Method Details

.call(environment) ⇒ Rack::Response

Process an API request.

Parameters:

  • environment (Hash)

    The request environment.

Returns:

  • (Rack::Response)

    A successful response.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pusher-fake/server/application.rb', line 15

def self.call(environment)
  id       = PusherFake.configuration.app_id
  request  = Rack::Request.new(environment)
  response = case request.path
             when %r{\A/apps/#{id}/events\Z}
               events(request)
             when %r{\A/apps/#{id}/channels\Z}
               channels(request)
             when %r{\A/apps/#{id}/channels/([^/]+)\Z}
               channel($1, request)
             when %r{\A/apps/#{id}/channels/([^/]+)/users\Z}
               users($1)
             else
               raise "Unknown path: #{request.path}"
             end

  Rack::Response.new(MultiJson.dump(response)).finish
rescue => error
  Rack::Response.new(error.message, 400).finish
end

.channel(name, request) ⇒ Hash

Return a hash of channel information.

Occupied status is always included. A user count may be requested for presence channels.

Parameters:

  • name (String)

    The channel name.

  • request (Rack::Request)

    The HTTP request.

Returns:

  • (Hash)

    A hash of channel information.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pusher-fake/server/application.rb', line 64

def self.channel(name, request)
  info = request.params["info"].to_s.split(",")

  if info.include?("user_count") && name !~ PRESENCE_PREFIX_MATCHER
    raise CHANNEL_USER_COUNT_ERROR
  end

  channels = PusherFake::Channel.channels || {}
  channel  = channels[name]

  {}.tap do |result|
    result[:occupied]   = !channel.nil? && channel.connections.length > 0
    result[:user_count] = channel.connections.length if channel && info.include?("user_count")
  end
end

.channels(request) ⇒ Hash

Returns a hash of occupied channels, optionally filtering with a prefix.

When filtering to presence chanenls, the user count maybe also be requested.

Parameters:

  • request (Rack::Request)

    The HTTP request.

Returns:

  • (Hash)

    A hash of occupied channels.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pusher-fake/server/application.rb', line 86

def self.channels(request)
  info   = request.params["info"].to_s.split(",")
  prefix = request.params["filter_by_prefix"].to_s

  if info.include?("user_count") && prefix !~ PRESENCE_PREFIX_MATCHER
    raise CHANNEL_FILTER_ERROR
  end

  filter   = Regexp.new(%r{\A#{prefix}})
  channels = PusherFake::Channel.channels || {}
  channels.inject(channels: {}) do |result, (name, channel)|
    unless filter && name !~ filter
      channels = result[:channels]
      channels[name] = {}
      channels[name][:user_count] = channel.connections.length if info.include?("user_count")
    end

    result
  end
end

.events(request) ⇒ Hash

Emit an event with data to the requested channel(s).

Parameters:

  • request (Rack::Request)

    The HTTP request.

Returns:

  • (Hash)

    An empty hash.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pusher-fake/server/application.rb', line 40

def self.events(request)
  event = MultiJson.load(request.body.read)
  data  = begin
            MultiJson.load(event["data"])
          rescue MultiJson::LoadError
            event["data"]
          end

  event["channels"].each do |channel_name|
    channel = Channel.factory(channel_name)
    channel.emit(event["name"], data, socket_id: event["socket_id"])
  end

  {}
end

.users(name) ⇒ Hash

Returns a hash of the IDs for the users in the channel.

Parameters:

  • name (String)

    The channel name.

Returns:

  • (Hash)

    A hash of user IDs.



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pusher-fake/server/application.rb', line 111

def self.users(name)
  channels = PusherFake::Channel.channels || {}
  channel  = channels[name]

  if channel
    users = channel.connections.map do |connection|
      { id: connection.id }
    end
  end

  { users: users || [] }
end