tsurezure 0.0.2 → 0.0.34

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: 1dd0633006bc7ba5837c37817e78d1f7a1fdc38520557c10a528fd40acff2573
4
- data.tar.gz: 420c2cc0bd0387c24cbe5d12b40aad574edd1e4e0f2a4bbf6b39eeb323ccdaec
3
+ metadata.gz: 818cbc747a3775c0d73c76bad34638238d99bdbb3363adf1bd1104e635e8ab3e
4
+ data.tar.gz: 00c69ab8c119c41aa6481460518b27b5b9d29b7405263473be83d0fdb2a3a0c9
5
5
  SHA512:
6
- metadata.gz: 358fbcfcd4504ea64c5afad8b80f5600a4bab693e48bcf536700315ce6766b54988bf4b25e8e6aeeb71facf7773515bdf0bb0bf4c2c1fc27687f984562af0ad1
7
- data.tar.gz: 6a581bcabeb9556e2649c6893b9cfdf7553e27726f86446e35feaaba48f19126b124f0019ad45090d095704102a00e3b5834908e3ab045c70c1d11913a021636
6
+ metadata.gz: a7c352282df3696c3e2458d0c883d416a5202d16d2df7394ea36989cd8b31985b5fad00040eed6b7771fd2294a4c888f713f70280205540b626016804afa488e
7
+ data.tar.gz: e2e008a906f18c39fddf09c64fd238d2fdf06b73ea9e2f89425d8a8d4fdaea91b86cd0dbc1083b10d3d95c390dcd7b14e5ef9893c2f59917919878bcab73a59f
@@ -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'
@@ -78,7 +81,7 @@ class Tsurezure
78
81
  # to initialize: session and length of response
79
82
  responder = HTTPUtils::ServerResponse.new(
80
83
  @session,
81
- res[:message].bytesize
84
+ res[:message].nil? ? '' : res[:message].bytesize
82
85
  )
83
86
 
84
87
  go_through_middleware request_object, responder, res, type
@@ -86,7 +89,7 @@ class Tsurezure
86
89
 
87
90
  def get_correct_middleware(request_object)
88
91
  @middleware.keys.select do |pat|
89
- HTTPUtils::URLUtils.matches_url_regex(pat, request_object[:url]) ||
92
+ HTTPUtils::URLUtils.matches_url_regex?(pat, request_object[:url]) ||
90
93
  pat == '*'
91
94
  end
92
95
  end
@@ -103,16 +106,29 @@ 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
112
130
  )
113
131
 
114
- # pp res
115
-
116
132
  responder.respond res[:message], res[:options] || {}, res[:status], type
117
133
  end
118
134
 
@@ -294,7 +310,7 @@ class Tsurezure
294
310
  end
295
311
 
296
312
  at_exit do
297
- if $TRZR_PROCESS_MODE == 'development' && $TRZR_LOG.true?
313
+ if $TRZR_PROCESS_MODE == 'development' && $TRZR_LOG == true
298
314
  time = Time.now.to_i - TRZR_STARTED_AT
299
315
  puts
300
316
  puts '[trzr_dev] 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
@@ -30,6 +30,7 @@ to build the gem: run `gem build tsurezure.gemspec`. then, install using `gem in
30
30
  - `rake start` will run the server in production mode
31
31
  - `rake dev` will run the server in development mode
32
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`
33
34
 
34
35
  ### actually using tsurezure:
35
36
 
@@ -39,9 +40,9 @@ as for how to use tsurezure, here's a simple script to get started:
39
40
  require 'tsurezure'
40
41
 
41
42
  # create an instance of tsurezure
42
- server = Tsurezure.new(8888)
43
+ server = Tsurezure.new 8888
43
44
 
44
- # url: http://localhost:8888/user/1
45
+ # url: http://localhost:8888
45
46
 
46
47
  # create an endpoint
47
48
  server.register 'get', '/user/:id', lambda { |req|
@@ -134,10 +135,14 @@ add_middleware path, callback, options
134
135
 
135
136
  `options` for middleware are the same as the `options` for endpoints.
136
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
+
137
140
  * * *
138
141
 
139
142
  ## todo
140
143
 
144
+ - [ ] I'm wondering if I should change the implementation of the middleware functionality. currently, the middleware has no idea where the request it's processing is about to go. I'm thinking it may be a good idea to pass in the request's determined `responder` function which is determined before middleware is called (see `tsurezure.rb:135-160`). this will allow users to choose whether to call the final responder function. I don't know if this would be better or just different.
145
+
141
146
  - [ ] 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
147
 
143
148
  - [ ] give the user an option to add middleware specifically for catching errors
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.2
4
+ version: 0.0.34
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