Sha256: 78facc9a9177f202dd8f4231b78a64caa4e42753f2ccbb85323859d5bf1606ce

Contents?: true

Size: 784 Bytes

Versions: 3

Compression:

Stored size: 784 Bytes

Contents

require 'rufus-json/automatic'

module Kindergarten
  class Event
    attr_reader :name, :purpose, :payload

    def self.load(hash)
      if hash.is_a?(String)
        hash = begin
          Rufus::Json.decode(hash)
        rescue => ex
          raise ArgumentError.new("The provided string could not be decoded as JSON")
        end
      end

      hash.symbolize_keys!
      self.new(hash[:name], hash[:purpose], hash[:payload])
    end

    def initialize(name, purpose, payload)
      @name    = name    || raise("An event must have a name")
      @purpose = purpose || raise("An event must have a purpose")
      @payload = payload
    end

    def to_json
      Rufus::Json.encode(
        :name => @name, :purpose => @purpose, :payload => @payload
      )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kindergarten-0.2.1 lib/kindergarten/event.rb
kindergarten-0.2.0 lib/kindergarten/event.rb
kindergarten-0.1.1 lib/kindergarten/event.rb