class EmbedRb::OpenGraph_

Public Class Methods

new(input, output, options, embeds) click to toggle source
# File lib/embedrb/embed_open_graph.rb, line 9
def initialize(input, output, options, embeds)
  @input = input
  @output = output
  @options = options
  @embeds = embeds
  @url_regex = EmbedRb.url_regex
  pattern = ['flickr\.com|youtube\.com|youtu\.be|\.mp4|\.ogv|\.webm|\.mp3|\.wav|\.gif|\.pdf|\.doc|\.ppt|\.docx|\.jpg|\.jpeg|\.tiff|\.png|\.svg|\.webp|\.ogg']
  openGraphOptions = options[:openGraphOptions]
  if openGraphOptions[:excluded_regex]
    pattern[1] = openGraphOptions[:excluded_regex]
  end
  @excluded_regex = Regexp.new(pattern.join("|"), Regexp::IGNORECASE)
end

Public Instance Methods

process() click to toggle source
# File lib/embedrb/embed_open_graph.rb, line 23
def process()
  @input.scan(@url_regex) {|match|
    url = match[2]
    short_url = shorten(url)
    if !exclude?(url) && !@options[:served].include?(short_url) && EmbedRb.process_more?(@options, :openGraphEndpoint, @embeds)
      data = fetch(url)
      if data
        @options[:served] << short_url
        @embeds << {
          :key => url,
          :text => render(data)
        }
      end
    end
  }

  @output
end

Private Instance Methods

exclude?(url) click to toggle source
# File lib/embedrb/embed_open_graph.rb, line 69
def exclude?(url)
  url.match(@excluded_regex) ? true : false
end
fetch(url) click to toggle source
# File lib/embedrb/embed_open_graph.rb, line 43
def fetch(url)
  begin
    OpenGraph.new(url)
  rescue StandardError => e
    # error handling
    p e
    return nil
  end
end
render(data) click to toggle source
# File lib/embedrb/embed_open_graph.rb, line 53
    def render(data)
      return <<EOF
        <div class="ejs-embed ejs-ogp">
                      <div class="ejs-ogp-thumb" style="background-image:url(#{data.images ? data.images[0] : ''})" ></div>
                      <div class="ejs-ogp-details">
                        <div class="ejs-ogp-title">
              <a href="#{data.url}" target="#{@options[:linkOptions][:target]}">
                #{data.title}
              </a>
            </div>
                        <div class="ejs-ogb-details">#{data.description}</div>
          </div>
        </div>
EOF
    end