Class: MacawFramework::Macaw
- Inherits:
-
Object
- Object
- MacawFramework::Macaw
- Defined in:
- lib/macaw_framework.rb
Overview
Class responsible for creating endpoints and starting the web server.
Instance Attribute Summary collapse
-
#bind ⇒ Object
Returns the value of attribute bind.
-
#cached_methods ⇒ Object
readonly
Returns the value of attribute cached_methods.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#macaw_log ⇒ Object
readonly
Returns the value of attribute macaw_log.
-
#port ⇒ Object
Returns the value of attribute port.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
-
#secure_header ⇒ Object
readonly
Returns the value of attribute secure_header.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#threads ⇒ Object
Returns the value of attribute threads.
Instance Method Summary collapse
-
#delete(path, cache: [], &block) ⇒ Object
Creates a DELETE endpoint associated with the respective path.
-
#get(path, cache: [], &block) ⇒ Object
Creates a GET endpoint associated with the respective path.
-
#initialize(custom_log: Logger.new($stdout), server: ThreadServer, dir: nil) ⇒ Macaw
constructor
Initialize Macaw Class.
-
#patch(path, cache: [], &block) ⇒ Object
Creates a PATCH endpoint associated with the respective path.
-
#post(path, cache: [], &block) ⇒ Object
Creates a POST endpoint associated with the respective path.
-
#put(path, cache: [], &block) ⇒ Object
Creates a PUT endpoint associated with the respective path.
-
#setup_job(interval: 60, start_delay: 0, job_name: "job_#{SecureRandom.uuid}", &block) ⇒ Object
Spawn and start a thread running the defined periodic job.
-
#start! ⇒ Object
Starts the web server.
-
#start_without_server! ⇒ Object
This method is intended to start the framework without an web server.
Constructor Details
#initialize(custom_log: Logger.new($stdout), server: ThreadServer, dir: nil) ⇒ Macaw
Initialize Macaw Class
31 32 33 34 35 36 |
# File 'lib/macaw_framework.rb', line 31 def initialize(custom_log: Logger.new($stdout), server: ThreadServer, dir: nil) (custom_log) create_endpoint_public_files(dir) setup_default_configs @server_class = server end |
Instance Attribute Details
#bind ⇒ Object
Returns the value of attribute bind.
24 25 26 |
# File 'lib/macaw_framework.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.rb', line 23 def cached_methods @cached_methods end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
23 24 25 |
# File 'lib/macaw_framework.rb', line 23 def config @config end |
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
23 24 25 |
# File 'lib/macaw_framework.rb', line 23 def jobs @jobs end |
#macaw_log ⇒ Object (readonly)
Returns the value of attribute macaw_log.
23 24 25 |
# File 'lib/macaw_framework.rb', line 23 def macaw_log @macaw_log end |
#port ⇒ Object
Returns the value of attribute port.
24 25 26 |
# File 'lib/macaw_framework.rb', line 24 def port @port end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
23 24 25 |
# File 'lib/macaw_framework.rb', line 23 def routes @routes end |
#secure_header ⇒ Object (readonly)
Returns the value of attribute secure_header.
23 24 25 |
# File 'lib/macaw_framework.rb', line 23 def secure_header @secure_header end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
23 24 25 |
# File 'lib/macaw_framework.rb', line 23 def session @session end |
#threads ⇒ Object
Returns the value of attribute threads.
24 25 26 |
# File 'lib/macaw_framework.rb', line 24 def threads @threads end |
Instance Method Details
#delete(path, cache: [], &block) ⇒ Object
Creates a DELETE endpoint associated with the respective path.
115 116 117 |
# File 'lib/macaw_framework.rb', line 115 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.
50 51 52 |
# File 'lib/macaw_framework.rb', line 50 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.
99 100 101 |
# File 'lib/macaw_framework.rb', line 99 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.
67 68 69 |
# File 'lib/macaw_framework.rb', line 67 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.
83 84 85 |
# File 'lib/macaw_framework.rb', line 83 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.
132 133 134 135 136 137 |
# File 'lib/macaw_framework.rb', line 132 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/macaw_framework.rb', line 141 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.close puts('Macaw stop flying for some seeds...') else @macaw_log.info('Stopping server') @server.close @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.
172 173 174 175 176 177 |
# File 'lib/macaw_framework.rb', line 172 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 |