macaw_framework 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20f895377b3f0eea2494e091fab22b81734f6625d840ed2414c557db4db29ac2
4
- data.tar.gz: 0ce55bbbbb41a5da4ab1622c72ced7300bfc4cb8b75091702fffb21822415ce2
3
+ metadata.gz: 8d2934b530ed442004aaecbd13430dc250fe2fde52c2b9abf2e0f95eed6c606a
4
+ data.tar.gz: 8989aaff7cf4be554e18edb4cee3928b918c263b35cbb9886f9df94ef8a5b0f9
5
5
  SHA512:
6
- metadata.gz: 033b20344e924fb6cf66a75df40dd18fcee89967738392529b7f6dbab02e7301af275156d81a5601222428a03ae9796af751b048ab09632debfe87fcc5c86516
7
- data.tar.gz: c2c09a27334e8904f2b7e714a6ac71df95140fe7e088ddcf23e71a29a125c0cc72cce2395939ce18d2e5ebb3998fb28c8a8a0a912de99c9e070004a0b78483a9
6
+ metadata.gz: 04ac3f80610994da626fef59bd5554bc2535c693357bca3fe8891170478cdf7b65af4552c83b8532c40ae704e6d50fafe9457afa2c03b6177080742e15e11611
7
+ data.tar.gz: 245ef53c503e22eea1c2f5a3e4a8a8ccf48a7444cba36c393fed048973415d1ff4dea8bba53c3bb5dd892422898e0284ca8a6d3b3f70ddaed0c3823d191df792
data/CHANGELOG.md CHANGED
@@ -3,3 +3,7 @@
3
3
  ## [0.1.0] - 2022-12-05
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.0] - 2022-12-06
8
+
9
+ - Adding support for headers and body
data/README.md CHANGED
@@ -35,9 +35,9 @@ Example of usage:
35
35
  require 'macaw_framework'
36
36
  require 'json'
37
37
 
38
- m = Macaw.new
38
+ m = MacawFramework::Macaw.new
39
39
 
40
- m.get('/hello_world') do
40
+ m.get('/hello_world') do |headers, body|
41
41
  return JSON.pretty_generate({ hello_message: 'Hello World!' }), 200
42
42
  end
43
43
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MacawFramework
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'macaw_framework/endpoint_not_mapped_error'
4
- require_relative 'macaw_framework/http_status_code'
5
- require_relative 'macaw_framework/version'
6
- require 'socket'
7
- require 'json'
3
+ require_relative "macaw_framework/endpoint_not_mapped_error"
4
+ require_relative "macaw_framework/http_status_code"
5
+ require_relative "macaw_framework/version"
6
+ require "socket"
7
+ require "json"
8
8
 
9
9
  module MacawFramework
10
10
  ##
@@ -14,8 +14,8 @@ module MacawFramework
14
14
  include(HttpStatusCode)
15
15
  def initialize
16
16
  begin
17
- config = JSON.parse(File.read('application.json'))
18
- @port = config['macaw']['port']
17
+ config = JSON.parse(File.read("application.json"))
18
+ @port = config["macaw"]["port"]
19
19
  rescue StandardError
20
20
  @port ||= 8080
21
21
  end
@@ -29,7 +29,7 @@ module MacawFramework
29
29
  # @param {Proc} block
30
30
  # @return {Integer, String}
31
31
  def get(path, &block)
32
- path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
32
+ path_clean = path[0] == "/" ? path[1..].gsub("/", "_") : path.gsub("/", "_")
33
33
  define_singleton_method("get_#{path_clean}", block)
34
34
  end
35
35
 
@@ -40,7 +40,7 @@ module MacawFramework
40
40
  # @param {Proc} block
41
41
  # @return {String, Integer}
42
42
  def post(path, &block)
43
- path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
43
+ path_clean = path[0] == "/" ? path[1..].gsub("/", "_") : path.gsub("/", "_")
44
44
  define_singleton_method("post_#{path_clean}", block)
45
45
  end
46
46
 
@@ -51,7 +51,7 @@ module MacawFramework
51
51
  # @param {Proc} block
52
52
  # @return {String, Integer}
53
53
  def put(path, &block)
54
- path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
54
+ path_clean = path[0] == "/" ? path[1..].gsub("/", "_") : path.gsub("/", "_")
55
55
  define_singleton_method("put_#{path_clean}", block)
56
56
  end
57
57
 
@@ -62,7 +62,7 @@ module MacawFramework
62
62
  # @param {Proc} block
63
63
  # @return {String, Integer}
64
64
  def patch(path, &block)
65
- path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
65
+ path_clean = path[0] == "/" ? path[1..].gsub("/", "_") : path.gsub("/", "_")
66
66
  define_singleton_method("patch_#{path_clean}", block)
67
67
  end
68
68
 
@@ -73,7 +73,7 @@ module MacawFramework
73
73
  # @param {Proc} block
74
74
  # @return {String, Integer}
75
75
  def delete(path, &block)
76
- path_clean = path[0] == '/' ? path[1..].gsub('/', '_') : path.gsub('/', '_')
76
+ path_clean = path[0] == "/" ? path[1..].gsub("/", "_") : path.gsub("/", "_")
77
77
  define_singleton_method("delete_#{path_clean}", block)
78
78
  end
79
79
 
@@ -84,12 +84,13 @@ module MacawFramework
84
84
  puts "Starting server at port #{@port}"
85
85
  loop do
86
86
  Thread.start(server.accept) do |client|
87
- method_name = extract_client_info(client)
87
+ client.select
88
+ method_name, headers, body = extract_client_info(client)
88
89
  raise EndpointNotMappedError unless respond_to?(method_name)
89
90
 
90
- message, status = send(method_name)
91
+ message, status = send(method_name, headers, body)
91
92
  status ||= 200
92
- message ||= 'Ok'
93
+ message ||= "Ok"
93
94
  client.puts "HTTP/1.1 #{status} #{HTTP_STATUS_CODE_MAP[status]} \r\n\r\n#{message}"
94
95
  client.close
95
96
  rescue EndpointNotMappedError
@@ -100,18 +101,39 @@ module MacawFramework
100
101
  client.close
101
102
  end
102
103
  end
104
+ rescue Interrupt
105
+ puts "Macaw stop flying for some seeds..."
103
106
  end
104
107
 
105
108
  private
106
109
 
107
110
  ##
108
- # Method for extracting headers and body from client request
109
- # STILL IN DEVELOPMENT
110
- # @todo finish implementation
111
+ # Method responsible for extracting information
112
+ # provided by the client like Headers and Body
111
113
  def extract_client_info(client)
112
- method_name = client.gets.gsub('HTTP/1.1', '').gsub('/', '_').strip!.downcase
113
- method_name.gsub!(' ', '')
114
- method_name
114
+ method_name = client.gets.gsub("HTTP/1.1", "").gsub("/", "_").strip!.downcase
115
+ method_name.gsub!(" ", "")
116
+ body_first_line, headers = extract_headers(client)
117
+ body = extract_body(client, body_first_line, headers["Content-Length"].to_i)
118
+ [method_name, headers, body]
119
+ end
120
+
121
+ ##
122
+ # Extract application headers
123
+ def extract_headers(client)
124
+ header = client.gets.delete("\n").delete("\r")
125
+ headers = {}
126
+ while header.match(%r{[a-zA-Z0-9\-/*]*: [a-zA-Z0-9\-/*]})
127
+ split_header = header.split(":")
128
+ headers[split_header[0]] = split_header[1][1..]
129
+ header = client.gets.delete("\n").delete("\r")
130
+ end
131
+ [header, headers]
132
+ end
133
+
134
+ def extract_body(client, body_first_line, content_length)
135
+ body = client.read(content_length)
136
+ body_first_line << body.to_s
115
137
  end
116
138
  end
117
139
  end
@@ -16,6 +16,10 @@ module MacawFramework
16
16
 
17
17
  private
18
18
 
19
+ def extract_body: -> string
20
+
19
21
  def extract_client_info: -> nil
22
+
23
+ def extract_headers: -> string
20
24
  end
21
25
  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.0
4
+ version: 0.1.1
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-06 00:00:00.000000000 Z
11
+ date: 2022-12-07 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:
@@ -34,6 +34,7 @@ homepage: https://github.com/ariasdiniz/macaw_framework
34
34
  licenses:
35
35
  - MIT
36
36
  metadata:
37
+ documentation_uri: https://rubydoc.info/gems/macaw_framework
37
38
  homepage_uri: https://github.com/ariasdiniz/macaw_framework
38
39
  source_code_uri: https://github.com/ariasdiniz/macaw_framework
39
40
  post_install_message:
@@ -44,7 +45,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
45
  requirements:
45
46
  - - ">="
46
47
  - !ruby/object:Gem::Version
47
- version: 2.6.0
48
+ version: 2.7.0
48
49
  required_rubygems_version: !ruby/object:Gem::Requirement
49
50
  requirements:
50
51
  - - ">="