Class: MacawFramework::Macaw

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

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)


34
35
36
37
38
39
# File 'lib/macaw_framework/macaw.rb', line 34

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

#bindObject

Returns the value of attribute bind.



27
28
29
# File 'lib/macaw_framework/macaw.rb', line 27

def bind
  @bind
end

#cached_methodsObject (readonly)

Returns the value of attribute cached_methods.



26
27
28
# File 'lib/macaw_framework/macaw.rb', line 26

def cached_methods
  @cached_methods
end

#configObject (readonly)

Returns the value of attribute config.



26
27
28
# File 'lib/macaw_framework/macaw.rb', line 26

def config
  @config
end

#jobsObject (readonly)

Returns the value of attribute jobs.



26
27
28
# File 'lib/macaw_framework/macaw.rb', line 26

def jobs
  @jobs
end

#macaw_logObject (readonly)

Returns the value of attribute macaw_log.



26
27
28
# File 'lib/macaw_framework/macaw.rb', line 26

def macaw_log
  @macaw_log
end

#portObject

Returns the value of attribute port.



27
28
29
# File 'lib/macaw_framework/macaw.rb', line 27

def port
  @port
end

#routesObject (readonly)

Returns the value of attribute routes.



26
27
28
# File 'lib/macaw_framework/macaw.rb', line 26

def routes
  @routes
end

#secure_headerObject (readonly)

Returns the value of attribute secure_header.



26
27
28
# File 'lib/macaw_framework/macaw.rb', line 26

def secure_header
  @secure_header
end

#sessionObject (readonly)

Returns the value of attribute session.



26
27
28
# File 'lib/macaw_framework/macaw.rb', line 26

def session
  @session
end

#threadsObject

Returns the value of attribute threads.



27
28
29
# File 'lib/macaw_framework/macaw.rb', line 27

def threads
  @threads
end

Instance Method Details

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

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)


118
119
120
# File 'lib/macaw_framework/macaw.rb', line 118

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

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

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)


53
54
55
# File 'lib/macaw_framework/macaw.rb', line 53

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

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

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)


102
103
104
# File 'lib/macaw_framework/macaw.rb', line 102

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

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

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)


70
71
72
# File 'lib/macaw_framework/macaw.rb', line 70

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

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

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)


86
87
88
# File 'lib/macaw_framework/macaw.rb', line 86

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

#setup_job(interval: 60, start_delay: 0, job_name: "job_#{SecureRandom.uuid}", &block) ⇒ Object

Spawn and start a thread running the defined periodic job.

Examples:


macaw = MacawFramework::Macaw.new
macaw.setup_job(interval: 60, start_delay: 60, job_name: "job 1") do
  puts "I'm a periodic job that runs every minute"
end

Parameters:

  • interval (Integer) (defaults to: 60)
  • start_delay (Integer?) (defaults to: 0)
  • job_name (String) (defaults to: "job_#{SecureRandom.uuid}")
  • block (Proc)


135
136
137
138
139
140
# File 'lib/macaw_framework/macaw.rb', line 135

def setup_job(interval: 60, start_delay: 0, job_name: "job_#{SecureRandom.uuid}", &block)
  @cron_runner ||= CronRunner.new(self)
  @jobs ||= []
  @cron_runner.start_cron_job_thread(interval, start_delay, job_name, &block)
  @jobs << job_name
end

#start!Object

Starts the web server



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/macaw_framework/macaw.rb', line 144

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, @prometheus, @prometheus_middleware)
  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

#start_without_server!Object

This method is intended to start the framework without an web server. This can be useful when you just want to keep cron jobs running, without mapping any HTTP endpoints.



175
176
177
178
179
180
# File 'lib/macaw_framework/macaw.rb', line 175

def start_without_server!
  @macaw_log.nil? ? puts('Application starting') : @macaw_log.info('Application starting')
  loop { sleep(3600) }
rescue Interrupt
  @macaw_log.nil? ? puts('Macaw stop flying for some seeds.') : @macaw_log.info('Macaw stop flying for some seeds.')
end