Class: PreviewGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/concerns/preview_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ PreviewGenerator


7
8
9
# File 'app/concerns/preview_generator.rb', line 7

def initialize(uri)
  @uri = uri
end

Instance Attribute Details

#codeObject

Returns the value of attribute code


5
6
7
# File 'app/concerns/preview_generator.rb', line 5

def code
  @code
end

#typeObject

Returns the value of attribute type


4
5
6
# File 'app/concerns/preview_generator.rb', line 4

def type
  @type
end

#uriObject

Returns the value of attribute uri


3
4
5
# File 'app/concerns/preview_generator.rb', line 3

def uri
  @uri
end

Instance Method Details

#generate_previewObject


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/concerns/preview_generator.rb', line 11

def generate_preview
  website = LinkThumbnailer.generate(uri)

  video = website.videos.find { |v| v.embed_code.present? }
  if video then
    self.code = video.embed_code
    self.type = :video
  else
    image = website.images.first
    if image then
      self.code = image.src
      self.type = :image
    else
      self.type = :none
    end
  end
rescue Net::HTTPServerException => e
  Rails.logger.warn("Could not generate preview for url #{uri}: #{e}")
end

#image?Boolean


35
36
37
# File 'app/concerns/preview_generator.rb', line 35

def image?
  self.type == :image
end

#none?Boolean


39
40
41
# File 'app/concerns/preview_generator.rb', line 39

def none?
  self.type == :none
end

#video?Boolean


31
32
33
# File 'app/concerns/preview_generator.rb', line 31

def video?
  self.type == :video
end