Module: GetOgp

Defined in:
lib/get_ogp.rb,
lib/get_ogp/version.rb

Constant Summary collapse

VERSION =
"0.1.4"

Class Method Summary collapse

Class Method Details

.get_data(url) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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