Class: Putio::Middleware::ParseRoot

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/putio-ruby/middleware/parse_root.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/putio-ruby/middleware/parse_root.rb', line 4

def on_complete(env)
  headers = env[:response_headers] ? env[:response_headers] : nil
  return unless headers["content-type"] && headers["content-type"].include?("json")
  plural = env.url.to_s.match(/api\.put\.io\/v2\/(\w+)\//)[1]
  singular = plural[0..-2]
  if env[:body][plural]
    env[:body] = env[:body][plural]
  elsif env[:body][singular]
    env[:body] = env[:body][singular]
  end
end