module GetOgp

Constants

VERSION

Public Class Methods

get_data(url) click to toggle source
# File lib/get_ogp.rb, line 9
def self.get_data(url)
  response = Faraday.get(url)
  html = Nokogiri::HTML.parse(response.body)
  ogp_date = {}
  html.css('meta').each do |meta|
    if (meta.attribute('property') && meta.attribute('property').to_s.match(/^og:(.+)$/i)) || (meta.attribute('name') && meta.attribute('name').to_s.match(/^og:(.+)$/i))
      ogp_date[$1] = meta.attribute('content').to_s
    end
  end
  ogp_date['title'] = html.css('title')&.text if !ogp_date.has_key?('title') && html.css('title')&.text.present?
  return false if ogp_date.keys.empty?
  ogp_date
rescue => e
  p e
  false
end