Class: LinkPreview::Parser

Inherits:
Hash
  • Object
show all
Defined in:
lib/link_preview/parser.rb

Direct Known Subclasses

Page

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Parser

Returns a new instance of Parser.



5
6
7
8
9
# File 'lib/link_preview/parser.rb', line 5

def initialize(uri)
  @uri = uri

  super()
end

Instance Attribute Details

#imagesArray (readonly)

Return all images from the page.

Returns:

  • (Array)


60
61
62
# File 'lib/link_preview/parser.rb', line 60

def images
  @images
end

#titleString (readonly)

Return the title from HTML document.

Returns:

  • (String)


29
30
31
# File 'lib/link_preview/parser.rb', line 29

def title
  @title
end

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/link_preview/parser.rb', line 3

def uri
  @uri
end

Instance Method Details

#descriptionString

Return the description from HTML document.

Returns:

  • (String)


37
38
39
# File 'lib/link_preview/parser.rb', line 37

def description
  LinkPreview::ExternalDescription.new(document)
end

#faviconString

Return the absolute path from a favicon element.

Returns:

  • (String)


52
53
54
# File 'lib/link_preview/parser.rb', line 52

def favicon
  @favicon ||= LinkPreview::ExternalFavicon.new(uri, document)
end

#parse!Object

Parse title, favicon and images from an external page.



13
14
15
16
17
# File 'lib/link_preview/parser.rb', line 13

def parse!
  merge!(link: @uri, title: title, favicon: favicon, images: images, type: type, description: description)
rescue StandardError => exception
  merge!(error: exception.message)
end

#read_attribute_for_serialization(attribute) ⇒ Object

Convenience method to be used in the active model serializers.



66
67
68
# File 'lib/link_preview/parser.rb', line 66

def read_attribute_for_serialization(attribute)
  self[attribute.to_sym]
end

#typeObject

For now all links are considering external. In the future this could be: external, youtube, images.



44
45
46
# File 'lib/link_preview/parser.rb', line 44

def type
  'external'
end

#valid?True, False

Return false if self contain error node. True otherwise.

Returns:

  • (True, False)

    return false if self contain error node. True otherwise.



21
22
23
# File 'lib/link_preview/parser.rb', line 21

def valid?
  self[:error].blank?
end