class NetworkProfile::DefaultProfile

Attributes

headers[RW]
mdi_icon[RW]

Public Class Methods

all_types() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 31
def self.all_types
  auto_extractor_link_types + [NetworkProfile::Custom]
end
new(link) click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 42
def initialize(link)
  @link = link
end
parse(link, include_fallback_custom: false) click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 35
def self.parse(link, include_fallback_custom: false)
  link_type = (include_fallback_custom ? all_types : auto_extractor_link_types).find { |i| i.handle?(link) }
  if link_type
    link_type.new(link.strip).data
  end
end

Public Instance Methods

data() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 62
def data
  {
    site_icon: self.class.mdi_icon,
    link: @link,
    title: title,
    text: text,
    image: image,
    type: self.class.name.underscore.split('/').last
  }.merge(extra_data)
end
extra_data() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 73
def extra_data
  {}
end
image() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 46
def image
  img = doc.at('meta[property=og\:image]')&.[]('content')
  if img && img[%r{^/\w+}]
    img = URI.join(@link, img).to_s
  end
  img
end
text() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 58
def text
  doc.at('meta[property=og\:description]')&.[]('content') || doc.at('meta[name=description]')&.[]('content')
end
title() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 54
def title
  doc.at('title')&.text
end

Private Instance Methods

doc() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 91
def doc
  @doc ||= Nokogiri.parse(response.body)
end
json_ld() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 95
def json_ld
  @json_ld ||=
    begin
      ld = doc.search('script[type*=ld]').first&.text
      if ld
        JSON.parse(ld)
      else
        {}
      end
    end
end
map_rdf(tree) click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 113
def map_rdf(tree)
  tree.
    transform_keys { |v| map_rdf_value(v) }.
    transform_values { |v| map_rdf_value(v) }
end
map_rdf_value(value) click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 119
def map_rdf_value(value)
  case value
  when RDF::Vocabulary::Term then value.fragment
  when RDF::URI then value.to_base
  when RDF::Node then value.id
  when RDF::Literal then value.value
  when Hash then map_rdf(value)
  when Array then value.map { |i| map_rdf_value(i) }
  else
    value
  end
end
rdf() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 107
def rdf
  @rdf ||= map_rdf(
    RDF::Microdata::Reader.new(response.body).to_h
  )
end
response() click to toggle source
# File lib/network_profile/extractors/default_profile.rb, line 79
def response
  options = {
    headers: NetworkProfile.headers,
    followlocation: true
  }
  if NetworkProfile.proxy
    options[:proxy] = NetworkProfile.proxy
    options[:proxyuserpwd] = NetworkProfile.proxy_user_pass if NetworkProfile.proxy_user_pass.present?
  end
  @response ||= Typhoeus.get(@link, options)
end