class Newgistics::ResponseHandlers::Search

Attributes

model_class[R]

Public Class Methods

new(model_class:) click to toggle source
# File lib/newgistics/response_handlers/search.rb, line 6
def initialize(model_class:)
  @model_class = model_class
end

Public Instance Methods

handle(response) click to toggle source
# File lib/newgistics/response_handlers/search.rb, line 10
def handle(response)
  if response.success?
    handle_successful_response(response)
  else
    handle_failed_response(response)
  end
end

Private Instance Methods

build_model(model_xml) click to toggle source
# File lib/newgistics/response_handlers/search.rb, line 41
def build_model(model_xml)
  model_class.new.tap do |model|
    XmlMarshaller.new.assign_attributes(model, model_xml)
  end
end
build_models(xml) click to toggle source
# File lib/newgistics/response_handlers/search.rb, line 35
def build_models(xml)
  xml.css(model_class.element_selector).map do |model_xml|
    build_model(model_xml)
  end
end
error_nodes(xml) click to toggle source
# File lib/newgistics/response_handlers/search.rb, line 31
def error_nodes(xml)
  xml.css('errors error') + xml.css('Errors Error')
end
handle_failed_response(response) click to toggle source
# File lib/newgistics/response_handlers/search.rb, line 47
def handle_failed_response(response)
  raise_error "API Error: #{response.status}"
end
handle_successful_response(response) click to toggle source
# File lib/newgistics/response_handlers/search.rb, line 20
def handle_successful_response(response)
  xml = Nokogiri::XML(response.body)
  errors = error_nodes(xml).map(&:text)

  if errors.empty?
    build_models(xml)
  else
    raise_error(errors.join(', '))
  end
end
raise_error(message) click to toggle source
# File lib/newgistics/response_handlers/search.rb, line 51
def raise_error(message)
  raise QueryError, message
end