# frozen_string_literal: true module Yext module Api module Utils module Middleware # The default response parser for Faraday to get the results from Yext into the structure # that is needed by Spyke. class ResponseParser < Faraday::Response::Middleware def call(env) @parse_env = env @app.call(env).on_complete do |environment| @response_env = environment on_complete(environment) end end def parse(body) # Yext Response: # http://developer.yext.com/docs/api-reference/#section/Policies-and-Conventions # # Example response format: # { # "meta": { # "uuid": "bb0c7e19-4dc3-4891-bfa5-8593b1f124ad", # "errors": [ # { # "code": ...error code..., # "type": ...error, fatal error, non fatal error, or warning..., # "message": ...explanation of the issue... # } # ] # }, # "response": { # ...results... # } # } # # Spyke Expected Response Format: # https://github.com/balvig/spyke#configuration # # { data: { id: 1, name: 'Bob' }, metadata: {}, errors: {} } # # Errors: # https://github.com/balvig/spyke#api-side-validations # # { title: [{ error: 'blank'}, { error: 'too_short', count: 10 }]} if body.starts_with?("