Sha256: 81446e6d0626567bf17f00ed8cb5c6e45230f1a07e55c06be1c19d65c991e7f9

Contents?: true

Size: 900 Bytes

Versions: 11

Compression:

Stored size: 900 Bytes

Contents

module Her
  module Middleware
    # This middleware treat the received first-level JSON structure as the resource data.
    class FirstLevelParseJSON < Faraday::Response::Middleware
      # Parse the response body
      #
      # @param [String] body The response body
      # @return [Mixed] the parsed response
      def parse(body) # {{{
        json = MultiJson.load(body, :symbolize_keys => true)
        errors = json.delete(:errors) || []
        metadata = json.delete(:metadata) || []
        {
          :data => json,
          :errors => errors,
          :metadata => metadata
        }
      end # }}}

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
her-0.3.3 lib/her/middleware/first_level_parse_json.rb
her-0.3.2 lib/her/middleware/first_level_parse_json.rb
her-0.3.1 lib/her/middleware/first_level_parse_json.rb
her-0.3 lib/her/middleware/first_level_parse_json.rb
her-0.2.6 lib/her/middleware/first_level_parse_json.rb
her-0.2.5 lib/her/middleware/first_level_parse_json.rb
her-0.2.4 lib/her/middleware/first_level_parse_json.rb
her-0.2.3 lib/her/middleware/first_level_parse_json.rb
her-0.2.2 lib/her/middleware/first_level_parse_json.rb
her-0.2.1 lib/her/middleware/first_level_parse_json.rb
her-0.2 lib/her/middleware/first_level_parse_json.rb