Class: SmstoolsApi::Middleware::Response::HashieJson

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/smstools_api/middleware/response/hashie_json.rb

Constant Summary collapse

CONTENT_TYPE =
"Content-Type".freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, client) ⇒ HashieJson

Returns a new instance of HashieJson.


9
10
11
12
# File 'lib/smstools_api/middleware/response/hashie_json.rb', line 9

def initialize(app, client)
  super(app)
  @client = client
end

Instance Method Details

#on_complete(env) ⇒ Object


14
15
16
17
18
19
20
21
22
23
# File 'lib/smstools_api/middleware/response/hashie_json.rb', line 14

def on_complete(env)
  type = env[:response_headers][CONTENT_TYPE].to_s
  type = type.split(";", 2).first if type.index(";")

  return unless type == "application/json"

  unless env[:body].strip.empty?
    env[:body] = Hashie::Mash.new(JSON.parse(env[:body]))
  end
end