Class: Ecircle::Message

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

Constant Summary collapse

MessageTypeUnknown =
Class.new(RuntimeError)
MessageGroupNotDefined =
Class.new(RuntimeError)

Instance Attribute Summary

Attributes inherited from Base

#all_fields, #id, #named_attrs

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #init_with_xml, #method_missing

Constructor Details

#initialize(hsh) ⇒ Message

Returns a new instance of Message.



23
24
25
26
27
# File 'lib/ecircle/message.rb', line 23

def initialize(hsh)
  super()
  @all_fields = hsh
  @id = self[:id]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ecircle::Base

Class Method Details

.allObject



18
19
20
# File 'lib/ecircle/message.rb', line 18

def all
  find_all_by_group_name("")
end

.find_all_by_group_name(group_name) ⇒ Object



8
9
10
11
# File 'lib/ecircle/message.rb', line 8

def find_all_by_group_name(group_name)
  ary = Ecircle.client.lookup_messages :lookupParams => { :groupName => group_name }
  ary.is_a?(Array) ? ary.collect { |a| Ecircle::Message.new(a) } : []
end

.find_by_id(idstr) ⇒ Object



13
14
15
16
# File 'lib/ecircle/message.rb', line 13

def find_by_id(idstr)
  ## TODO no lookupMessageById, hence this workaround.
  all.reject { |msg| msg.id != idstr }.first
end

Instance Method Details

#deleteObject



39
40
41
42
43
44
# File 'lib/ecircle/message.rb', line 39

def delete
  Ecircle.client.delete_message :messageId => self.id
  true
rescue Ecircle::Client::PermissionDenied => e
  false
end

#groupObject



33
34
35
36
37
# File 'lib/ecircle/message.rb', line 33

def group
  # a single message could potentially have no group_id? Either case this
  # should just return nil instead of raising an exception.
  Ecircle::Group.find_by_id(self[:group_id]) if self[:group_id]
end

#send_to_user(user, parameters = nil) ⇒ Object

This does one of two things, if the message is of type ‘single’, then it uses send_single_message_to_user, else if the type is ‘normal’ then it uses the send_group_message_to_user.

If parameters are given and this is a single message, then a parameterized version of the message is sent.

Return value is an array with the first value being boolean to indicate success status. The second value is the original result returned by the ecircle API. The thing is, that ecircle will return nil on success, so in that case, we return [true, nil].



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ecircle/message.rb', line 57

def send_to_user(user, parameters = nil)
  result = case self[:type]

           when /single/
             if parameters.nil?
               Ecircle.client.
                 send_single_message_to_user(:singleMessageId => @id,
                                             :userId => user.id)
             else
               paras = { :singleMessageId => @id,
                         :userId          => user.id,
                         :names           => parameters.keys,
                         :values          => parameters.values,
                       }
               Ecircle.client.send_parametrized_single_message_to_user(paras)
             end

           when /normal/
             # raise an exception because this is inconsistent: a group message without
             # group_id is not possible.
             raise MessageGroupNotDefined, "MsgId: #{self.id}" unless self[:group_id]

             Ecircle.client.
               send_group_message_to_user(:userId    => user.id,
                                          :messageId => @id,
                                          :groupid   => self[:group_id])
           else
             raise(MessageTypeUnknown, "Type: #{self[:type]} unknown for "+
                   "MsgId: #{self.id}")
           end

  # strangely, if the message sending worked out, then ecircle sends nil, i.e.
  # nothing back. Else we get some sort of strange error or exception.
  result.nil? ? [true, nil] : [false, result]
end

#subjectObject



29
30
31
# File 'lib/ecircle/message.rb', line 29

def subject
  self[:subject]
end