Class: LinkPreview::Content

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

Constant Summary collapse

PROPERTIES =
[
  :title,
  :description,
  :site_name,
  :site_url,
  :image_url,
  :image_data,
  :image_content_type,
  :image_file_name,
  :content_url,
  :content_type,
  :content_width,
  :content_height
].freeze
SOURCES =
[:initial, :image, :oembed, :opengraph_embed, :opengraph, :html].freeze
SOURCE_PROPERTIES_TABLE =
{
  oembed: {
    site_name: :provider_name,
    site_url: :provider_url,
    image_url: :thumbnail_url
  },
  opengraph: {
    image_url: [:image_secure_url, :image_url],
    content_url: [:video_secure_url, :video_url],
    content_type: :video_type,
    content_width: :video_width,
    content_height: :video_height
  },
  opengraph_embed: {
    image_url: [:image_secure_url, :image_url],
    content_url: [:video_secure_url, :video_url],
    content_type: :video_type,
    content_width: :video_width,
    content_height: :video_height
  }
}.freeze
PROPERTIES_SOURCE_TABLE =
Hash.new { |h, k| h[k] = {} }.tap do |reverse_property_table|
  SOURCE_PROPERTIES_TABLE.each do |source, table|
    table.invert.each_pair do |keys, val|
      Array.wrap(keys).each do |key|
        reverse_property_table[source][key] = val
      end
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, content_uri, options = {}, sources = {}) ⇒ Content

Returns a new instance of Content.



81
82
83
84
85
86
87
88
89
# File 'lib/link_preview/content.rb', line 81

def initialize(config, content_uri, options = {}, sources = {})
  @config = config
  @content_uri = content_uri
  @options = options
  @sources = Hash.new { |h, k| h[k] = {} }
  crawler.enqueue!(@content_uri)

  add_source_properties!(sources)
end

Instance Attribute Details

#sourcesObject (readonly)

Returns the value of attribute sources.



116
117
118
# File 'lib/link_preview/content.rb', line 116

def sources
  @sources
end

Instance Method Details

#as_oembedObject



118
119
120
121
122
123
124
# File 'lib/link_preview/content.rb', line 118

def as_oembed
  if content_type_embed? || content_type_iframe? || content_type_video? || content_type_flash?
    @sources[:oembed].reverse_merge(as_oembed_video)
  else
    @sources[:oembed].reverse_merge(as_oembed_link)
  end
end

#empty?Boolean

Returns true of at least one content property is present.

Returns:

  • (Boolean)

    true of at least one content property is present



109
110
111
112
113
114
# File 'lib/link_preview/content.rb', line 109

def empty?
  extract_all
  SOURCES.none? do |source|
    @sources[source].any?(&:present?)
  end
end

#found?Boolean

Returns true of at least related content URI has been successfully fetched.

Returns:

  • (Boolean)

    true of at least related content URI has been successfully fetched



103
104
105
106
# File 'lib/link_preview/content.rb', line 103

def found?
  extract_all
  crawler.success?
end

#urlString

Returns permalink URL of resource.

Returns:

  • (String)

    permalink URL of resource



92
93
94
# File 'lib/link_preview/content.rb', line 92

def url
  extract(:url) || @content_uri
end