Class: ActiveShopifyGraphQL::SearchQuery::ValueSanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_shopify_graphql/search_query/value_sanitizer.rb

Overview

Sanitizes values by escaping special characters for Shopify search syntax

Class Method Summary collapse

Class Method Details

.sanitize(value) ⇒ String

Sanitizes a value by escaping special characters

Parameters:

  • The value to sanitize

Returns:

  • The sanitized value



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_shopify_graphql/search_query/value_sanitizer.rb', line 10

def self.sanitize(value)
  value
    .gsub('\\', '\\\\\\\\') # Escape backslashes first: \ becomes \\
    .gsub('"', '\\"') # Escape double quotes with a single backslash
    # Escape single quotes: O'Reilly becomes O\\'Reilly
    # Why 4 backslashes? The escaping happens in layers:
    # 1. Ruby string literal: "\\\\\\\\''" = literal string "\\\\''"
    # 2. String interpolation in "#{key}:'#{escaped_value}'": the \\\' becomes \\'
    # 3. Final GraphQL query: customers(query: "title:'O\\'Reilly'")
    # The double backslash is required by Shopify's search syntax to properly escape the single quote
    .gsub("'", "\\\\\\\\'")
end