Class: Yext::Api::Utils::Middleware::ResponseParser

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/yext/api/utils/middleware/response_parser.rb

Overview

The default response parser for Faraday to get the results from Yext into the structure that is needed by Spyke.

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/yext/api/utils/middleware/response_parser.rb', line 10

def call(env)
  @parse_env = env

  @app.call(env).on_complete do |environment|
    @response_env = environment
    on_complete(environment)
  end
end

#parse(body) ⇒ Object



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/yext/api/utils/middleware/response_parser.rb', line 19

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?("<html")
    (parse_env, {}, response_env[:status], body: body)

    return {}
  end

  @yext_response_json = {}

  data      = data_value(body)
   = 

  (parse_env, , response_env[:status], data)

  { data: data, metadata: , errors: {} }
end