Class: Amazon::AWS::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/aws.rb

Overview

This is the base class of all AWS operations.

Constant Summary collapse

OPERATIONS =

These are the types of AWS operation currently implemented by Ruby/AWS.

%w[
	BrowseNodeLookup      CustomerContentLookup   CustomerContentSearch
	Help		      ItemLookup	      ItemSearch
	ListLookup	      ListSearch	      MultipleOperation
	SellerListingLookup   SellerListingSearch     SellerLookup
	SimilarityLookup      TagLookup		      TransactionLookup
	VehiclePartLookup     VehiclePartSearch	      VehicleSearch

	CartAdd		      CartClear		      CartCreate
	CartGet		      CartModify
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters) ⇒ Operation

Returns a new instance of Operation.



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/amazon/aws.rb', line 612

def initialize(parameters)

	op_kind = self.class.to_s.sub( /^.*::/, '' )
	unless OPERATIONS.include?( op_kind )
	  raise "Bad operation: #{op_kind}"
	end
	#raise 'Too many parameters' if parameters.size > 10

	@kind = op_kind
	@params = { 'Operation' => op_kind }.merge( parameters )

	if ResponseGroup::DEFAULT.key?( op_kind )
	  @response_group =
	    ResponseGroup.new( ResponseGroup::DEFAULT[op_kind] )
	else
	  @response_group = nil
	end
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



609
610
611
# File 'lib/amazon/aws.rb', line 609

def kind
  @kind
end

#paramsObject

Returns the value of attribute params.



610
611
612
# File 'lib/amazon/aws.rb', line 610

def params
  @params
end

#response_groupObject

Returns the value of attribute response_group.



610
611
612
# File 'lib/amazon/aws.rb', line 610

def response_group
  @response_group
end

Instance Method Details

#batch(*operations) ⇒ Object

Group together operations of the same class in a batch request. operations should be either an operation of the same class as self or an array of such operations.

If you need to batch operations from different classes, use a MultipleOperation instead.

Example:

is = ItemSearch.new( 'Books', { 'Title' => 'ruby programming' } )
is2 = ItemSearch.new( 'Music', { 'Artist' => 'stranglers' } )
is.response_group = ResponseGroup.new( :Small )
is2.response_group = ResponseGroup.new( :Tracks )
is.batch( is2 )

Please see MultipleOperation.new for implementation details that also apply to batched operations.



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/amazon/aws.rb', line 650

def batch(*operations)

	# Remove the Operation parameter to avoid batch syntax being applied.
	# We'll readd it at the end.
	#
	op_type = @params.delete( 'Operation' )

	operations.flatten.each do |op|

	  unless self.class == op.class
	    raise BatchError, "You can't batch different classes of operation. Use class MultipleOperation."
	  end

	  # Remove the Operation parameter.
	  #
	  op.params.delete( 'Operation' )

	  # Apply batch syntax.
	  #
	  @params = batch_parameters( @params, op.params )
	  @response_group.params = batch_response_groups( op )
	end

	# Reinstate the Operation parameter.
	#
	@params.merge!( { 'Operation' => op_type } )
end

#batch_parameters(params, *b_params) ⇒ Object

Convert parameters to batch format, e.g. ItemSearch.1.Title.



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/amazon/aws.rb', line 681

def batch_parameters(params, *b_params)  # :nodoc:

	@index ||= 1

	unless b_params.empty?
	  op_str = @kind || self.class.to_s.sub( /^.*::/, '' )

	  # Fudge the operation string if we're dealing with a shopping cart.
	  #
	  op_str = 'Item' if op_str =~ /^Cart/

	  all_parameters = []

	  # Shopping carts pass an empty hash in params, so we have to ditch
	  # params in such a case to prevent the batch index from being off by
	  # one.
	  #
	  all_parameters.concat( [ params ] ) unless params.empty?
	  all_parameters.concat( b_params )

	  params = {}
	  index = 0

	  all_parameters.each do |hash|

	    next if hash.empty?

	    # Don't batch an already batched hash.
	    #
	    if hash.to_a[0][0] =~ /^.+\..+\..+$/
 params.merge!( hash )
 @index += 1
 next
	    end

	    hash.each do |tag, val|
 shared_param = '%s.%d.%s' % [ op_str, @index + index, tag ]
 params[shared_param] = val
	    end

	    index += 1
	  end

	  @index += b_params.size

	end

	params
end