Class: LinkPreview::Parser
- Inherits:
-
Hash
- Object
- Hash
- LinkPreview::Parser
- Defined in:
- lib/link_preview/parser.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#images ⇒ Array
readonly
Return all images from the page.
-
#title ⇒ String
readonly
Return the title from HTML document.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#description ⇒ String
Return the description from HTML document.
-
#favicon ⇒ String
Return the absolute path from a favicon element.
-
#initialize(uri) ⇒ Parser
constructor
A new instance of Parser.
-
#parse! ⇒ Object
Parse title, favicon and images from an external page.
-
#read_attribute_for_serialization(attribute) ⇒ Object
Convenience method to be used in the active model serializers.
-
#type ⇒ Object
For now all links are considering external.
-
#valid? ⇒ True, False
Return false if self contain error node.
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
#images ⇒ Array (readonly)
Return all images from the page.
60 61 62 |
# File 'lib/link_preview/parser.rb', line 60 def images @images end |
#title ⇒ String (readonly)
Return the title from HTML document.
29 30 31 |
# File 'lib/link_preview/parser.rb', line 29 def title @title end |
#uri ⇒ Object (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
#description ⇒ String
Return the description from HTML document.
37 38 39 |
# File 'lib/link_preview/parser.rb', line 37 def description LinkPreview::ExternalDescription.new(document) end |
#favicon ⇒ String
Return the absolute path from a favicon element.
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.) 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 |
#type ⇒ Object
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.
21 22 23 |
# File 'lib/link_preview/parser.rb', line 21 def valid? self[:error].blank? end |