qeweney 0.2 → 0.3

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: 468a536796430b202206a5b2e6fe1e05cdc2b9ff248735ceab4fa7ca458b812e
4
- data.tar.gz: 379a9bdb10b73d2a5fbce967ec10149fd0c36ce58819fe27da847a3e1194ae27
3
+ metadata.gz: 63226f0b1696e99fb7c6cb322420ddf08fa6b9a0abd3652a5867c9186e0dd5b2
4
+ data.tar.gz: abb173aa33639169d3341e553bcbc91a8595727fae03844daebc0cb82e51b881
5
5
  SHA512:
6
- metadata.gz: 17e66c2fbcf0b60c7cf8023ff7c0cda04e3ec3c527da5019838bf66ed454e10c4fada6b45e6ad56de479812a2e215e692fd566dc3167457e92ed8e88fc54de39
7
- data.tar.gz: d46cabcc28e1da7ba15f4603eff436a47198e1c8008e97a3d4c6fb6e7166fddb08f7bea13c537e521784b06c898c9f97d8c9af15677f31516f37a649740ce693
6
+ metadata.gz: 9d43894a867eaf901cb4cdecf0d907f9257ffcdf369dc89ad1528738f006c854d07bf9850e54fe2fef6e316576593fa8d829f13bc82b5a8ba700a2726daa861b
7
+ data.tar.gz: d5f6d8eb6b520200b0864defcf5ef16f53965de0bac7304ca0bdf685faadf3f7528b8fecf0ba9377a3338bffbd993b851eb9e76af22fdd5dd966d4348a5ee28b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.3 2021-02-12
2
+
3
+ - Implement `Request#serve_io`, `Request#serve_file`, `Request#redirect`
4
+ - Add basic MIME types
5
+ - Add HTTP status codes
6
+
1
7
  ## 0.2 2021-02-11
2
8
 
3
9
  - Gem renamed to Qeweney
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qeweney (0.2)
4
+ qeweney (0.3)
5
5
  escape_utils (~> 1.2.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # QNA
1
+ # Qeweney
2
2
 
3
3
  ## Cross-library feature rich HTTP request / response API
4
4
 
5
- QNA provides a uniform API for dealing with HTTP requests and responses.
5
+ Qeweney provides a uniform API for dealing with HTTP requests and responses.
6
6
 
7
7
  ## Features
8
8
 
data/TODO.md ADDED
@@ -0,0 +1,34 @@
1
+ ## Serve files / arbitrary content
2
+
3
+ - `#serve_file(path, opts)`
4
+ - See here: https://golang.org/pkg/net/http/#ServeFile
5
+ - support for `Range` header
6
+ - support for caching (specified in opts)
7
+ - support for serving from routing path:
8
+
9
+ ```ruby
10
+ req.route do
11
+ req.on 'assets' do
12
+ req.serve_file(req.routing_path, base_path: STATIC_PATH)
13
+ end
14
+ end
15
+
16
+ # or convenience method
17
+ req.route do
18
+ req.on_static_route 'assets', base_path: STATIC_PATH
19
+ end
20
+ ```
21
+
22
+ - `#serve_content(io, opts)`
23
+ - See here: https://golang.org/pkg/net/http/#ServeContent
24
+ - support for `Range` header
25
+ - support for caching (specified in opts)
26
+ - usage:
27
+
28
+ ```ruby
29
+ req.route do
30
+ req.on 'mypdf' do
31
+ File.open('my.pdf', 'r') { |f| req.serve_content(io) }
32
+ end
33
+ end
34
+ ```
data/lib/qeweney.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Qeweney
3
+ module Qeweney
4
4
  end
5
5
 
6
6
  require_relative 'qeweney/request.rb'
7
+ require_relative 'qeweney/status.rb'
8
+
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Qeweney
4
+ # File extension to MIME type mapping
5
+ module MimeTypes
6
+ TYPES = {
7
+ 'html' => 'text/html',
8
+ 'css' => 'text/css',
9
+ 'js' => 'application/javascript',
10
+ 'txt' => 'text/plain'
11
+
12
+ 'gif' => 'image/gif',
13
+ 'jpg' => 'image/jpeg',
14
+ 'jpeg' => 'image/jpeg',
15
+ 'png' => 'image/png',
16
+ 'ico' => 'image/x-icon',
17
+
18
+ 'pdf' => 'application/pdf',
19
+ 'json' => 'application/json',
20
+ }.freeze
21
+
22
+ def [](ref)
23
+ case ref
24
+ when Symbol
25
+ TYPES[ref.to_s]
26
+ when /\.?([^\.]+)$/
27
+ TYPES[Regexp.last_match(1)]
28
+ else
29
+ raise "Invalid argument #{ref.inspect}"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,9 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'status'
4
+
3
5
  module Qeweney
4
6
  module ResponseMethods
5
- def redirect(url)
6
- respond(nil, ':status' => 302, 'Location' => url)
7
+ def redirect(url, status = Status::FOUND)
8
+ respond(nil, ':status' => status, 'Location' => url)
9
+ end
10
+
11
+ def serve_file(path, opts)
12
+ File.open(file_full_path(path, opts), 'r') do |f|
13
+ serve_io(f, opts)
14
+ end
15
+ rescue Errno::ENOENT
16
+ respond(nil, ':status' => Status::NOT_FOUND)
17
+ end
18
+
19
+ def file_full_path(path, opts)
20
+ if (base_path = opts[:base_path])
21
+ File.join(opts[:base_path], path)
22
+ else
23
+ path
24
+ end
25
+ end
26
+
27
+ def serve_io(io, opts)
28
+ respond(io.read)
7
29
  end
8
30
  end
9
31
  end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Qeweney
4
+ # HTTP status codes
5
+ module Status
6
+ # translated from https://golang.org/pkg/net/http/#pkg-constants
7
+ # https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
8
+
9
+ CONTINUE = 100 # RFC 7231, 6.2.1
10
+ SWITCHING_PROTOCOLS = 101 # RFC 7231, 6.2.2
11
+ PROCESSING = 102 # RFC 2518, 10.1
12
+ EARLY_HINTS = 103 # RFC 8297
13
+
14
+ OK = 200 # RFC 7231, 6.3.1
15
+ CREATED = 201 # RFC 7231, 6.3.2
16
+ ACCEPTED = 202 # RFC 7231, 6.3.3
17
+ NON_AUTHORITATIVE_INFO = 203 # RFC 7231, 6.3.4
18
+ NO_CONTENT = 204 # RFC 7231, 6.3.5
19
+ RESET_CONTENT = 205 # RFC 7231, 6.3.6
20
+ PARTIAL_CONTENT = 206 # RFC 7233, 4.1
21
+ MULTI_STATUS = 207 # RFC 4918, 11.1
22
+ ALREADY_REPORTED = 208 # RFC 5842, 7.1
23
+ IM_USED = 226 # RFC 3229, 10.4.1
24
+
25
+ MULTIPLE_CHOICES = 300 # RFC 7231, 6.4.1
26
+ MOVED_PERMANENTLY = 301 # RFC 7231, 6.4.2
27
+ FOUND = 302 # RFC 7231, 6.4.3
28
+ SEE_OTHER = 303 # RFC 7231, 6.4.4
29
+ NOT_MODIFIED = 304 # RFC 7232, 4.1
30
+ USE_PROXY = 305 # RFC 7231, 6.4.5
31
+
32
+ TEMPORARY_REDIRECT = 307 # RFC 7231, 6.4.7
33
+ PERMANENT_REDIRECT = 308 # RFC 7538, 3
34
+
35
+ BAD_REQUEST = 400 # RFC 7231, 6.5.1
36
+ UNAUTHORIZED = 401 # RFC 7235, 3.1
37
+ PAYMENT_REQUIRED = 402 # RFC 7231, 6.5.2
38
+ FORBIDDEN = 403 # RFC 7231, 6.5.3
39
+ NOT_FOUND = 404 # RFC 7231, 6.5.4
40
+ METHOD_NOT_ALLOWED = 405 # RFC 7231, 6.5.5
41
+ NOT_ACCEPTABLE = 406 # RFC 7231, 6.5.6
42
+ PROXY_AUTH_REQUIRED = 407 # RFC 7235, 3.2
43
+ REQUEST_TIMEOUT = 408 # RFC 7231, 6.5.7
44
+ CONFLICT = 409 # RFC 7231, 6.5.8
45
+ GONE = 410 # RFC 7231, 6.5.9
46
+ LENGTH_REQUIRED = 411 # RFC 7231, 6.5.10
47
+ PRECONDITION_FAILED = 412 # RFC 7232, 4.2
48
+ REQUEST_ENTITY_TOO_LARGE = 413 # RFC 7231, 6.5.11
49
+ REQUEST_URI_TOO_LONG = 414 # RFC 7231, 6.5.12
50
+ UNSUPPORTED_MEDIA_TYPE = 415 # RFC 7231, 6.5.13
51
+ REQUESTED_RANGE_NOT_SATISFIABLE = 416 # RFC 7233, 4.4
52
+ EXPECTATION_FAILED = 417 # RFC 7231, 6.5.14
53
+ TEAPOT = 418 # RFC 7168, 2.3.3
54
+ MISDIRECTED_REQUEST = 421 # RFC 7540, 9.1.2
55
+ UNPROCESSABLE_ENTITY = 422 # RFC 4918, 11.2
56
+ LOCKED = 423 # RFC 4918, 11.3
57
+ FAILED_DEPENDENCY = 424 # RFC 4918, 11.4
58
+ TOO_EARLY = 425 # RFC 8470, 5.2.
59
+ UPGRADE_REQUIRED = 426 # RFC 7231, 6.5.15
60
+ PRECONDITION_REQUIRED = 428 # RFC 6585, 3
61
+ TOO_MANY_REQUESTS = 429 # RFC 6585, 4
62
+ REQUEST_HEADER_FIELDS_TOO_LARGE = 431 # RFC 6585, 5
63
+ UNAVAILABLE_FOR_LEGAL_REASONS = 451 # RFC 7725, 3
64
+
65
+ INTERNAL_SERVER_ERROR = 500 # RFC 7231, 6.6.1
66
+ NOT_IMPLEMENTED = 501 # RFC 7231, 6.6.2
67
+ BAD_GATEWAY = 502 # RFC 7231, 6.6.3
68
+ SERVICE_UNAVAILABLE = 503 # RFC 7231, 6.6.4
69
+ GATEWAY_TIMEOUT = 504 # RFC 7231, 6.6.5
70
+ HTTP_VERSION_NOT_SUPPORTED = 505 # RFC 7231, 6.6.6
71
+ VARIANT_ALSO_NEGOTIATES = 506 # RFC 2295, 8.1
72
+ INSUFFICIENT_STORAGE = 507 # RFC 4918, 11.5
73
+ LOOP_DETECTED = 508 # RFC 5842, 7.2
74
+ NOT_EXTENDED = 510 # RFC 2774, 7
75
+ NETWORK_AUTHENTICATION_REQUIRED = 511 # RFC 6585, 6
76
+ end
77
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Qeweney
4
- VERSION = '0.2'
4
+ VERSION = '0.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qeweney
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-11 00:00:00.000000000 Z
11
+ date: 2021-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils
@@ -81,11 +81,14 @@ files:
81
81
  - LICENSE
82
82
  - README.md
83
83
  - Rakefile
84
+ - TODO.md
84
85
  - lib/qeweney.rb
86
+ - lib/qeweney/mime_types.rb
85
87
  - lib/qeweney/request.rb
86
88
  - lib/qeweney/request_info.rb
87
89
  - lib/qeweney/response.rb
88
90
  - lib/qeweney/routing.rb
91
+ - lib/qeweney/status.rb
89
92
  - lib/qeweney/version.rb
90
93
  - qeweney.gemspec
91
94
  - test/helper.rb