bigsword 0.1.4

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: 6aba013112da55b8ccf876015f8071dcd643689a
4
+ data.tar.gz: e72428bb11dc93543055faaac4575c04d039879e
5
+ SHA512:
6
+ metadata.gz: 2c87a9d28fa611acab6327ac9837695e763798faf20aeb757d88036dfcbdbbece3e913815c983ad7d5fd58a296d215de06f4a8f0f6f604440aa99f53685a1788
7
+ data.tar.gz: 5db589aff32c16c7b42e6a8bfb26f6dcb60a60f7f004dc8c316f671eb6199ed042ab1c0a319a01744e7354574ac04ac4386095702be589a70450cca695c19499
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Patricio Mac Adden
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/README.md ADDED
@@ -0,0 +1,70 @@
1
+ BIG SWORD
2
+ ===
3
+
4
+ ![](https://raw.githubusercontent.com/Wuyue/bigsword/master/img/BigSword_Normal.png)
5
+
6
+
7
+ 基于rack的,微型web api开发框架。参考[hobbit](https://github.com/patriciomacadden/hobbit)和[sinatra](https://github.com/bmizerany/sinatra)。
8
+
9
+ A microframework for web api built on rack, reference to [hobbit](https://github.com/patriciomacadden/hobbit) and [sinatra](https://github.com/bmizerany/sinatra).
10
+
11
+
12
+ ## 安装Installation
13
+
14
+ 在Gemfile中添加
15
+
16
+ Add this line to your application's Gemfile:
17
+ ```ruby
18
+ gem 'bigsword'
19
+ ```
20
+ 然后执行
21
+
22
+ And then execute:
23
+
24
+ ```bash
25
+ $ bundle install
26
+ ```
27
+ 或者,你可以手动安装
28
+
29
+ Or install it yourself as:
30
+ ```bash
31
+ $ gem install bigsword
32
+ ```
33
+
34
+ ## 使用Usage
35
+
36
+ *Hello World example*
37
+
38
+ 新建一个app.rb文件
39
+
40
+ Create a file called app.rb:
41
+ ```ruby
42
+ require "bigsword"
43
+
44
+ get "/hello" do
45
+ "hello bigsword!"
46
+ end
47
+ ```
48
+
49
+ 创建config.ru文件
50
+
51
+ Create a config.ru file:
52
+ ```ruby
53
+ require './app'
54
+ run Proc.new { |env| _call env }
55
+ ```
56
+
57
+ 执行
58
+
59
+ run:
60
+ ```bash
61
+ $ rackup
62
+ ```
63
+
64
+ 打开浏览器,输入网址[http://localhost:9292/hello](http://localhost:9292/hello)。
65
+
66
+ View your app at [http://localhost:9292/hello](http://localhost:9292/hello).
67
+
68
+ ## License
69
+
70
+ See the [LICENSE](https://github.com/Wuyue/bigsword/blob/master/LICENSE).
data/bigsword.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bigsword/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'bigsword'
8
+ spec.version = BigSword::VERSION
9
+ spec.authors = ['Wu Yue, Zhu Fujiun']
10
+ spec.email = ['wuyueit@gmail.com','549978074@qq.com']
11
+ spec.description = %q{A microframework for web api built on rack}
12
+ spec.summary = %q{A microframework for web api built on rack}
13
+ spec.homepage = 'https://github.com/wuyue/bigsword'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.require_paths = ['lib']
18
+ end
19
+
Binary file
@@ -0,0 +1,99 @@
1
+ require 'forwardable'
2
+
3
+ def before; end
4
+ def after; end
5
+
6
+ def convert_to_lambda &block
7
+ obj = Object.new
8
+ obj.define_singleton_method(:_, &block)
9
+ return obj.method(:_).to_proc
10
+ end
11
+
12
+ %w(DELETE GET HEAD OPTIONS PATCH POST PUT).each do |verb|
13
+ define_method(verb.downcase) do |path, &block|
14
+ routes[verb] << compile_route(path, &block)
15
+ end
16
+ end
17
+
18
+ def routes
19
+ $routes ||= Hash.new { |hash, key| hash[key] = [] }
20
+ end
21
+
22
+ %w(BEFORE AFTER).each do |verb|
23
+ define_method(verb.downcase) do |&block|
24
+ filters[verb.downcase.to_sym] << {block: block}
25
+ end
26
+ end
27
+
28
+ def filters
29
+ $filters ||= Hash.new { |hash, key| hash[key] = [] }
30
+ end
31
+
32
+ def compile_route(path, &block)
33
+ route = {block: block, compiled_path: nil, extra_params: [], path: path}
34
+
35
+ compiled_path = path.gsub(/:\w+/) do |match|
36
+ route[:extra_params] << match.gsub(':', '').to_sym
37
+ '([^/?#]+)'
38
+ end
39
+
40
+ route[:compiled_path] = /^#{compiled_path}$/
41
+ route
42
+ end
43
+
44
+ def _call(env)
45
+ $env = env
46
+ $request = Request.new $env
47
+ $response = Response.new
48
+ catch(:halt) { route_eval }
49
+ end
50
+
51
+ def halt(response)
52
+ throw :halt, response
53
+ end
54
+
55
+ def filters_eval(type)
56
+ filters[type].map { |filter| convert_to_lambda(&filter[:block]).call}
57
+ end
58
+
59
+ def route_eval
60
+ filters_eval(:before)
61
+ route = find_route
62
+
63
+ if route
64
+ proc = convert_to_lambda &route[:block]
65
+ $response.body = proc.call
66
+ # $response.write instance_eval(&route[:block])
67
+ else
68
+ $response.status = 404
69
+ end
70
+ filters_eval(:after)
71
+ $response.finish
72
+ end
73
+
74
+ def find_route
75
+ route = routes[$request.request_method].detect do |r|
76
+ r[:compiled_path] =~ $request.path_info
77
+ end
78
+
79
+ if route
80
+ $~.captures.each_with_index do |value, index|
81
+ param = route[:extra_params][index]
82
+ $request.params[param] = value
83
+ end
84
+ end
85
+
86
+ route
87
+ end
88
+
89
+ def params
90
+ $request.params.deep_symbolize_keys!
91
+ end
92
+
93
+ def response
94
+ $response
95
+ end
96
+
97
+ def env
98
+ $env
99
+ end
@@ -0,0 +1,8 @@
1
+ require 'rack/request'
2
+
3
+ class Request < Rack::Request
4
+ def initialize(env)
5
+ env['PATH_INFO'] = '/' if env['PATH_INFO'].empty?
6
+ super
7
+ end
8
+ end
@@ -0,0 +1,39 @@
1
+ require 'forwardable'
2
+ require 'json'
3
+
4
+ class Response
5
+ attr_accessor :status
6
+ attr_reader :headers, :body
7
+ extend Forwardable
8
+ def_delegators :headers
9
+
10
+ def initialize(body = [], status = 200, headers = {'Content-Type' => 'application/json; charset=utf-8'})
11
+ @body, @headers, @status = [], headers, status
12
+ # @length = 0
13
+
14
+ if body.respond_to? :to_str
15
+ write body.to_str
16
+ elsif body.respond_to? :each
17
+ body.each { |i| write i.to_s }
18
+ else
19
+ raise TypeError, 'body must #respond_to? #to_str or #each'
20
+ end
21
+ end
22
+
23
+ def finish
24
+ result = body.first.to_json
25
+
26
+ headers['Content-Length'] = result.bytesize.to_s
27
+ [status, headers, [result]]
28
+ end
29
+
30
+ def redirect(target, status = 302)
31
+ self.status = status
32
+ headers['Location'] = target
33
+ end
34
+
35
+ def body=(value)
36
+ value.class == Array or value = [value]
37
+ @body = value
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module BigSword
2
+ VERSION = '0.1.4'
3
+ end
data/lib/bigsword.rb ADDED
@@ -0,0 +1,4 @@
1
+ require File.expand_path('../bigsword/base', __FILE__)
2
+ require File.expand_path('../bigsword/request', __FILE__)
3
+ require File.expand_path('../bigsword/response', __FILE__)
4
+ require 'bigsword/version'
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bigsword
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Wu Yue, Zhu Fujiun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A microframework for web api built on rack
14
+ email:
15
+ - wuyueit@gmail.com
16
+ - 549978074@qq.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - bigsword.gemspec
26
+ - img/BigSword_Normal.png
27
+ - lib/bigsword.rb
28
+ - lib/bigsword/base.rb
29
+ - lib/bigsword/request.rb
30
+ - lib/bigsword/response.rb
31
+ - lib/bigsword/version.rb
32
+ homepage: https://github.com/wuyue/bigsword
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.2.2
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: A microframework for web api built on rack
56
+ test_files: []