Class: CommunityZero::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/community_zero/server.rb

Overview

A single instance of the Community Server.

Author:

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :host => '0.0.0.0',
  :port => 3000
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Create a new Community site server.

Parameters:

  • options (Hash) (defaults to: {})

    a list of options to pass in

Options Hash (options):

  • :host (String)

    the host to listen on (default is 0.0.0.0)

  • :port (String, Fixnum)

    the port to listen on (default is 3000)



44
45
46
# File 'lib/community_zero/server.rb', line 44

def initialize(options = {})
  @options  = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#optionsHash (readonly)

The list of options passed to the server.

Returns:

  • (Hash)


33
34
35
# File 'lib/community_zero/server.rb', line 33

def options
  @options
end

Instance Method Details

#running?Boolean

Determine if the server is currently running.

Returns:

  • (Boolean)

    true if the server is currently running, false otherwise



71
72
73
# File 'lib/community_zero/server.rb', line 71

def running?
  server && server.running?
end

#startThread

Start the community server.

Returns:

  • (Thread)

    the thread the server is running in



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/community_zero/server.rb', line 52

def start
  if options[:publish]
    puts ">> Starting Community Zero (v#{CommunityZero::VERSION})..."
    puts ">> Puma (v#{Puma::Const::PUMA_VERSION}) is listening at #{url}"
    puts ">> Press CTRL+C to stop"
  end

  begin
    thread = server.run.join
  rescue Object, Interrupt
    puts "\n>> Stopping Puma..." if options[:publish]
    server.stop(true) if running?
  end
end

#urlObject

Returns the URL the server is listening on.

Examples:

server = CommunityZero.new
server.url #=> http://0.0.0.0:3000
server = CommunityZero.new(host: 'example.com', port: 80)
server.url #=> http://example.com:80


84
85
86
# File 'lib/community_zero/server.rb', line 84

def url
  @url ||= "http://#{options[:host]}:#{options[:port]}"
end