Class: Ecircle::Client

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

Constant Summary collapse

LoginFailed =
Class.new(RuntimeError)
NotLoggedIn =
Class.new(RuntimeError)
PermissionDenied =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



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

def initialize
  @session_token = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

This is the magic. All we do here is take the method name, camelcase it and then pass it to eCircle. It is assumed that the args array contains a hash as the first element. This then contains the arguments for the call. E.g.:

Ecircle.client.lookup_user_by_email :email => "[email protected]"

This will do a number of things:

1. login if session token is undefined
2. set the session parameter along with the email argument
3. check for specific eCircle exceptions and attempt to handle them.


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ecircle/client.rb', line 49

def method_missing(method, *args, &block)
  logon if @session_token.nil?

  method_name, body = method.to_s.camelcase, args.first || {}
  body[:session] = @session_token

  response = begin
               client.request(method_name) { soap.body = body }
             rescue Savon::SOAP::Fault => e
               handle_savon_fault(e, :for_method => method)
             end

  data = response.
    body["#{method}_response".to_sym]["#{method}_return".to_sym]

  if block_given?
    yield(data)
  else
    case data
    when /^[<]user/ then Ecircle::User.new(data)
    when /^[<]member/ then Ecircle::Member.new(data)
    when Hash
      # If the response is only an empty element, the data will look like this:
      # {:"@xmlns:ns1"=>"http://webservices.ecircleag.com/rpcns"}
      # Return nil if we only have attributes.
      data.all? { |key, value| key.to_s =~ /^@/ } ? nil : data
    else
      data
    end
  end
end

Instance Attribute Details

#session_tokenObject (readonly)

Returns the value of attribute session_token.



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

def session_token
  @session_token
end

Class Method Details

.attempt(retries = 2, &block) ⇒ Object

helper method for allowing clients to automatically retry requests to ecircle.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ecircle/client.rb', line 16

def attempt(retries = 2, &block)
  begin
    yield
  rescue Ecircle::Client::NotLoggedIn => e
    raise e unless retries > 0
    attempt(retries - 1, &block)

  rescue Ecircle::Client::PermissionDenied => e
    raise e unless retries > 0
    Ecircle.client.logon
    attempt(retries - 1, &block)

  rescue NoMethodError => e
    raise e unless retries > 0
    ## FIXME horrible hack because ecircle sometimes forgets that they
    ## FIXME have a FindMembershipsByEmail method - old software with bad memory?
    if e.message =~ /No such operation 'FindMembershipsByEmail'/
      attempt(retries - 1, &block)
    else
      raise e
    end
  end
end

Instance Method Details

#clientObject

Generate a Savon::Client to use for making requests. The configuration for the client is taken from the configure object.



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

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

#logonObject

Send logon request and store the session token if successful. The session token is then passed into each subsequent request to eCircle.



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

def logon
  @session_token = (client.request :logon do
    soap.body = {
      :user   => Ecircle.configure.user,
      :realm  => Ecircle.configure.realm,
      :passwd => Ecircle.configure.password
    }
  end).body[:logon_response][:logon_return].to_s
rescue Savon::SOAP::Fault => e
  @session_token = nil
  raise LoginFailed, "Msg: #{e.message}"
end

#logoutObject

Invalid the session token and set it to nil. The session token automagically expires after 10 minutes of inactivity, so you might have to logout if methods are with NotLoggedIn



110
111
112
113
114
115
116
117
118
# File 'lib/ecircle/client.rb', line 110

def logout
  client.request :logout do
    soap.body = {
      :session => @session_token
    }
  end
ensure
  @session_token = nil
end