Sha256: 0561c836cb8542a32277d76010f4a902d98bfe3004e27117fd1d97e934f4d941

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

module Yaks
  class Serializer
    class CollectionJson < self
      Serializer.register self, :collection_json, 'application/vnd.collection+json'

      include FP

      def serialize_resource(resource)
        result = {
          version: "1.0",
          items: serialize_items(resource)
        }
        result[:href] = resource.self_link.uri if resource.self_link
        {collection: result}
      end

      def serialize_items(resource)
        resource.map do |item|
          attrs = item.attributes.map do |name, value|
            {
              name: name,
              value: value
            }
          end
          result = { data: attrs }
          result[:href] = item.self_link.uri if item.self_link
          item.links.each do |link|
            next if link.rel == :self
            result[:links] ||= []
            result[:links] << {rel: link.rel, href: link.uri}
            result[:links].last[:name] = link.name if link.name
          end
          result
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yaks-0.4.2 lib/yaks/serializer/collection_json.rb
yaks-0.4.1 lib/yaks/serializer/collection_json.rb
yaks-0.4.0 lib/yaks/serializer/collection_json.rb
yaks-0.4.0.rc1 lib/yaks/serializer/collection_json.rb