Class: ArticleJSON::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/article_json/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



19
20
21
22
# File 'lib/article_json/configuration.rb', line 19

def initialize
  @oembed_user_agent = nil
  @custom_element_exporters = {}
end

Instance Attribute Details

#oembed_user_agentObject

Returns the value of attribute oembed_user_agent.



17
18
19
# File 'lib/article_json/configuration.rb', line 17

def oembed_user_agent
  @oembed_user_agent
end

Instance Method Details

#element_exporter_for(exporter_type, element_type) ⇒ Class|nil

Get custom exporter class for a given exporter and element type

Parameters:

  • exporter_type (Symbol)
  • element_type (Symbol)

Returns:

  • (Class|nil)


75
76
77
# File 'lib/article_json/configuration.rb', line 75

def element_exporter_for(exporter_type, element_type)
  @custom_element_exporters.dig(exporter_type, element_type)
end

#html_element_exportersHash[Symbol => Class]

Deprecated.

use ‘#exporter_for` instead

Return custom HTML exporters

Returns:

  • (Hash[Symbol => Class])


35
36
37
# File 'lib/article_json/configuration.rb', line 35

def html_element_exporters
  @custom_element_exporters[:html] || {}
end

#html_element_exporters=(value) ⇒ Object

Deprecated.

use ‘#register_element_exporters(:html, …)` instead

Set custom HTML exporters

Parameters:

  • value (Hash[Symbol => Class])


42
43
44
# File 'lib/article_json/configuration.rb', line 42

def html_element_exporters=(value)
  @custom_element_exporters[:html] = value
end

#register_element_exporters(exporter, type_class_mapping) ⇒ Object

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

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

Parameters:

  • exporter (Symbol)
  • type_class_mapping (Hash[Symbol => Class])


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/article_json/configuration.rb', line 54

def register_element_exporters(exporter, type_class_mapping)
  unless i(html amp).include?(exporter)
    raise ArgumentError, '`exporter` needs to be either `:html` or `:amp` '\
                         "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

#register_html_element_exporter(type, klass) ⇒ Object

Deprecated.

Use ‘#register_element_exporters_for(:html, …)` instead

Register a new HTML element exporter or overwrite existing ones.

Parameters:

  • type (Symbol)
  • klass (Class)


28
29
30
# File 'lib/article_json/configuration.rb', line 28

def register_html_element_exporter(type, klass)
  register_element_exporters(:html, type => klass)
end