Class: Redd::Response::ParseJson

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/redd/response/parse_json.rb

Overview

Faraday Middleware that parses JSON using Oj.

Instance Method Summary collapse

Instance Method Details

#call(faraday) ⇒ Object

Call the middleware.

Parameters:

  • faraday


12
13
14
15
16
# File 'lib/redd/response/parse_json.rb', line 12

def call(faraday)
  @app.call(faraday).on_complete do |env|
    env[:body] = parse(env[:body])
  end
end

#parse(body) ⇒ Hash (private)

Parse a JSON string and return a symbolized hash.

Parameters:

  • body (String)

    The JSON string to parse.

Returns:

  • (Hash)

    A symbolized parsed JSON hash.



24
25
26
27
28
# File 'lib/redd/response/parse_json.rb', line 24

def parse(body)
  MultiJson.load(body, symbolize_keys: true)
rescue MultiJson::ParseError
  body
end