class OpenStax::Content::Fragment::Exercise

Constants

ABSOLUTIZED_EMBED_URL_CSS

CSS to find the exercise embed queries after the urls are absolutized

ABSOLUTIZED_EMBED_URL_REGEX

Regex to extract the appropriate embed queries from the absolutized urls

EXERCISE_EMBED_URL_CSS

CSS to find the embed code attributes

EXERCISE_EMBED_URL_REGEXES

Regex to extract the appropriate tag from the embed code(s)

Attributes

embed_queries[R]

Public Class Methods

absolutize_exercise_urls!(node) click to toggle source

This code is run from lib/openstax/cnx/v1/page.rb during import

# File lib/openstax/content/fragment/exercise.rb, line 23
def self.absolutize_exercise_urls!(node)
  uri = Addressable::URI.parse OpenStax::Content.exercises_search_api_url

  node.css(EXERCISE_EMBED_URL_CSS).each do |anchor|
    href = anchor.attribute('href')

    EXERCISE_EMBED_URL_REGEXES.each do |field, regex|
      embed_match = regex.match(href.value)
      next if embed_match.nil?

      uri.query_values = { q: "#{field}:\"#{embed_match[1]}\"" }
      href.value = uri.to_s
      anchor['data-type'] = 'exercise'
      break
    end
  end
end
new(node:, title: nil, labels: []) click to toggle source
Calls superclass method OpenStax::Content::Fragment::new
# File lib/openstax/content/fragment/exercise.rb, line 41
def initialize(node:, title: nil, labels: [])
  super

  @embed_queries = node.css(ABSOLUTIZED_EMBED_URL_CSS).map do |anchor|
    url = anchor.attribute('href').value
    match = ABSOLUTIZED_EMBED_URL_REGEX.match(url)
    next if match.nil?

    [ match[1].to_sym, URI.decode_www_form_component(match[2]) ]
  end.compact
end

Public Instance Methods

blank?() click to toggle source
# File lib/openstax/content/fragment/exercise.rb, line 53
def blank?
  embed_queries.empty? && (node_id.nil? || node_id.empty?)
end