Class: Landable::ScreenshotService

Inherits:
Object
  • Object
show all
Defined in:
app/services/landable/screenshot_service.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.capture(url) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/landable/screenshot_service.rb', line 8

def capture(url)
  if !Landable.configuration.publicist_url
    Rails.logger.warn "Couldn't generate screenshot for #{url}; no Landable.configuration.publicist_url configured"
  else
    screenshots_uri = URI(Landable.configuration.publicist_url)
    screenshots_uri.path = '/api/services/screenshots'

    response = Net::HTTP.post_form screenshots_uri, 'screenshot[url]' => url

    if response.code == '200'
      file = Tempfile.new ['screenshot-', '.png']
      file.binmode
      file.write response.body
      file.rewind

      file
    else
      fail Error, "Received #{response.code} back from #{screenshots_uri}"
    end
  end
end