Module: Refried::Puter::InstanceMethods

Defined in:
lib/refried/puter.rb

Instance Method Summary collapse

Instance Method Details

#generate_message(payload) ⇒ String

This method converts the payload object into a format for injection

into the queue

Parameters:

  • payload

    the object that should be converted into a message (it must respond to the to_json method)

Returns:

  • (String)

    the appropriately serialized representation of the payload



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/refried/puter.rb', line 88

def generate_message(payload)
  if payload.is_a? Fixnum
    payload.to_json
  else
    if payload.nil?
      nil
    elsif payload.respond_to?(:empty?) && payload.empty?
      nil
    elsif payload.respond_to? :to_edible
      payload.to_edible
    else
      # Not sure that this is the appropriate implementation, perhaps to_s is better
      payload.to_json
    end
  end
end

#put(item, tube_alias = nil) ⇒ Object

I want to support a number of types of mappings

  1. in the simple case the tube name should be associated with the Puter’s class

  2. a map of item types to tube names

  3. a map of tube aliases to tube names (this corresponds to how ESIndexer & funding override workers will use the gem)



48
49
50
51
52
53
54
55
56
# File 'lib/refried/puter.rb', line 48

def put (item, tube_alias = nil)
  self.attempt_to_log "Puter#put message received. #{item} and tube_alias #{tube_alias}"
  puts tube_alias
  tube = self.tube(alias: tube_alias)
  payload = self.generate_message item
  r = tube.put payload
  self.attempt_to_log "Puter#put message queued containing #{item}, result = #{r}"
  r
end

#puter_tube_alias_mapObject Also known as: alias_map



35
36
37
# File 'lib/refried/puter.rb', line 35

def puter_tube_alias_map
  @puter_tube_alias_map ||= Hash.new
end

#puter_tube_alias_map=(alias_map) ⇒ Object



40
41
42
# File 'lib/refried/puter.rb', line 40

def puter_tube_alias_map=(alias_map)
  @puter_tube_alias_map = alias_map
end

#tube(selectors = {}) ⇒ Object

If the puter mode is :type_map the :type key should contain the class of the item being put as a symbol, which should be a key in the puter_tube_type_map If the puter mode is :alias_map the value for the :alias key is the tube_alias,

the tube_alias is used as the key in the puter_tube_alias_map whose value is the tube's name


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

def tube (selectors = {})
  unless self.locatable? selectors
    a_map = self.alias_map
    a_map ||= nil
    raise ArgumentError, "Selectors to #tube were unexpected for current puter_mode; alias_map = #{a_map}; puter mode = #{self.class.puter_mode}; selectors = #{selectors}"
  end
  case self.class.puter_mode
    when :simple
      tube_name = self.class.to_s.downcase
    when :alias_map
      tube_alias = selectors[:alias]
      tube_name = self.alias_map[tube_alias]
    else
      raise ArgumentError, 'Invalid puter mode detected in the #tube method.'
  end
  tube_name ||= nil
  tube ||= Refried.tubes.find tube_name
end