Class: Knod::Server

Inherits:
Object
  • Object
show all
Includes:
FileUtilities
Defined in:
lib/knod/server.rb

Constant Summary collapse

DEFAULT_PORT =
4444
DEFAULT_WEB_ROOT =
'./'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileUtilities

#concat_json, #file_extension

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
15
# File 'lib/knod/server.rb', line 10

def initialize(options = {})
  port     = options.fetch(:port) { DEFAULT_PORT }
  @root    = options.fetch(:root) { DEFAULT_WEB_ROOT }
  @logging = options.fetch(:logging) { true }
  @server  = TCPServer.new('0.0.0.0', port)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args, &block) ⇒ Object (private)



186
187
188
189
190
191
192
# File 'lib/knod/server.rb', line 186

def method_missing(method_sym, *args, &block)
  if method_sym.to_s.start_with?('do_')
    respond(501, '"not implemented"')
  else
    super
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/knod/server.rb', line 5

def client
  @client
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/knod/server.rb', line 5

def request
  @request
end

#serverObject (readonly)

Returns the value of attribute server.



5
6
7
# File 'lib/knod/server.rb', line 5

def server
  @server
end

Instance Method Details

#accept_request_and_respond(client) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/knod/server.rb', line 26

def accept_request_and_respond(client)
  @client = client
  @request = Request.new(client)
  log request_line
  public_send "do_#{request.method}"
rescue => e
  log "#{e.class}: #{e.message}"
  log e.backtrace
  respond 500
ensure
  client.close if client
end

#do_DELETEObject



59
60
61
62
63
# File 'lib/knod/server.rb', line 59

def do_DELETE
  path = requested_path
  delete_file(path) if file?(path)
  respond 204
end

#do_GET(head = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/knod/server.rb', line 39

def do_GET(head = false)
  path = requested_path

  if file?(path)
    File.open(path, 'rb') do |file|
      client.print file_response_header(file)
      IO.copy_stream(file, client) unless head
    end
  elsif directory?(path)
    respond(200, concat_json(path))
  else
    message = head ? '' : "\"File not found\""
    respond(404, message)
  end
end

#do_HEADObject



55
56
57
# File 'lib/knod/server.rb', line 55

def do_HEAD
  do_GET(head = true)
end

#do_PATCHObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/knod/server.rb', line 72

def do_PATCH
  path = requested_path
  data = if file?(path)
           merge_json(read_file(path), request.body)
         else
           request.body
         end
  write_to_path(path, data)
  respond(200, "\"Success\"")
end

#do_POSTObject



83
84
85
86
87
88
89
# File 'lib/knod/server.rb', line 83

def do_POST
  path = requested_path
  create_directory(path)
  next_id = max_id_in_path(path) + 1
  write_file(join_path(path, "#{next_id}.json"), request.body)
  respond(201, "{\"id\":#{next_id}}")
end

#do_PUTObject



65
66
67
68
# File 'lib/knod/server.rb', line 65

def do_PUT
  write_to_path(requested_path, request.body)
  respond(200, "\"Success\"")
end

#portObject



91
92
93
# File 'lib/knod/server.rb', line 91

def port
  server.addr[1]
end

#startObject



17
18
19
20
21
22
23
24
# File 'lib/knod/server.rb', line 17

def start
  log "Starting server on port #{port}"
  loop do
    Thread.start(server.accept) do |client|
      dup.accept_request_and_respond(client)
    end
  end
end