Class: Onebox::Engine::InstagramOnebox

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

Constant Summary

Constants included from Onebox::Engine

DEFAULT

Instance Attribute Summary

Attributes included from Onebox::Engine

#timeout, #uri, #url

Instance Method Summary collapse

Methods included from LayoutSupport

#layout, max_text, #to_html

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, #options, #options=, origins_to_regexes, #placeholder_html, #to_html

Instance Method Details

#clean_urlObject



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

def clean_url
  url.scan(/^https?:\/\/(?:www\.)?(?:instagram\.com|instagr\.am)\/?(?:.*)\/p\/[a-zA-Z\d_-]+/).flatten.first
end

#dataObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/onebox/engine/instagram_onebox.rb', line 17

def data
  og = get_opengraph

  # There are at least two different versions of the description. e.g.
  # - "3,227 Likes, 88 Comments - An Account (@user.name) on Instagram: “Look at my picture!”"
  # - "@user.name posted on their Instagram profile: “Look at my picture!”"

  m = og.description.match(/\(@([\w\.]+)\) on Instagram/)
  author_name = m[1] if m

  author_name ||= begin
    m = og.description.match(/^\@([\w\.]+)\ posted/)
    m[1] if m
  end

  raise "Author username not found for post #{clean_url}" unless author_name

  permalink = clean_url.gsub("/#{author_name}/", "/")

  { link: permalink,
    title: "@#{author_name}",
    image: og.image,
    description: Onebox::Helpers.truncate(og.title, 250)
  }
end