lydia 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c5b7ef25cf346d0026ebcb068c101290b20d358
4
- data.tar.gz: 7b0f29651e251ec7a231ef5929e57de53322285f
3
+ metadata.gz: 674c9480a5fc9134caefdc5cdd657a325b774f3c
4
+ data.tar.gz: b4fac7a13bf1724d54d47dd205cb08fd8b72061c
5
5
  SHA512:
6
- metadata.gz: b2007f3265c8aa888d328d0a054a524603939a5a463661aaa2d704af650c5b23bfafa9b9e34f83a4a91135cb3282890bfa4567e559fcfb1e3be657748f777f52
7
- data.tar.gz: c0a9e836f9e04d48c096656f563c8cde80bede7e61f0b08411085a6d9496a49169aff9b115d4cdf4eb19083a3cf56d9c0d4e6feb5d0dd9cc584a55a171749fa3
6
+ metadata.gz: 5e0623433d619e658d3b2d7a6e13d029e4661771ad747977b7ce43e24f4904d619dead18d1c48374bcbe2d6e65fb46f8d7050418b71d7893d8ff5e845bce2f8e
7
+ data.tar.gz: dfddf8e23a78e7ec479f9acc1264d767ade0c22092205994556ac76d578179f40fd8d82ca189cf86145660b69bf149225f0751806f828578581cf8235f35a0a9
@@ -0,0 +1,61 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ DisplayCopNames: true
5
+ Exclude:
6
+ - 'db/migrate/*'
7
+ - 'db/schema.rb'
8
+ - 'config/**/*'
9
+ - 'bin/*'
10
+ - lydia.gemspec
11
+ # Other common or autogenerated ignores
12
+ - 'lib/tasks/cucumber.rake'
13
+ # Project specific ignores goes here
14
+
15
+ Style/BarePercentLiterals:
16
+ EnforcedStyle: percent_q
17
+
18
+ Style/EmptyLineBetweenDefs:
19
+ AllowAdjacentOneLineDefs: true
20
+
21
+ Style/MultilineOperationIndentation:
22
+ EnforcedStyle: indented
23
+
24
+ Metrics/LineLength:
25
+ Max: 100
26
+
27
+ Lint/EndAlignment:
28
+ AlignWith: variable
29
+
30
+ Style/TrailingCommaInLiteral:
31
+ EnforcedStyleForMultiline: comma
32
+
33
+ Lint/AssignmentInCondition:
34
+ Enabled: false
35
+
36
+ Style/DoubleNegation:
37
+ Enabled: false
38
+
39
+ Style/SingleLineBlockParams:
40
+ Enabled: false
41
+
42
+ Documentation:
43
+ Enabled: false
44
+
45
+ Rails:
46
+ Enabled: true
47
+
48
+ RSpec/DescribeClass:
49
+ Enabled: false
50
+
51
+ RSpec/InstanceVariable:
52
+ Enabled: false
53
+
54
+ RSpec/MultipleExpectations:
55
+ Enabled: false
56
+
57
+ RSpec/ExampleLength:
58
+ Max: 10
59
+
60
+ Rails/HttpPositionalArguments:
61
+ Enabled: false
@@ -1,11 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.2
4
- - 1.9.3
5
- - 2.0.0
6
- - 2.1
7
- - 2.2
3
+ - 2.2.2
8
4
  - 2.3.0
9
- - rbx-2
10
- - jruby
11
- before_install: gem install bundler -v 1.10.6
5
+ - 2.3.1
6
+ before_install: gem install bundler
data/README.md CHANGED
@@ -13,9 +13,7 @@ Lightweight, fast and easy to use small ruby web framework.
13
13
 
14
14
  Add this line to your application's Gemfile:
15
15
 
16
- ```ruby
17
- gem 'lydia'
18
- ```
16
+ gem 'lydia'
19
17
 
20
18
  And then execute:
21
19
 
@@ -27,7 +25,7 @@ Or install it yourself as:
27
25
 
28
26
  ## Another ruby web framework? WTF?
29
27
 
30
- This project is not intended to become a top notch framework or the new rails, it's just an experiment.
28
+ This project is not intended to become a top notch framework or the new rails, it's just an experiment.
31
29
  The goals of this project are:
32
30
 
33
31
  * [Rack](https://github.com/rack/rack/) based.
@@ -44,35 +42,35 @@ The goals of this project are:
44
42
  ## Usage
45
43
 
46
44
  ### First example
47
- Create a ruby file, fior example hello_world.rb, require 'lydia' and using the routing functions without creating an application object.
45
+ Create a ruby file, for example hello_world.rb, require 'lydia' and using the routing functions without creating an application object.
48
46
 
49
47
  require 'lydia'
50
-
48
+
51
49
  get '/' do
52
50
  'Hello world!'
53
51
  end
54
52
 
55
53
  Just run it to start a webrick server that responds hello world to root.
56
-
57
- $ ruby hello_world.rb
54
+
55
+ $ ruby hello_world.rb
58
56
 
59
57
  ### Application
60
58
  If preferred it's possible to create an application object and run using rackup command, in this case don't require lydia but lydia/application to avoid the server auto start. For example a minimal config.ru file can be:
61
59
 
62
60
  require 'lydia/application'
63
-
61
+
64
62
  class App < Lydia::Application
65
63
  get '/' do
66
64
  'Hello world!'
67
65
  end
68
66
  end
69
-
67
+
70
68
  run App.new
71
-
69
+
72
70
  Start the server using rackup command:
73
71
 
74
72
  $ rackup
75
-
73
+
76
74
  ### Router
77
75
 
78
76
  #### Stand alone router
@@ -80,10 +78,10 @@ If needed the router can be used stand alone, for example if best performances a
80
78
  Stand alone example, note that the return type must be in rack standard format, an array of three that is status, header, body (as array):
81
79
 
82
80
  require 'lydia/router'
83
-
81
+
84
82
  class App < Lydia::Router
85
83
  get '/' do
86
- body = 'Hellow world!'
84
+ body = 'Hello world!'
87
85
  [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]]
88
86
  end
89
87
  end
@@ -91,26 +89,101 @@ Stand alone example, note that the return type must be in rack standard format,
91
89
  #### HTTP verbs
92
90
  Supports standard HTTP verbs: HEAD GET PATCH PUT POST DELETE OPTIONS.
93
91
 
94
- ### DOCUMENTATION IN PROGRESS...
92
+ #### Query parameters
95
93
 
96
- #### Parameters
94
+ # matches /querystring&name=mirko
95
+ get '/querystring' do
96
+ # do something
97
+ # request.params[:name] contains 'mirko'
98
+ end
97
99
 
98
- #### Wildcards
100
+ #### Wildcard
101
+
102
+ # matches /wildcard/everything
103
+ get '/wildcard/* ' do
104
+ # do something
105
+ end
99
106
 
100
107
  #### Named route parameters
101
108
 
109
+ # matches /users/1/comments/3/edit
110
+ get '/users/:id/comments/:comment_id' do
111
+ # do something
112
+ # request.params[:id] contains 1
113
+ # request.params[:comment_id] contains 3
114
+ end
115
+
116
+ Automatically add to response.params every route params.
117
+
102
118
  #### Regular expressions
103
119
 
104
- #### Not found routes and errors
120
+ # matches /regexp
121
+ get %r{/regexp$}i do
122
+ # do something
123
+ end
105
124
 
106
125
  #### Skip to next route
126
+ To skip to the next matching route use next_route method.
127
+
128
+ get '/next_route' do
129
+ next_route
130
+ end
131
+
132
+ get '/next_route' do
133
+ 'Hello this is the next route'
134
+ end
107
135
 
108
136
  #### Halting
137
+ To halt the execution raising an Halt error use halt method, by default the standard halt page is displayed but it's possible to pass a custom response as halt parameter.
138
+
139
+ get '/halt' do
140
+ halt
141
+ end
142
+
143
+ get '/custom_halt' do
144
+ halt 'Custom halt'
145
+ end
109
146
 
110
147
  ### Return types
148
+ Lydia supports various returns types other that the standard rack response object. The supported type are:
149
+
150
+ #### Rack::Response or Lydia::Response
151
+ Using the standard rack response the framework does nothing other than pass the response to rack. If response finish method was not called the framework will.
152
+
153
+ #### String
154
+ Returning a string is intended as the response body, the headers and a 200 status are automatically added.
155
+
156
+ #### Array of 2 or 3 elements
157
+ Returning an array of 2 elements means that the first is the status and the second the body.
158
+ Returning an array of 3 elements means that the first is the status, the second the headers, and the third the body.
159
+
160
+ #### Fixnum
161
+ Returning a fixnum is intended as the response code. Useful to return a response code without a body.
162
+
163
+ #### Hash
164
+ An hash is intended as a json, json content type is automatically added.
165
+
166
+ #### Object that responds to :each
167
+ Returning a generic object is admitted accorind rack specifications if responds to :each method.
111
168
 
112
169
  ### Filters
113
170
 
171
+ #### Before and after Filters
172
+ Before and after filters are available as in the following example:
173
+
174
+ before do
175
+ # do something
176
+ end
177
+
178
+ after do
179
+ # do something
180
+ end
181
+
182
+ #### Redirects
183
+ To define a redirect use the following syntax:
184
+
185
+ redirect '/from_route', to: '/to_route'
186
+
114
187
  ### Templates
115
188
 
116
189
  Extensive templates support using [tilt](https://github.com/rtomayko/tilt/)
@@ -118,8 +191,7 @@ To render a template simply use the render function:
118
191
 
119
192
  get '/render_erb' do
120
193
  render 'template.erb', nil, message: 'template'
121
- end
122
-
194
+ end
123
195
 
124
196
  ### Helpers
125
197
 
@@ -135,7 +207,7 @@ It's possible to read request parameters using params helper:
135
207
 
136
208
  get '/test' do
137
209
  params['my_param']
138
- end
210
+ end
139
211
 
140
212
  #### Content type
141
213
  It's possible to force the response return type using content_type helper:
@@ -157,4 +229,3 @@ It's possible to force the response return type using content_type helper:
157
229
  ## License
158
230
 
159
231
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
160
-
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "lydia/application"
3
+ require 'bundler/setup'
4
+ require 'lydia/application'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "lydia/application"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
@@ -6,3 +6,5 @@ class App < Lydia::Application
6
6
  'Hello world!'
7
7
  end
8
8
  end
9
+
10
+ run App.new
@@ -3,4 +3,4 @@ require 'lydia'
3
3
 
4
4
  get '/' do
5
5
  'Hello world!'
6
- end
6
+ end
@@ -4,7 +4,7 @@ require 'lydia/delegator'
4
4
  require 'lydia/version'
5
5
 
6
6
  module Lydia
7
- at_exit { Rack::Handler.default.run(Application) }
7
+ at_exit { Rack::Handler.default.run(Application.new) }
8
8
  end
9
9
 
10
- extend Lydia::Delegator
10
+ extend Lydia::Delegator
@@ -12,7 +12,7 @@ module Lydia
12
12
  include Templates
13
13
  include Filters
14
14
  include Helpers
15
-
15
+
16
16
  def process
17
17
  result = super
18
18
  if result.nil?
@@ -23,31 +23,28 @@ module Lydia
23
23
  @response.build(result)
24
24
  end
25
25
  end
26
-
26
+
27
27
  def new_request(env)
28
28
  Lydia::Request.new(env)
29
29
  end
30
-
30
+
31
31
  def new_response(body = [], status = 200, header = {})
32
32
  Lydia::Response.new(body, status, header)
33
33
  end
34
-
34
+
35
35
  class << self
36
36
  extend Forwardable
37
-
37
+
38
38
  def_delegators :builder, :map, :use, :run
39
-
39
+
40
40
  def builder
41
41
  @builder ||= Rack::Builder.new
42
42
  end
43
43
 
44
- alias new! new
45
-
46
44
  def new(*args, &bk)
47
- app = new!(*args, &bk)
48
- builder.run(app)
45
+ builder.run(super(*args, &bk))
49
46
  builder.to_app
50
47
  end
51
48
  end
52
- end
49
+ end
53
50
  end
@@ -4,9 +4,9 @@ require 'forwardable'
4
4
  module Lydia
5
5
  module Delegator
6
6
  extend Forwardable
7
- def_delegators Lydia::Application,
7
+ def_delegators Lydia::Application,
8
8
  :head, :get, :patch, :put, :post, :delete, :options,
9
9
  :namespace, :before, :after,
10
10
  :map, :use, :run
11
11
  end
12
- end
12
+ end
@@ -5,24 +5,24 @@ module Lydia
5
5
  def self.included(base)
6
6
  base.extend(ClassMethods)
7
7
  end
8
-
8
+
9
9
  module ClassMethods
10
10
  def filters
11
11
  @filters ||= Hash.new { |h, k| h[k] = [] }
12
12
  end
13
-
13
+
14
14
  %w(before after).each do |filter|
15
15
  define_method(filter) do |pattern = '/*', options = {}, &block|
16
16
  filters[filter.to_sym] << Route.new(filter, @namespace || '', pattern, options, &block)
17
17
  end
18
18
  end
19
-
19
+
20
20
  def redirect(pattern, options = {})
21
21
  return ArgumentError.new('Options must contains :to') unless options.include?(:to)
22
22
  filters[:redirect] << Route.new('redirect', @namespace || '', pattern, options)
23
23
  end
24
24
  end
25
-
25
+
26
26
  def dispatch(env)
27
27
  process_redirects(env)
28
28
  process_before_filters(env)
@@ -30,7 +30,7 @@ module Lydia
30
30
  process_after_filters(env)
31
31
  result
32
32
  end
33
-
33
+
34
34
  %w(before after).each do |filter_type|
35
35
  define_method("process_#{filter_type}_filters") do |env|
36
36
  self.class.filters[filter_type.to_sym].each do |filter|
@@ -38,15 +38,14 @@ module Lydia
38
38
  end
39
39
  end
40
40
  end
41
-
41
+
42
42
  def process_redirects(env)
43
43
  self.class.filters[:redirect].each do |redirect|
44
- if redirect.match?(env)
45
- env['PATH_INFO'] = redirect.namespace + redirect.options[:to]
46
- @request = new_request(env)
47
- break
48
- end
44
+ next unless redirect.match?(env)
45
+ env['PATH_INFO'] = redirect.namespace + redirect.options[:to]
46
+ @request = new_request(env)
47
+ break
49
48
  end
50
49
  end
51
50
  end
52
- end
51
+ end