Class: ADN::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/adn/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_message) ⇒ Message

Returns a new instance of Message.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/adn/message.rb', line 25

def initialize(raw_message)
  set_values(raw_message)
  message_id = id

  if raw_message.length == 2 and
    raw_message.key? :id     and
    raw_message.key? :channel_id

    # If we only have the bare minimum data,
    # assume we want to get values from the server
    message_details = details

    if message_details.has_key? "data"
      set_values(message_details["data"])
    end
  end
end

Instance Attribute Details

#annotationsObject

Returns the value of attribute annotations.



5
6
7
# File 'lib/adn/message.rb', line 5

def annotations
  @annotations
end

#channel_idObject

Returns the value of attribute channel_id.



5
6
7
# File 'lib/adn/message.rb', line 5

def channel_id
  @channel_id
end

#created_atObject



57
58
59
# File 'lib/adn/message.rb', line 57

def created_at
  DateTime.parse(@created_at)
end

#htmlObject

Returns the value of attribute html.



5
6
7
# File 'lib/adn/message.rb', line 5

def html
  @html
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/adn/message.rb', line 5

def id
  @id
end

#machine_onlyObject

Returns the value of attribute machine_only.



5
6
7
# File 'lib/adn/message.rb', line 5

def machine_only
  @machine_only
end

#message_idObject

Returns the value of attribute message_id.



5
6
7
# File 'lib/adn/message.rb', line 5

def message_id
  @message_id
end

#num_repliesObject

Returns the value of attribute num_replies.



5
6
7
# File 'lib/adn/message.rb', line 5

def num_replies
  @num_replies
end

#reply_toObject

Returns the value of attribute reply_to.



5
6
7
# File 'lib/adn/message.rb', line 5

def reply_to
  @reply_to
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/adn/message.rb', line 5

def source
  @source
end

#textObject

Returns the value of attribute text.



5
6
7
# File 'lib/adn/message.rb', line 5

def text
  @text
end

#thread_idObject

Returns the value of attribute thread_id.



5
6
7
# File 'lib/adn/message.rb', line 5

def thread_id
  @thread_id
end

#userObject



61
62
63
# File 'lib/adn/message.rb', line 61

def user
  ADN::User.new(@user)
end

Class Method Details

.by_id(channel_id, id) ⇒ Object



20
21
22
23
# File 'lib/adn/message.rb', line 20

def self.by_id(channel_id, id)
  result = ADN::API::Message.by_id(channel_id, id)
  Message.new(result["data"])
end

.send_message(params) ⇒ Object



14
15
16
17
18
# File 'lib/adn/message.rb', line 14

def self.send_message(params)
  channel_id = params.delete('channel_id')
  result = ADN::API::Message.create(channel_id, params)
  Message.new(result["data"])
end

Instance Method Details

#deleteObject



65
66
67
68
# File 'lib/adn/message.rb', line 65

def delete
  result = ADN::API::Message.delete(channel_id, id)
  ADN.create_instance(result["data"], Message)
end

#detailsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/adn/message.rb', line 43

def details
  # if we have a source, then we've loaded
  # stuff from the server
  if source
    value = self.instance_variables.map do |i|
      [i.to_s.slice(1..-1), self.instance_variable_get(i)]
    end

    Hash[value]
  else
    ADN::API::Message.by_id(channel_id, message_id)
  end
end

#set_values(values) ⇒ Object



70
71
72
73
74
# File 'lib/adn/message.rb', line 70

def set_values(values)
  values.each_pair do |k, v|
    send("#{k}=", v) if respond_to?("#{k}=")
  end
end