Module: WebFetch::HTTPHelpers
- Included in:
- Server
- Defined in:
- lib/web_fetch/http_helpers.rb
Overview
Convenience methods for WebFetch HTTP layer
Instance Method Summary collapse
- #accept_gzip? ⇒ Boolean
- #compress(string) ⇒ Object
- #default_headers(response) ⇒ Object
- #fail_(request, response) ⇒ Object
- #failure(request) ⇒ Object
- #pending(result, response) ⇒ Object
- #post_data ⇒ Object
- #request_params ⇒ Object
- #respond_immediately(result, response) ⇒ Object
- #succeed(request, response) ⇒ Object
- #success(request) ⇒ Object
Instance Method Details
#accept_gzip? ⇒ Boolean
85 86 87 88 |
# File 'lib/web_fetch/http_helpers.rb', line 85 def accept_gzip? # em-http-request doesn't do us any favours with parsing the HTTP headers @http_headers.downcase.include?('accept-encoding: gzip') end |
#compress(string) ⇒ Object
22 23 24 25 26 |
# File 'lib/web_fetch/http_helpers.rb', line 22 def compress(string) return string unless accept_gzip? ActiveSupport::Gzip.compress(string) end |
#default_headers(response) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/web_fetch/http_helpers.rb', line 28 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' if accept_gzip? response.headers['Vary'] = 'Accept-Encoding' end |
#fail_(request, response) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/web_fetch/http_helpers.rb', line 66 def fail_(request, response) response.status = 200 response.content = compress(JSON.dump(failure(request))) response.send_response storage.delete(request[:uid]) end |
#failure(request) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/web_fetch/http_helpers.rb', line 73 def failure(request) result = request[:deferred] { response: { success: false, body: result.response, headers: result.headers, status: result.response_header.status, error: (result.error&.inspect) }, uid: request[:uid] } end |
#pending(result, response) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/web_fetch/http_helpers.rb', line 12 def pending(result, response) respond_immediately({ payload: { uid: result[:request][:uid], pending: true, message: I18n.t(:pending) } }, response) end |
#post_data ⇒ Object
42 43 44 45 46 |
# File 'lib/web_fetch/http_helpers.rb', line 42 def post_data return nil unless @http_post_content JSON.parse(@http_post_content, symbolize_names: true) end |
#request_params ⇒ Object
35 36 37 38 39 40 |
# File 'lib/web_fetch/http_helpers.rb', line 35 def request_params { method: @http_request_method, query_string: @http_query_string, post_data: post_data, server: self } end |
#respond_immediately(result, response) ⇒ Object
6 7 8 9 10 |
# File 'lib/web_fetch/http_helpers.rb', line 6 def respond_immediately(result, response) response.status = result[:status] response.content = compress(result[:payload].to_json) response.send_response end |
#succeed(request, response) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/web_fetch/http_helpers.rb', line 48 def succeed(request, response) response.status = 200 response.content = compress(JSON.dump(success(request))) response.send_response storage.delete(request[:uid]) end |
#success(request) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/web_fetch/http_helpers.rb', line 55 def success(request) result = request[:deferred] { response: { success: true, body: result.response, headers: result.headers, status: result.response_header.status }, uid: request[:uid] } end |