Class: SoftLayer::BaseClass

Inherits:
Object
  • Object
show all
Defined in:
lib/softlayer/baseclass.rb

Overview

The Base class for our generated class.

Constant Summary collapse

WSDLBASE =
'http://api.service.softlayer.com/soap/v3'
WSDLPARAM =
'?wsdl'
@@wsdl =
{ }
@@apiUser =
nil
@@apiKey =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ BaseClass

The initializer. Arguments:

user

The API User

key

The API Key

initParams

This object’s initParam (just the key)

debug

Enable debug after driver creation. (IO handler)

user and key are optional. The first time they’re presented they’re saved to class variables and reused later as necessary. Supplying user and key later does not overwrite the class variables. initParams is required where the api requires it.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/softlayer/baseclass.rb', line 43

def initialize(args)
  @apiUser = args[:user] unless args[:user].nil?
  @apiKey = args[:key] unless args[:key].nil?
  @initParam = args[:initParam]
  @initParam = Param.new("#{self.soapClass}InitParameters", { 'id' => args[:initParam] }) unless args[:initParam].nil?

  @@apiUser = args[:user] unless (args[:user].nil? || !@@apiUser.nil?)
  @@apiKey = args[:key] unless (args[:key].nil? || !@@apiKey.nil?)
  @apiUser = @@apiUser unless (@@apiUser.nil? || !@apiUser.nil?)
  @apiKey = @@apiKey unless (@@apiKey.nil? || !@apiKey.nil?)
  @authHeader = Param.new('authenticate', {'username' => @apiUser, 'apiKey' => @apiKey})

  self.class.cacheWSDL
  @slapi = @@wsdl[self.soapClass].create_rpc_driver unless @@wsdl[self.soapClass].nil?
  raise SoftLayer::Exception.new(:message => 'WSDL endpoint not available.') if @slapi.nil?

  self.debug=args[:debug] unless args[:debug].nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject

Make a direct api call. The values of the paramaters are passed to the method (the keys generally are not), unless it’s a magic paramater. Magic Paramaters:

initParam

Initialization paramater for this call (just the key), therwise @initParam is used.

limit

A Result Limit array of two elements range and offset. If @resultLimit is set it’s used \

if limit is not and if neither is set, no limit is applied.

header

Extra headers to pass to the method in an array.

If a block is provided, the limit’s range (or fewer) elements will yield to the block until the dataset is exhausted. If no limit is provided with the block a limit of [1,0] is assumed initially (sorta). Aliased to #method_missing. Alias the above slapiCall to #method_missing.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/softlayer/baseclass.rb', line 175

def slapiCall(method, args = {}, &block)      
  initParam = args[:initParam] unless args[:initParam].nil?
  args.delete(:initParam) unless args[:initParam].nil?
  initParam = Param.new("#{self.soapClass}InitParameters", { 'id' => initParam }) unless initParam.nil?
  initParam = @initParam if initParam.nil?
  resultLimit = ResultLimit.new('resultLimit', args[:limit]) unless args[:limit].nil?
  args.delete(:limit) unless args[:limit].nil?
  resultLimit = @resultLimit if resultLimit.nil?
  unroll = true if resultLimit.nil? && block_given?
  resultLimit = ResultLimit.new('resultLimit', [5,0]) if resultLimit.nil? && block_given?
  headers = args[:header]
  args.delete(:header) unless args[:header].nil?

  headers = [] if headers.nil?
  headers << initParam unless @slapi.headerhandler.include?(initParam)
  headers << @objectMask unless @objectMask.nil?
  headers << resultLimit unless resultLimit.nil?
  argshash = { :method => method, :headers => headers, :args => args }
  argshash[:yield] = true if block_given?
  catch :done do
    while true do
      res = realCall(argshash)
      return res unless block_given?
      return res if res.nil?
      res.each { |e|  yield(e) } if unroll && res.respond_to?(:each)
      yield(res) unless unroll || (res.respond_to?(:empty) && res.empty?)
      throw :done if (res.respond_to?(:size) && (res.size < resultLimit.limit))
      resultLimit.offset=resultLimit.offset + resultLimit.limit
    end
  end
  headerClean(headers) if argshash[:yield]
end

Instance Attribute Details

#initParamObject (readonly)

Returns the value of attribute initParam.



23
24
25
# File 'lib/softlayer/baseclass.rb', line 23

def initParam
  @initParam
end

#slapiObject (readonly)

Returns the value of attribute slapi.



23
24
25
# File 'lib/softlayer/baseclass.rb', line 23

def slapi
  @slapi
end

Class Method Details

.cacheWSDLObject

Get the WSDL, parse it, and save it to a Class level hash. Returns false of we couldn’t parse the WSDL.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/softlayer/baseclass.rb', line 186

def self.cacheWSDL
  return unless @@wsdl[self.soapClass].nil?

  begin
    # XXX: Silence soap4r's bogus use of Kernel#warn
    v = $VERBOSE
    $VERBOSE=nil
    @@wsdl[self.soapClass] = SOAP::WSDLDriverFactory.new(self.wsdlUrl)
    $VERBOSE = v
    return true
  rescue => e
    return SoftLayer::Exception.new(:exception => e)
  end 
end

.soapClassObject

Returns this Class’s SOAP Class.



212
213
214
# File 'lib/softlayer/baseclass.rb', line 212

def self.soapClass
  self.name.to_s.gsub(/::/, '_')
end

.wsdlObject

Return this Class’s WSDL.



202
203
204
# File 'lib/softlayer/baseclass.rb', line 202

def self.wsdl
  return @@wsdl[self.soapClass]
end

.wsdlUrlObject

Return this Class’s WSDL URL.



207
208
209
# File 'lib/softlayer/baseclass.rb', line 207

def self.wsdlUrl
  return URI.parse("#{WSDLBASE}/#{self.soapClass}#{WSDLPARAM}")
end

Instance Method Details

#[](key) ⇒ Object

This returns key values from this Service’s associated Type (retrieved using #getObject).



68
69
70
71
# File 'lib/softlayer/baseclass.rb', line 68

def [](key)
  @slapiObject = self.getObject if @slapiobject.nil?
  return @slapiObject[key.to_s]
end

#debug=(dev) ⇒ Object

Enable (or disable) debug. (paramater is the IO handler to write to)



180
181
182
# File 'lib/softlayer/baseclass.rb', line 180

def debug=(dev)
  @slapi.wiredump_dev=(dev)
end

#objectMaskObject



105
106
107
# File 'lib/softlayer/baseclass.rb', line 105

def objectMask
  return @objectMask
end

#objectMask=(mask) ⇒ Object

Set the object mask which ia passed as a hash of optional hashes (otherwise the hash elements should have a nil value). Using the example from the wiki:

<SoftLayer_AccountObjectMask xsi:type="v3:SoftLayer_AccountObjectMask">
  <mask xsi:type="slt:SoftLayer_Account" xmlns:slt="http://api.service.softlayer.com/soap/v3/SLTypes/">
      <domains>
          <resourceRecords />
      </domains>
      <openTickets>
          <assignedUser />
          <attachedHardware />
          <updates />
      </openTickets>
      <userCount />
  </mask>
</SoftLayer_AccountObjectMask>

{ 'domains' => { 'resourceRecords' => nil }, 'openTicket' => { 'assignedUser' => nil, 'attachedHardware' => nil, 'updates' => nil },
 userCount => nil }

Changing this resets the cached object used by #[]



96
97
98
99
100
101
102
103
# File 'lib/softlayer/baseclass.rb', line 96

def objectMask=(mask)
  if mask.class == ObjectMask
    @objectMask = mask
  else
    @objectMask = ObjectMask.new("#{self.soapClass}ObjectMask", mask)
  end
  @slapiObject = nil
end

#resultLimitObject



125
126
127
# File 'lib/softlayer/baseclass.rb', line 125

def resultLimit
  return @resultLimit
end

#resultLimit=(arg) ⇒ Object

Set an object wide result set (or clear it) arg can be one of three things:

  • nil clears the resultLimit

  • A Result Limit array of two elements range and offset.

  • An existing ResultLimit object



114
115
116
117
118
119
120
121
122
123
# File 'lib/softlayer/baseclass.rb', line 114

def resultLimit=(arg)
  case arg.class
  when NilClass
    @resultLimit = nil
  when Array
    @resultLimit = ResultLimit.new('resultLimit',arg)
  when ResultLimit
    @resultLimit = arg
  end
end

#setObject(obj) ⇒ Object



73
74
75
# File 'lib/softlayer/baseclass.rb', line 73

def setObject(obj)
  @slapiObject = obj
end

#slapiCall(method, args = {}, &block) ⇒ Object Also known as: method_missing, call

Make a direct api call. The values of the paramaters are passed to the method (the keys generally are not), unless it’s a magic paramater. Magic Paramaters:

initParam

Initialization paramater for this call (just the key), therwise @initParam is used.

limit

A Result Limit array of two elements range and offset. If @resultLimit is set it’s used \

if limit is not and if neither is set, no limit is applied.

header

Extra headers to pass to the method in an array.

If a block is provided, the limit’s range (or fewer) elements will yield to the block until the dataset is exhausted. If no limit is provided with the block a limit of [1,0] is assumed initially (sorta). Aliased to #method_missing.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/softlayer/baseclass.rb', line 141

def slapiCall(method, args = {}, &block)      
  initParam = args[:initParam] unless args[:initParam].nil?
  args.delete(:initParam) unless args[:initParam].nil?
  initParam = Param.new("#{self.soapClass}InitParameters", { 'id' => initParam }) unless initParam.nil?
  initParam = @initParam if initParam.nil?
  resultLimit = ResultLimit.new('resultLimit', args[:limit]) unless args[:limit].nil?
  args.delete(:limit) unless args[:limit].nil?
  resultLimit = @resultLimit if resultLimit.nil?
  unroll = true if resultLimit.nil? && block_given?
  resultLimit = ResultLimit.new('resultLimit', [5,0]) if resultLimit.nil? && block_given?
  headers = args[:header]
  args.delete(:header) unless args[:header].nil?

  headers = [] if headers.nil?
  headers << initParam unless @slapi.headerhandler.include?(initParam)
  headers << @objectMask unless @objectMask.nil?
  headers << resultLimit unless resultLimit.nil?
  argshash = { :method => method, :headers => headers, :args => args }
  argshash[:yield] = true if block_given?
  catch :done do
    while true do
      res = realCall(argshash)
      return res unless block_given?
      return res if res.nil?
      res.each { |e|  yield(e) } if unroll && res.respond_to?(:each)
      yield(res) unless unroll || (res.respond_to?(:empty) && res.empty?)
      throw :done if (res.respond_to?(:size) && (res.size < resultLimit.limit))
      resultLimit.offset=resultLimit.offset + resultLimit.limit
    end
  end
  headerClean(headers) if argshash[:yield]
end

#soapClassObject

Return this object’s matching SLAPI SOAP Class.



63
64
65
# File 'lib/softlayer/baseclass.rb', line 63

def soapClass
  return self.class.to_s.gsub(/::/, '_')
end