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.



48
49
50
51
# File 'lib/metapage.rb', line 48

def initialize(url)
  @url = url
  title
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#canonical_urlObject



89
90
91
92
93
94
95
# File 'lib/metapage.rb', line 89

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

#content_typeObject



113
114
115
# File 'lib/metapage.rb', line 113

def content_type
  mimemagic and mimemagic.type
end

#descriptionObject



66
67
68
69
70
# File 'lib/metapage.rb', line 66

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

#idObject



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

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

#image_urlObject



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

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



109
110
111
# File 'lib/metapage.rb', line 109

def media_type
  mimemagic and mimemagic.mediatype
end

#site_nameObject



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

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

#titleObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/metapage.rb', line 53

def title
  unless image?
    @title ||= (metatag_content('og:title') || html_content('title')).tap do |title|
      if title
        checked_title = title.downcase.gsub(' ', '')
        if IGNORE_LIST.any? {|word| checked_title.include? word }
          raise IgnoredTitleError
        end
      end
    end
  end
end

#to_hObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/metapage.rb', line 117

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



131
132
133
# File 'lib/metapage.rb', line 131

def to_json
  to_h.to_json
end

#typeObject



81
82
83
84
85
86
87
# File 'lib/metapage.rb', line 81

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