odex 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c7c61c0f5000ec3c865c9a1c08de459df7f7ae2
4
+ data.tar.gz: a0e3d37c71ed16f5939f4ef2a4d0d61fe79b24b1
5
+ SHA512:
6
+ metadata.gz: 379ec12671a1b51038c9abe0408bcbcc2b827f2f8e19264080f6295599ab4fad2c5d4c2c1284d273acaede716c73036889fe78b6d9c0385fa99a04515c092ca3
7
+ data.tar.gz: 34d036481807d30be53abbb6891691b7c3f06f18cfbb95f9d1ed88673117c6a53ea08623d67692174f4cdb0783029b818af4c5eee395435e895aae05681d531f
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'coveralls', :require => false
7
+ end
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Odex.
2
+ Small web framework.
3
+
4
+ ```ruby
5
+ # odex_test.rb
6
+
7
+ require 'odex'
8
+ class App < Odex::App
9
+ get '/' do
10
+ 'Hello world!'
11
+ end
12
+ end
13
+
14
+ App.go(3000)!
15
+ ```
16
+
17
+ Run the file:
18
+
19
+ ```bash
20
+ ruby myapp.rb
21
+ ```
22
+ Install the gem:
23
+
24
+ ```bash
25
+ gem install odex
26
+ ```
27
+
28
+ # Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/odex/app.rb ADDED
@@ -0,0 +1,245 @@
1
+ module Odex
2
+ # Odex Builder
3
+ class OdexBuilder < Rack::Builder
4
+ end
5
+
6
+ # Odex Request
7
+ class OdexRequest < ActionDispatch::Journey::Routes
8
+ end
9
+
10
+ # Odex Router
11
+ class OdexRouter < ActionDispatch::Journey::Router
12
+ end
13
+
14
+ # Odex Pattern
15
+ class OdexPattern < ActionDispatch::Journey::Path::Pattern
16
+ end
17
+
18
+ # Request
19
+
20
+ class Req
21
+ extend Forwardable
22
+
23
+ attr_reader :request, :response
24
+ def_delegators :request, :session, :params
25
+ def_delegators :response, :headers
26
+
27
+ def initialize(env)
28
+ @request = Request.new(env)
29
+ @request.params.merge!(env['odex.params'])
30
+ @response = Response.new([], 200, {'Content-Type' => 'text/html'})
31
+ end
32
+
33
+ def cookies
34
+ @cookies ||= Rack::Cookies::CookieJar.new(request.cookies)
35
+ end
36
+
37
+ def status(code)
38
+ response.status = code
39
+ end
40
+
41
+ def halt(status, headers={}, body='')
42
+ @response = Response.new(body, status, headers)
43
+ cookies.finish!(response) if @cookies
44
+ throw(:halt, response)
45
+ end
46
+
47
+ def system_redirect_to(uri, status=302)
48
+ halt(status, {'Location' => uri})
49
+ end
50
+ alias_method :redirect, :system_redirect_to
51
+
52
+ def apply_to(&handler)
53
+ data = instance_eval(&handler)
54
+ data.respond_to?(:each) ? response.body = data : response.write(data)
55
+ cookies.finish!(response) if @cookies
56
+ response
57
+ end
58
+ end
59
+
60
+ # Router
61
+
62
+ class Router
63
+ attr_reader :c, :od_jr, :hook_before, :hook_after, :fallback
64
+ def initialize(options)
65
+ @c = options[:c]
66
+ @hook_before = options[:hook_before]
67
+ @hook_after = options[:hook_after]
68
+ @fallback = options[:fallback]
69
+
70
+ prepare_for_od_jr(options[:route_defs])
71
+ end
72
+
73
+ def call(env)
74
+ response = od_jr.call(env)
75
+
76
+ if response[0] == 40 and
77
+ fallback.call(env)
78
+ else
79
+ response
80
+ end
81
+ end
82
+
83
+ def prepare_for_od_jr(route_defs)
84
+ routes = Odex::OdexRequest.new
85
+ @od_jr = Odex::OdexRouter.new(routes, {
86
+ :parameters_key => 'odex.params',
87
+ :request_class => Odex::Request
88
+ })
89
+
90
+ route_defs.each do |path, options, handler|
91
+ pat = Odex::OdexPattern.new(path)
92
+ constraints = options.fetch(:constraints, {})
93
+ defaults = options.fetch(:defaults, {})
94
+
95
+ @od_jr.routes.add_route compile(handler), pat, constraints, defaults
96
+ end
97
+ end
98
+
99
+ def compile(handler)
100
+ Proc.new do |env|
101
+ scope = c.new(env)
102
+
103
+ response = catch (:halt) do
104
+ hook_before.each {|h| scope.instance_eval &h }
105
+ scope.apply_to &handler
106
+ end
107
+
108
+ catch (:halt) do
109
+ hook_after.each {|h| scope.instance_eval &h }
110
+ end
111
+
112
+ response.finish
113
+ end
114
+ end
115
+ end
116
+
117
+ # Initializer
118
+
119
+ module Init
120
+ def self.included(base)
121
+ base.class_eval do
122
+ def self.init_methods name, value
123
+ @_methods ||= []
124
+ @_methods << name
125
+ self.class.send(:attr_accessor, name)
126
+ self.send("#{name}=", value)
127
+ end
128
+
129
+ def self.inherited(subclass)
130
+ @_methods.each do |attr|
131
+ subclass.init_methods attr, self.send(attr).deep_dup
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ # Rack Request
139
+ class Request < Rack::Request
140
+ def params
141
+ @params ||= ActiveSupport::HashWithIndifferentAccess.new(super)
142
+ end
143
+ end
144
+
145
+ # Rack Response
146
+
147
+ class Response < Rack::Response
148
+ end
149
+
150
+ # Odex Base
151
+
152
+ class Base
153
+ include Odex::Init
154
+ METHODS = [:options, :patch, :post, :put, :trace, :delete, :get, :head]
155
+
156
+ init_methods :c, Class.new(Req)
157
+ init_methods :route_defs, []
158
+ init_methods :hook_before, []
159
+ init_methods :hook_after, []
160
+ init_methods :default_constraints, {}
161
+ init_methods :map, {}
162
+
163
+ def initialize(app=nil)
164
+ builder = odex_rack_builder
165
+ builder.run Router.new({
166
+ :c => self.class.c,
167
+ :route_defs => self.class.route_defs,
168
+ :hook_before => self.class.hook_before,
169
+ :hook_after => self.class.hook_after,
170
+ :fallback => app
171
+ })
172
+
173
+ @app = builder.to_app
174
+ end
175
+
176
+ def odex_rack_builder
177
+ rack_builder = Odex::OdexBuilder.new
178
+ self.class.map.each do |url, map_class|
179
+ rack_builder.map(url) do
180
+ use map_class
181
+ end
182
+ end
183
+ rack_builder
184
+ end
185
+
186
+ def call env
187
+ @app.call env
188
+ end
189
+
190
+ class << self
191
+ METHODS.each do |method|
192
+ define_method method do |path, options={}, &block|
193
+ options[:constraints] = options[:constraints] || {}
194
+ options[:constraints].merge!(:request_method => method.to_s.upcase)
195
+ route path, options, &block
196
+ end
197
+ end
198
+
199
+ def group(url, app=nil, &block)
200
+ scope = self.c
201
+
202
+ app ||= Class.new(self.superclass) do
203
+ self.c = scope
204
+ class_eval(&block)
205
+ end
206
+
207
+ map[url] = app
208
+ end
209
+
210
+ def before_proc(&block)
211
+ hook_before << Proc.new(&block)
212
+ end
213
+
214
+ def after_proc(&block)
215
+ hook_after << Proc.new(&block)
216
+ end
217
+
218
+ def ext(*extensions)
219
+ extensions.each do |ext|
220
+ extend ext
221
+ ext.module_is_registered(self) if ext.respond_to?(:module_is_registered)
222
+ end
223
+ end
224
+
225
+ def helpers(*args, &block)
226
+ args << Module.new(&block) if block_given?
227
+ args.each {|m| c.send :include, m }
228
+ end
229
+
230
+ def route(path, options, &block)
231
+ self.route_defs << [path, options, Proc.new(&block)]
232
+ end
233
+
234
+ end
235
+ end
236
+
237
+ class App < Base
238
+ class << self
239
+ def go!(port = 1453)
240
+ Rack::Handler.pick(['webrick']).run new, :Port => port
241
+ end
242
+ end
243
+ end
244
+
245
+ end
@@ -0,0 +1,22 @@
1
+ require 'tilt'
2
+ require 'slim'
3
+
4
+ module Odex
5
+ module Templates
6
+ module Helpers
7
+ def render template, locals = {}, options = {}, &block
8
+ template_cache.fetch(template) do
9
+ Slim::Template.new(template, options)
10
+ end.render(self, locals, &block)
11
+ end
12
+
13
+ def template_cache
14
+ Thread.current[:template_cache] ||= Tilt::Cache.new
15
+ end
16
+ end
17
+
18
+ def self.module_is_registered app
19
+ app.helpers Helpers
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Odex
2
+ VERSION = "0.0.1"
3
+ end
data/lib/odex.rb ADDED
@@ -0,0 +1,19 @@
1
+ # Version
2
+ require 'odex/version'
3
+
4
+ # General
5
+ require 'rack'
6
+ require 'active_support/hash_with_indifferent_access'
7
+ require 'active_support/core_ext/object/deep_dup'
8
+ require 'forwardable'
9
+ require 'rack/contrib/cookies'
10
+ require 'active_support/concern'
11
+ require 'action_dispatch/routing'
12
+ require 'action_dispatch/journey'
13
+ require 'pathname'
14
+
15
+ # Modules
16
+ require 'odex/modules/templates'
17
+
18
+ # Application
19
+ require 'odex/app'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: odex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sedat ÇİFTÇİ
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.14.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.14.1
41
+ description: Small web framework.
42
+ email:
43
+ - me@sedat.us
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - Gemfile
50
+ - Rakefile
51
+ - lib/odex/app.rb
52
+ - lib/odex/modules/templates.rb
53
+ - lib/odex/version.rb
54
+ - lib/odex.rb
55
+ homepage: http://odex.sedat.us
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.0.14
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Odex
79
+ test_files: []