low_loop 0.3.0 → 0.3.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: 61a2d187bf64d4db14f12ab1dbdbe215cb3f608e3d529e17b5af8694c4bf453d
4
- data.tar.gz: cea153ef1443bd98f22cecb8370e77136e7bfddb0143171bf94cbf18882b9e26
3
+ metadata.gz: 3bd2e532e273f35fcbcd9d8e9f0f4b0d9261e536d293908026371412ec11573e
4
+ data.tar.gz: 39b83d8235d14d0d7359ea742f1e9c3d4ab4bdd1e0cceca01006950d2d942cc6
5
5
  SHA512:
6
- metadata.gz: 0dae5020c04416c92d23463f1def7f5dafd14bfe324d059e3242e2e1ecc65e583df12064c4981b8c34621dba10fab2162aa04bdb534cc3db81a53d1d9bfe7809
7
- data.tar.gz: ec738cd515d3fee61f5ee98055c4fdeab4ae510c9ee50cc7591a5922874030c876da9d224b6e4f1060d17b2b098c11a1b0cb64ab73a99393ea815a76c6df7de9
6
+ metadata.gz: 17b1927035f2900208d1cbe6399b7814ec812f19afa1b22734e83995fd7e8f5bbf5d6724403553c55a83ef0779cf7b7bcaa56e56763ee8e2b67814980fab2b53
7
+ data.tar.gz: 29b8756fbb3957e9ab50a5748391b745ca244af98cea79ae2d3e9ed8955ec07aa3f7aad9d339a6154270e004b2058592272a8db132717354c6f5af200245f017
@@ -4,21 +4,23 @@ require 'protocol/http'
4
4
  require 'protocol/http/body/file'
5
5
 
6
6
  module Low
7
- class ResponseFactory
8
- class << self
9
- def html(body:)
10
- headers = Protocol::HTTP::Headers.new(['content-type', 'text/html'])
11
- body = Protocol::HTTP::Body::Buffered.wrap(body)
7
+ module Factories
8
+ class ResponseFactory
9
+ class << self
10
+ def html(body:)
11
+ headers = Protocol::HTTP::Headers.new(['content-type', 'text/html'])
12
+ body = Protocol::HTTP::Body::Buffered.wrap(body)
12
13
 
13
- Protocol::HTTP::Response.new('http/1.1', 200, headers, body)
14
- end
14
+ Protocol::HTTP::Response.new('http/1.1', 200, headers, body)
15
+ end
15
16
 
16
- def file(path:, content_type:)
17
- headers = Protocol::HTTP::Headers.new(['content-type', content_type])
18
- file = File.open(path, 'rb')
19
- body = Protocol::HTTP::Body::File.new(file)
17
+ def file(path:, content_type:)
18
+ headers = Protocol::HTTP::Headers.new(['content-type', content_type])
19
+ file = File.open(path, 'rb')
20
+ body = Protocol::HTTP::Body::File.new(file)
20
21
 
21
- Protocol::HTTP::Response.new('http/1.1', 200, headers, body)
22
+ Protocol::HTTP::Response.new('http/1.1', 200, headers, body)
23
+ end
22
24
  end
23
25
  end
24
26
  end
data/lib/low_loop.rb CHANGED
@@ -10,66 +10,62 @@ require_relative 'factories/response_factory'
10
10
  require_relative 'requests/request_parser'
11
11
  require_relative 'responses/response_builder'
12
12
 
13
- module Low
14
- class Loop
15
- include Observers
13
+ class LowLoop
14
+ include Observers
16
15
 
17
- attr_reader :config
16
+ attr_reader :config
18
17
 
19
- def initialize(config:, dependencies: [])
20
- @config = config
18
+ def initialize(config:, dependencies: [])
19
+ @config = config
21
20
 
22
- dependencies.each do |dependency|
23
- observers << dependency
24
- end
25
-
26
- observers.push(self, action: :mirror) if config.mirror_mode
21
+ dependencies.each do |dependency|
22
+ observers << dependency
27
23
  end
28
24
 
29
- def start
30
- server = start_server
25
+ observers.push(self, action: :mirror) if config.mirror_mode
26
+ end
27
+
28
+ def start
29
+ server = start_server
31
30
 
32
- Fiber.set_scheduler(Async::Scheduler.new)
31
+ Fiber.set_scheduler(Async::Scheduler.new)
33
32
 
34
- Fiber.schedule do
35
- loop do
36
- socket = server.accept
33
+ Fiber.schedule do
34
+ loop do
35
+ socket = server.accept
37
36
 
38
- Fiber.schedule do
39
- request = RequestParser.parse(socket:, host: config.host, port: config.port)
40
- response_event = take(event: Events::RequestEvent.new(request:))
41
- response = response_event.response
37
+ Fiber.schedule do
38
+ request = Low::RequestParser.parse(socket:, host: config.host, port: config.port)
39
+ response_event = take(event: Low::Events::RequestEvent.new(request:))
40
+ response = response_event.response
42
41
 
43
- ResponseBuilder.respond(config:, socket:, response:)
44
- rescue StandardError => e
45
- puts e.message
46
- ensure
47
- socket&.close
48
- end
42
+ Low::ResponseBuilder.respond(config:, socket:, response:)
43
+ rescue StandardError => e
44
+ puts e.message
45
+ ensure
46
+ socket&.close
49
47
  end
50
48
  end
51
49
  end
50
+ end
52
51
 
53
- def start_server
54
- puts "Starting server @ #{config.host}:#{config.port}" unless config.matrix_mode
52
+ def start_server
53
+ puts "Starting server @ #{config.host}:#{config.port}" unless config.matrix_mode
55
54
 
56
- server = TCPServer.new(config.host, config.port)
57
- server.listen(10)
58
- server
59
- end
60
-
61
- # Fallback mode for when there's no dependencies and you want to know that the server is still working.
62
- def mirror(event:)
63
- request = event.request
64
- response = ResponseFactory.html(body: "Thank you for visiting #{request.path} with body: '#{request.body}'")
65
- Events::ResponseEvent.new(response:)
66
- end
55
+ server = TCPServer.new(config.host, config.port)
56
+ server.listen(10)
57
+ server
58
+ end
67
59
 
68
- # Consider LowLoop a value object in the context of Observers (there can only be one).
69
- def ==(other) = other.class == self.class
70
- def eql?(other) = self == other
71
- def hash = [self.class].hash
60
+ # Fallback mode for when there's no dependencies and you want to know that the server is still working.
61
+ def mirror(event:)
62
+ request = event.request
63
+ response = Low::Factories::ResponseFactory.html(body: "Thank you for visiting #{request.path} with body: '#{request.body}'")
64
+ Low::Events::ResponseEvent.new(response:)
72
65
  end
73
- end
74
66
 
75
- LowLoop = Low::Loop
67
+ # Consider LowLoop a value object in the context of Observers (there can only be one).
68
+ def ==(other) = other.class == self.class
69
+ def eql?(other) = self == other
70
+ def hash = [self.class].hash
71
+ end
@@ -9,8 +9,8 @@ module Low
9
9
  file = event.file
10
10
 
11
11
  if File.exist?(file.path)
12
- response = ResponseFactory.file(path: file.path, content_type: file.content_type)
13
- return Low::Events::ResponseEvent.new(response:)
12
+ response = Factories::ResponseFactory.file(path: file.path, content_type: file.content_type)
13
+ return Events::ResponseEvent.new(response:)
14
14
  end
15
15
 
16
16
  nil
@@ -15,7 +15,7 @@ module Low
15
15
  @web_root = web_root
16
16
  @content_types = content_types.transform_keys(&:to_s)
17
17
 
18
- observers(File) << FileResponse
18
+ observers(Events::FileEvent) << FileResponse
19
19
  end
20
20
 
21
21
  def extension(filepath:)
@@ -34,9 +34,9 @@ module Low
34
34
  extension = extension(filepath:)
35
35
  return nil if extension.nil?
36
36
 
37
- file = FileState.new(path: safe_path(filepath), content_type: @content_types[extension])
37
+ file = States::FileState.new(path: safe_path(filepath), content_type: @content_types[extension])
38
38
 
39
- trigger File, event: Events::FileEvent.new(file:, request: event.request)
39
+ Events::FileEvent.trigger(file:, request: event.request)
40
40
  end
41
41
 
42
42
  private
@@ -1,12 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Low
4
- class FileState
5
- attr_reader :path, :content_type
6
-
7
- def initialize(path:, content_type:)
8
- @path = path
9
- @content_type = content_type
10
- end
4
+ module States
5
+ FileState = Data.define(:path, :content_type)
11
6
  end
12
7
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Low
4
- LOOP_VERSION = '0.3.0'
4
+ module Loop
5
+ VERSION = '0.3.1'
6
+ end
5
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: low_loop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi