Module: Onebox::Engine::StandardEmbed

Included in:
ImgurOnebox, SlidesOnebox, SoundCloudOnebox, WhitelistedGenericOnebox, YoutubeOnebox
Defined in:
lib/onebox/engine/standard_embed.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_oembed_provider(regexp, endpoint) ⇒ Object



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

def self.add_oembed_provider(regexp, endpoint)
  oembed_providers[regexp] = endpoint
end

.add_opengraph_provider(regexp) ⇒ Object



17
18
19
# File 'lib/onebox/engine/standard_embed.rb', line 17

def self.add_opengraph_provider(regexp)
  opengraph_providers.push(regexp)
end

.oembed_providersObject



5
6
7
# File 'lib/onebox/engine/standard_embed.rb', line 5

def self.oembed_providers
  @@oembed_providers ||= {}
end

.opengraph_providersObject



9
10
11
# File 'lib/onebox/engine/standard_embed.rb', line 9

def self.opengraph_providers
  @@opengraph_providers ||= Array.new
end

Instance Method Details

#always_https?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/onebox/engine/standard_embed.rb', line 31

def always_https?
  WhitelistedGenericOnebox.host_matches(uri, WhitelistedGenericOnebox.https_hosts)
end

#rawObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/onebox/engine/standard_embed.rb', line 35

def raw
  return @raw if @raw

  StandardEmbed.oembed_providers.each do |regexp, endpoint|
    if url =~ regexp
      fetch_oembed_raw("#{endpoint}?url=#{url}")
      return @raw if @raw
    end
  end

  response = Onebox::Helpers.fetch_response(url)
  html_doc = Nokogiri::HTML(response.body)

  StandardEmbed.opengraph_providers.each do |regexp|
    if url =~ regexp
      @raw = parse_open_graph(html_doc, url)
      return @raw if @raw
    end
  end

  # Determine if we should use oEmbed or OpenGraph (prefers oEmbed)
  oembed_alternate = html_doc.at("//link[@type='application/json+oembed']") || html_doc.at("//link[@type='text/json+oembed']")
  # Do not use oEmbed for WordPress sites (https://meta.discourse.org/t/onebox-for-wordpress-4-4-sites/36765)
  fetch_oembed_raw(oembed_alternate) unless oembed_alternate.nil? || oembed_alternate['href'] =~ /public-api.wordpress.com\/oembed/ || oembed_alternate['href'] =~ /wp-json\/oembed/

  open_graph = parse_open_graph(html_doc, url)
  if @raw
    @raw[:image] = open_graph.images.first if @raw[:image].nil? && open_graph && open_graph.images
    return @raw
  end

  @raw = open_graph
end