Sha256: 5a21742df800c991c3d5744662dfc8175c88a9346890e2a697fc27050c415363

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

require 'json'

module StructuredData
  class Repository
    def initialize
      @items = []
    end

    def <<(item)
      @items << item
    end

    def dump
      "[" + @items.map(&:dump).join(',') + "]"
    end
  end

  class BreadcrumbList
    Item = Struct.new('Item', :url, :name)

    def initialize
      @items = []
    end

    def add(item)
      @items << item
    end

    def <<(item)
      add(item)
    end

    def empty?
      @items.empty?
    end

    def dump
      hash = {
        "@context": "http://schema.org",
        "@type": "BreadcrumbList",
      }

      hash['itemListElement'] = items_to_hash
      JSON.pretty_generate(hash)
    end

    def each(&block)
      @items.map { |i| Item.new(i[:url], i[:name]) }.each(&block)
    end

    def size
      @items.size
    end

    private

    def items_to_hash
      @items.map.with_index do |item, i|
        {
          "@type": "ListItem",
          "position": i + 1,
          "item": {
            "@id": item[:url],
            "name": item[:name]
          }
        }
      end
    end
  end

  class SiteNavigationElement
    def initialize
      @urls  = []
      @names = []
    end

    def <<(item)
      @urls  << item[:url]
      @names << item[:name]

      item
    end

    def empty?
      @urls.empty? && @names.empty?
    end

    def dump
      hash = {
        "@context": "http://schema.org",
        "@type": "SiteNavigationElement",
      }

      hash[:url] = @urls
      hash[:name] = @names

      JSON.pretty_generate(hash)
    end
  end
end

require 'structured_data/rails_extentions'

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
structured-data-0.2.1 lib/structured_data.rb
structured-data-0.2.0 lib/structured_data.rb