tsurezure 0.0.31 → 0.0.32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/readme.md +152 -150
- metadata +3 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cd3e3004d9cab6a40d5eb0c2e0e522ef3423f04e4ea7a8d476e2599352bf3fd
|
4
|
+
data.tar.gz: 7a7142acec9a8fb170e15eaca64e18a3a6ddcd319b63978cefb787105a2d38b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3b55a314479b66ca54718a3f739ee574edf8cea0b02b59645c2f5f84063a59da7a15549130913f71bc8ae0ec5503fda63ee8b24d12727dd5229212b022501e8
|
7
|
+
data.tar.gz: ea4dae6aef7898ab8e6decab4720a4c825f1a124295ea8751225a85c70d513605b3a824d5403354ee504ba40b1d33157a6757c7808d479b4496d5e2062c6b5e4
|
data/readme.md
CHANGED
@@ -1,150 +1,152 @@
|
|
1
|
-
# tsurezure
|
2
|
-
|
3
|
-
this is a simple web server framework written in ruby. mainly made as a way for me to quickly put together rest apis in my favorite language.
|
4
|
-
|
5
|
-
it can be used in a very similar manner to the javascript framework express.
|
6
|
-
|
7
|
-
* * *
|
8
|
-
|
9
|
-
## usage
|
10
|
-
|
11
|
-
### installing (from rubygems)
|
12
|
-
|
13
|
-
just run `gem install tsurezure` and you'll have whatever the latest version is that I've put up.
|
14
|
-
|
15
|
-
### installing (from source):
|
16
|
-
|
17
|
-
requires:
|
18
|
-
|
19
|
-
- ruby
|
20
|
-
- nodejs + nodemon (**only** for hot reloading server in development mode, not necessarily required)
|
21
|
-
|
22
|
-
after cloning this repo, from the root project directory, just run `rake start` to start in production mode, or `rake dev` to run in development mode, which adds hot reloading with nodemon. gem dependencies will install automatically.
|
23
|
-
|
24
|
-
to build the gem: run `gem build tsurezure.gemspec`. then, install using `gem install tsurezure-version-number`. `version-number` is whatever version is installed based on the `.gemspec` file.
|
25
|
-
|
26
|
-
### commands
|
27
|
-
|
28
|
-
- `rake install` will install dependencies
|
29
|
-
- `rake check_deps` will install dependencies if not installed
|
30
|
-
- `rake start` will run the server in production mode
|
31
|
-
- `rake dev` will run the server in development mode
|
32
|
-
- `rake dev_silent` will run the server in development mode with no logs
|
33
|
-
- `rake build` will build a `.gem` file based on `tsurezure.gemspec`
|
34
|
-
|
35
|
-
### actually using tsurezure:
|
36
|
-
|
37
|
-
as for how to use tsurezure, here's a simple script to get started:
|
38
|
-
|
39
|
-
```ruby
|
40
|
-
require 'tsurezure'
|
41
|
-
|
42
|
-
# create an instance of tsurezure
|
43
|
-
server = Tsurezure.new
|
44
|
-
|
45
|
-
# url: http://localhost:8888
|
46
|
-
|
47
|
-
# create an endpoint
|
48
|
-
server.register 'get', '/user/:id', lambda { |req|
|
49
|
-
url_vars = req[:vars] # { "id" => "1" }
|
50
|
-
params = req[:params] # {}
|
51
|
-
|
52
|
-
# create a respsonse for the endpoint
|
53
|
-
{
|
54
|
-
status: 200,
|
55
|
-
message: {
|
56
|
-
message: "hello user ##{url_vars['id']}!"
|
57
|
-
}.to_json
|
58
|
-
}
|
59
|
-
}, content_type: 'application/json' # options hash
|
60
|
-
|
61
|
-
# throw in some middleware
|
62
|
-
server.add_middleware '/user/:id', lambda { |req|
|
63
|
-
url_vars = req[:vars]
|
64
|
-
|
65
|
-
# show a different response based on the request itself.
|
66
|
-
# if you return from middleware, the return value will
|
67
|
-
# be sent as the final response.
|
68
|
-
if req[:vars]['id'] == '1'
|
69
|
-
return {
|
70
|
-
status: 200, message: {
|
71
|
-
message: "hey user #1! you're the first one here!"
|
72
|
-
}.to_json
|
73
|
-
}
|
74
|
-
|
75
|
-
end
|
76
|
-
}, content_type: 'application/json'
|
77
|
-
|
78
|
-
#listen for connections
|
79
|
-
server.listen
|
80
|
-
```
|
81
|
-
|
82
|
-
after you run this file, open up your browser or whatever and go to `http://localhost:8888/user/1`. you should see a json response that looks like this:
|
83
|
-
|
84
|
-
```json
|
85
|
-
{
|
86
|
-
"message": "hey user #1! you're the first one here!"
|
87
|
-
}
|
88
|
-
```
|
89
|
-
|
90
|
-
the `listen` method can be called with no arguments to just start the server. you can also pass in a lambda or proc that will run when the server has started. the only argument that will be passed to that proc is a hash called `server_opts`. it contains some information about the current configuration:
|
91
|
-
|
92
|
-
```ruby
|
93
|
-
{
|
94
|
-
port, # port that tsurezure is running on
|
95
|
-
endpoints, # endpoints object containing the endpoints you've added
|
96
|
-
middleware # middleware object containing the middleware you've added
|
97
|
-
}
|
98
|
-
```
|
99
|
-
|
100
|
-
simple example of usage:
|
101
|
-
|
102
|
-
```ruby
|
103
|
-
server.listen lambda { |opts|
|
104
|
-
puts "listening on port #{opts[:port]}!"
|
105
|
-
}
|
106
|
-
```
|
107
|
-
|
108
|
-
the registration function for creating endpoints is very simple:
|
109
|
-
|
110
|
-
```ruby
|
111
|
-
register http_method, path, callback, options
|
112
|
-
```
|
113
|
-
|
114
|
-
`http_method` is the method to access the endpoint with. `path` is just the url.
|
115
|
-
|
116
|
-
`path` can be a path that contains variables (such as `/user/:id`). see the example above to see how it works.
|
117
|
-
|
118
|
-
`callback` is a lambda that contains the logic used to send a response. it will recieve one argument: the request that was sent to that endpoint. whatever is returned from the proc will be sent as the response from that endpoint.
|
119
|
-
|
120
|
-
`options` is a hash containing various options to somehow modify the response. valid options:
|
121
|
-
|
122
|
-
- `content_type (default: text/plain)` - determines the mime type of the response
|
123
|
-
- `location` - if a location header is required (301, etc), this is used to provide it.
|
124
|
-
- `method` - if an allow header is required (405), this is used to provide it.
|
125
|
-
|
126
|
-
for middleware, it's much the same:
|
127
|
-
|
128
|
-
```ruby
|
129
|
-
add_middleware path, callback, options
|
130
|
-
```
|
131
|
-
|
132
|
-
`path` can be a path that contains variables. used in the same way as the `path` for endpoints.
|
133
|
-
|
134
|
-
`callback` is a lambda that you can use to intercept and pre-process responses. if you return from a callback in middleware, then that return value will be sent as the final response.
|
135
|
-
|
136
|
-
`options` for middleware are the same as the `options` for endpoints.
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
- [ ]
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
1
|
+
# tsurezure
|
2
|
+
|
3
|
+
this is a simple web server framework written in ruby. mainly made as a way for me to quickly put together rest apis in my favorite language.
|
4
|
+
|
5
|
+
it can be used in a very similar manner to the javascript framework express.
|
6
|
+
|
7
|
+
* * *
|
8
|
+
|
9
|
+
## usage
|
10
|
+
|
11
|
+
### installing (from rubygems)
|
12
|
+
|
13
|
+
just run `gem install tsurezure` and you'll have whatever the latest version is that I've put up.
|
14
|
+
|
15
|
+
### installing (from source):
|
16
|
+
|
17
|
+
requires:
|
18
|
+
|
19
|
+
- ruby
|
20
|
+
- nodejs + nodemon (**only** for hot reloading server in development mode, not necessarily required)
|
21
|
+
|
22
|
+
after cloning this repo, from the root project directory, just run `rake start` to start in production mode, or `rake dev` to run in development mode, which adds hot reloading with nodemon. gem dependencies will install automatically.
|
23
|
+
|
24
|
+
to build the gem: run `gem build tsurezure.gemspec`. then, install using `gem install tsurezure-version-number`. `version-number` is whatever version is installed based on the `.gemspec` file.
|
25
|
+
|
26
|
+
### commands
|
27
|
+
|
28
|
+
- `rake install` will install dependencies
|
29
|
+
- `rake check_deps` will install dependencies if not installed
|
30
|
+
- `rake start` will run the server in production mode
|
31
|
+
- `rake dev` will run the server in development mode
|
32
|
+
- `rake dev_silent` will run the server in development mode with no logs
|
33
|
+
- `rake build` will build a `.gem` file based on `tsurezure.gemspec`
|
34
|
+
|
35
|
+
### actually using tsurezure:
|
36
|
+
|
37
|
+
as for how to use tsurezure, here's a simple script to get started:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require 'tsurezure'
|
41
|
+
|
42
|
+
# create an instance of tsurezure
|
43
|
+
server = Tsurezure.new 8888
|
44
|
+
|
45
|
+
# url: http://localhost:8888
|
46
|
+
|
47
|
+
# create an endpoint
|
48
|
+
server.register 'get', '/user/:id', lambda { |req|
|
49
|
+
url_vars = req[:vars] # { "id" => "1" }
|
50
|
+
params = req[:params] # {}
|
51
|
+
|
52
|
+
# create a respsonse for the endpoint
|
53
|
+
{
|
54
|
+
status: 200,
|
55
|
+
message: {
|
56
|
+
message: "hello user ##{url_vars['id']}!"
|
57
|
+
}.to_json
|
58
|
+
}
|
59
|
+
}, content_type: 'application/json' # options hash
|
60
|
+
|
61
|
+
# throw in some middleware
|
62
|
+
server.add_middleware '/user/:id', lambda { |req|
|
63
|
+
url_vars = req[:vars]
|
64
|
+
|
65
|
+
# show a different response based on the request itself.
|
66
|
+
# if you return from middleware, the return value will
|
67
|
+
# be sent as the final response.
|
68
|
+
if req[:vars]['id'] == '1'
|
69
|
+
return {
|
70
|
+
status: 200, message: {
|
71
|
+
message: "hey user #1! you're the first one here!"
|
72
|
+
}.to_json
|
73
|
+
}
|
74
|
+
|
75
|
+
end
|
76
|
+
}, content_type: 'application/json'
|
77
|
+
|
78
|
+
#listen for connections
|
79
|
+
server.listen
|
80
|
+
```
|
81
|
+
|
82
|
+
after you run this file, open up your browser or whatever and go to `http://localhost:8888/user/1`. you should see a json response that looks like this:
|
83
|
+
|
84
|
+
```json
|
85
|
+
{
|
86
|
+
"message": "hey user #1! you're the first one here!"
|
87
|
+
}
|
88
|
+
```
|
89
|
+
|
90
|
+
the `listen` method can be called with no arguments to just start the server. you can also pass in a lambda or proc that will run when the server has started. the only argument that will be passed to that proc is a hash called `server_opts`. it contains some information about the current configuration:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
{
|
94
|
+
port, # port that tsurezure is running on
|
95
|
+
endpoints, # endpoints object containing the endpoints you've added
|
96
|
+
middleware # middleware object containing the middleware you've added
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
simple example of usage:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
server.listen lambda { |opts|
|
104
|
+
puts "listening on port #{opts[:port]}!"
|
105
|
+
}
|
106
|
+
```
|
107
|
+
|
108
|
+
the registration function for creating endpoints is very simple:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
register http_method, path, callback, options
|
112
|
+
```
|
113
|
+
|
114
|
+
`http_method` is the method to access the endpoint with. `path` is just the url.
|
115
|
+
|
116
|
+
`path` can be a path that contains variables (such as `/user/:id`). see the example above to see how it works.
|
117
|
+
|
118
|
+
`callback` is a lambda that contains the logic used to send a response. it will recieve one argument: the request that was sent to that endpoint. whatever is returned from the proc will be sent as the response from that endpoint.
|
119
|
+
|
120
|
+
`options` is a hash containing various options to somehow modify the response. valid options:
|
121
|
+
|
122
|
+
- `content_type (default: text/plain)` - determines the mime type of the response
|
123
|
+
- `location` - if a location header is required (301, etc), this is used to provide it.
|
124
|
+
- `method` - if an allow header is required (405), this is used to provide it.
|
125
|
+
|
126
|
+
for middleware, it's much the same:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
add_middleware path, callback, options
|
130
|
+
```
|
131
|
+
|
132
|
+
`path` can be a path that contains variables. used in the same way as the `path` for endpoints.
|
133
|
+
|
134
|
+
`callback` is a lambda that you can use to intercept and pre-process responses. if you return from a callback in middleware, then that return value will be sent as the final response.
|
135
|
+
|
136
|
+
`options` for middleware are the same as the `options` for endpoints.
|
137
|
+
|
138
|
+
**anything** returned from a middleware will be interpreted as you trying to send a modified response. and of course, ruby will interpret the last reached statement in a method as an implicit return. to avoid this, if you want to have a middleware that doesn't necessarily send a response, just use a `return` at the end of your method to return `nil`.
|
139
|
+
|
140
|
+
* * *
|
141
|
+
|
142
|
+
## todo
|
143
|
+
|
144
|
+
- [ ] make it so registered uris can only be accessed with the specified method, and everything else returns a 405 (maybe make this an option??)
|
145
|
+
|
146
|
+
- [ ] give the user an option to add middleware specifically for catching errors
|
147
|
+
|
148
|
+
## misc
|
149
|
+
|
150
|
+
disclaimer: I don't know ruby, and this is my first time using it to make something.
|
151
|
+
|
152
|
+
the name comes from yukueshirezutsurezure, one of my favorite bands. it's pronounced 'tsɯ-ɾe-dzɯ-ɾe.'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tsurezure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.32
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jpegzilla
|
@@ -14,22 +14,16 @@ dependencies:
|
|
14
14
|
name: json
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.8'
|
20
17
|
- - ">="
|
21
18
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
19
|
+
version: 2.3.0
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
|
-
- - "~>"
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '1.8'
|
30
24
|
- - ">="
|
31
25
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
26
|
+
version: 2.3.0
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: pry
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|