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



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

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
    elsif payload.is_a? String
      payload
    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)



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

def put (item, tube_alias = nil)
  begin
    self.attempt_to_log "Puter#put message received. #{item} and tube_alias #{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}"
  rescue => e
    self.attempt_to_log "Puter#put failure - exception encountered type = #{e.class} and message = #{e.message}"
    raise e
  end
  r
end

#puter_tube_alias_mapObject Also known as: alias_map



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

def puter_tube_alias_map
  @puter_tube_alias_map ||= Hash.new
end

#puter_tube_alias_map=(alias_map) ⇒ Object



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

def puter_tube_alias_map=(alias_map)
  @puter_tube_alias_map = alias_map
end

#puter_tube_name(selectors = {}) ⇒ Symbol Also known as: tube_name

Get the currently registered tube name

Parameters:

  • selectors (Hash) (defaults to: {})

    this argument is passed in to specify mappings when using the :alias_map puter mode

Returns:

  • (Symbol)

    the tube name



113
114
115
116
117
# File 'lib/refried/puter.rb', line 113

def puter_tube_name(selectors = {})
  @puter_tube_name = self.class.to_s.downcase.to_sym if self.class.puter_mode == :simple
  @puter_tube_name = self.alias_map[selectors[:alias]].to_sym if self.class.puter_mode == :alias_map
  @puter_tube_name ||= nil
end

#puter_tube_name=(tube_name) ⇒ Object Also known as: tube_name=

Set the tube name - this only has an impact when using the :tube_name puter mode

Parameters:

  • tube_name (Symbol)

    the value to set the tube name



123
124
125
# File 'lib/refried/puter.rb', line 123

def puter_tube_name=(tube_name)
  @puter_tube_name = tube_name
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


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

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 :alias_map
      tube_name = self.puter_tube_name selectors
    when :simple, :tube_name
      tube_name = self.puter_tube_name
    else
      raise ArgumentError, 'Invalid puter mode detected in the #tube method.'
  end
  tube_name ||= nil
  tube ||= ::Refried.tubes.find tube_name.to_s
end