lithium 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32f4389fbd397277db472e01f4f5485164dbc4bd
4
- data.tar.gz: 3be619abbb461e3cee9760b31eb49cd5d81eb1ad
3
+ metadata.gz: 1d20b742247809e17b6784aeae96cac85981cedc
4
+ data.tar.gz: b6009b546724ffde5b29820e23d6ecfa57862f69
5
5
  SHA512:
6
- metadata.gz: 65d26a59cefe07dcf38c1f91fc39652e1b7e274f515107e3a4b5fbbba6588583cf96cb40cb189d8a9c2e1d7819f3979f8011f0095b701de86b72b1ddf05804cb
7
- data.tar.gz: 0422a870787a57491f238ab6028ecc6e5011d3e348e024a73eb71a21296db7b43cbb8d1a6170d42cdd89d21317dabc4f21d12b6254523777dbae65fefd6da2bc
6
+ metadata.gz: 909d65c818d90627ca6279187225b2d31f48187bcc82a0c7dcd1fcb22355382a77a4cd7aa5c5df22ab6bbad6c3a45c06724a56eea3217cb6a9b06c41666f0e9c
7
+ data.tar.gz: 0ca0144f25c8f229de6898fa0fad1bbd815c3b7e698b5d997be48a9f461225b20e611e66edb70814aa04a91e181889a09ab702aa138b1adbafb1988ab690ce8a
data/Gemfile CHANGED
@@ -1,4 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in lithium.gemspec
3
+
4
+
5
+
6
+
7
+
8
+
4
9
  gemspec
data/README.md CHANGED
@@ -1,29 +1,42 @@
1
- # Lithium
1
+ # Lithium.
2
+ Small web framework.
2
3
 
3
- TODO: Write a gem description
4
+ ```ruby
5
+ # lithium_test.rb
4
6
 
5
- ## Installation
7
+ require 'lithium'
8
+ class App < Lithium::Base
9
+ get '/' do
10
+ 'Hello world!'
11
+ end
12
+ end
13
+ ```
6
14
 
7
- Add this line to your application's Gemfile:
15
+ ```ruby
16
+ # config.ru
8
17
 
9
- gem 'lithium'
18
+ require './app'
10
19
 
11
- And then execute:
20
+ run FirstApp
12
21
 
13
- $ bundle
22
+ ```
14
23
 
15
- Or install it yourself as:
16
24
 
17
- $ gem install lithium
25
+ Run the file:
18
26
 
19
- ## Usage
27
+ ```bash
28
+ rackup
29
+ ```
30
+ Install the gem:
20
31
 
21
- TODO: Write usage instructions here
32
+ ```bash
33
+ gem install lithium
34
+ ```
22
35
 
23
- ## Contributing
36
+ # Contributing
24
37
 
25
- 1. Fork it ( https://github.com/[my-github-username]/lithium/fork )
38
+ 1. Fork it
26
39
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
40
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
41
  4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create a new Pull Request
42
+ 5. Create new Pull Request
@@ -1,3 +1,6 @@
1
- require "lithium/version"
2
- require "lithium/help"
3
- require "server/server"
1
+ # Modules
2
+ require 'lithium/modules/templates/slim'
3
+ require 'lithium/modules/templates/erb'
4
+
5
+ require 'lithium/base'
6
+ require 'lithium/version'
@@ -0,0 +1,150 @@
1
+ require 'forwardable'
2
+
3
+ module Lithium
4
+
5
+ class Request < Rack::Request
6
+ def initialize(env)
7
+ env['PATH_INFO'] = '/' if env['PATH_INFO'].empty?
8
+ super
9
+ end
10
+ end
11
+
12
+
13
+ class Response
14
+ attr_accessor :status
15
+ attr_reader :headers, :body
16
+ extend Forwardable
17
+ def_delegators :headers, :[], :[]=
18
+
19
+ def initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' })
20
+ @body, @headers, @status = [], headers, status
21
+ @length = 0
22
+
23
+ if body.respond_to? :to_str
24
+ write body.to_str
25
+ elsif body.respond_to? :each
26
+ body.each { |i| write i.to_s }
27
+ else
28
+ raise TypeError, 'body must #respond_to? #to_str or #each'
29
+ end
30
+ end
31
+
32
+ def finish
33
+ unless (100..199).include?(status) || status == 204
34
+ headers['Content-Length'] = @length.to_s
35
+ end
36
+ [status, headers, body]
37
+ end
38
+
39
+ def redirect(target, status = 302)
40
+ self.status = status
41
+ headers['Location'] = target
42
+ end
43
+
44
+ def write(string)
45
+ s = string.to_s
46
+ @length += s.bytesize
47
+
48
+ body << s
49
+ end
50
+ end
51
+
52
+
53
+ class Base
54
+ class << self
55
+ extend Forwardable
56
+
57
+ def_delegators :stack, :map, :use
58
+
59
+ %w(DELETE GET HEAD OPTIONS PATCH POST PUT).each do |verb|
60
+ define_method(verb.downcase) { |path, &block| routes[verb] << compile_route(path, &block) }
61
+ end
62
+
63
+ %w(before after).each do |filter|
64
+ define_method(filter) do |&block|
65
+ filters[filter.to_sym] = block
66
+ end
67
+ end
68
+
69
+ alias :_new :new
70
+ def new(*args, &block)
71
+ stack.run _new(*args, &block)
72
+ stack
73
+ end
74
+
75
+ def call(env)
76
+ new.call env
77
+ end
78
+
79
+ def routes
80
+ @routes ||= Hash.new { |hash, key| hash[key] = [] }
81
+ end
82
+
83
+ def filters
84
+ @filters ||= Hash.new
85
+ end
86
+
87
+ def stack
88
+ @stack ||= Rack::Builder.new
89
+ end
90
+
91
+ private
92
+
93
+ def compile_route(path, &block)
94
+ route = { block: block, pattern: nil, extra_params: [], path: path }
95
+
96
+ pattern = path.gsub(/:\w+/) do |match|
97
+ route[:extra_params] << match.gsub(':', '').to_sym
98
+ "([^/?#]+)"
99
+ end
100
+ route[:pattern] = /^#{pattern}$/
101
+ route
102
+ end
103
+ end
104
+
105
+ attr_reader :env, :request, :response
106
+
107
+ def call(env)
108
+ dup.call env
109
+ end
110
+
111
+ def call(env)
112
+ @env = env
113
+ @request = Lithium::Request.new @env
114
+ @response = Lithium::Response.new
115
+ catch(:halt) { run_route }
116
+ end
117
+
118
+ def halt(response)
119
+ throw :halt, response
120
+ end
121
+
122
+ def session
123
+ request.env["rack.session"] || raise("Rack::Session handler is missing")
124
+ end
125
+
126
+ private
127
+
128
+ def run_route
129
+ route = self.class.routes[request.request_method].detect do |r|
130
+ r[:pattern] =~ request.path_info
131
+ end
132
+ if route
133
+ $~.captures.each_with_index do |value, index|
134
+ param = route[:extra_params][index]
135
+ request.params[param] = value
136
+ end
137
+ end
138
+
139
+ if route
140
+ instance_exec(&self.class.filters[:before]) if self.class.filters[:before]
141
+ response.write instance_eval(&route[:block])
142
+ instance_exec(&self.class.filters[:after]) if self.class.filters[:after]
143
+ else
144
+ response.status = 404
145
+ end
146
+
147
+ response.finish
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,11 @@
1
+ module Lithium
2
+ module Modules
3
+ module Templates
4
+ module Erb
5
+ def render template
6
+ ERB.new(File.open(template).read).result(binding)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ require 'tilt'
2
+ require 'slim'
3
+ module Lithium
4
+ module Modules
5
+ module Templates
6
+ module Slim
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[:templates_cache] ||= Tilt::Cache.new
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Lithium
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,101 +1,72 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lithium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - Sedat ÇİFTÇİ
7
+ - Sedat CIFTCI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-24 00:00:00.000000000 Z
11
+ date: 2015-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
18
- - !ruby/object:Gem::Version
19
- version: '1.6'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ~>
25
- - !ruby/object:Gem::Version
26
- version: '1.6'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - '>='
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - '>='
17
+ - - ">="
39
18
  - !ruby/object:Gem::Version
40
19
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: Dysnomia
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ~>
46
- - !ruby/object:Gem::Version
47
- version: 0.0.2
48
20
  type: :runtime
49
21
  prerelease: false
50
22
  version_requirements: !ruby/object:Gem::Requirement
51
23
  requirements:
52
- - - ~>
24
+ - - ">="
53
25
  - !ruby/object:Gem::Version
54
- version: 0.0.2
26
+ version: '0'
55
27
  - !ruby/object:Gem::Dependency
56
- name: em-websocket
28
+ name: tilt
57
29
  requirement: !ruby/object:Gem::Requirement
58
30
  requirements:
59
- - - ~>
31
+ - - ">="
60
32
  - !ruby/object:Gem::Version
61
- version: 0.5.1
33
+ version: '0'
62
34
  type: :runtime
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
- - - ~>
38
+ - - ">="
67
39
  - !ruby/object:Gem::Version
68
- version: 0.5.1
40
+ version: '0'
69
41
  - !ruby/object:Gem::Dependency
70
- name: colorize
42
+ name: slim
71
43
  requirement: !ruby/object:Gem::Requirement
72
44
  requirements:
73
- - - ~>
45
+ - - ">="
74
46
  - !ruby/object:Gem::Version
75
- version: 0.7.3
47
+ version: '0'
76
48
  type: :runtime
77
49
  prerelease: false
78
50
  version_requirements: !ruby/object:Gem::Requirement
79
51
  requirements:
80
- - - ~>
52
+ - - ">="
81
53
  - !ruby/object:Gem::Version
82
- version: 0.7.3
83
- description: Server Statistics
54
+ version: '0'
55
+ description: Ruby minimalistic microframework.
84
56
  email:
85
57
  - me@sedat.us
86
58
  executables: []
87
59
  extensions: []
88
60
  extra_rdoc_files: []
89
61
  files:
90
- - .gitignore
91
62
  - Gemfile
92
- - LICENSE.txt
93
63
  - README.md
94
- - Rakefile
95
64
  - lib/lithium.rb
65
+ - lib/lithium/base.rb
66
+ - lib/lithium/modules/templates/erb.rb
67
+ - lib/lithium/modules/templates/slim.rb
96
68
  - lib/lithium/version.rb
97
- - lithium.gemspec
98
- homepage: ''
69
+ homepage: https://github.com/amerobin/lithium
99
70
  licenses:
100
71
  - MIT
101
72
  metadata: {}
@@ -105,18 +76,18 @@ require_paths:
105
76
  - lib
106
77
  required_ruby_version: !ruby/object:Gem::Requirement
107
78
  requirements:
108
- - - '>='
79
+ - - ">="
109
80
  - !ruby/object:Gem::Version
110
81
  version: '0'
111
82
  required_rubygems_version: !ruby/object:Gem::Requirement
112
83
  requirements:
113
- - - '>='
84
+ - - ">="
114
85
  - !ruby/object:Gem::Version
115
86
  version: '0'
116
87
  requirements: []
117
88
  rubyforge_project:
118
- rubygems_version: 2.0.14
89
+ rubygems_version: 2.2.2
119
90
  signing_key:
120
91
  specification_version: 4
121
- summary: Server Statistics
92
+ summary: Ruby minimalistic microframework.
122
93
  test_files: []
data/.gitignore DELETED
@@ -1,22 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
18
- *.bundle
19
- *.so
20
- *.o
21
- *.a
22
- mkmf.log
@@ -1,22 +0,0 @@
1
- Copyright (c) 2014 TODO: Write your name
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require "bundler/gem_tasks"
2
-
@@ -1,27 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'lithium/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "lithium"
8
- spec.version = Lithium::VERSION
9
- spec.authors = ["Sedat ÇİFTÇİ"]
10
- spec.email = ["me@sedat.us"]
11
- spec.summary = %q{Server Statistics}
12
- spec.description = %q{Server Statistics}
13
- spec.homepage = ""
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files -z`.split("\x0")
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake"
23
-
24
- spec.add_runtime_dependency 'Dysnomia', '~> 0.0.2'
25
- spec.add_runtime_dependency 'em-websocket', '~> 0.5.1'
26
- spec.add_runtime_dependency 'colorize', '~> 0.7.3'
27
- end