Class: ECS::Help

Inherits:
Object
  • Object
show all
Defined in:
lib/ecs/help.rb

Constant Summary collapse

@@help_map =
{
  :required_parameters => { :call_path => 'HelpResponse.Information.OperationInformation.RequiredParameters.Parameter' },
  :available_parameters => { :call_path => 'HelpResponse.Information.OperationInformation.AvailableParameters.Parameter' },
  :default_response_groups => { :call_path => 'HelpResponse.Information.OperationInformation.DefaultResponseGroups.ResponseGroup' },
  :available_response_groups => { :call_path => 'HelpResponse.Information.OperationInformation.AvailableResponseGroups.ResponseGroup' }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/ecs/help.rb', line 273

def method_missing( meth, *args )
  method_name = meth.to_s

  # Note that if there is an element in the xml called each_*,
  # in order to get to it, you can't pass a block
  if method_name =~ /^each_(.*)$/ && block_given?
    x = self.send( $1.to_sym, *args )
    if [ Array, XML::Node::Set].include?( x.class )
      x.each { |y| yield( y ) }
    else
      yield( x )
    end
  elsif method_name =~ /^(.*)_count$/
    # Note that this will return the count for all descendants
    # Not just children
    x = self.send( $1.to_sym, *args )
    x.respond_to?( :size ) ? x.size : 1
  else
    instance_variable_name = "@#{method_name}_var"
    self.instance_eval( "
      def #{method_name}( force=false )
        if force || #{instance_variable_name}.nil?
          #{instance_variable_name} = self.xml.find( '//#{method_name}' )
          #{instance_variable_name} = #{instance_variable_name}.first if #{instance_variable_name}.size == 1
        end
        #{instance_variable_name}
      end
    " )
  
    self.send( meth, *args )
  end
end

Instance Attribute Details

#parametersObject

Returns the value of attribute parameters.



21
22
23
# File 'lib/ecs/help.rb', line 21

def parameters
  @parameters
end

#xml(force = false) ⇒ Object

Returns the value of attribute xml.



21
22
23
# File 'lib/ecs/help.rb', line 21

def xml
  @xml
end

Class Method Details

.available_parameters(force = false) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/ecs/help.rb', line 83

def self.available_parameters( force=false )
  if force || @available_parameters.nil?
    @available_parameters = []
    self.help_xml.HelpResponse.Information.OperationInformation.AvailableParameters.Parameter.each do |p|
      @available_parameters << p.content.to_s.to_sym
    end
  end
  @available_parameters
end

.available_response_groups(force = false) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/ecs/help.rb', line 105

def self.available_response_groups( force=false )
  if force || @available_response_groups.nil?
    @available_response_groups = []
    self.help_xml.HelpResponse.Information.OperationInformation.AvailableResponseGroups.ResponseGroup.each do |p|
      @available_response_groups << ECS.resolve_response_group_klass( "#{p.content.to_s}ResponseGroup" )
    end
  end
  @available_response_groups
end

.default_response_groups(force = false) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/ecs/help.rb', line 96

def self.default_response_groups( force=false )
  if force || @default_response_groups.nil?
    @default_response_groups = []
    self.help_xml.HelpResponse.Information.OperationInformation.DefaultResponseGroups.ResponseGroup.each do |p|
      @default_response_groups << ECS.resolve_response_group_klass( "#{p.content.to_s}ResponseGroup" )
    end
  end
  @default_response_groups
end

.errors_from_aws(x) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ecs/help.rb', line 51

def self.errors_from_aws( x )
  xml = x
  unless x.is_a?( XML::Document )
    p = XML::Parser.new
    p.string = x
    xml = p.parse
  end

  errors = []
  xml.find( '//Errors/Error' ).each do |error_node|
    begin
      m = error_node.Message.content
      c = ECS.resolve_error_klass( error_node.Code.content )
      errors << c.new( m )
    rescue Exception => e
    end
  end
  errors
end

.help_xml(force = false) ⇒ Object



27
28
29
30
31
32
# File 'lib/ecs/help.rb', line 27

def self.help_xml( force=false )
  if force || @help_xml.nil?
    @help_xml = ECS.help( :HelpType => 'Operation', :About => self.operation_name ) { |x| ECS::Help.valid_aws_response( x ) }.xml
  end
  @help_xml
end

.operation_nameObject



24
25
26
# File 'lib/ecs/help.rb', line 24

def self.operation_name
  @operation_name ||= self.resolve_operation_name( self.name )
end

.optional_parameters(force = false) ⇒ Object



92
93
94
# File 'lib/ecs/help.rb', line 92

def self.optional_parameters( force=false )
  self.available_parameters( force )
end

.required_parameters(force = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ecs/help.rb', line 72

def self.required_parameters( force=false )
  if force || @required_parameters.nil?
    @required_parameters = []
    self.help_xml.HelpResponse.Information.OperationInformation.RequiredParameters.Parameter.each do |p|
      @required_parameters << p.content.to_s.to_sym
    end
  end
  @required_parameters
rescue
  []
end

.resolve_error_name(t) ⇒ Object



115
116
117
# File 'lib/ecs/help.rb', line 115

def self.resolve_error_name( t )
  t.gsub( /[\.\s:]+/, '' ).camel_case
end

.resolve_operation_name(t) ⇒ Object



119
120
121
# File 'lib/ecs/help.rb', line 119

def self.resolve_operation_name( t )
  t =~ /::([^:]*)$/ ? $1 : t
end

.valid_aws_response?(x) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ecs/help.rb', line 35

def self.valid_aws_response?( x )
  xml = x
  unless x.is_a?( XML::Document )
    p = XML::Parser.new
    p.string = x
    xml = p.parse
  end
  
  if self.errors_from_aws( xml ).empty?
    n = xml.find( '//Request/IsValid' ).first
    n.nil? || n.content.downcase != 'false'
  else
    false
  end
end

.yaml_new(klass, tag, val) ⇒ Object



146
147
148
149
# File 'lib/ecs/help.rb', line 146

def self.yaml_new( klass, tag, val )
  operation = val['operation_name'].nil? ? ( tag =~ /:([^:]*)$/ ? $1 : 'help' ) : val['operation_name']
  ECS.send( operation.to_sym, val['parameters'] )
end

Instance Method Details

#arguments(force = false) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/ecs/help.rb', line 205

def arguments( force=false )
  if force || @arguments.nil?
    @arguments = {}
    self.xml.find( '//OperationRequest/Arguments/Argument' ).each do |argument|
      name = ''
      value = ''
      argument.each_attr do |attribute|
        name = attribute.value.to_sym if attribute.name == 'Name'
        value = attribute.value if attribute.name == 'Value'
      end
      @arguments[name] = value
    end
  end
  @arguments
rescue
  @arguments = {}
end

#cached?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/ecs/help.rb', line 170

def cached?
  ECS.cached?( self.parameters_for_xml )
end

#cached_contentObject



174
175
176
# File 'lib/ecs/help.rb', line 174

def cached_content
  ECS.read_cache( self.parameters_for_xml )
end

#errors(force = false) ⇒ Object



262
263
264
265
# File 'lib/ecs/help.rb', line 262

def errors( force=false )
  @errors = self.class.errors_from_aws( self.xml ) if force || @errors.nil?
  @errors
end

#headers(force = false) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/ecs/help.rb', line 178

def headers( force=false )
  if force || @headers.nil?
    @headers = {}
    self.xml.find( '//OperationRequest/HTTPHeaders/Header' ).each do |header|
      name = ''
      value = ''
      header.each_attr do |attribute|
        name = attribute.value.to_sym if attribute.name == 'Name'
        value = attribute.value if attribute.name == 'Value'
      end
      @headers[name] = value
    end
  end
  @headers
rescue
  @headers = []
end

#help_xml(force = false) ⇒ Object



127
128
129
# File 'lib/ecs/help.rb', line 127

def help_xml( force=false )
  self.class.help_xml( force )
end

#operation_nameObject



123
124
125
# File 'lib/ecs/help.rb', line 123

def operation_name
  self.class.operation_name
end

#parameters_for_xmlObject



166
167
168
# File 'lib/ecs/help.rb', line 166

def parameters_for_xml
  self.parameters.merge( :Operation => self.operation_name )
end

#potential_elements(force = false) ⇒ Object



251
252
253
254
255
256
257
258
259
260
# File 'lib/ecs/help.rb', line 251

def potential_elements( force=false )
  if force || @potential_elements.nil?
    @potential_elements = []
    self.response_groups.each do |response_group|
      @potential_elements << response_group.elements
    end
    @potential_elements.flatten!.uniq!
  end
  @potential_elements
end

#request_id(force = false) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/ecs/help.rb', line 196

def request_id( force=false )
  if force || @request_id.nil?
    @request_id = self.xml.find( '//OperationRequest/RequestId' ).first.content
  end
  @request_id
rescue
  @request_id = 'Could not determine request_id'
end

#request_processing_time(force = false) ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/ecs/help.rb', line 223

def request_processing_time( force=false )
  if force || @request_processing_time.nil?
    @request_processing_time = self.xml.find( '//OperationRequest/RequestProcessingTime' ).first.content.to_f
  end
  @request_processing_time
rescue
  @request_processing_time = -1.0
end

#response_groups(force = false) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/ecs/help.rb', line 232

def response_groups( force=false )
  #these are the response groups that are included in this instance,
  #not all those available to the class
  if force || @response_groups.nil?
    @response_groups = []

    response_group_names = self.arguments[:ResponseGroup]

    if response_group_names.nil?
      @response_groups = self.class.default_response_groups
    else
      response_group_names.split( ',' ).each do |response_group_name|
        @response_groups << ECS.resolve_response_group_klass( "#{response_group_name}ResponseGroup" )
      end
    end
  end
  @response_groups
end

#to_yaml(opts = {}) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/ecs/help.rb', line 137

def to_yaml( opts = {} )
  YAML.quick_emit( self.object_id, opts ) { |out|
    out.map( taguri ) { |map|
      map.add( 'parameters', self.parameters )
      map.add( 'operation_name', self.operation_name.underscore )
    }
  }
end

#valid?(force = false) ⇒ Boolean

Returns:

  • (Boolean)


267
268
269
270
# File 'lib/ecs/help.rb', line 267

def valid?( force=false )
  @valid = self.class.valid_aws_response?( self.xml ) if force || @valid.nil?
  @valid
end