Class: MacawFramework::Macaw

Inherits:
Object
  • Object
show all
Defined in:
lib/macaw_framework/macaw.rb,
sig/macaw_framework/macaw.rbs

Overview

Class responsible for creating endpoints and starting the web server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_log: Logger.new($stdout), server: ThreadServer, dir: nil) ⇒ Macaw

Initialize Macaw Class

Parameters:

  • custom_log (Logger) (defaults to: Logger.new($stdout))
  • server (ThreadServer) (defaults to: ThreadServer)
  • dir (String?) (defaults to: nil)


31
32
33
34
35
36
# File 'lib/macaw_framework/macaw.rb', line 31

def initialize(custom_log: Logger.new($stdout), server: ThreadServer, dir: nil)
  apply_options(custom_log)
  create_endpoint_public_files(dir)
  setup_default_configs
  @server_class = server
end

Instance Attribute Details

#bind ⇒ String

Returns the value of attribute bind.

Returns:

  • (String)


24
25
26
# File 'lib/macaw_framework/macaw.rb', line 24

def bind
  @bind
end

#cached_methods ⇒ Object (readonly)

Returns the value of attribute cached_methods.



23
24
25
# File 'lib/macaw_framework/macaw.rb', line 23

def cached_methods
  @cached_methods
end

#config ⇒ Hash[String, untyped] (readonly)

Returns the value of attribute config.

Returns:

  • (Hash[String, untyped])


23
24
25
# File 'lib/macaw_framework/macaw.rb', line 23

def config
  @config
end

#jobs ⇒ Array[String] (readonly)

Returns the value of attribute jobs.

Returns:

  • (Array[String])


22
23
24
# File 'sig/macaw_framework/macaw.rbs', line 22

def jobs
  @jobs
end

#keep_alive_timeout ⇒ Object (readonly)

Returns the value of attribute keep_alive_timeout.



23
24
25
# File 'lib/macaw_framework/macaw.rb', line 23

def keep_alive_timeout
  @keep_alive_timeout
end

#macaw_log ⇒ Logger? (readonly)

Returns the value of attribute macaw_log.

Returns:

  • (Logger, nil)


23
24
25
# File 'lib/macaw_framework/macaw.rb', line 23

def macaw_log
  @macaw_log
end

#max_body_size ⇒ Object (readonly)

Returns the value of attribute max_body_size.



23
24
25
# File 'lib/macaw_framework/macaw.rb', line 23

def max_body_size
  @max_body_size
end

#port ⇒ Integer

Returns the value of attribute port.

Returns:

  • (Integer)


24
25
26
# File 'lib/macaw_framework/macaw.rb', line 24

def port
  @port
end

#routes ⇒ Array[String] (readonly)

Returns the value of attribute routes.

Returns:

  • (Array[String])


23
24
25
# File 'lib/macaw_framework/macaw.rb', line 23

def routes
  @routes
end

#threads ⇒ Integer

Returns the value of attribute threads.

Returns:

  • (Integer)


24
25
26
# File 'lib/macaw_framework/macaw.rb', line 24

def threads
  @threads
end

Instance Method Details

#delete(path, cache: [], &block) ⇒ nil

Creates a DELETE endpoint associated with the respective path.

Examples:


macaw = MacawFramework::Macaw.new
macaw.delete("/hello") do |context|
  return "Hello World!", 200, { "Content-Type" => "text/plain" }
end

Parameters:

  • path (String)
  • block (Proc)

Returns:

  • (nil)


115
116
117
# File 'lib/macaw_framework/macaw.rb', line 115

def delete(path, cache: [], &block)
  map_new_endpoint('delete', cache, path, &block)
end

#extract_body ⇒ string

Returns:

  • (string)


44
# File 'sig/macaw_framework/macaw.rbs', line 44

def extract_body: -> string

#extract_client_info ⇒ nil

Returns:

  • (nil)


46
# File 'sig/macaw_framework/macaw.rbs', line 46

def extract_client_info: -> nil

#extract_headers ⇒ string

Returns:

  • (string)


48
# File 'sig/macaw_framework/macaw.rbs', line 48

def extract_headers: -> string

#get(path, cache: [], &block) ⇒ nil

Creates a GET endpoint associated with the respective path.

Examples:

macaw = MacawFramework::Macaw.new
macaw.get("/hello") do |context|
  return "Hello World!", 200, { "Content-Type" => "text/plain" }
end

Parameters:

  • path (String)
  • block (Proc)

Returns:

  • (nil)


50
51
52
# File 'lib/macaw_framework/macaw.rb', line 50

def get(path, cache: [], &block)
  map_new_endpoint('get', cache, path, &block)
end

#patch(path, cache: [], &block) ⇒ nil

Creates a PATCH endpoint associated with the respective path.

Examples:


macaw = MacawFramework::Macaw.new
macaw.patch("/hello") do |context|
  return "Hello World!", 200, { "Content-Type" => "text/plain" }
end

Parameters:

  • path (String)
  • block (Proc)

Returns:

  • (nil)


99
100
101
# File 'lib/macaw_framework/macaw.rb', line 99

def patch(path, cache: [], &block)
  map_new_endpoint('patch', cache, path, &block)
end

#post(path, cache: [], &block) ⇒ nil

Creates a POST endpoint associated with the respective path.

Examples:


macaw = MacawFramework::Macaw.new
macaw.post("/hello") do |context|
  return "Hello World!", 200, { "Content-Type" => "text/plain" }
end

Parameters:

  • path (String)
  • cache (Boolean) (defaults to: [])
  • block (Proc)

Returns:

  • (nil)


67
68
69
# File 'lib/macaw_framework/macaw.rb', line 67

def post(path, cache: [], &block)
  map_new_endpoint('post', cache, path, &block)
end

#put(path, cache: [], &block) ⇒ nil

Creates a PUT endpoint associated with the respective path.

Examples:


macaw = MacawFramework::Macaw.new
macaw.put("/hello") do |context|
  return "Hello World!", 200, { "Content-Type" => "text/plain" }
end

Parameters:

  • path (String)
  • block (Proc)

Returns:

  • (nil)


83
84
85
# File 'lib/macaw_framework/macaw.rb', line 83

def put(path, cache: [], &block)
  map_new_endpoint('put', cache, path, &block)
end

#setup_job ⇒ nil

Returns:

  • (nil)


38
# File 'sig/macaw_framework/macaw.rbs', line 38

def setup_job: -> nil

#start! ⇒ nil

Starts the web server

Returns:

  • (nil)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/macaw_framework/macaw.rb', line 121

def start!
  if @macaw_log.nil?
    puts('---------------------------------')
    puts("Starting server at port #{@port}")
    puts("Number of threads: #{@threads}")
    puts('---------------------------------')
  else
    @macaw_log.info('---------------------------------')
    @macaw_log.info("Starting server at port #{@port}")
    @macaw_log.info("Number of threads: #{@threads}")
    @macaw_log.info('---------------------------------')
  end
  @server = @server_class.new(self, @endpoints_to_cache, @cache)
  Signal.trap('TERM') { raise Interrupt }
  server_loop(@server)
rescue Interrupt
  if @macaw_log.nil?
    puts('Stopping server')
    @server.shutdown
    puts('Macaw stop flying for some seeds...')
  else
    @macaw_log.info('Stopping server')
    @server.shutdown
    @macaw_log.info('Macaw stop flying for some seeds...')
  end
end