Class: Itsi::Server::Config::Location

Inherits:
Middleware
  • Object
show all
Defined in:
lib/itsi/server/config/middleware/location.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigHelpers

included, load_and_register, #normalize_keys!

Constructor Details

#initialize(location, *routes, methods: [], protocols: [], schemes: [], hosts: [], ports: [], extensions: [], content_types: [], accepts: [], &block) ⇒ Location

Returns a new instance of Location.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/itsi/server/config/middleware/location.rb', line 32

def initialize(location,
  *routes,
  methods: [],
  protocols: [],
  schemes: [],
  hosts: [],
  ports: [],
  extensions: [],
  content_types: [],
  accepts: [],
  &block
)

  @location = location
  params = self.schema.new({
    routes: routes,
    methods: methods,
    protocols: protocols,
    schemes: schemes,
    hosts: hosts,
    ports: ports,
    extensions: extensions,
    content_types: content_types,
    accepts: accepts,
    block: block
  }).to_h
  @routes = params[:routes].empty? ? ["*"] : params[:routes]
  @methods = params[:methods].map { |s| s.is_a?(Regexp) ? s : s.to_s }
  @protocols = (params[:protocols] | params[:schemes]).map { |s| s.is_a?(Regexp) ? s : s.to_s }
  @hosts = params[:hosts].map { |s| s.is_a?(Regexp) ? s : s.to_s }
  @ports = params[:ports].map { |s| s.is_a?(Regexp) ? s : s.to_s }
  @extensions = params[:extensions].map { |s| s.is_a?(Regexp) ? s : s.to_s }
  @content_types = params[:content_types].map { |s| s.is_a?(Regexp) ? s : s.to_s }
  @accepts = params[:accepts].map { |s| s.is_a?(Regexp) ? s : s.to_s }
  @block = block
end

Instance Attribute Details

#acceptsObject

Returns the value of attribute accepts.



29
30
31
# File 'lib/itsi/server/config/middleware/location.rb', line 29

def accepts
  @accepts
end

#blockObject

Returns the value of attribute block.



29
30
31
# File 'lib/itsi/server/config/middleware/location.rb', line 29

def block
  @block
end

#content_typesObject

Returns the value of attribute content_types.



29
30
31
# File 'lib/itsi/server/config/middleware/location.rb', line 29

def content_types
  @content_types
end

#extensionsObject

Returns the value of attribute extensions.



29
30
31
# File 'lib/itsi/server/config/middleware/location.rb', line 29

def extensions
  @extensions
end

#hostsObject

Returns the value of attribute hosts.



29
30
31
# File 'lib/itsi/server/config/middleware/location.rb', line 29

def hosts
  @hosts
end

#locationObject

Returns the value of attribute location.



29
30
31
# File 'lib/itsi/server/config/middleware/location.rb', line 29

def location
  @location
end

#portsObject

Returns the value of attribute ports.



29
30
31
# File 'lib/itsi/server/config/middleware/location.rb', line 29

def ports
  @ports
end

#protocolsObject

Returns the value of attribute protocols.



29
30
31
# File 'lib/itsi/server/config/middleware/location.rb', line 29

def protocols
  @protocols
end

#routesObject

Returns the value of attribute routes.



29
30
31
# File 'lib/itsi/server/config/middleware/location.rb', line 29

def routes
  @routes
end

Instance Method Details

#build!Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/itsi/server/config/middleware/location.rb', line 79

def build!
  build_child = lambda {
    child = DSL.new(
      location,
      routes: routes,
      methods: intersect(http_methods, location.http_methods),
      protocols: intersect(protocols, location.protocols),
      hosts: intersect(hosts, location.hosts),
      ports: intersect(ports, location.ports),
      extensions: intersect(extensions, location.extensions),
      content_types: intersect(content_types, location.content_types),
      accepts: intersect(accepts, location.accepts),
      controller: location.controller,
      &block
    )
    child.options[:nested_locations].each(&:call)
    location.children << child
  }
  location.options[:nested_locations] << build_child
end

#http_methodsObject



69
70
71
# File 'lib/itsi/server/config/middleware/location.rb', line 69

def http_methods
  @methods
end

#intersect(a, b) ⇒ Object



73
74
75
76
77
# File 'lib/itsi/server/config/middleware/location.rb', line 73

def intersect(a, b)
  return b if a.empty?
  return a if b.empty?
  a & b
end