Class: WebFetch::Server

Inherits:
EM::Connection
  • Object
show all
Includes:
EM::HttpServer, EventMachineHelpers, HTTPHelpers
Defined in:
lib/web_fetch/server.rb

Overview

Web server that accepts requests to gather and retrieve external HTTP requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventMachineHelpers

#tick_loop, #wait_for_response

Methods included from HTTPHelpers

#accept_gzip?, #compress, #default_headers, #fail_, #failure, #post_data, #request_params, #respond_immediately, #succeed, #success

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



7
8
9
# File 'lib/web_fetch/server.rb', line 7

def storage
  @storage
end

Instance Method Details

#gather(targets) ⇒ Object

Note that #gather is called by WebFetch itself to asynchronously gather the required HTTP objects. All public API requests go via #process_http_request and subsequently WebFetch::Router#route



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/web_fetch/server.rb', line 36

def gather(targets)
  targets.each do |target|
    request = target[:request]
    async_request = EM::HttpRequest.new(request[:url])
    method = request.fetch(:method, 'GET').downcase.to_sym
    http = async_request.public_send(method,
                                     head: request[:headers],
                                     query: request.fetch(:query, {}),
                                     body: request.fetch(:body, nil))
    @storage.store(target[:uid], uid: target[:uid], http: http)
  end
end

#post_initObject



13
14
15
16
17
18
# File 'lib/web_fetch/server.rb', line 13

def post_init
  super
  @router = Router.new
  @storage = Storage
  no_environment_strings
end

#process_http_requestObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/web_fetch/server.rb', line 20

def process_http_request
  result = @router.route(@http_request_uri, request_params)
  response = EM::DelegatedHttpResponse.new(self)

  default_headers(response)

  if result[:deferred].nil?
    respond_immediately(result, response)
  else
    wait_for_response(result[:deferred], response)
  end
end