Sha256: 366c59c2a9baa075ba0af3da4781cc1978875755891d68d3ab9d0c7f79984bee

Contents?: true

Size: 1.15 KB

Versions: 7

Compression:

Stored size: 1.15 KB

Contents

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

      include FP

      # @param [Yaks::Resource] resource
      # @return [Hash]
      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

      # @param [Yaks::Resource] resource
      # @return [Array]
      def serialize_items(resource)
        resource.seq.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.equal? :self
            result[:links] = [] unless result.key?(:links)
            result[:links] << {rel: link.rel, href: link.uri}
            result[:links].last[:name] = link.title if link.title
          end
          result
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
yaks-0.7.6 lib/yaks/format/collection_json.rb
yaks-0.7.5 lib/yaks/format/collection_json.rb
yaks-0.7.4 lib/yaks/format/collection_json.rb
yaks-0.7.3 lib/yaks/format/collection_json.rb
yaks-0.7.2 lib/yaks/format/collection_json.rb
yaks-0.7.1 lib/yaks/format/collection_json.rb
yaks-0.7.0 lib/yaks/format/collection_json.rb