Sha256: 8bb750f2ad9640e4be14e79eae0c93f53475874e075f6924540275c9db003655

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module Sched
  class Event
    SCHED_ATTRIBUTES = [
      # Required
      :session_key, :name, :session_start, :session_end, :session_type,
      # Optional
      :session_subtype, :description, :panelists, :url, :media_url, :venue,
      :address, :map, :tags, :active
    ].freeze
    SCHED_ATTRIBUTES.each { |attribute| attr_accessor attribute }
    attr_accessor :client

    def initialize(session_key, client = nil)
      @session_key = session_key
      @client = client
    end

    def get_attribute(key)
      send(key.to_s)
    end

    def configure(options = {})
      options.each do |key, value|
        send("#{key}=", value) if SCHED_ATTRIBUTES.include?(key)
      end
      self
    end

    def data
      data = {}
      SCHED_ATTRIBUTES.each do |attribute|
        next if get_attribute(attribute).nil?
        value = get_attribute(attribute)
        value = "Y" if value == true
        value = "N" if value == false
        data[attribute] = value
      end
      data
    end

    def save
      if exists?
        update
      else
        create
      end
    end

    def create
      client.request("session/add", data)
    end

    def update
      client.request("session/mod", data)
    end

    def exists?
      client.events.map(&:session_key).include?(session_key) ? true : false
    end

    def destroy
      return unless exists?
      client.request("session/del", session_key: session_key)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sched-0.1.14 lib/sched/event.rb
sched-0.1.13 lib/sched/event.rb
sched-0.1.12 lib/sched/event.rb