Class: DropboxApi::MiddleWare::DecodeResult

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/dropbox_api/middleware/decode_result.rb

Instance Method Summary collapse

Instance Method Details

#call(rq_env) ⇒ Object


3
4
5
6
7
8
9
10
11
# File 'lib/dropbox_api/middleware/decode_result.rb', line 3

def call(rq_env)
  @app.call(rq_env).on_complete do |rs_env|
    if !rs_env[:response_headers]['Dropbox-Api-Result'].nil?
      rs_env[:api_result] = decode rs_env[:response_headers]['Dropbox-Api-Result']
    elsif rs_env[:response_headers]['content-type'] == 'application/json'
      rs_env[:api_result] = decode rs_env[:body]
    end
  end
end

#decode(json) ⇒ Object


13
14
15
16
17
18
19
20
21
22
# File 'lib/dropbox_api/middleware/decode_result.rb', line 13

def decode(json)
  # Dropbox may send a response with the string 'null' in its body, this
  # would be a void result. `add_folder_member` is an example of an
  # endpoint without return values.
  if json == "null"
    {}
  else
    JSON.parse json
  end
end