Sha256: fa0be2a4a5d91811367b2589c6eccc9b245f428c3a067860a041063db715088e

Contents?: true

Size: 1.48 KB

Versions: 30

Compression:

Stored size: 1.48 KB

Contents

module Her
  module Middleware
    # This middleware expects the resource/collection data to be contained in the `data`
    # key of the JSON object
    class MnoeApiV1ParseJson < ParseJSON
      # Parse the response body
      #
      # @param [String] body The response body
      # @return [Mixed] the parsed response
      # @private
      def parse(body)
        json = parse_json(body)
        parse_types({
          :data => json[:data] || {},
          :errors => json[:errors] || {},
          :metadata => json[:metadata] || {}
        })
      end
      
      def parse_types(res)
        case
        when res.kind_of?(Array)
          return res.map { |e| parse_types(e) }
        when res.kind_of?(Hash) && res[:cents] && res[:currency]
          Money.new(res[:cents],res[:currency])
        when res.kind_of?(Hash)
          hash = res.dup
          hash.each do |k,v|
            hash[k] = parse_types(v)
          end
          return hash
        when res.is_a?(String) && res =~ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/i
          return Time.iso8601(res)
        else
          return res
        end
      end

      # This method is triggered when the response has been received. It modifies
      # the value of `env[:body]`.
      #
      # @param [Hash] env The response environment
      # @private
      def on_complete(env)
        env[:body] = case env[:status]
        when 204
          parse('{}')
        else
          parse(env[:body])
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
mno-enterprise-core-3.4.0 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.3.3 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.3.2 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.2.1 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.3.1 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.3.0 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.2.0 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.1.4 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.0.7 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-2.0.9 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.1.3 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.0.6 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-2.0.8 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.1.2 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.0.5 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-2.0.7 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-2.0.6 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-2.0.5 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.1.1 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb
mno-enterprise-core-3.0.4 lib/her_extension/middleware/mnoe_api_v1_parse_json.rb