Class: Ecircle::User

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

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(xml_string = nil) ⇒ User

Returns a new instance of User.



27
28
29
30
# File 'lib/ecircle/user.rb', line 27

def initialize(xml_string = nil)
  super()
  initialize_with_xml(xml_string) if xml_string
end

Dynamic Method Handling

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

Class Method Details

.create_by_email(email) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/ecircle/user.rb', line 5

def create_by_email(email)
  u = User.new
  u.email = email
  ## TODO why must the title be defined
  u.title = "-1"
  u.id = Ecircle.client.create_user :userXmlSpec => u.to_xml
  u
end

.find_by_email(email) ⇒ Object



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

def find_by_email(email)
  Ecircle.client.lookup_user_by_email :email => email
end

.find_by_id(idstr) ⇒ Object



22
23
24
# File 'lib/ecircle/user.rb', line 22

def find_by_id(idstr)
  Ecircle.client.lookup_user_by_id :userId => idstr
end

.find_by_identifier(idstr) ⇒ Object



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

def find_by_identifier(idstr)
  Ecircle.client.lookup_user_by_identifier :identifier => idstr
end

Instance Method Details

#create_or_update(send_message = false) ⇒ Object



40
41
42
43
# File 'lib/ecircle/user.rb', line 40

def create_or_update(send_message = false)
  Ecircle.client.
    create_or_update_user_by_email :userXml => to_xml, :sendMessage => send_message
end

#deleteObject



36
37
38
# File 'lib/ecircle/user.rb', line 36

def delete
  Ecircle.client.delete_user :user_id => @id
end

#emailObject



32
33
34
# File 'lib/ecircle/user.rb', line 32

def email
  self[:email]
end

#group_idsObject

Returns the group ids this user is signed up to as an Array of strings.



50
51
52
# File 'lib/ecircle/user.rb', line 50

def group_ids
  [Ecircle.client.find_memberships_by_email(:email => email)].flatten.compact
end

#groupsObject Also known as: memberships



54
55
56
# File 'lib/ecircle/user.rb', line 54

def groups
  group_ids.collect { |grpid| Ecircle::Group.find_by_id(grpid) }
end

#in_group?(group_or_id) ⇒ Boolean

group_or_id may be a Ecircle::Group object, containing the group’s id, or the id directly.

Returns:

  • (Boolean)


61
62
63
# File 'lib/ecircle/user.rb', line 61

def in_group?(group_or_id)
  group_ids.include?(Ecircle::User.group_id(group_or_id))
end

#join_group(group, send_invite = false, send_message = false) ⇒ Object



65
66
67
# File 'lib/ecircle/user.rb', line 65

def join_group(group, send_invite = false, send_message = false)
  group.add_member self, send_invite, send_message
end

#leave_group(group_or_id, send_message = false) ⇒ Object

Unsubscribe this user from the given group. group may be a Ecircle::Group object, containing the group’s id, or the id directly. Always returns true.



72
73
74
75
76
77
# File 'lib/ecircle/user.rb', line 72

def leave_group(group_or_id, send_message = false)
  Ecircle.client.
    unsubscribe_member_by_email(:groupId     => Ecircle::User.group_id(group_or_id),
                                :email       => email,
                                :sendMessage => send_message)
end

#saveObject



45
46
47
# File 'lib/ecircle/user.rb', line 45

def save
  Ecircle.client.update_user :userXmlSpec => to_xml
end

#to_xmlObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ecircle/user.rb', line 79

def to_xml
  obj = self # in instance_eval 'self' will be something else, so create new
             # reference to te self containing all the data.

  # prefer to use u.send(...) but that creates a new xml element called 'send'!
  # hence this is using instance_eval with a string.
  Savon::SOAP::XML.new.xml do |x|
    x.user(:id => @id) do |u|
      # email is special, so so so special.
      u.email(self.email)

      # base attributes of a user
      [:title, :firstname, :lastname, :nickname, :dob_dd,
       :dob_mm, :dob_yyyy,:countrycode,:languagecode].each do |field_name|
        u.instance_eval "%s(obj[:%s])" % ([field_name]*2)
      end

      # cust_attr_X
      9.times.collect { |idx| "cust_attr_#{idx+1}" }.
        each do |field_name|
        u.instance_eval "%s(obj[:%s])" % ([field_name]*2)
      end

      # named attributes, these are generic and defined by some guy in a suit.
      named_attrs.each do |key,value|
        u.namedattr({:name => key}, value)
      end
    end
  end
end