Module: WebFetch::HttpHelpers

Defined in:
lib/web_fetch/concerns/http_helpers.rb

Instance Method Summary collapse

Instance Method Details

#compress(string) ⇒ Object



5
6
7
# File 'lib/web_fetch/concerns/http_helpers.rb', line 5

def compress(string)
  ActiveSupport::Gzip.compress(string)
end

#default_headers(response) ⇒ Object



9
10
11
12
13
14
# File 'lib/web_fetch/concerns/http_helpers.rb', line 9

def default_headers(response)
  response.headers['Content-Type'] = 'application/json; charset=utf-8'
  response.headers['Cache-Control'] = 'max-age=0, private, must-revalidate'
  response.headers['Content-Encoding'] = 'gzip'
  response.headers['Vary'] = 'Accept-Encoding'
end

#fail_(deferred, response) ⇒ Object



46
47
48
49
50
# File 'lib/web_fetch/concerns/http_helpers.rb', line 46

def fail_(deferred, response)
  response.status = 200
  response.content = compress(JSON.dump(failure(deferred)))
  response.send_response
end

#failure(deferred) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/web_fetch/concerns/http_helpers.rb', line 52

def failure(deferred)
  result = deferred[:http]
  { response: {
    success: false,
    body: result.response,
    headers: result.headers,
    status: result.response_header.status,
    error: (result.error&.inspect)
  },
    uid: deferred[:uid] }
end

#post_dataObject



23
24
25
26
27
# File 'lib/web_fetch/concerns/http_helpers.rb', line 23

def post_data
  return nil unless @http_post_content

  JSON.parse(@http_post_content, symbolize_names: true)
end

#request_paramsObject



16
17
18
19
20
21
# File 'lib/web_fetch/concerns/http_helpers.rb', line 16

def request_params
  { method: @http_request_method,
    query_string: @http_query_string,
    post_data: post_data,
    server: self }
end

#succeed(deferred, response) ⇒ Object



29
30
31
32
33
# File 'lib/web_fetch/concerns/http_helpers.rb', line 29

def succeed(deferred, response)
  response.status = 200
  response.content = compress(JSON.dump(success(deferred)))
  response.send_response
end

#success(deferred) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/web_fetch/concerns/http_helpers.rb', line 35

def success(deferred)
  result = deferred[:http]
  { response: {
    success: true,
    body: result.response,
    headers: result.headers,
    status: result.response_header.status
  },
    uid: deferred[:uid] }
end