Class: Oxy::RSVP

Inherits:
Object
  • Object
show all
Defined in:
lib/oxy/middleware/rsvp.rb

Overview

RSVP middleware to transform ‘RSVP’ requests into corresponding contacts in your Google Contacts via Google People API.

Constant Summary collapse

ELIGIBLE_FORMS_FIELDS =

The set of allowed fields. Requests that do not have fields present in this list will not be eligible to be enqueued.

['email_address', 'first_name', 'last_name']
EMAIL_REGEX_VALIDATOR =

Email validation regex

/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
ALLOWED_STRING_LENGTH =

First and last name allowed string length

60

Instance Method Summary collapse

Constructor Details

#initialize(app, logger = $stderr) ⇒ RSVP

ctor.



15
16
17
18
19
20
# File 'lib/oxy/middleware/rsvp.rb', line 15

def initialize(app, logger = $stderr)
  @app = app
  @logger = logger
  # middleware to deflect abusive clients, 10/90 rps
  @deflect = Rack::Deflect.new(app, :log => logger, :request_threshold => 10, :interval => 90, :log_format => "[Rack::Deflect]: (%s) ~> %s")
end

Instance Method Details

#call(env) ⇒ Object

Middleware’s entry point



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/oxy/middleware/rsvp.rb', line 23

def call(env)
  # instantiate the request object
  req = Rack::Request.new(env)
  if req.path == "/rsvp" && req.post?
    # first ask deflect layer for permission to process the request
    resp = @deflect.call(env)
    return resp if forbidden(resp)
    # next ask validation layer for permission to process the request
    # ...
    # enqueue background processing for valid submissions only
    Threaded.enqueue(Subscribe, req.POST, @logger) if valid_form(req.POST)
    # redirect anyways
    [302, { "Location" => "/thank-you" }, []]
  else
    @app.call(env)
  end
end