Sha256: 9b5d7e816e0d30800700c1422fb9f171170dfaf0afb8b36035f73549c3cf161e

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 KB

Contents

require 'json'

module Quandl
module Client
module Middleware

class ParseJSON < Faraday::Response::Middleware

  def on_complete(env)
    env[:body] = case env[:status]
    when 204
      parse('{}', env)
    else
      parse(env[:body], env)
    end
  end
  
  def parse(body, env)
    json = parse_json(body, env)
    json.has_key?(:docs) ? format_collection( json, env ) : format_record( json, env )
  end
  
  def format_record(json, env)
    errors = json.delete(:errors) || {}
    metadata = json.delete(:metadata) || {}
    # collect some response data
    metadata.merge!({
      status:                 env[:status],
      headers:                env[:response_headers],
      })
    # return object
    object = {
      :data => json,
      :errors => errors,
      :metadata => metadata
    }
    env[:status] = 200
    object
  end
  
  def format_collection(json, env)
    errors = json.delete(:errors) || {}
    metadata = json.delete(:metadata) || {}
    docs = json.delete(:docs)
    # collect some response data
    metadata.merge!(json).merge!({
      status:                 env[:status],
      headers:                env[:response_headers],
      })
    # each doc metadata references metadata
    docs.each{|d| d[:metadata] = metadata }
    # return object
    object = {
      :data => docs,
      :errors => errors,
      :metadata => metadata
    }
    env[:status] = 200
    object
  end

  def parse_json(body = nil, env)
    body ||= '{}'
    json = begin
      JSON.parse(body).symbolize_keys!
    rescue JSON::ParserError
      nil
    end
    # invalid json body?
    if json.blank?
      # fallback to error message
      json = { 
        id: 1, 
        errors: {
          parse_errors:  [ "Quandl::Client::ParseJSON error. status: #{env[:status]}, body: #{body.inspect}" ]
        }
      }
    end
    json
  end

end

end
end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
quandl_client-2.7.9 lib/quandl/client/middleware/parse_json.rb
quandl_client-2.7.8 lib/quandl/client/middleware/parse_json.rb
quandl_client-2.7.7 lib/quandl/client/middleware/parse_json.rb
quandl_client-2.7.6 lib/quandl/client/middleware/parse_json.rb
quandl_client-2.7.3 lib/quandl/client/middleware/parse_json.rb
quandl_client-2.7.2 lib/quandl/client/middleware/parse_json.rb
quandl_client-2.7.1 lib/quandl/client/middleware/parse_json.rb
quandl_client-2.7.0 lib/quandl/client/middleware/parse_json.rb