Class: HTMLRender::Renderers::HTTPRenderer
- Defined in:
- lib/html_render/renderers.rb
Overview
Renders using an HTTP server built for the very purpose.
One should POST the HTML to the given URL, and the server should respond with a PNG image.
Defined Under Namespace
Classes: ServerError
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url) ⇒ HTTPRenderer
constructor
A new instance of HTTPRenderer.
- #render(html) ⇒ Object
Constructor Details
#initialize(url) ⇒ HTTPRenderer
Returns a new instance of HTTPRenderer.
22 23 24 |
# File 'lib/html_render/renderers.rb', line 22 def initialize(url) @url = url end |
Instance Attribute Details
#url ⇒ Object
Returns the value of attribute url.
20 21 22 |
# File 'lib/html_render/renderers.rb', line 20 def url @url end |
Instance Method Details
#render(html) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/html_render/renderers.rb', line 26 def render(html) client = HTTPClient.new begin response = client.post(url, html, { 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8' }) rescue HTTPClient::BadResponseError => e raise ServerError.new("Bad response from #{url}: #{e.}") rescue HTTPClient::TimeoutError => e raise ServerError.new("Timeout from #{url}: #{e.}") rescue HTTPClient::RetryableResponse => e raise ServerError.new("Retryable response from #{url}: #{e.}") rescue HTTPClient::KeepAliveDisconnected => e raise ServerError.new("Keep-Alive disconnected from #{url}: #{e.}") end if response.status != 200 raise ServerError.new("Unexpected HTTP server response from #{url}: #{response.inspect}") end HTMLRender::Images::PNGImage.new(response.content) end |