Module: Firering::Instantiator

Included in:
Message, Room, Upload, User
Defined in:
lib/firering/instantiator.rb

Instance Method Summary collapse

Instance Method Details

#instantiate(conn, data, base_key = nil, &callback) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/firering/instantiator.rb', line 4

def instantiate(conn, data, base_key = nil, &callback)
  instance = new
  instance.connection = conn

  attributes = data.is_a?(Hash) ? data : Yajl::Parser.parse(data, :symbolize_keys => true)
  attributes = attributes[base_key] if base_key
  attributes ||= Hash.new

  attributes.each do |key, val|
    setter = "#{key}="
    value = ( key.to_s =~ /(_at|_on)$/ ) ? (Time.parse(val) rescue val) : val
    if instance.respond_to?(setter)
      instance.send(setter, value)
    else
      msg = "WARNING: Could not set attribute '#{key}' to value '#{value}' on #{self} object."
      msg << " It is likely the Campfire API has changed. Please report this! (https://github.com/EmmanuelOga/firering/issues)"
      instance.connection.logger.warn msg
    end
  end

  callback.call(instance) if callback
  instance
end