Class: K8::RackApplication

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

Instance Method Summary collapse

Constructor Details

#initialize(urlpath_mapping = [], urlpath_cache_size: 0) ⇒ RackApplication

Returns a new instance of RackApplication.



1431
1432
1433
1434
1435
1436
1437
1438
# File 'lib/keight.rb', line 1431

def initialize(urlpath_mapping=[], urlpath_cache_size: 0)
  @router = ActionRouter.new(urlpath_cache_size: urlpath_cache_size)
  init_default_param_patterns(@router.default_patterns)
  #; [!vkp65] mounts urlpath mappings if provided.
  urlpath_mapping.each do |urlpath, klass|
    @router.mount(urlpath, klass)
  end if urlpath_mapping
end

Instance Method Details

#call(env) ⇒ Object



1479
1480
1481
1482
1483
# File 'lib/keight.rb', line 1479

def call(env)
  #; [!uvmxe] takes env object.
  #; [!gpe4g] returns status, headers and content.
  return handle_request(REQUEST_CLASS.new(env), RESPONSE_CLASS.new)
end

#each_mapping(&block) ⇒ Object



1568
1569
1570
1571
1572
# File 'lib/keight.rb', line 1568

def each_mapping(&block)
  #; [!cgjyv] yields full urlpath pattern, action class and action methods.
  @router.each_mapping(&block)
  self
end

#find(req_path) ⇒ Object



1474
1475
1476
1477
# File 'lib/keight.rb', line 1474

def find(req_path)
  #; [!o0rnr] returns action class, action methods, urlpath names and values.
  return @router.find(req_path)
end

#mount(urlpath_pattern, action_class_or_array) ⇒ Object

ex:

mount '/',         WelcomeAction
mount '/books',    BooksAction
mount '/admin',    [
        ['/session',    AdminSessionAction],
        ['/books',      AdminBooksAction],
      ]


1468
1469
1470
1471
1472
# File 'lib/keight.rb', line 1468

def mount(urlpath_pattern, action_class_or_array)
  #; [!zwva6] mounts action class to urlpath pattern.
  @router.mount(urlpath_pattern, action_class_or_array)
  return self
end

#show_mappingsObject



1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
# File 'lib/keight.rb', line 1574

def show_mappings()
  #; [!u1g77] returns all mappings as YAML string.
  req_methods = HTTP_REQUEST_METHODS.values() + [:ANY]
  s = ""
  each_mapping do |full_urlpath_pat, action_class, action_methods|
    arr = req_methods.collect {|req_meth|
      action_method = action_methods[req_meth]
      action_method ? "#{req_meth}: #{action_method}" : nil
    }.compact()
    s << "- urlpath: #{full_urlpath_pat}\n"
    s << "  class:   #{action_class}\n"
    s << "  methods: {#{arr.join(', ')}}\n"
    s << "\n"
  end
  return s
end