Sha256: 8d1cc34c19c8100ad2c7ca0cbfffcf7492ded2ee08017e7d5dc93980878af45a

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Yaks
  class Format
    class SimpleJson < self
      register :simple_json, :json, 'application/json'

      include FP

      # @param [yaks::resource] resource
      # @return [hash]
      def serialize_resource(resource)
        result = {}
        items = if resource.is_a?(Yaks::CollectionResource)
                  resource.members.map {|r| serialize_item(r)}
                else
                  serialize_item(resource.attributes)
                end
        result[pluralize(resource.type)] = items

        result
      end

      # @param [yaks::resource] resource
      # @return [hash]
      def serialize_item(resource)
        resource.attributes
      end

      # @param [Yaks::Resource] subresource
      # @return [Hash]
      def serialize_links(subresources)
        {}
      end

      # @param [Yaks::Resource] resource
      # @return [Array, String]
      def serialize_link(resource)
        {}
      end

      # @param [Hash] subresources
      # @param [Array] array
      # @return [Array]
      def serialize_linked_subresources(subresources, array)
        []
      end

      # @param [Array] resources
      # @param [Array] linked
      # @return [Array]
      def serialize_linked_resources(subresource, linked)
        []
      end

      # {shows => [{id: 3, name: 'foo'}]}
      #
      # @param [Yaks::Resource] resource
      # @param [Hash] linked
      # @return [Hash]
      def serialize_subresource(resource, linked)
        serialize_resource(resource)
      end

      def inverse
        Yaks::Reader::SimpleJson.new
      end
    end

    class Reader
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yaks-simple-0.0.1 lib/yaks/format/simple_json.rb