tsurezure 0.0.1 → 0.0.33

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
  SHA256:
3
- metadata.gz: a04f09bb09e26d531a84b5738dd7719b2dee55ef2c73845bca3700c1a5f20c25
4
- data.tar.gz: 6b3c83431259f111dd024f00860dfa9e10a8caad5e123f51b7085ca6dbced9db
3
+ metadata.gz: 1278a0f85466408f80cdf8978acb8ebe6bd34faa4b5824d7e0fde9a323839454
4
+ data.tar.gz: 1d98b1c81f4a81955c5983c34aabf9390b953fee5778892a8f1705c573b6669b
5
5
  SHA512:
6
- metadata.gz: '038ed41337233b9e7debfaed3da1d2984f8d2e11b46e02ed175da71e12eab878433a4151cefa06fbf0b247dd19d4eff5acb2fb252b6951f0df0084397eae4c14'
7
- data.tar.gz: b9a87b64323fb67155ff68f24f3c390807663de010b39c57d2a73a5aa4559181994603bc41b11ba44af74bc3824b78e317368166a63a37e4c2091b2159b342fa
6
+ metadata.gz: 4174e3d1fb4fac4d64ff98e128df388a64ab0e95c41aa67e1e97e70dbaef24f7944b38a191b703218a62a5688cd923fc68e569e197b2937421cd7d37ab47735d
7
+ data.tar.gz: e707b8e7871b1b47dd6aca8cfee1953b1b3794b3d9c8b7cfc5d226096bb1648e7b95bab439ac9d142739a708eb73de1ca49d847d47c9ea9f0e3064fd8685d614
@@ -12,6 +12,10 @@ require_relative 'utils/response' # handles request and generates responses.
12
12
 
13
13
  $TRZR_PROCESS_MODE = nil
14
14
  $TRZR_LOG = true
15
+ TRZR_STARTED_AT = Time.now.to_i
16
+
17
+ INVALID_RESPONSE_FORMAT = "if responding from a middleware, \
18
+ you must return a hash that includes a :message property."
15
19
 
16
20
  ARGV.each do |arg|
17
21
  $TRZR_PROCESS_MODE = 'development' if arg == '--development'
@@ -85,7 +89,7 @@ class Tsurezure
85
89
 
86
90
  def get_correct_middleware(request_object)
87
91
  @middleware.keys.select do |pat|
88
- HTTPUtils::URLUtils.matches_url_regex(pat, request_object[:url]) ||
92
+ HTTPUtils::URLUtils.matches_url_regex?(pat, request_object[:url]) ||
89
93
  pat == '*'
90
94
  end
91
95
  end
@@ -102,16 +106,29 @@ class Tsurezure
102
106
  request
103
107
  end
104
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
+
105
122
  def send_middleware_response(req, resp, type)
106
123
  res = resp.merge req
107
124
 
125
+ return respond_with_error INVALID_RESPONSE_FORMAT if res[:message].nil?
126
+
108
127
  responder = HTTPUtils::ServerResponse.new(
109
128
  @session,
110
129
  res[:message].bytesize
111
130
  )
112
131
 
113
- # pp res
114
-
115
132
  responder.respond res[:message], res[:options] || {}, res[:status], type
116
133
  end
117
134
 
@@ -205,8 +222,14 @@ class Tsurezure
205
222
 
206
223
  ##
207
224
  # run when the server is prepared to accept requests.
208
- def listen
209
- puts "running on port #{@port}!"
225
+ def listen(callback = nil)
226
+ if $TRZR_PROCESS_MODE == 'development'
227
+ puts "[trzr_dev] running on port #{@port}!"
228
+ end
229
+
230
+ # call the callback if there's one provided
231
+ callback.call server_opts if callback.is_a? Proc
232
+
210
233
  # create a new thread for handle each incoming request
211
234
  loop do
212
235
  Thread.start(@server.accept) do |client|
@@ -215,8 +238,20 @@ class Tsurezure
215
238
  end
216
239
  end
217
240
 
241
+ def kill
242
+ abort
243
+ end
244
+
218
245
  private
219
246
 
247
+ def server_opts
248
+ {
249
+ port: @port,
250
+ endpoints: @endpoints,
251
+ middleware: @middleware
252
+ }
253
+ end
254
+
220
255
  # ----------------------------------------
221
256
  # :section: registration of endpoints and
222
257
  # all endpoint management methods follow.
@@ -272,10 +307,14 @@ class Tsurezure
272
307
  # add endpoint to list of registered endpoints
273
308
  @endpoints[method][endpoint[:path]] = endpoint
274
309
  end
310
+ end
275
311
 
276
- def kill
277
- abort
312
+ at_exit do
313
+ if $TRZR_PROCESS_MODE == 'development' && $TRZR_LOG == true
314
+ time = Time.now.to_i - TRZR_STARTED_AT
315
+ puts
316
+ puts '[trzr_dev] shutting down. goodbye...'
317
+ puts "[trzr_dev] shut down after #{Time.at(time).utc.strftime('%H:%M:%S')}."
318
+ puts
278
319
  end
279
320
  end
280
-
281
- at_exit { puts 'shutting down. goodbye...' }
@@ -25,6 +25,8 @@ module HTTPUtils
25
25
  end
26
26
 
27
27
  def self.url_path_matches?(url, path)
28
+ return true if url == path
29
+
28
30
  split_url = url.split '/'
29
31
  split_path = path.split '/'
30
32
 
@@ -49,7 +51,7 @@ module HTTPUtils
49
51
  hash_with_variables
50
52
  end
51
53
 
52
- def self.matches_url_regex(url, regex)
54
+ def self.matches_url_regex?(url, regex)
53
55
  return unless url_path_matches? url, regex
54
56
 
55
57
  matches = url.scan %r{((?<=\/):[^\/]+)}
@@ -2,6 +2,19 @@
2
2
 
3
3
  require_relative './http_utils' # mainly used to create http responses.
4
4
 
5
+ VALID_METHODS = %w[
6
+ CONNECT COPY DELETE GET HEAD
7
+ LINK LOCK MKCOL MOVE OPTIONS
8
+ PATCH POST PROPFIND PROPPATCH
9
+ PURGE PUT TRACE UNLINK UNLOCK
10
+ VIEW
11
+ ].freeze
12
+
13
+ CHECK_METHOD_WARNING = "not found. \
14
+ please ensure you're using the right method!"
15
+
16
+ INVALID_METHOD_WARNING = 'an invalid method was used!'
17
+
5
18
  ##
6
19
  # module for handling all incoming requests to the server
7
20
  # stands for TsurezureResponse
@@ -9,36 +22,24 @@ module TResponse
9
22
  include HTTPUtils
10
23
  # anything that will be needed to create responses
11
24
  class Utils
25
+ attr_reader :valid_methods
26
+
12
27
  def initialize
13
- @valid_methods = %w[
14
- CONNECT COPY DELETE GET HEAD
15
- LINK LOCK MKCOL MOVE OPTIONS
16
- OPTIONS PATCH POST PROPFIND
17
- PROPPATCH PURGE PUT TRACE
18
- UNLINK UNLOCK VIEW
19
- ]
28
+ @valid_methods = VALID_METHODS
20
29
  end
21
30
 
22
- attr_reader :valid_methods
23
-
24
31
  def self.validate_request(request_params)
25
32
  # make sure the user has provided a valid http
26
33
  # method, a valid uri, and a valid response /
27
34
  # response type
28
- valid_methods = %w[
29
- CONNECT COPY DELETE GET HEAD
30
- LINK LOCK MKCOL MOVE OPTIONS
31
- OPTIONS PATCH POST PROPFIND
32
- PROPPATCH PURGE PUT TRACE
33
- UNLINK UNLOCK VIEW
34
- ]
35
-
36
- return false unless valid_methods.include? request_params[:method]
35
+ return true if VALID_METHODS.include? request_params[:method]
36
+
37
+ Logbook::Dev.log(INVALID_METHOD_WARNING)
37
38
  end
38
39
 
39
40
  def self.get_correct_endpoint(request_object, endpoints)
40
41
  endpoints.keys.select do |pat|
41
- HTTPUtils::URLUtils.matches_url_regex(pat, request_object[:url])
42
+ HTTPUtils::URLUtils.matches_url_regex?(pat, request_object[:url])
42
43
  end
43
44
  end
44
45
 
@@ -61,8 +62,9 @@ module TResponse
61
62
  @endpoints = endpoints[request[:method]]
62
63
 
63
64
  # if no endpoint, respond with root endpoint or 404 middleware
64
-
65
65
  unless Utils.ensure_response(request, @endpoints) == true
66
+ Logbook::Dev.log(CHECK_METHOD_WARNING)
67
+
66
68
  return { options: { content_type: 'application/json' },
67
69
  code: 22, status: 404,
68
70
  message: { status: 404, message: 'undefined endpoint' }.to_json }
data/readme.md CHANGED
@@ -23,17 +23,26 @@ after cloning this repo, from the root project directory, just run `rake start`
23
23
 
24
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
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
+
26
35
  ### actually using tsurezure:
27
36
 
28
- as for how to use tsurezure, here's a simple hello world to get started:
37
+ as for how to use tsurezure, here's a simple script to get started:
29
38
 
30
39
  ```ruby
31
40
  require 'tsurezure'
32
41
 
33
42
  # create an instance of tsurezure
34
- server = Tsurezure.new(8888)
43
+ server = Tsurezure.new 8888
35
44
 
36
- # url: http://localhost:8888/user/1
45
+ # url: http://localhost:8888
37
46
 
38
47
  # create an endpoint
39
48
  server.register 'get', '/user/:id', lambda { |req|
@@ -78,6 +87,24 @@ after you run this file, open up your browser or whatever and go to `http://loca
78
87
  }
79
88
  ```
80
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
+
81
108
  the registration function for creating endpoints is very simple:
82
109
 
83
110
  ```ruby
@@ -92,7 +119,7 @@ register http_method, path, callback, options
92
119
 
93
120
  `options` is a hash containing various options to somehow modify the response. valid options:
94
121
 
95
- - `content_type` - determines the mime type of the response
122
+ - `content_type (default: text/plain)` - determines the mime type of the response
96
123
  - `location` - if a location header is required (301, etc), this is used to provide it.
97
124
  - `method` - if an allow header is required (405), this is used to provide it.
98
125
 
@@ -108,6 +135,8 @@ add_middleware path, callback, options
108
135
 
109
136
  `options` for middleware are the same as the `options` for endpoints.
110
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
+
111
140
  * * *
112
141
 
113
142
  ## todo
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.1
4
+ version: 0.0.33
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: 1.8.3
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: 1.8.3
26
+ version: 2.3.0
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: pry
35
29
  requirement: !ruby/object:Gem::Requirement