Class: PusherFake::Server::Application

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

Overview

The fake web application.

Constant Summary collapse

CHANNEL_FILTER_ERROR =
"user_count may only be requested for presence " \
"channels - please supply filter_by_prefix " \
"begining with presence-"
CHANNEL_USER_COUNT_ERROR =
"Cannot retrieve the user count unless the " \
"channel is a presence channel"
REQUEST_PATHS =
{
  %r{\A/apps/:id/batch_events\z}           => :batch_events,
  %r{\A/apps/:id/events\z}                 => :events,
  %r{\A/apps/:id/channels\z}               => :channels,
  %r{\A/apps/:id/channels/([^/]+)\z}       => :channel,
  %r{\A/apps/:id/channels/([^/]+)/users\z} => :users
}.freeze

Class Method Summary collapse

Class Method Details

.batch_events(request) ⇒ Hash

Emit batch events with data to the requested channel(s).

Parameters:

  • request (Rack::Request)

    The HTTP request.

Returns:

  • (Hash)

    An empty hash.


39
40
41
42
43
44
45
46
# File 'lib/pusher-fake/server/application.rb', line 39

def self.batch_events(request)
  batch = MultiJson.load(request.body.read)["batch"]
  batch.each do |event|
    send_event(event)
  end

  {}
end

.call(environment) ⇒ Rack::Response

Process an API request.

Parameters:

  • environment (Hash)

    The request environment.

Returns:

  • (Rack::Response)

    A successful response.


26
27
28
29
30
31
32
33
# File 'lib/pusher-fake/server/application.rb', line 26

def self.call(environment)
  request  = Rack::Request.new(environment)
  response = response_for(request)

  Rack::Response.new(MultiJson.dump(response)).finish
rescue StandardError => 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.

Raises:


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

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

  raise CHANNEL_USER_COUNT_ERROR if invalid_channel_to_count?(name, count)

  channel     = Channel.channels[name]
  connections = channel ? channel.connections : []

  result = { occupied: connections.any? }
  result[:user_count] = connections.size if count
  result
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.

rubocop:disable Metrics/AbcSize

Parameters:

  • request (Rack::Request)

    The HTTP request.

Returns:

  • (Hash)

    A hash of occupied channels.

Raises:


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

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

  raise CHANNEL_FILTER_ERROR if invalid_channel_to_count?(prefix, count)

  PusherFake::Channel
    .channels
    .each_with_object(channels: {}) do |(name, channel), result|
      next unless name.start_with?(prefix)

      channels = result[:channels].merge!(name => {})
      channels[name][:user_count] = channel.connections.size if count
    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.


52
53
54
55
56
57
58
# File 'lib/pusher-fake/server/application.rb', line 52

def self.events(request)
  event = MultiJson.load(request.body.read)

  send_event(event)

  {}
end

.response_for(request) ⇒ Hash

Attempt to provide a response for the provided request.

Parameters:

  • request (Rack::Request)

    The HTTP request.

Returns:

  • (Hash)

    A response hash.


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

def self.response_for(request)
  id = PusherFake.configuration.app_id

  REQUEST_PATHS.each do |path, method|
    matcher = Regexp.new(path.to_s.sub(":id", id))
    matches = matcher.match(request.path)

    next if matches.nil?

    arguments = [matches[1], request].compact

    return public_send(method, *arguments)
  end

  raise "Unknown path: #{request.path}"
end

.users(name, _request = nil) ⇒ 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.


131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/pusher-fake/server/application.rb', line 131

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

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

  { users: users || [] }
end