Class: Slimmer::Processors::SearchParameterInserter

Inherits:
Object
  • Object
show all
Defined in:
lib/slimmer/processors/search_parameter_inserter.rb

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ SearchParameterInserter

Returns a new instance of SearchParameterInserter.



5
6
7
# File 'lib/slimmer/processors/search_parameter_inserter.rb', line 5

def initialize(response)
  @response = response
end

Instance Method Details

#add_hidden_input(search_form, name, value, doc) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/slimmer/processors/search_parameter_inserter.rb', line 26

def add_hidden_input(search_form, name, value, doc)
  element = Nokogiri::XML::Node.new("input", doc)
  element["type"] = "hidden"
  element["name"] = name
  element["value"] = value.to_s
  search_form.add_child(element)
end

#filter(_old_doc, new_doc) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/slimmer/processors/search_parameter_inserter.rb', line 9

def filter(_old_doc, new_doc)
  search_form = new_doc.at_css("form#search")
  if search_parameters && search_form
    search_parameters.each_pair do |name, value|
      # Value can either be a string or an array of values
      if value.is_a? Array
        array_name = "#{name}[]"
        value.each do |array_value|
          add_hidden_input(search_form, array_name, array_value, new_doc)
        end
      else
        add_hidden_input(search_form, name, value, new_doc)
      end
    end
  end
end

#parse_search_parametersObject



38
39
40
41
# File 'lib/slimmer/processors/search_parameter_inserter.rb', line 38

def parse_search_parameters
  header_value = @response.headers.fetch(Slimmer::Headers::SEARCH_PARAMETERS_HEADER, "{}")
  JSON.parse(header_value)
end

#search_parametersObject



34
35
36
# File 'lib/slimmer/processors/search_parameter_inserter.rb', line 34

def search_parameters
  @search_parameters ||= parse_search_parameters
end