tynn 1.4.0 → 2.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,127 +1,74 @@
1
- require "syro"
2
- require_relative "tynn/request"
3
- require_relative "tynn/response"
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tynn/base"
4
+ require_relative "tynn/default_headers"
4
5
  require_relative "tynn/settings"
5
6
  require_relative "tynn/version"
6
7
 
7
8
  class Tynn
8
- include Syro::Deck::API
9
-
10
- # Public: Loads given +plugin+ into the application.
9
+ # Loads given <tt>plugin</tt> into the application.
11
10
  #
12
- # Examples
11
+ # [plugin]
12
+ # A module that can contain a <tt>ClassMethods</tt> or <tt>InstanceMethods</tt>
13
+ # module to extend Tynn. If <tt>plugin</tt> responds to <tt>setup</tt>, it will
14
+ # be called last, and should be used to set up the plugin.
13
15
  #
14
- # require "tynn"
15
- # require "tynn/protection"
16
- # require "tynn/session"
16
+ # [*args]
17
+ # A list of arguments passed to <tt>plugin#setup</tt>.
17
18
  #
18
- # Tynn.plugin(Tynn::Protection)
19
- # Tynn.plugin(Tynn::Session, secret: "__a_random_secret_key")
19
+ # [&block]
20
+ # A block passed to <tt>plugin#setup</tt>.
20
21
  #
21
- def self.plugin(plugin, *args, &block)
22
- if defined?(plugin::InstanceMethods)
23
- self.include(plugin::InstanceMethods)
24
- end
25
-
26
- if defined?(plugin::ClassMethods)
27
- self.extend(plugin::ClassMethods)
28
- end
29
-
30
- if plugin.respond_to?(:setup)
31
- plugin.setup(self, *args, &block)
32
- end
33
- end
34
-
35
- # Internal: Default plugins.
36
- plugin(Tynn::Settings)
37
-
38
- # Public: Sets the application handler.
22
+ # <tt></tt>
39
23
  #
40
- # Examples
24
+ # # Using default plugins
25
+ # require "tynn"
26
+ # require "tynn/environment"
27
+ # require "tynn/static"
41
28
  #
42
- # class Users < Tynn
43
- # end
29
+ # Tynn.plugin(Tynn::Environment)
30
+ # Tynn.plugin(Tynn::Static, %(/css /js /images))
31
+ #
32
+ # # Using a custom plugin
33
+ # class MyAppNamePlugin
34
+ # def self.setup(app, name, &block)
35
+ # app.app_name = name
36
+ # end
44
37
  #
45
- # Users.define do
46
- # on(:id) do |id|
47
- # get do
48
- # res.write("GET /users")
38
+ # module ClassMethods
39
+ # def app_name
40
+ # @app_name
49
41
  # end
50
42
  #
51
- # post do
52
- # res.write("POST /users")
43
+ # def app_name=(name)
44
+ # @app_name = name
53
45
  # end
54
46
  # end
55
- # end
56
47
  #
57
- def self.define(&block)
58
- build_app(Syro.new(self, &block))
59
- end
60
-
61
- # Public: Adds given Rack +middleware+ to the stack.
62
- #
63
- # Examples
64
- #
65
- # require "rack/common_logger"
66
- # require "rack/show_exceptions"
48
+ # module InstanceMethods
49
+ # def app_name
50
+ # self.class.app_name
51
+ # end
52
+ # end
53
+ # end
67
54
  #
68
- # Tynn.use(Rack::CommonLogger)
69
- # Tynn.use(Rack::ShowExceptions)
55
+ # Tynn.plugin(MyAppNamePlugin, "MyApp")
70
56
  #
71
- def self.use(middleware, *args, &block)
72
- self.middleware << proc { |app| middleware.new(app, *args, &block) }
73
- end
74
-
75
- def self.call(env) # :nodoc:
76
- return @app.call(env)
77
- end
78
-
79
- def self.build_app(syro) # :nodoc:
80
- if middleware.empty?
81
- @app = syro
82
- else
83
- @app = middleware.reverse.inject(syro) { |a, m| m.call(a) }
57
+ def self.plugin(plugin, *args, &block)
58
+ if defined?(plugin::InstanceMethods)
59
+ include(plugin::InstanceMethods)
84
60
  end
85
- end
86
-
87
- # Internal: Returns middleware stack.
88
- def self.middleware
89
- return @middleware ||= []
90
- end
91
-
92
- def self.reset! # :nodoc:
93
- @app = nil
94
- @middleware = []
95
- end
96
-
97
- # Public: Sets an +option+ to the given +value+.
98
- #
99
- # Examples
100
- #
101
- # require "tynn"
102
- # require "tynn/environment"
103
- #
104
- # Tynn.plugin(Tynn::Environment)
105
- #
106
- # Tynn.set(:environment, :staging)
107
- # Tynn.environment
108
- # # => :staging
109
- #
110
- def self.set(option, value)
111
- settings[option] = value
112
- end
113
61
 
114
- set(:default_headers, {})
115
-
116
- def default_headers # :nodoc:
117
- return Hash[self.class.settings[:default_headers]]
118
- end
62
+ if defined?(plugin::ClassMethods)
63
+ extend(plugin::ClassMethods)
64
+ end
119
65
 
120
- def request_class # :nodoc:
121
- return Tynn::Request
66
+ if plugin.respond_to?(:setup)
67
+ plugin.setup(self, *args, &block)
68
+ end
122
69
  end
123
70
 
124
- def response_class # :nodoc:
125
- return Tynn::Response
126
- end
71
+ plugin(Tynn::Base)
72
+ plugin(Tynn::Settings)
73
+ plugin(Tynn::DefaultHeaders)
127
74
  end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "syro"
4
+ require_relative "request"
5
+ require_relative "response"
6
+
7
+ class Tynn
8
+ module Base
9
+ module ClassMethods
10
+ # Sets the application handler.
11
+ #
12
+ # class Users < Tynn
13
+ # end
14
+ #
15
+ # Users.define do
16
+ # on(:id) do |id|
17
+ # get do
18
+ # res.write("GET /users/#{ id }")
19
+ # end
20
+ #
21
+ # post do
22
+ # res.write("POST /users/#{ id }")
23
+ # end
24
+ # end
25
+ # end
26
+ #
27
+ def define(&block)
28
+ @__app = build_app(Syro.new(self, &block))
29
+ end
30
+
31
+ def build_app(app) # :nodoc:
32
+ middleware.freeze.reverse.inject(app) { |a, e| e.call(a) }
33
+ end
34
+
35
+ # Adds given Rack <tt>middleware</tt> to the stack.
36
+ #
37
+ # [middleware] A Rack middleware.
38
+ # [*args] A list of arguments passed to the middleware initialization.
39
+ # [&block] A block passed to the middleware initialization.
40
+ #
41
+ # require "rack/common_logger"
42
+ # require "rack/show_exceptions"
43
+ #
44
+ # Tynn.use(Rack::CommonLogger)
45
+ # Tynn.use(Rack::ShowExceptions)
46
+ #
47
+ def use(middleware, *args, &block)
48
+ self.middleware.push(proc { |app| middleware.new(app, *args, &block) })
49
+ end
50
+
51
+ def middleware # :nodoc:
52
+ @__middleware ||= []
53
+ end
54
+
55
+ def call(env) # :nodoc:
56
+ app.call(env)
57
+ end
58
+
59
+ def app # :nodoc:
60
+ (defined?(@__app) && @__app) or
61
+ raise("Application handler is missing. Try #{ self }.define {}")
62
+ end
63
+ end
64
+
65
+ module InstanceMethods # :nodoc:
66
+ include Syro::Deck::API
67
+
68
+ # Overrides Syro::Deck::API#on to support passing the receiver to
69
+ # the capturing matchers.
70
+ #
71
+ # on :name do |name|
72
+ # end
73
+ #
74
+ # # instead of
75
+ #
76
+ # on :name do
77
+ # name = inbox[:name]
78
+ # end
79
+ #
80
+ def on(arg)
81
+ default { yield((arg.is_a?(Symbol) ? inbox[arg] : nil)) } if match(arg)
82
+ end
83
+
84
+ # Overrides request class used by Syro's router.
85
+ # See Tynn::Request for more information.
86
+ def request_class
87
+ Tynn::Request
88
+ end
89
+
90
+ # Overrides response class used by Syro's router.
91
+ # See Tynn::Response for more information.
92
+ def response_class
93
+ Tynn::Response
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Tynn
4
+ # Adds support to set default headers for responses.
5
+ #
6
+ # require "tynn"
7
+ # require "tynn/test"
8
+ #
9
+ # Tynn.set(:default_headers, {
10
+ # "Content-Type" => "application/json"
11
+ # })
12
+ #
13
+ # Tynn.define { }
14
+ #
15
+ # app = Tynn::Test.new
16
+ # app.get("/")
17
+ #
18
+ # app.res.headers
19
+ # # => { "Content-Type" => "application/json" }
20
+ #
21
+ # This plugin is included by default.
22
+ #
23
+ module DefaultHeaders
24
+ def self.setup(app) # :nodoc:
25
+ app.set(:default_headers, {})
26
+ end
27
+
28
+ module ClassMethods
29
+ # Returns a Hash with the default headers.
30
+ #
31
+ # Tynn.set(:default_headers, {
32
+ # "Content-Type" => "application/json"
33
+ # })
34
+ #
35
+ # Tynn.default_headers["Content-Type"]
36
+ # # => "application/json"
37
+ #
38
+ def default_headers
39
+ settings[:default_headers]
40
+ end
41
+ end
42
+
43
+ module InstanceMethods # :nodoc:
44
+ # Overrides Syro's default implementation of default headers.
45
+ def default_headers
46
+ Hash[self.class.default_headers]
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,52 +1,69 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Tynn
2
- # Public: Adds helper methods to get and check the current environment.
3
- #
4
- # Examples
4
+ # Adds helper methods to get and check the current environment.
5
+ # By default, the environment is based on <tt>ENV["RACK_ENV"]</tt>.
5
6
  #
6
7
  # require "tynn"
7
8
  # require "tynn/environment"
8
9
  #
9
10
  # Tynn.plugin(Tynn::Environment)
10
11
  #
12
+ # # Accessing the current environment.
11
13
  # Tynn.environment # => :development
12
14
  #
15
+ # # Checking the current environment.
13
16
  # Tynn.development? # => true
14
17
  # Tynn.production? # => false
15
18
  # Tynn.test? # => false
19
+ # Tynn.staging? # => false
16
20
  #
17
- # By default, the environment is based on <tt>ENV["RACK_ENV"]</tt>.
18
- #
19
- # Examples
21
+ # # Changing the current environment.
22
+ # Tynn.set(:environment, :test)
20
23
  #
21
- # Tynn.plugin(Tynn::Environment, env: ENV["RACK_ENV"])
24
+ # # Performing operations in specific environments.
25
+ # Tynn.configure(:development, :test) do
26
+ # # ...
27
+ # end
22
28
  #
23
29
  module Environment
24
- # Internal: Configures current environment.
25
- def self.setup(app, env: ENV["RACK_ENV"])
30
+ def self.setup(app, env: ENV["RACK_ENV"]) # :nodoc:
26
31
  app.set(:environment, (env || :development).to_sym)
27
32
  end
28
33
 
29
34
  module ClassMethods
30
- # Public: Returns current environment.
35
+ # Yields if current environment matches one of the given environments.
31
36
  #
32
- # Examples
37
+ # class MyApp < Tynn
38
+ # configure(:development, :staging) do
39
+ # use(BetterErrors::Middleware)
40
+ # end
41
+ #
42
+ # configure(:production) do
43
+ # plugin(Tynn::SSL)
44
+ # end
45
+ # end
46
+ #
47
+ def configure(*envs)
48
+ yield(self) if envs.include?(environment)
49
+ end
50
+
51
+ # Returns the current environment for the application.
33
52
  #
34
53
  # Tynn.environment
35
54
  # # => :development
36
55
  #
37
- # Tynn.set(:environment, :test)
56
+ # Tynn.set(environment, :test)
38
57
  #
39
58
  # Tynn.environment
40
59
  # # => :test
41
60
  #
42
61
  def environment
43
- return settings[:environment]
62
+ settings[:environment]
44
63
  end
45
64
 
46
- # Public: Returns +true+ if the current environment is +:development+.
47
- # Otherwise returns +false+.
48
- #
49
- # Examples
65
+ # Checks if current environment is development. Returns <tt>true</tt> if
66
+ # <tt>environment</tt> is <tt>:development</tt>. Otherwise, <tt>false</tt>.
50
67
  #
51
68
  # Tynn.set(:environment, :test)
52
69
  # Tynn.development? # => false
@@ -55,13 +72,11 @@ class Tynn
55
72
  # Tynn.development? # => true
56
73
  #
57
74
  def development?
58
- return environment == :development
75
+ environment == :development
59
76
  end
60
77
 
61
- # Public: Returns +true+ if the current environment is +:test+.
62
- # Otherwise returns +false+.
63
- #
64
- # Examples
78
+ # Checks if current environment is test. Returns <tt>true</tt> if
79
+ # <tt>environment</tt> is <tt>:test</tt>. Otherwise, <tt>false</tt>.
65
80
  #
66
81
  # Tynn.set(:environment, :development)
67
82
  # Tynn.test? # => false
@@ -70,13 +85,11 @@ class Tynn
70
85
  # Tynn.test? # => true
71
86
  #
72
87
  def test?
73
- return environment == :test
88
+ environment == :test
74
89
  end
75
90
 
76
- # Public: Returns +true+ if the current environment is +:production+.
77
- # Otherwise returns +false+.
78
- #
79
- # Examples
91
+ # Checks if current environment is production. Returns <tt>true</tt> if
92
+ # <tt>environment</tt> is <tt>:production</tt>. Otherwise, <tt>false</tt>.
80
93
  #
81
94
  # Tynn.set(:environment, :development)
82
95
  # Tynn.production? # => false
@@ -85,7 +98,20 @@ class Tynn
85
98
  # Tynn.production? # => true
86
99
  #
87
100
  def production?
88
- return environment == :production
101
+ environment == :production
102
+ end
103
+
104
+ # Checks if current environment is staging. Returns <tt>true</tt> if
105
+ # <tt>environment</tt> is <tt>:staging</tt>. Otherwise, <tt>false</tt>.
106
+ #
107
+ # Tynn.set(environment, :test)
108
+ # Tynn.staging? # => false
109
+ #
110
+ # Tynn.set(:environment, :staging)
111
+ # Tynn.staging? # => true
112
+ #
113
+ def staging?
114
+ environment == :staging
89
115
  end
90
116
  end
91
117
  end