Class: Skypager::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/skypager/router.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Router

Returns a new instance of Router.



8
9
10
11
# File 'lib/skypager/router.rb', line 8

def initialize(app, options={})
  @app = app
  @options = options
end

Class Attribute Details

.proxiesObject

Returns the value of attribute proxies.



14
15
16
# File 'lib/skypager/router.rb', line 14

def proxies
  @proxies
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



6
7
8
# File 'lib/skypager/router.rb', line 6

def app
  @app
end

Class Method Details

.add(subdomain, options = {}) ⇒ Object



25
26
27
28
29
# File 'lib/skypager/router.rb', line 25

def self.add(subdomain, options={})
  subdomain = subdomain.to_s
  self.proxies ||= {}
  self.proxies[subdomain] = Skypager.proxy(options)
end

.authenticator(&block) ⇒ Object



35
36
37
38
# File 'lib/skypager/router.rb', line 35

def self.authenticator &block
  @authenticator = block if block_given?
  @authenticator
end

Instance Method Details

#authenticatorObject



40
41
42
# File 'lib/skypager/router.rb', line 40

def authenticator
  self.class.authenticator
end

#call(env) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/skypager/router.rb', line 44

def call(env)
  request = Rack::Request.new(env)
  subdomain = request.host.split('.').first

  handler = proxies.fetch(subdomain) do |key|
    app
  end

  if authenticator.respond_to?(:call)
    if !!authenticator.call(env, subdomain)
      return [401, {}, [""]]
    end
  end

  handler.call(env)
end

#inspectObject



21
22
23
# File 'lib/skypager/router.rb', line 21

def inspect
  to_s
end

#proxiesObject



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

def proxies
  self.class.proxies
end

#to_sObject



17
18
19
# File 'lib/skypager/router.rb', line 17

def to_s
  "Skypager Router. Serving #{ proxies.keys.join(", ") }"
end