phaedra 0.4.1 → 0.5.0

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: 47c1b79ae715e83f3dd098fb9d0d29c5d89299f96c01d084f1cb44573f216022
4
- data.tar.gz: e716ca8ef384fdeadd773485042099d7bf7840b007e636588ce6fb28e40ab4b3
3
+ metadata.gz: ed786e8ee4e64448d25817144069d17839cbae1416b6d55daec427f336183101
4
+ data.tar.gz: 8d436c4e1560ca6acda2a03c5c68a86c6d82909a78cef5c4b0052d132fa9438c
5
5
  SHA512:
6
- metadata.gz: cf41193d15f6bc3678ebccd9239e7185479b3bd4029f593887202ca3e88b02529702688f64f2b373c90e54fed766b264a9f12c2a8a39e4c90039d0d708d9b89a
7
- data.tar.gz: 23686c007d52d61763ac393ced71d6485dbf3b3c45cc153828950f6e9c1e8018934834a0dfb90a7908d3357334c3542b34e74629d9774663607f86b21080fb94
6
+ metadata.gz: 54debc72ff247af26e5414dd4684ac1fe543e4390a7ad96ed8f784ab8f8c32febb4b1b95076afb3aa62c6c72de6d9f7f6cacfaca9b9a39628fe2ba71cbe46de5
7
+ data.tar.gz: 7b1489588052128fd22902674ad41c3d28fb620ca1bdeefbf5d8131651aaa240478e6ee52d4d1f2f0758c64edb13f71edb02ceece474994eb3d18fdccf546fcf
@@ -1,9 +1,9 @@
1
- require "phaedra"
1
+ require_relative "../phaedra/initializers"
2
2
 
3
3
  class PhaedraFunction < Phaedra::Base
4
4
  def get(params)
5
5
  response["Content-Type"] = "text/html; charset=utf-8"
6
- "<p>This is Interesting. 😁</p>"
6
+ "<p>This is Interesting. 😁 #{Phaedra.the_time}</p>"
7
7
  end
8
8
  end
9
9
 
@@ -1,3 +1,3 @@
1
- require "phaedra"
1
+ require "phaedra/rack_app"
2
2
 
3
3
  run Phaedra::RackApp.new
@@ -0,0 +1,11 @@
1
+ require "phaedra"
2
+
3
+ module Phaedra
4
+ Initializers.register self do
5
+ the_time("123")
6
+ end
7
+
8
+ def self.the_time(init = nil)
9
+ @the_time ||= "#{Time.now} + #{init}"
10
+ end
11
+ end
@@ -1,7 +1,4 @@
1
1
  require "phaedra/version"
2
-
3
2
  require "webrick"
4
- require "rack"
5
-
3
+ require "phaedra/initializers"
6
4
  require "phaedra/base"
7
- require "phaedra/rack_app"
@@ -7,6 +7,10 @@ module Phaedra
7
7
  class Base
8
8
  include CallbacksActionable
9
9
 
10
+ before_action do
11
+ Initializers.run
12
+ end
13
+
10
14
  # Used by WEBrick
11
15
  def self.get_instance(server, *options)
12
16
  self.new(server, *options)
@@ -112,7 +116,7 @@ module Phaedra
112
116
 
113
117
  def set_initial_status
114
118
  @res.status = 200
115
- @res["Content-Type"] = "application/json"
119
+ @res["Content-Type"] = "application/json; charset=utf-8"
116
120
  end
117
121
 
118
122
  def call_method_action(params)
@@ -123,7 +127,7 @@ module Phaedra
123
127
  def complete_response
124
128
  if @res.body.is_a?(String) && !@res["Content-Type"].start_with?("text/")
125
129
  @res["Content-Type"] = "text/plain; charset=utf-8"
126
- elsif @res["Content-Type"] == "application/json"
130
+ elsif @res["Content-Type"].start_with? "application/json"
127
131
  @res.body = @res.body.to_json
128
132
  end
129
133
  end
@@ -11,14 +11,14 @@ module Phaedra
11
11
  end
12
12
 
13
13
  module ClassMethods
14
- def before_action(*args)
15
- set_callback :action, :before, *args
14
+ def before_action(*args, &block)
15
+ set_callback :action, :before, *args, &block
16
16
  end
17
- def after_action(*args)
18
- set_callback :action, :after, *args
17
+ def after_action(*args, &block)
18
+ set_callback :action, :after, *args, &block
19
19
  end
20
- def around_action(*args)
21
- set_callback :action, :around, *args
20
+ def around_action(*args, &block)
21
+ set_callback :action, :around, *args, &block
22
22
  end
23
23
  end
24
24
  end
@@ -0,0 +1,69 @@
1
+ module Phaedra
2
+ module Initializers
3
+ Registration = Struct.new(
4
+ :origin,
5
+ :priority,
6
+ :block,
7
+ keyword_init: true
8
+ ) do
9
+ def to_s
10
+ "#{owner}:#{priority} for #{block}"
11
+ end
12
+ end
13
+
14
+ DEFAULT_PRIORITY = 20
15
+
16
+ PRIORITY_MAP = {
17
+ low: 10,
18
+ normal: 20,
19
+ high: 30,
20
+ }.freeze
21
+
22
+ # initial empty hooks
23
+ @registry = []
24
+
25
+ NotAvailable = Class.new(RuntimeError)
26
+ Uncallable = Class.new(RuntimeError)
27
+
28
+ # Ensure the priority is a Fixnum
29
+ def self.priority_value(priority)
30
+ return priority if priority.is_a?(Integer)
31
+
32
+ PRIORITY_MAP[priority] || DEFAULT_PRIORITY
33
+ end
34
+
35
+ def self.register(origin, priority: DEFAULT_PRIORITY, &block)
36
+ raise Uncallable, "Initializers must respond to :call" unless block.respond_to? :call
37
+
38
+ @registry << Registration.new(
39
+ origin: origin,
40
+ priority: priority_value(priority),
41
+ block: block
42
+ )
43
+
44
+ block
45
+ end
46
+
47
+ def self.remove(origin)
48
+ @registry.delete_if { |item| item.origin == origin }
49
+ end
50
+
51
+ def self.run(force: false)
52
+ if !@initializers_ran || force
53
+ prioritized_initializers.each do |initializer|
54
+ initializer.block.call
55
+ end
56
+ end
57
+
58
+ @initializers_ran = true
59
+ end
60
+
61
+ def self.prioritized_initializers
62
+ # sort initializers according to priority and load order
63
+ grouped_initializers = @registry.group_by(&:priority)
64
+ grouped_initializers.keys.sort.reverse.map do |priority|
65
+ grouped_initializers[priority]
66
+ end.flatten
67
+ end
68
+ end
69
+ end
@@ -1,3 +1,6 @@
1
+ require "rack"
2
+ require "phaedra"
3
+
1
4
  module Phaedra
2
5
  class Request < Rack::Request
3
6
  def query
@@ -0,0 +1,2 @@
1
+ require "phaedra/rack_middleware/not_found"
2
+ require "phaedra/rack_middleware/static"
@@ -1,3 +1,3 @@
1
1
  module Phaedra
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phaedra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jared White
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-11 00:00:00.000000000 Z
11
+ date: 2020-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -84,13 +84,15 @@ files:
84
84
  - example/api/simple.rb
85
85
  - example/api/the-time.rb
86
86
  - example/config.ru
87
+ - example/phaedra/initializers.rb
87
88
  - lib/phaedra.rb
88
89
  - lib/phaedra/base.rb
89
90
  - lib/phaedra/concerns/callbacks_actionable.rb
90
- - lib/phaedra/middleware.rb
91
- - lib/phaedra/middleware/not_found.rb
92
- - lib/phaedra/middleware/static.rb
91
+ - lib/phaedra/initializers.rb
93
92
  - lib/phaedra/rack_app.rb
93
+ - lib/phaedra/rack_middleware.rb
94
+ - lib/phaedra/rack_middleware/not_found.rb
95
+ - lib/phaedra/rack_middleware/static.rb
94
96
  - lib/phaedra/version.rb
95
97
  - phaedra.gemspec
96
98
  homepage: https://github.com/whitefusionhq/phaedra
@@ -112,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
114
  - !ruby/object:Gem::Version
113
115
  version: '0'
114
116
  requirements: []
115
- rubygems_version: 3.0.6
117
+ rubygems_version: 3.0.8
116
118
  signing_key:
117
119
  specification_version: 4
118
120
  summary: Write serverless Ruby functions via a REST-like microframework compatible
@@ -1,2 +0,0 @@
1
- require "phaedra/middleware/not_found"
2
- require "phaedra/middleware/static"