macaw_framework 0.1.2 → 0.1.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: 512b14576a1be6337300fc758b7be6acf48603fa605c0d60c21d494b2d411a10
4
- data.tar.gz: 29f0115f9e93539c740e56834515d7d66dd768a2b5a8c5ef2668beb82d403fa5
3
+ metadata.gz: 46f8af6ed22b7980942f7ec3def269395d1e65e9c7bfd06a96d161c31666997d
4
+ data.tar.gz: 258e71f4b693c410c325e7a7751f8a2702e3b2db814752d0de39566fa1b94576
5
5
  SHA512:
6
- metadata.gz: aa4dd23ceaa6579ab7f0b77caa42b8745fb48f830aa8f5db9eb713bff5330d1879653209781e888c873b071c45b11afd5744282401d77c9c7609c0ed9f721e4b
7
- data.tar.gz: 2cc30aff12830763c3e9a19c8597fa4cd5f8e8ec7bfc1201f5c367883e93c480cef243d2590725084b62922538e6e27798f53840df0660b8872e09be01336f8a
6
+ metadata.gz: cc2b8fbd4d25b795df95a47025b7465b3cfb6bd340008bebdfe527232a009bad959e25f9c4528b7eb4ce98f14f6148a47dd44e56b19b4d4c23187a2b4a24b56c
7
+ data.tar.gz: 0d0f265ea93a3467718cbccd2ed93d85d0d98f9c84e53a821d3071b20243ca251e7670a788bd6e848f6381cee213c16ee8164dd27a32a2a28b77de5979a23326
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.6
2
+ TargetRubyVersion: 2.7
3
3
 
4
4
  Style/StringLiterals:
5
5
  Enabled: true
@@ -11,3 +11,9 @@ Style/StringLiteralsInInterpolation:
11
11
 
12
12
  Layout/LineLength:
13
13
  Max: 120
14
+
15
+ Metrics/MethodLength:
16
+ Max: 30
17
+
18
+ Metrics/AbcSize:
19
+ Max: 35
data/CHANGELOG.md CHANGED
@@ -14,3 +14,7 @@
14
14
  - Adding logs to the framework activity
15
15
  - Removing undefined Status Codes from http_status_code hash
16
16
  - Moving methods from Macaw class to RequestDataFiltering module, respecting SOLID
17
+
18
+ ## [0.1.3] - 2022-12-13
19
+
20
+ - Adding logger gem to Macaw class to fix a bug on the application start
@@ -4,7 +4,7 @@
4
4
  # Error raised when the client calls
5
5
  # for a path that doesn't exist.
6
6
  class EndpointNotMappedError < StandardError
7
- def initialize(msg = 'Undefined endpoint')
7
+ def initialize(msg = "Undefined endpoint")
8
8
  super
9
9
  end
10
10
  end
@@ -7,18 +7,18 @@ module RequestDataFiltering
7
7
  # Method responsible for extracting information
8
8
  # provided by the client like Headers and Body
9
9
  def self.extract_client_info(client)
10
- path, parameters = extract_url_parameters(client.gets.gsub('HTTP/1.1', ''))
11
- method_name = path.gsub('/', '_').strip!.downcase
12
- method_name.gsub!(' ', '')
10
+ path, parameters = extract_url_parameters(client.gets.gsub("HTTP/1.1", ""))
11
+ method_name = path.gsub("/", "_").strip!.downcase
12
+ method_name.gsub!(" ", "")
13
13
  body_first_line, headers = extract_headers(client)
14
- body = extract_body(client, body_first_line, headers['Content-Length'].to_i)
14
+ body = extract_body(client, body_first_line, headers["Content-Length"].to_i)
15
15
  [path, method_name, headers, body, parameters]
16
16
  end
17
17
 
18
18
  ##
19
19
  # Method responsible for extracting the path from URI
20
20
  def self.extract_path(path)
21
- path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
21
+ path[0] == "/" ? path[1..].gsub("/", "_") : path.gsub("/", "_")
22
22
  end
23
23
 
24
24
  ##
@@ -27,7 +27,7 @@ module RequestDataFiltering
27
27
  header = client.gets.delete("\n").delete("\r")
28
28
  headers = {}
29
29
  while header.match(%r{[a-zA-Z0-9\-/*]*: [a-zA-Z0-9\-/*]})
30
- split_header = header.split(':')
30
+ split_header = header.split(":")
31
31
  headers[split_header[0]] = split_header[1][1..]
32
32
  header = client.gets.delete("\n").delete("\r")
33
33
  end
@@ -46,12 +46,12 @@ module RequestDataFiltering
46
46
  def self.extract_url_parameters(http_first_line)
47
47
  return http_first_line, nil unless http_first_line =~ /\?/
48
48
 
49
- path_and_parameters = http_first_line.split('?', 2)
49
+ path_and_parameters = http_first_line.split("?", 2)
50
50
  path = "#{path_and_parameters[0]} "
51
- parameters_array = path_and_parameters[1].split('&')
51
+ parameters_array = path_and_parameters[1].split("&")
52
52
  parameters_array.map! do |item|
53
- split_item = item.split('=')
54
- { split_item[0] => split_item[1].gsub("\n", '').gsub("\r", '').gsub("\s", '') }
53
+ split_item = item.split("=")
54
+ { split_item[0] => split_item[1].gsub("\n", "").gsub("\r", "").gsub("\s", "") }
55
55
  end
56
56
  parameters = {}
57
57
  parameters_array.each { |item| parameters.merge!(item) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MacawFramework
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -4,6 +4,7 @@ require_relative "macaw_framework/endpoint_not_mapped_error"
4
4
  require_relative "macaw_framework/request_data_filtering"
5
5
  require_relative "macaw_framework/http_status_code"
6
6
  require_relative "macaw_framework/version"
7
+ require "logger"
7
8
  require "socket"
8
9
  require "json"
9
10
 
@@ -17,8 +18,8 @@ module MacawFramework
17
18
  # @param {Logger} custom_log
18
19
  def initialize(custom_log = nil)
19
20
  begin
20
- config = JSON.parse(File.read('application.json'))
21
- @port = config['macaw']['port']
21
+ config = JSON.parse(File.read("application.json"))
22
+ @port = config["macaw"]["port"]
22
23
  rescue StandardError
23
24
  @port ||= 8080
24
25
  end
@@ -33,9 +34,7 @@ module MacawFramework
33
34
  # @param {Proc} block
34
35
  # @return {Integer, String}
35
36
  def get(path, &block)
36
- path_clean = RequestDataFiltering.extract_path(path)
37
- @macaw_log.info("Defining GET endpoint at #{path_clean}")
38
- map_new_endpoint('get', path_clean, &block)
37
+ map_new_endpoint("get", path, &block)
39
38
  end
40
39
 
41
40
  ##
@@ -45,9 +44,7 @@ module MacawFramework
45
44
  # @param {Proc} block
46
45
  # @return {String, Integer}
47
46
  def post(path, &block)
48
- path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
49
- @macaw_log.info("Defining POST endpoint at #{path_clean}")
50
- map_new_endpoint('post', path_clean, &block)
47
+ map_new_endpoint("post", path, &block)
51
48
  end
52
49
 
53
50
  ##
@@ -57,9 +54,7 @@ module MacawFramework
57
54
  # @param {Proc} block
58
55
  # @return {String, Integer}
59
56
  def put(path, &block)
60
- path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
61
- @macaw_log.info("Defining PUT endpoint at #{path_clean}")
62
- map_new_endpoint('put', path_clean, &block)
57
+ map_new_endpoint("put", path, &block)
63
58
  end
64
59
 
65
60
  ##
@@ -69,9 +64,7 @@ module MacawFramework
69
64
  # @param {Proc} block
70
65
  # @return {String, Integer}
71
66
  def patch(path, &block)
72
- path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
73
- @macaw_log.info("Defining PATCH endpoint at #{path_clean}")
74
- map_new_endpoint('patch', path_clean, &block)
67
+ map_new_endpoint("patch", path, &block)
75
68
  end
76
69
 
77
70
  ##
@@ -81,9 +74,7 @@ module MacawFramework
81
74
  # @param {Proc} block
82
75
  # @return {String, Integer}
83
76
  def delete(path, &block)
84
- path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
85
- @macaw_log.info("Defining DELETE endpoint at #{path_clean}")
86
- map_new_endpoint('delete', path_clean, &block)
77
+ map_new_endpoint("delete", path, &block)
87
78
  end
88
79
 
89
80
  ##
@@ -93,15 +84,25 @@ module MacawFramework
93
84
  time = Time.now
94
85
  server = TCPServer.open(@port)
95
86
  @macaw_log.info("Server started in #{Time.now - time} seconds.")
87
+ server_loop(server)
88
+ rescue Interrupt
89
+ @macaw_log.info("Stopping server")
90
+ server.close
91
+ @macaw_log.info("Macaw stop flying for some seeds...")
92
+ end
93
+
94
+ private
95
+
96
+ def server_loop(server)
96
97
  loop do
97
98
  Thread.start(server.accept) do |client|
98
99
  path, method_name, headers, body, parameters = RequestDataFiltering.extract_client_info(client)
99
100
  raise EndpointNotMappedError unless respond_to?(method_name)
100
101
 
101
- @macaw_log.info("Running #{path.gsub("\n", '').gsub("\r", '')}")
102
+ @macaw_log.info("Running #{path.gsub("\n", "").gsub("\r", "")}")
102
103
  message, status = send(method_name, headers, body, parameters)
103
104
  status ||= 200
104
- message ||= 'Ok'
105
+ message ||= "Ok"
105
106
  client.puts "HTTP/1.1 #{status} #{HTTP_STATUS_CODE_MAP[status]} \r\n\r\n#{message}"
106
107
  client.close
107
108
  rescue EndpointNotMappedError
@@ -113,16 +114,12 @@ module MacawFramework
113
114
  client.close
114
115
  end
115
116
  end
116
- rescue Interrupt
117
- @macaw_log.info('Stopping server')
118
- server.close
119
- @macaw_log.info('Macaw stop flying for some seeds...')
120
117
  end
121
118
 
122
- private
123
-
124
119
  def map_new_endpoint(prefix, path, &block)
125
- define_singleton_method("#{prefix}_#{path}", block)
120
+ path_clean = RequestDataFiltering.extract_path(path)
121
+ @macaw_log.info("Defining #{prefix.upcase} endpoint at /#{path}")
122
+ define_singleton_method("#{prefix}_#{path_clean}", block)
126
123
  end
127
124
  end
128
125
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: macaw_framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aria Diniz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-11 00:00:00.000000000 Z
11
+ date: 2022-12-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A project started for study purpose that I intend to keep working on.
14
14
  email: