Sha256: e9458ba4ec7f4e04c067a97a1effedf477808b68f56689543f82906f8d7fbc2d

Contents?: true

Size: 637 Bytes

Versions: 2

Compression:

Stored size: 637 Bytes

Contents

require 'faraday_middleware/response_middleware'

module FaradayMiddleware
  class ParseOpenfireJson < FaradayMiddleware::ResponseMiddleware

    dependency do
      require 'json' unless defined?(::JSON)
    end

    define_parser do |body|
      if body.strip.empty?
        { data: {}, errors: [] }
      else
        parsed = ::JSON.parse body
        case parsed.keys.length
        when 0
          parsed = []
        when 1
          parsed = parsed.values.first
        end

        if parsed.is_a?(Hash)
          { data: parsed.symbolize_keys }
        else
          { data: parsed }
        end
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
openfire-ruby-0.1.2 lib/faraday_middleware/parse_openfire_json.rb
openfire-ruby-0.1.1 lib/faraday_middleware/parse_openfire_json.rb