Class: ProductTaxonomy::MappingRule

Inherits:
Object
  • Object
show all
Defined in:
lib/product_taxonomy/models/mapping_rule.rb

Overview

A mapping rule for converting between the integration's taxonomy and Shopify's taxonomy.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_category:, output_category:) ⇒ MappingRule

Returns a new instance of MappingRule.



50
51
52
53
# File 'lib/product_taxonomy/models/mapping_rule.rb', line 50

def initialize(input_category:, output_category:)
  @input_category = input_category
  @output_category = output_category
end

Instance Attribute Details

#input_categoryObject (readonly)

Returns the value of attribute input_category.



47
48
49
# File 'lib/product_taxonomy/models/mapping_rule.rb', line 47

def input_category
  @input_category
end

#output_categoryObject

Returns the value of attribute output_category.



48
49
50
# File 'lib/product_taxonomy/models/mapping_rule.rb', line 48

def output_category
  @output_category
end

Class Method Details

.load_rules_from_source(integration_path:, direction:, full_names_by_id:) ⇒ Array<MappingRule>?

Load mapping rules from the provided source data directory for a single direction.

Parameters:

  • integration_path (String)

    The path to the integration version source data directory.

  • direction (Symbol)

    The direction of the mapping rules to load (:from_shopify or :to_shopify).

  • full_names_by_id (Hash<String, Hash>)

    A hash of full names by ID.

Returns:

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/product_taxonomy/models/mapping_rule.rb', line 13

def load_rules_from_source(integration_path:, direction:, full_names_by_id:)
  file_path = File.expand_path("mappings/#{direction}.yml", integration_path)
  return unless File.exist?(file_path)

  data = YAML.safe_load_file(file_path)
  raise ArgumentError, "Mapping rules file does not contain a hash: #{file_path}" unless data.is_a?(Hash)
  raise ArgumentError, "Mapping rules file does not have a `rules` key: #{file_path}" unless data.key?("rules")
  unless data["rules"].is_a?(Array)
    raise ArgumentError, "Mapping rules file `rules` value is not an array: #{file_path}"
  end

  data["rules"].map do |rule|
    input_id = rule.dig("input", "product_category_id")&.to_s
    output_id = rule.dig("output", "product_category_id")&.first&.to_s

    raise ArgumentError, "Invalid mapping rule: #{rule}" if input_id.nil? || output_id.nil?

    is_to_shopify = direction == :to_shopify
    input_category = is_to_shopify ? full_names_by_id[input_id] : Category.find_by(id: input_id)
    # We set output_category to be the raw output ID if is_to_shopify is true, with an expectation that it will
    # be resolved to a `Category` before the rule is serialized to JSON or TXT.
    # See `IntegrationVersion#resolve_to_shopify_mappings`.
    output_category = is_to_shopify ? output_id : full_names_by_id[output_id]

    raise ArgumentError, "Input category not found for mapping rule: #{rule}" unless input_category
    raise ArgumentError, "Output category not found for mapping rule: #{rule}" unless output_category

    new(input_category:, output_category:)
  rescue TypeError, NoMethodError
    raise ArgumentError, "Invalid mapping rule: #{rule}"
  end
end

Instance Method Details

#input_txt_equals_output_txt?Boolean

Whether the input and output categories have the same full name.

Returns:

  • (Boolean)


82
83
84
# File 'lib/product_taxonomy/models/mapping_rule.rb', line 82

def input_txt_equals_output_txt?
  category_txt(@input_category) == category_txt(@output_category)
end

#to_jsonHash

Generate a JSON representation of the mapping rule.

Returns:

  • (Hash)


58
59
60
61
62
63
64
65
66
67
# File 'lib/product_taxonomy/models/mapping_rule.rb', line 58

def to_json
  {
    input: {
      category: category_json(@input_category),
    },
    output: {
      category: [category_json(@output_category)],
    },
  }
end

#to_txtString

Generate a TXT representation of the mapping rule.

Returns:

  • (String)


72
73
74
75
76
77
# File 'lib/product_taxonomy/models/mapping_rule.rb', line 72

def to_txt
  <<~TXT
#{category_txt(@input_category)}
#{category_txt(@output_category)}
  TXT
end