Sha256: cf70b9ab09ae66bfd4c55d0d7f2311269281174fb05fd4e32030d4949b6bd757

Contents?: true

Size: 814 Bytes

Versions: 3

Compression:

Stored size: 814 Bytes

Contents

require 'faraday_middleware'
require 'faraday/response'

module Payable
  module Middleware
    class SymbolizeKeys < FaradayMiddleware::ParseJson
      define_parser do |body|
        break if body.strip.empty?
        symbolize_keys ::JSON.parse(body)
      end

      def self.symbolize_keys(hash)
        hash.inject({}) do |result, (key, value)|
          new_key = case key
          when String then key.to_sym
          else key
          end

          new_value = case value
          when Hash then symbolize_keys(value)
          when Array then value.map{|v| symbolize_keys(v) }
          else value
          end

          result[new_key] = new_value

          result
        end
      end
    end
  end
end

Faraday::Response.register_middleware symbolize_keys: Payable::Middleware::SymbolizeKeys

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
payable-api-0.1.2 lib/payable/middleware.rb
payable-api-0.1.1 lib/payable/middleware.rb
payable-api-0.1.0 lib/payable/middleware.rb