class DocTemplate::Tags::PdTag

Constants

CG_RE
PDF_HTTP_RE
PDF_HTTP_REPLACE_RE
PDF_RE
TAG_NAME
TEMPLATES
TYPE_CG
TYPE_PDF
TYPE_PODCAST
TYPE_YOUTUBE

Attributes

description[R]
opts[R]
resource[R]
start[R]
stop[R]
subject[R]
title[R]
url[R]

Public Instance Methods

parse(node, opts = {}) click to toggle source
# File lib/doc_template/tags/pd_tag.rb, line 22
def parse(node, opts = {})
  @opts = opts
  @url, @title, @description, @start, @stop = opts[:value].split(';').map(&:strip)

  unless (embeded = fetch_data)
    node.remove
    return self
  end

  params = {
    description: description,
    resource: resource,
    start: start,
    stop: stop,
    subject: subject,
    title: title
  }.merge(embeded)
  @content = parse_template params, template_name(opts)
  replace_tag node
  self
end

Private Instance Methods

embeded_object() click to toggle source
# File lib/doc_template/tags/pd_tag.rb, line 48
def embeded_object
  if url.index('soundcloud.com')
    embeded_object_soundcloud
  elsif url.index('youtube.com') || url.index('youtu.be')
    embeded_object_youtube
  elsif (id = url.scan(CG_RE).flatten.first)
    embeded_object_cg(id)
  elsif url.match?(PDF_RE)
    embeded_object_pdf
  end
end
embeded_object_cg(id) click to toggle source
# File lib/doc_template/tags/pd_tag.rb, line 60
def embeded_object_cg(id)
  return unless (cg = ContentGuide.find_by(permalink: id) || ContentGuide.find(id))

  @description = @title.presence || cg.description
  @title = cg.title
  @resource = cg

  grade = cg.grades.list.first.presence
  grade = cg.grades.grade_abbr(grade).presence || 'base'

  uri = URI.parse(url)
  cg_url = uri.path
  cg_url += "##{uri.fragment}" unless uri.fragment.blank?

  {
    color: "#{cg.subject}-#{grade}",
    content_guide: cg,
    resource_url: content_guide_path(cg.permalink || cg.id, slug: cg.slug),
    type: TYPE_CG,
    url: cg_url
  }
end
embeded_object_pdf() click to toggle source
# File lib/doc_template/tags/pd_tag.rb, line 83
def embeded_object_pdf
  pdf_url = url.match?(PDF_HTTP_RE) ? url : "https://#{url}"
  pdf_url = pdf_url.sub(PDF_HTTP_REPLACE_RE, 'https:')
  {
    type: TYPE_PDF,
    url: pdf_url
  }
end
embeded_object_soundcloud() click to toggle source
# File lib/doc_template/tags/pd_tag.rb, line 92
def embeded_object_soundcloud
  {
    content: Lcms::Engine::MediaEmbed.soundcloud(url, subject),
    resource_url: (media_path(resource.id) if resource.present?),
    type: TYPE_PODCAST
  }
end
embeded_object_youtube() click to toggle source
# File lib/doc_template/tags/pd_tag.rb, line 100
def embeded_object_youtube
  query = {}.tap do |q|
    q[:start] = start if start.present?
    q[:end] = stop if stop.present?
  end.compact
  youtube_url = "https://www.youtube.com/embed/#{Lcms::Engine::MediaEmbed.video_id(url)}?#{query.to_query}"
  {
    resource_url: (media_path(resource.id) if resource.present?),
    type: TYPE_YOUTUBE,
    url: youtube_url
  }
end
fetch_data() click to toggle source
# File lib/doc_template/tags/pd_tag.rb, line 113
def fetch_data
  @resource = Lcms::Engine::Resource.find_by url: url
  if (title.blank? || description.blank?) && resource
    @title = resource.title if title.blank?
    @description = resource.teaser if description.blank?
  end

  @subject = opts[:metadata].resource_subject
  embeded_object
end