Class: Etapper::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/etapper/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



21
22
23
24
25
26
27
28
29
30
# File 'lib/etapper/client.rb', line 21

def initialize
  @driver = API::MessagingService.new
  # Set the session handler
  @driver.streamhandler.filterchain << SessionFilter.new
  self.url = Etapper::ETAP_URL   # 'nil' is a valid value
  @autoconnect = true
  @username = nil
  @password = nil      
  @connected = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object

Our primary proxy. Sends anything we don’t know about to the driver for processing.

Raises:

  • (NoMethodError)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/etapper/client.rb', line 97

def method_missing(method, *params)
  raise NoMethodError if method == :driver  # This is protected for a reason
  tries = 0
  begin
    unless connected?
      if autoconnect
        connect
      else
        raise ConnectionError, "Autoconnect is disabled! Use the 'connect' method before making any API calls."
      end
    end
    driver.send(method, *params)
  rescue StandardError, SocketError => e
    # Try three times, waiting a few seconds and forcing login again each time
    while tries < 3
      sleep (tries += 1)
      disconnect
      retry
    end
    raise  # We give up
  end 
end

Instance Attribute Details

#autoconnectObject

Returns the value of attribute autoconnect.



15
16
17
# File 'lib/etapper/client.rb', line 15

def autoconnect
  @autoconnect
end

Instance Method Details

#connectObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/etapper/client.rb', line 64

def connect
  unless connected?
    raise ConnectionError, "Username is required!" unless username
    raise ConnectionError, "Password is required!" unless password
    begin
      result = driver.(@username, @password)
      if result == ''
        @connected = true
      else  # May need a redirect
        newurl = URI.parse(result)
        newurl.query = nil   # Strip off the '?wsdl' parameter at the end
        driver.endpoint_url = newurl.to_s
        @connected = connect
      end
    rescue  # We just can't connect right now, it seems
      @connected = false
      false
    end
  end
end

#connected?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/etapper/client.rb', line 60

def connected?
  @connected
end

#disconnectObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/etapper/client.rb', line 85

def disconnect
  begin
    driver.logout if connected?
    true
  rescue  # We don't care what went wrong, we just need to know we're not connected
    false
  ensure
    @connected = false
  end
end

#logObject

Delegates from the driver’s annoyingly named wiredump_dev property



51
52
53
# File 'lib/etapper/client.rb', line 51

def log
  driver.wiredump_dev
end

#log=(logthing) ⇒ Object

Delegates to the driver’s annoyingly named wiredump_dev property



56
57
58
# File 'lib/etapper/client.rb', line 56

def log=(logthing)
  driver.wiredump_dev = logthing
end

#passwordObject



41
42
43
# File 'lib/etapper/client.rb', line 41

def password
  @password ||= ENV['ETAPPER_PASSWORD'] || config_file["password"]
end

#password=(val) ⇒ Object



45
46
47
48
# File 'lib/etapper/client.rb', line 45

def password=(val)
  @password = val
  disconnect
end

#usernameObject



32
33
34
# File 'lib/etapper/client.rb', line 32

def username
  @username ||= ENV['ETAPPER_USERNAME'] || config_file["username"]
end

#username=(val) ⇒ Object



36
37
38
39
# File 'lib/etapper/client.rb', line 36

def username=(val)
  @username = val
  disconnect
end