tsurezure 0.0.3 → 0.0.31

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tsurezure.rb +18 -0
  3. data/readme.md +150 -149
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea5a7cacb68e7a40425ab3e5bd34441a9c332011af18eb29c17d1b4efb558c57
4
- data.tar.gz: 64375f61e8060485b344f9b25bb5e2f5287f974b0e5accad0d6f2f08a37db621
3
+ metadata.gz: a3f470685236ef86ac858933ef4c913a99f21a4e0d42572971518781056c68f1
4
+ data.tar.gz: b6aede8db5d91dc1be40ddca3fc5011e1494084eb5e38f7df30edd8a426a316d
5
5
  SHA512:
6
- metadata.gz: 30f7604ff09e33af954dfba8ce4ff1d42ddcee8fe7d876060045b8519587467d0dab1ae907b3d911426f3195dceca4299c57bd27ee0f8556278675b415228b2b
7
- data.tar.gz: 81f2b015395092371bb8452169ec4231987788bc28feb9a41c7f826bd3b0fa6a4a67ad52ef6174f7ceeae3e5e21ca088137e9d173da71f001d04492dff848533
6
+ metadata.gz: 73dc499f770f090d7c5ad90ebfd3cf59922c105e84bb4a65713694b0d802c79ecc1e1a36191de3dd6e90984df689319bc4d3cdfb9e89de3e87891448bf543c91
7
+ data.tar.gz: 4e9f7c5e21fa6b3f9d422c21026a9bf07a1c02269fad26c1a16f36f155bb8a17f48c045a22b637fdd3ed31e6f0dde8b4b70d11449c0ff6b0fc7a0eadd8cd091f
@@ -14,6 +14,9 @@ $TRZR_PROCESS_MODE = nil
14
14
  $TRZR_LOG = true
15
15
  TRZR_STARTED_AT = Time.now.to_i
16
16
 
17
+ INVALID_RESPONSE_FORMAT = "if responding from a middleware, \
18
+ you must return a hash that includes a :message property."
19
+
17
20
  ARGV.each do |arg|
18
21
  $TRZR_PROCESS_MODE = 'development' if arg == '--development'
19
22
  $TRZR_PROCESS_MODE = 'production' if arg == '--production'
@@ -103,9 +106,24 @@ class Tsurezure
103
106
  request
104
107
  end
105
108
 
109
+ def respond_with_error(error)
110
+ Logbook::Dev.log(error)
111
+
112
+ message = { error: error }.to_json
113
+
114
+ responder = HTTPUtils::ServerResponse.new(
115
+ @session,
116
+ message.bytesize
117
+ )
118
+
119
+ responder.respond message, {}, 500, 'application/json'
120
+ end
121
+
106
122
  def send_middleware_response(req, resp, type)
107
123
  res = resp.merge req
108
124
 
125
+ return respond_with_error INVALID_RESPONSE_FORMAT if res[:message].nil?
126
+
109
127
  responder = HTTPUtils::ServerResponse.new(
110
128
  @session,
111
129
  res[:message].bytesize
data/readme.md CHANGED
@@ -1,149 +1,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
-
34
- ### actually using tsurezure:
35
-
36
- as for how to use tsurezure, here's a simple script to get started:
37
-
38
- ```ruby
39
- require 'tsurezure'
40
-
41
- # create an instance of tsurezure
42
- server = Tsurezure.new(8888)
43
-
44
- # url: http://localhost:8888/user/1
45
-
46
- # create an endpoint
47
- server.register 'get', '/user/:id', lambda { |req|
48
- url_vars = req[:vars] # { "id" => "1" }
49
- params = req[:params] # {}
50
-
51
- # create a respsonse for the endpoint
52
- {
53
- status: 200,
54
- message: {
55
- message: "hello user ##{url_vars['id']}!"
56
- }.to_json
57
- }
58
- }, content_type: 'application/json' # options hash
59
-
60
- # throw in some middleware
61
- server.add_middleware '/user/:id', lambda { |req|
62
- url_vars = req[:vars]
63
-
64
- # show a different response based on the request itself.
65
- # if you return from middleware, the return value will
66
- # be sent as the final response.
67
- if req[:vars]['id'] == '1'
68
- return {
69
- status: 200, message: {
70
- message: "hey user #1! you're the first one here!"
71
- }.to_json
72
- }
73
-
74
- end
75
- }, content_type: 'application/json'
76
-
77
- #listen for connections
78
- server.listen
79
- ```
80
-
81
- 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:
82
-
83
- ```json
84
- {
85
- "message": "hey user #1! you're the first one here!"
86
- }
87
- ```
88
-
89
- 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:
90
-
91
- ```ruby
92
- {
93
- port, # port that tsurezure is running on
94
- endpoints, # endpoints object containing the endpoints you've added
95
- middleware # middleware object containing the middleware you've added
96
- }
97
- ```
98
-
99
- simple example of usage:
100
-
101
- ```ruby
102
- server.listen lambda { |opts|
103
- puts "listening on port #{opts[:port]}!"
104
- }
105
- ```
106
-
107
- the registration function for creating endpoints is very simple:
108
-
109
- ```ruby
110
- register http_method, path, callback, options
111
- ```
112
-
113
- `http_method` is the method to access the endpoint with. `path` is just the url.
114
-
115
- `path` can be a path that contains variables (such as `/user/:id`). see the example above to see how it works.
116
-
117
- `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.
118
-
119
- `options` is a hash containing various options to somehow modify the response. valid options:
120
-
121
- - `content_type (default: text/plain)` - determines the mime type of the response
122
- - `location` - if a location header is required (301, etc), this is used to provide it.
123
- - `method` - if an allow header is required (405), this is used to provide it.
124
-
125
- for middleware, it's much the same:
126
-
127
- ```ruby
128
- add_middleware path, callback, options
129
- ```
130
-
131
- `path` can be a path that contains variables. used in the same way as the `path` for endpoints.
132
-
133
- `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.
134
-
135
- `options` for middleware are the same as the `options` for endpoints.
136
-
137
- * * *
138
-
139
- ## todo
140
-
141
- - [ ] make it so registered uris can only be accessed with the specified method, and everything else returns a 405 (maybe make this an option??)
142
-
143
- - [ ] give the user an option to add middleware specifically for catching errors
144
-
145
- ## misc
146
-
147
- disclaimer: I don't know ruby, and this is my first time using it to make something.
148
-
149
- the name comes from yukueshirezutsurezure, one of my favorite bands. it's pronounced 'tsɯ-ɾe-dzɯ-ɾe.'
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/user/1
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
+ ## todo
141
+
142
+ - [ ] make it so registered uris can only be accessed with the specified method, and everything else returns a 405 (maybe make this an option??)
143
+
144
+ - [ ] give the user an option to add middleware specifically for catching errors
145
+
146
+ ## misc
147
+
148
+ disclaimer: I don't know ruby, and this is my first time using it to make something.
149
+
150
+ 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.3
4
+ version: 0.0.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - jpegzilla