class ArticleJSON::Configuration

Attributes

oembed_user_agent[RW]

Public Class Methods

new() click to toggle source
# File lib/article_json/configuration.rb, line 19
def initialize
  @oembed_user_agent = nil
  @custom_element_exporters = {}
end

Public Instance Methods

element_exporter_for(exporter_type, element_type) click to toggle source

Get custom exporter class for a given exporter and element type @param [Symbol] exporter_type @param [Symbol] element_type @return [Class|nil]

# File lib/article_json/configuration.rb, line 54
def element_exporter_for(exporter_type, element_type)
  @custom_element_exporters.dig(exporter_type, element_type)
end
register_element_exporters(exporter, type_class_mapping) click to toggle source

Register new element exporters or overwrite existing ones for a given exporter type. Usage example:

register_element_exporters(:html,
                           image: MyImageExporter,
                           advertisement: MyAdExporter)

@param [Symbol] exporter @param [Hash[Symbol => Class]] type_class_mapping

# File lib/article_json/configuration.rb, line 32
def register_element_exporters(exporter, type_class_mapping)
  valid_exporters = %i(html amp facebook_instant_article plain_text)
  unless valid_exporters.include?(exporter)
    raise ArgumentError, '`exporter` needs to be one of ' \
                         "#{valid_exporters} but is `#{exporter.inspect}`"
  end
  if !type_class_mapping.is_a?(Hash) ||
      type_class_mapping.keys.any? { |key| !key.is_a? Symbol } ||
      type_class_mapping.values.any? { |value| !value.is_a? Class }
    raise ArgumentError, '`type_class_mapping` has to be a Hash with '\
                         'symbolized keys and classes as values but is '\
                         "`#{type_class_mapping.inspect}`"
  end

  @custom_element_exporters[exporter.to_sym] ||= {}
  @custom_element_exporters[exporter.to_sym].merge!(type_class_mapping)
end