Class: Metapage::Metadata

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Metadata

Returns a new instance of Metadata.


45
46
47
48
# File 'lib/metapage.rb', line 45

def initialize(url)
  @url = url
  title
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.


44
45
46
# File 'lib/metapage.rb', line 44

def url
  @url
end

Instance Method Details

#canonical_urlObject


79
80
81
82
83
84
85
# File 'lib/metapage.rb', line 79

def canonical_url
  if image?
    url
  else
    @canonical_url ||= metatag_content('og:url') || link_rel('canonical') || url
  end
end

#content_typeObject


103
104
105
# File 'lib/metapage.rb', line 103

def content_type
  mimemagic and mimemagic.type
end

#descriptionObject


56
57
58
59
60
# File 'lib/metapage.rb', line 56

def description
  unless image?
    @description ||= metatag_content('og:description') || metatag_content('description')
  end
end

#idObject


87
88
89
90
91
# File 'lib/metapage.rb', line 87

def id
  if canonical_url
    @id ||= Digest::SHA1.hexdigest(canonical_url)
  end
end

#image_urlObject


62
63
64
65
66
67
68
69
# File 'lib/metapage.rb', line 62

def image_url
  if image?
    url
  else
    # Fallback to apple-touch-icon, fluid-icon, ms-tileicon etc
    @image_url ||= metatag_content('og:image:secure_url') || metatag_content('og:image') || link_rel('apple-touch-icon-precomposed')
  end
end

#media_typeObject


99
100
101
# File 'lib/metapage.rb', line 99

def media_type
  mimemagic and mimemagic.mediatype
end

#site_nameObject


93
94
95
96
97
# File 'lib/metapage.rb', line 93

def site_name
  unless image?
    @site_name ||= metatag_content('og:site_name') || host
  end
end

#titleObject


50
51
52
53
54
# File 'lib/metapage.rb', line 50

def title
  unless image?
    @title ||= metatag_content('og:title') || html_content('title')
  end
end

#to_hObject


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/metapage.rb', line 107

def to_h
  {
    id: id,
    title: title,
    description: description,
    image_url: image_url,
    type: type,
    canonical_url: canonical_url,
    site_name: site_name,
    media_type: media_type,
    content_type: content_type
  }
end

#to_jsonObject


121
122
123
# File 'lib/metapage.rb', line 121

def to_json
  to_h.to_json
end

#typeObject


71
72
73
74
75
76
77
# File 'lib/metapage.rb', line 71

def type
  if image?
    'image'
  else
    @type ||= metatag_content('og:type') || 'website'
  end
end