Class: Tynn

Inherits:
Object
  • Object
show all
Includes:
Syro::Deck::API
Defined in:
lib/tynn/version.rb,
lib/tynn.rb,
lib/tynn/ssl.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/response.rb,
lib/tynn/settings.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, HMote, JSON, NotFound, Protection, Render, SecureHeaders, Session, Settings, Static Classes: Request, Response, SSL, Test

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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

.middlewareObject

Internal: Returns middleware stack.



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

def self.middleware
  return @middleware ||= []
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:



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

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


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

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)
  self.middleware << proc { |app| middleware.new(app, *args, &block) }
end

Instance Method Details

#default_headersObject

:nodoc:



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

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

#request_classObject

:nodoc:



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

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

#response_classObject

:nodoc:



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

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