Class: Ecircle::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



3
4
5
# File 'lib/ecircle/client.rb', line 3

def auth_token
  @auth_token
end

Instance Method Details

#clientObject



46
47
48
49
50
51
52
# File 'lib/ecircle/client.rb', line 46

def client
  @client ||= Savon::Client.new do
    wsdl.document =  Ecircle.configuration.wsdl
    wsdl.endpoint =  Ecircle.configuration.endpoint
    wsdl.namespace = Ecircle.configuration.namespace
  end
end

#create_member(user_id, group_id, invite = false, send_message = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ecircle/client.rb', line 54

def create_member user_id, group_id, invite = false, send_message = false
  ensuring_logon do
    @response = client.request :createMember do
      soap.body = {
        :session     => auth_token,
        :userId      => user_id,
        :groupId     => group_id,
        :invite      => invite.to_s,
        :sendMessage => send_message.to_s
      }
    end
    @response.body[:create_member_response][:create_member_return].to_s
  end
end

#create_or_update_user_by_email(email) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ecircle/client.rb', line 69

def create_or_update_user_by_email email
  ensuring_logon do
    @response = client.request :createOrUpdateUserByEmail do
      soap.body = {
        :session     => auth_token, # TODO We can't use @auth_token here cause then the session_id is nil. Why?
        :userXml     => "<user><email>#{email}</email></user>",
        :sendMessage => 0
      }
    end
    @response.body[:create_or_update_user_by_email_response][:create_or_update_user_by_email_return].to_s
  end
end

#delete_member(member_id) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ecircle/client.rb', line 82

def delete_member member_id
  ensuring_logon do
    @response = client.request :deleteMember do
      soap.body = {
        :session  => auth_token,
        :memberId => member_id
      }
    end
    @response.body[:delete_member_response][:delete_member_return].to_s
  end
end

#ensuring_logon(&block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ecircle/client.rb', line 5

def ensuring_logon &block
  begin
    @auth_token ||= logon
  rescue Savon::SOAP::Fault => e
    # If we are here this probably means that our login credentials are wrong.
    response = e.to_hash
    if response[:fault][:faultcode] == 'soapenv:Server.userException' && response[:fault][:detail][:fault][:code] == '502'
      help = <<-doc
      !!!
      Got an authentication exception, chances are good that you're credentials are wrong, so better double check that.
      You can explicitly check for it by calling something like:
      Ecircle.configure do..
      Ecircle.logon
      !!!
      doc
      puts help
    else
      puts "!!! Got an unexpected fault code from Savon: #{response.inspect} !!!"
    end
    raise
  rescue => e
    puts  "!!! Got unexpected non-Savon exception: #{e.class} !!!"
    raise
  end

  first_try = true
  begin
    block.call
  rescue Savon::SOAP::Fault => e
    # If we are here that probably means that our session token has expired.
    if first_try
      first_try = false
      @auth_token = logon
      retry
    else
      puts "!!! Could not re-authenticate after session expired: #{e.to_hash.inspect} !!!"
      raise
    end
  end
end

#logonObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/ecircle/client.rb', line 94

def logon
  @response = client.request :logon do
    soap.body = {
      :user   => Ecircle.configuration.user,
      :realm  => Ecircle.configuration.sync_realm,
      :passwd => Ecircle.configuration.password
    }
  end
  @response.body[:logon_response][:logon_return].to_s
end

#send_parametrized_single_message_to_user(user_id, message_id, names = [], values = []) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ecircle/client.rb', line 105

def send_parametrized_single_message_to_user user_id, message_id, names = [], values = []
  ensuring_logon do
    @response = client.request :sendParametrizedSingleMessageToUser do
      soap.body = {
        :session           => auth_token,
        :singleMessageId   => message_id,
        :userId            => user_id,
        :names             => names,
        :values            => values
      }
    end
  end
end