acrylic 0.2.0 → 0.2.1

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: 4a0411b437d94694ff9761a6f9466fa909d650eb933ecd5fc0b6438f66ff10fc
4
- data.tar.gz: c04dd9a1311e53216b18600f0847dc6004895ae826daf36cea728ede98b59c70
3
+ metadata.gz: f8251c55a20aa3b06422986f9b1cac67ab1fe1d9da8fbc15340b0ecad2fdfe22
4
+ data.tar.gz: 7671c90e53d8afdca28c41f9f19dfb0cb124e6ecedd2b18d2add9d39f0648480
5
5
  SHA512:
6
- metadata.gz: 71b6752fa603ed57e4c2f30545456baf445a011a54d21b13c4223825b646d0e4c5fe2e0ef80a106eb6c1732611663e31b38c2b8e3fdcc8c0a69fd1723c1fe5b5
7
- data.tar.gz: c7ebde805dd400055725b095ea7f1ce78e0ac6fafb03ec3904dcf8a777aadc3111d09e9a7d868623f377abf4387115fcc42af841386dfc936a34c47d3ed16750
6
+ metadata.gz: b4963caac24597a63e8c817e4dbe48e56a02a1bd710a26ba2200ee97aa482cffb831026e162b5b6c5f4c311e34bf371d6316cb0c5b5633b568845b323401d89e
7
+ data.tar.gz: ea7340afbce3d8e8e6dc530a13495328ff3e34f867cea23b7e0ba71242572b0957127e833ec68260ff9c2c10088da85b55f3947eecb3f9db168f1b7a22a2ed66
data/lib/acrylic.rb CHANGED
@@ -1,3 +1,9 @@
1
1
  require 'rack'
2
2
  require_relative 'bits'
3
3
  require_relative 'cascade'
4
+
5
+ require 'yaml'
6
+
7
+ module Acrylic
8
+ CONFIG = YAML.load File.read "data/config.yml"
9
+ end
@@ -51,7 +51,7 @@ module Acrylic
51
51
  case code
52
52
  when 400
53
53
  p do "Your browser (or HTTP client) sent a request that completely bamboozled the server." end
54
- p do "If you're a developer, please RTFM. If you're a user, please tell the developer to fix their app." end
54
+ p do "If you're a developer, please <strong>RTFM</strong>. If you're a user, please tell the developer to fix their app." end
55
55
 
56
56
  when 403
57
57
  p do "You are not allowed to access the resource at this address." end
@@ -62,12 +62,15 @@ module Acrylic
62
62
  p do "If someone sent you here, please tell them to <strong>fix their hyperlinks</strong>." end
63
63
 
64
64
  when 418
65
- p do "You pathetic attempt at an HTTP Request was so humerous, it even made the server laugh." end
65
+ p do "You pathetic attempt at an HTTP Request was so humerous, it even made the webserver laugh." end
66
+
67
+ when 451
68
+ p do "Sorry, but you live in an authoritarian dystopia, and they do not allow you to access this website." end
66
69
 
67
70
  when 429
68
71
  h1 do "IT'S TIME TO STOP." end
69
72
  h1 do "IT'S TIME TO STOP, OKAY!???" end
70
-
73
+
71
74
  else
72
75
  p do "An error has occured. I have no idea what it is though. ¯\\_(ツ)_/¯" end
73
76
  end
@@ -80,7 +83,7 @@ module Acrylic
80
83
  end
81
84
  end
82
85
  end
83
-
86
+
84
87
  # returns body of response.
85
88
  def self.error_body code, req
86
89
  route = ERROR_ROUTES.find do |route| route.match? code, req end
data/lib/cascade.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rack'
2
+ require 'ostruct'
2
3
 
3
4
  module Acrylic
4
5
  ROUTES = []
@@ -17,38 +18,59 @@ module Acrylic
17
18
  @verbs = (args[:verb] or :GET).containerize.map do |verb| verb.downcase.to_sym end
18
19
  end
19
20
 
21
+ # transforms a rule from string (e.g. "/User/<id>") to wildcard
22
+ def make_into_wildcard rule
23
+ rule.gsub /<.*?>/, '*'
24
+ end
25
+
20
26
  # checks if a path matches one of the rules.
27
+ # returns false if no match, else rule that matched.
21
28
  def path_match? path
22
29
  @paths.each do |rule|
23
- if ((rule.is_a? Regexp) and (rule.match? path)) \
24
- or ((rule.is_a? Proc) and (rule.call path)) \
25
- or ((rule.is_a? String) and (File.fnmatch rule, path, File::Constants::FNM_EXTGLOB | File::Constants::FNM_PATHNAME))
26
- then return true end
30
+ if ((rule.is_a? Regexp) and (rule.match? path)) \
31
+ or ((rule.respond_to? :call) and (rule.call path)) \
32
+ or ((rule.respond_to? :to_s) and (File.fnmatch make_into_wildcard(rule.to_s), path, 0b10010))
33
+ then return rule end
27
34
  end
28
35
 
29
36
  return false
30
37
  end
31
38
 
39
+ def args_for_path path
40
+ rule = path_match? path
41
+ return [] unless rule.is_a? String
42
+
43
+ rule_tokens = rule.split '/'
44
+ path_tokens = path.split '/'
45
+
46
+ rule_tokens.map.with_index do |rule_token, i|
47
+ path_tokens[i] if /<.*?>/.match? rule_token
48
+ end.compact
49
+ end
50
+
32
51
  def match? req
33
52
  # match against paths
34
53
  (path_match? req.path) and (@verbs.include? req.request_method.downcase.to_sym )
35
54
  end
36
55
 
37
- def call req
38
- @handler.call req
56
+ def call req, *args
57
+ @handler.call req, *args
39
58
  end
40
59
  end
41
60
 
42
61
  class Cascade
62
+
43
63
  # returns a response based on the defined routes.
44
64
  def self.handle req
45
65
  headers = {}
66
+ config = OpenStruct.new
46
67
 
47
68
  # loop through all the routes to find a final one
48
69
  Acrylic::ROUTES.each do |route|
49
70
  next unless route.match? req
50
71
 
51
- res = route.call req
72
+ res = route.call config, req, *(route.args_for_path req.path)
73
+
52
74
  res = case true
53
75
  when (res.is_a? Rack::Response) then res
54
76
  when (res.respond_to? :to_h) then res.to_h
@@ -82,8 +104,8 @@ module Acrylic
82
104
  # why rack uses a goddamn empty array instead of nil i have no clue of
83
105
  # (it doesnt give a fuck about strings, but it TURNS NILS INTO ARRAYS!? WHY!? WHY!!!!!!?)
84
106
  if res.body == [] then res.body = error_body res.status, req end
85
-
86
- # 2. transform every header key of res into a string
107
+
108
+ # 3. transform every header key of res into a string
87
109
  headers = res.headers.map do |k, v| [k.to_s, v] end.to_h
88
110
 
89
111
  Rack::Response.new res.body, res.status, headers
@@ -91,7 +113,6 @@ module Acrylic
91
113
  end
92
114
  end
93
115
 
94
-
95
116
  def route *wargs, **kwargs, &route
96
117
  args = (Hash.from_keys wargs).merge kwargs
97
118
  Acrylic::ROUTES << (Acrylic::Route.new args, route)
data/lib/version.rb CHANGED
@@ -2,7 +2,7 @@ module Acrylic
2
2
  class Version
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- HOTFIX = 0
5
+ HOTFIX = 1
6
6
 
7
7
  def self.semver
8
8
  "#{MAJOR}.#{MINOR}.#{HOTFIX}"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acrylic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - fmixolydian
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-12-16 00:00:00.000000000 Z
10
+ date: 2025-12-31 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rack
@@ -28,9 +28,6 @@ dependencies:
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - "~>"
31
- - !ruby/object:Gem::Version
32
- version: '0.9'
33
- - - ">="
34
31
  - !ruby/object:Gem::Version
35
32
  version: 0.9.4
36
33
  type: :runtime
@@ -38,11 +35,22 @@ dependencies:
38
35
  version_requirements: !ruby/object:Gem::Requirement
39
36
  requirements:
40
37
  - - "~>"
41
- - !ruby/object:Gem::Version
42
- version: '0.9'
43
- - - ">="
44
38
  - !ruby/object:Gem::Version
45
39
  version: 0.9.4
40
+ - !ruby/object:Gem::Dependency
41
+ name: ostruct
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.6.3
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 0.6.3
46
54
  description: |2
47
55
  Acrylic is a web framework that simplifies your life by automatically generating migrations,
48
56
  as well as providing several macros for repeating common operations (e.g. defining REST APIs).