Class: Onebox::Engine::AllowlistedGenericOnebox

Inherits:
Object
  • Object
show all
Includes:
Onebox::Engine, StandardEmbed, LayoutSupport
Defined in:
lib/onebox/engine/allowlisted_generic_onebox.rb

Instance Attribute Summary

Attributes included from Onebox::Engine

#errors, #options, #timeout, #uri, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LayoutSupport

#layout, max_text

Methods included from StandardEmbed

add_oembed_provider, add_opengraph_provider, #always_https?, oembed_providers, opengraph_providers, #raw

Methods included from Onebox::Engine

all_iframe_origins, engines, included, #initialize, origins_to_regexes

Class Method Details

.===(other) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 57

def self.===(other)
  if other.is_a?(URI)
    (
      begin
        IPAddr.new(other.hostname)
      rescue StandardError
        nil
      end
    ).nil?
  else
    true
  end
end

.allowed_twitter_labelsObject



53
54
55
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 53

def self.allowed_twitter_labels
  ["brand", "price", "usd", "cad", "reading time", "likes"]
end

.article_html_hostsObject



45
46
47
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 45

def self.article_html_hosts
  %w[imdb.com]
end

.default_html_providersObject

Often using the ‘html` attribute is not what we want, like for some blogs that include the entire page HTML. However for some providers like Flickr it allows us to return gifv and galleries.



20
21
22
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 20

def self.default_html_providers
  %w[Flickr Meetup]
end

.host_matches(uri, list) ⇒ Object



49
50
51
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 49

def self.host_matches(uri, list)
  !!list.find { |h| /(^|\.)#{Regexp.escape(h)}$/.match(uri.host) }
end

.html_providersObject



24
25
26
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 24

def self.html_providers
  @html_providers ||= default_html_providers.dup
end

.html_providers=(new_provs) ⇒ Object



28
29
30
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 28

def self.html_providers=(new_provs)
  @html_providers = new_provs
end

.https_hostsObject



41
42
43
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 41

def self.https_hosts
  %w[slideshare.net dailymotion.com livestream.com imgur.com flickr.com]
end

.priorityObject



13
14
15
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 13

def self.priority
  200
end

.rewritesObject

A re-written URL converts http:// -> https://



33
34
35
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 33

def self.rewrites
  @rewrites ||= https_hosts.dup
end

.rewrites=(new_list) ⇒ Object



37
38
39
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 37

def self.rewrites=(new_list)
  @rewrites = new_list
end

Instance Method Details

#dataObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 91

def data
  @data ||=
    begin
      html_entities = HTMLEntities.new
      d = { link: link }.merge(raw)

      if !Onebox::Helpers.blank?(d[:title])
        d[:title] = html_entities.decode(Onebox::Helpers.truncate(d[:title], 80))
      end

      d[:description] ||= d[:summary]
      if !Onebox::Helpers.blank?(d[:description])
        d[:description] = html_entities.decode(Onebox::Helpers.truncate(d[:description], 250))
      end

      if !Onebox::Helpers.blank?(d[:site_name])
        d[:domain] = html_entities.decode(Onebox::Helpers.truncate(d[:site_name], 80))
      elsif !Onebox::Helpers.blank?(d[:domain])
        d[:domain] = "http://#{d[:domain]}" unless d[:domain] =~ %r{^https?://}
        d[:domain] = begin
          URI(d[:domain]).host.to_s.sub(/^www\./, "")
        rescue StandardError
          nil
        end
      end

      # prefer secure URLs
      d[:image] = d[:image_secure_url] || d[:image_url] || d[:thumbnail_url] || d[:image]
      d[:image] = Onebox::Helpers.get_absolute_image_url(d[:image], @url)
      d[:image] = Onebox::Helpers.normalize_url_for_output(html_entities.decode(d[:image]))
      d[:image] = nil if Onebox::Helpers.blank?(d[:image])

      d[:video] = d[:video_secure_url] || d[:video_url] || d[:video]
      d[:video] = nil if Onebox::Helpers.blank?(d[:video])

      d[:published_time] = d[:article_published_time] unless Onebox::Helpers.blank?(
        d[:article_published_time],
      )
      if !Onebox::Helpers.blank?(d[:published_time])
        d[:article_published_time] = Time.parse(d[:published_time]).strftime("%-d %b %y")
        d[:article_published_time_title] = Time.parse(d[:published_time]).strftime(
          "%I:%M%p - %d %B %Y",
        )
      end

      # Twitter labels
      if !Onebox::Helpers.blank?(d[:label1]) && !Onebox::Helpers.blank?(d[:data1]) &&
           !!AllowlistedGenericOnebox.allowed_twitter_labels.find { |l|
             d[:label1] =~ /#{l}/i
           }
        d[:label_1] = Onebox::Helpers.truncate(d[:label1])
        d[:data_1] = Onebox::Helpers.truncate(d[:data1])
      end
      if !Onebox::Helpers.blank?(d[:label2]) && !Onebox::Helpers.blank?(d[:data2]) &&
           !!AllowlistedGenericOnebox.allowed_twitter_labels.find { |l|
             d[:label2] =~ /#{l}/i
           }
        if Onebox::Helpers.blank?(d[:label_1])
          d[:label_1] = Onebox::Helpers.truncate(d[:label2])
          d[:data_1] = Onebox::Helpers.truncate(d[:data2])
        else
          d[:label_2] = Onebox::Helpers.truncate(d[:label2])
          d[:data_2] = Onebox::Helpers.truncate(d[:data2])
        end
      end

      if Onebox::Helpers.blank?(d[:label_1]) && !Onebox::Helpers.blank?(d[:price_amount]) &&
           !Onebox::Helpers.blank?(d[:price_currency])
        d[:label_1] = "Price"
        d[:data_1] = Onebox::Helpers.truncate(
          "#{d[:price_currency].strip} #{d[:price_amount].strip}",
        )
      end

      skip_missing_tags = [:video]
      d.each do |k, v|
        next if skip_missing_tags.include?(k)
        if v == nil || v == ""
          errors[k] ||= []
          errors[k] << "is blank"
        end
      end

      d
    end
end

#placeholder_htmlObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 75

def placeholder_html
  return article_html if (is_article? || force_article_html?)
  return image_html if is_image?
  if !SiteSetting.enable_diffhtml_preview? && (is_video? || is_card?)
    return Onebox::Helpers.video_placeholder_html
  end
  if !SiteSetting.enable_diffhtml_preview? && is_embedded?
    return Onebox::Helpers.generic_placeholder_html
  end
  to_html
end

#to_htmlObject



71
72
73
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 71

def to_html
  rewrite_https(generic_html)
end

#verified_dataObject



87
88
89
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 87

def verified_data
  data
end