bolseragency-ecircle 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ecircle.rb ADDED
@@ -0,0 +1,135 @@
1
+ module Ecircle
2
+ require 'rubygems'
3
+ require 'hpricot'
4
+ gem 'soap4r'
5
+ require "soap/wsdlDriver"
6
+ require "builder"
7
+
8
+ class Authentication
9
+ attr_accessor :driver
10
+ attr_accessor :session
11
+
12
+ def authenticate(authentication_parameters)
13
+ require File.expand_path(File.dirname(__FILE__) + '/../synchronous/synchronousDriver')
14
+ self.driver = EcMSoapBridge.new
15
+ self.session = driver.logon(:realm => authentication_parameters['ecmessenger_url'],
16
+ :user => authentication_parameters['ecmessenger_username'],
17
+ :passwd => authentication_parameters['ecmessenger_password'] ).logonReturn
18
+ end
19
+ end
20
+
21
+ class Member < Authentication
22
+ attr_accessor :doc #the xml document from ecircle
23
+ attr_accessor :email #the persons email address
24
+ attr_accessor :groupId
25
+
26
+ def self.find_by_email(email, groupId, authentication_parameters)
27
+ member = Member.new
28
+ member.authenticate(authentication_parameters)
29
+ member.email = email
30
+ lookup = member.driver.lookupMemberByEmail_v2_0(:session => member.session, :groupId => groupId, :email => email, :onlyActive => false).lookupMemberByEmail_v2_0Return
31
+ return nil if lookup.nil?
32
+ member.doc = Hpricot.XML(lookup)
33
+ member.groupId = groupId
34
+ return member
35
+ end
36
+
37
+ def id
38
+ @doc.nil? ? nil : (@doc/"member").first.attributes['id'].to_i
39
+ end
40
+
41
+
42
+ def method_missing(method_name, *args)
43
+ if method_name.to_s =~ /^.*=$/
44
+ return instance_variable_set("@#{method_name.to_s.gsub('=','')}", args[0])
45
+ else
46
+ unless instance_variable_get("@#{method_name}").nil?
47
+ return instance_variable_get("@#{method_name}")
48
+ end
49
+ if self.custom_atributes.include?(method_name)
50
+ if element = (@doc/"namedattr[@name='#{method_name}']")
51
+ if element.respond_to?(:inner_html)
52
+ instance_variable_set("@#{method_name}", element.inner_html) if instance_variable_get("@#{method_name}").nil?
53
+ return instance_variable_get("@#{method_name}")
54
+ end
55
+ end
56
+ elsif self.standard_attributes.include?(method_name)
57
+ if element = (@doc/"#{method_name}")
58
+ if element.respond_to?(:inner_html)
59
+ instance_variable_set("@#{method_name}", element.inner_html) if instance_variable_get("@#{method_name}").nil?
60
+ return instance_variable_get("@#{method_name}")
61
+ end
62
+ end
63
+ end
64
+ end
65
+ super
66
+ end
67
+
68
+
69
+ def unsubscribe(sendMessage = false)
70
+ driver.unsubscribeMemberByEmail(:session => session, :email => email, :groupId => groupId, :sendMessage => false).unsubscribeMemberByEmailResponse
71
+ end
72
+
73
+ def save
74
+ driver.createOrUpdateUserMemberByEmail(:session => session, :memberXml => self.to_xml, :groupId => groupId, :sendMessage => false)
75
+ end
76
+
77
+ def self.create(attributes, authentication_parameters)
78
+ member = Member.new
79
+ member.authenticate(authentication_parameters)
80
+
81
+ #setup default values of the standard attributes any standard
82
+ member.standard_attributes.each { |attribute| member.instance_variable_set("@#{attribute}", "") }
83
+
84
+ attributes.each do |field, value|
85
+ member.instance_variable_set("@#{field}", value)
86
+ unless member.standard_attributes.include?(field)
87
+ #must be a custom attribute. Add it to the custom_atributes array
88
+ @custom_attributes = Array.new if @custom_attributes.nil?
89
+ @custom_attributes << field
90
+ end
91
+ end
92
+
93
+
94
+ member.set_custom_attributes @custom_attributes
95
+ member.save
96
+ return member
97
+ end
98
+
99
+ def to_xml
100
+ x = Builder::XmlMarkup.new
101
+ x.member do |member|
102
+ standard_attributes.each do |attribute|
103
+ val = self.send(attribute)
104
+ eval("member.#{attribute}('#{self.send(attribute)}')") unless self.id.nil? && self.send(attribute) == '' #if value is blank, only set if updating
105
+ end
106
+ custom_atributes.each do |attribute|
107
+ member.namedattr({:name => attribute}, self.send(attribute)) unless self.id.nil? && self.send(attribute) == '' #if value is blank, only set if updating
108
+ end
109
+ end
110
+ end
111
+
112
+ def custom_atributes
113
+ @custom_atributes ||= (@doc/"namedattr").collect { |field| field.attributes['name'].to_sym }
114
+ end
115
+
116
+ def set_custom_attributes(attributes)
117
+ @custom_atributes = attributes
118
+ end
119
+
120
+ def standard_attributes
121
+ [:email, :title, :firstname, :lastname, :dob_dd, :dob_mm, :dob_yyyy, :countrycode, :languagecode, :nickname,
122
+ :cust_attr_0, :cust_attr_1, :cust_attr_2, :cust_attr_3, :cust_attr_4, :cust_attr_5, :cust_attr_6, :cust_attr_7,
123
+ :cust_attr_8, :cust_attr_9]
124
+ end
125
+
126
+ def send_message(message_id)
127
+ driver.sendParametrizedSingleMessageToUser(:session => session,
128
+ :singleMessageId => message_id,
129
+ :userId => self.id,
130
+ :names => "",
131
+ :values => "")
132
+
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,106 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'CGI'
3
+
4
+ describe "Ecircle" do
5
+ before(:each) do
6
+ @configuration = YAML::load( File.open( File.expand_path(File.dirname(__FILE__) + '/ecircle.yml') ) )
7
+ @matt = Ecircle::Member.find_by_email("mail@matthewfawcett.co.uk", groupId = 351026868, @configuration)
8
+ end
9
+
10
+ it "should be able to find a user" do
11
+ @matt.firstname.should eql("Matt")
12
+ @matt.lastname.should eql("Fawcett")
13
+ end
14
+
15
+ it "should return nil if the user is not found" do
16
+ @fake = Ecircle::Member.find_by_email(email = "fakeuser123588@fakedomain.com", groupId = 351026868, @configuration)
17
+ @fake.should be_nil
18
+ end
19
+
20
+ it "should allow me access to custom attributes" do
21
+ @matt.FullName.should eql("Matt Fawcett")
22
+ end
23
+
24
+ it "should give me asccess to the user id" do
25
+ @matt.id.should eql(3500322370)
26
+ end
27
+
28
+ it "should allow me to update a standard attribute" do
29
+ #update the name
30
+ @matt.firstname = "New name"
31
+ @matt.save
32
+ # #now refresh and make sure its changed
33
+ @matt = Ecircle::Member.find_by_email("mail@matthewfawcett.co.uk", groupId = 351026868, @configuration)
34
+ @matt.firstname.should eql("New name")
35
+ # #Now reset it back again for future tests
36
+ @matt.firstname = "Matt"
37
+ @matt.save
38
+ end
39
+
40
+ it "should allow me to update a custom attribute" do
41
+ #update the name
42
+ @matt.FullName = "New full name"
43
+ @matt.save
44
+ #now refresh and make sure its changed
45
+ @matt = Ecircle::Member.find_by_email("mail@matthewfawcett.co.uk", groupId = 351026868, @configuration)
46
+ @matt.FullName.should eql("New full name")
47
+ #Now reset it back again for future tests
48
+ @matt.FullName = "Matt Fawcett"
49
+ @matt.save
50
+ end
51
+
52
+ it "should allow me to set an attribute to blank" do
53
+ pending #Iv emailed Ecircle as it seems to igore blank fields
54
+ #update the name
55
+ @matt.firstname = ""
56
+ @matt.save
57
+ # #now refresh and make sure its changed
58
+ @matt = Ecircle::Member.find_by_email("mail@matthewfawcett.co.uk", groupId = 351026868, @configuration)
59
+ @matt.firstname.should eql("")
60
+ # #Now reset it back again for future tests
61
+ @matt.firstname = "Matt"
62
+ @matt.save
63
+ end
64
+
65
+ describe "custom_atributes" do
66
+ it "should return an array of custom atributes on the user" do
67
+ @matt.custom_atributes.should include(:Addr1)
68
+ @matt.custom_atributes.should include(:Addr2)
69
+ @matt.custom_atributes.should include(:Country)
70
+ @matt.custom_atributes.should include(:FreeTitle)
71
+ @matt.custom_atributes.should include(:FullName)
72
+ @matt.custom_atributes.should include(:LastName)
73
+ @matt.custom_atributes.should include(:Postcode)
74
+ @matt.custom_atributes.should include(:RestID)
75
+ @matt.custom_atributes.should include(:RestName)
76
+ @matt.custom_atributes.should include(:RestNameCaps)
77
+ @matt.custom_atributes.should include(:Sport)
78
+ @matt.custom_atributes.should include(:Tel)
79
+ @matt.custom_atributes.should include(:TemplateRef)
80
+ @matt.custom_atributes.should include(:ask_list_id)
81
+ @matt.custom_atributes.should include(:ask_source_description)
82
+ @matt.custom_atributes.should include(:xLanguage)
83
+ end
84
+ end
85
+
86
+ describe "send_message" do
87
+ it "should send a message to the user" do
88
+ @matt.send_message(351197566).should be_true
89
+ end
90
+ end
91
+
92
+ describe "creating a new user" do
93
+ it "should craete an instance of a new user with standard and custom attributes" do
94
+ @unique = Time.now.to_i + rand(1000)
95
+ @user = Ecircle::Member.create({:email => "user_#{@unique}@matthewfawcett.co.uk",
96
+ :firstname => "FN#{@unique}",
97
+ :FullName => "Full Name #{@unique}",
98
+ :groupId => 351026868},
99
+ @configuration)
100
+ @user.firstname.should eql("FN#{@unique}")
101
+ @user.FullName.should eql("Full Name #{@unique}")
102
+ end
103
+
104
+ end
105
+
106
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'ecircle'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end