Class: Zaikio::Directory::JSONParser

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/zaikio/directory/json_parser.rb

Instance Method Summary collapse

Instance Method Details

#connection_error(env) ⇒ Object



14
15
16
17
18
# File 'lib/zaikio/directory/json_parser.rb', line 14

def connection_error(env)
  Zaikio::Directory.configuration.logger
                   .error("Zaikio::Directory Status Code #{env.status}, #{env.body}")
  raise Spyke::ConnectionError, "Status Code #{env.status}, #{env.body}"
end

#on_complete(env) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/zaikio/directory/json_parser.rb', line 6

def on_complete(env)
  connection_error(env) unless /^(2\d\d)|422|404$/.match?(env.status.to_s)

  raise Spyke::ResourceNotFound if env.status.to_s == "404"

  env.body = parse_body(env.body)
end

#parse_body(body) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/zaikio/directory/json_parser.rb', line 20

def parse_body(body)
  json = MultiJson.load(body, symbolize_keys: true)
  {
    data: json,
    metadata: {},
    errors: json.is_a?(Hash) ? json[:errors] : {}
  }
rescue MultiJson::ParseError
  {
    data: {},
    metadata: {},
    errors: {}
  }
end