Class: Tynn

Inherits:
Object
  • Object
show all
Includes:
Syro::Deck::API
Defined in:
lib/tynn/version.rb,
lib/tynn.rb,
lib/tynn/hsts.rb,
lib/tynn/json.rb,
lib/tynn/test.rb,
lib/tynn/hmote.rb,
lib/tynn/render.rb,
lib/tynn/static.rb,
lib/tynn/request.rb,
lib/tynn/session.rb,
lib/tynn/matchers.rb,
lib/tynn/response.rb,
lib/tynn/settings.rb,
lib/tynn/force_ssl.rb,
lib/tynn/not_found.rb,
lib/tynn/protection.rb,
lib/tynn/all_methods.rb,
lib/tynn/environment.rb,
lib/tynn/secure_headers.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: AllMethods, Environment, ForceSSL, HMote, HSTS, JSON, Matchers, NotFound, Protection, Render, SecureHeaders, Session, Settings, Static Classes: Request, Response, Test

Constant Summary collapse

VERSION =
[
  MAJOR_VERSION = 1,
  MINOR_VERSION = 2,
  PATCH_VERSION = 0,
  PRE_VERSION   = nil
].compact.join(".")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__middlewareObject

:nodoc:



87
88
89
# File 'lib/tynn.rb', line 87

def self.__middleware # :nodoc:
  return @middleware ||= []
end

.build_app(syro) ⇒ Object

:nodoc:



79
80
81
82
83
84
85
# File 'lib/tynn.rb', line 79

def self.build_app(syro) # :nodoc:
  if __middleware.empty?
    @app = syro
  else
    @app = __middleware.reverse.inject(syro) { |a, m| m.call(a) }
  end
end

.call(env) ⇒ Object

:nodoc:



75
76
77
# File 'lib/tynn.rb', line 75

def self.call(env) # :nodoc:
  return @app.call(env)
end

.define(&block) ⇒ Object

Public: Sets the application handler.

Examples

class Users < Tynn
end

Users.define do
  on(:id) do |id|
    get do
      res.write("GET /users")
    end

    post do
      res.write("POST /users")
    end
  end
end


57
58
59
# File 'lib/tynn.rb', line 57

def self.define(&block)
  build_app(Syro.new(self, &block))
end

.plugin(plugin, *args, &block) ⇒ Object

Public: Loads given plugin into the application.

Examples

require "tynn"
require "tynn/protection"
require "tynn/session"

Tynn.plugin(Tynn::Protection)
Tynn.plugin(Tynn::Session, secret: "__a_random_secret_key")


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tynn.rb', line 21

def self.plugin(plugin, *args, &block)
  if defined?(plugin::InstanceMethods)
    self.include(plugin::InstanceMethods)
  end

  if defined?(plugin::ClassMethods)
    self.extend(plugin::ClassMethods)
  end

  if plugin.respond_to?(:setup)
    plugin.setup(self, *args, &block)
  end
end

.reset!Object

:nodoc:



91
92
93
94
# File 'lib/tynn.rb', line 91

def self.reset! # :nodoc:
  @app = nil
  @middleware = []
end

.set(option, value) ⇒ Object

Public: Sets an option to the given value.

Examples

require "tynn"
require "tynn/environment"

Tynn.plugin(Tynn::Environment)

Tynn.set(:environment, :staging)
Tynn.environment
# => :staging


109
110
111
# File 'lib/tynn.rb', line 109

def self.set(option, value)
  settings[option] = value
end

.use(middleware, *args, &block) ⇒ Object

Public: Adds given Rack middleware to the stack.

Examples

require "rack/common_logger"
require "rack/show_exceptions"

Tynn.use(Rack::CommonLogger)
Tynn.use(Rack::ShowExceptions)


71
72
73
# File 'lib/tynn.rb', line 71

def self.use(middleware, *args, &block)
  __middleware << proc { |app| middleware.new(app, *args, &block) }
end

Instance Method Details

#default_headersObject

:nodoc:



115
116
117
# File 'lib/tynn.rb', line 115

def default_headers # :nodoc:
  return Hash[self.class.settings[:default_headers]]
end

#request_classObject

:nodoc:



119
120
121
# File 'lib/tynn.rb', line 119

def request_class # :nodoc:
  return Tynn::Request
end

#response_classObject

:nodoc:



123
124
125
# File 'lib/tynn.rb', line 123

def response_class # :nodoc:
  return Tynn::Response
end