class Alephant::Publisher::Queue::Writer

Attributes

config[R]
message[R]
parser[R]
storage[R]

Public Class Methods

new(config, message) click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 18
def initialize(config, message)
  @config   = config
  @message  = message
end

Public Instance Methods

renderer() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 23
def renderer
  @renderer ||= Alephant::Renderer.create(config, data)
end
run!() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 40
def run!
  if component_records_write_successful?
    seq_for(config[:renderer_id]).validate(message) do
      # block needed in sequencer. Need to make optional with `block.given?`
      # https://github.com/BBC-News/alephant-sequencer/blob/master/lib/alephant/sequencer/sequencer.rb#L41
    end
  end
end

Protected Instance Methods

component_records_write_successful?() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 55
def component_records_write_successful?
  process_components.all? { |response| response.respond_to?(:successful?) && response.successful? }
end
data() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 157
def data
  @data ||= parser.parse(message)
end
location_for(component) click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 121
def location_for(component)
  "#{config[:renderer_id]}/#{component}/#{opt_hash}/#{seq_id}"
end
lookup() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 161
def lookup
  Alephant::Lookup.create(config[:lookup_table_name], config)
end
opt_hash() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 149
def opt_hash
  @opt_hash ||= Crimp.signature(options)
end
options() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 153
def options
  @options ||= data[:options]
end
process_components() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 51
def process_components
  views.map { |component, view| write(component, view) }
end
seq_for(id) click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 125
def seq_for(id)
  Alephant::Sequencer.create(
    config[:sequencer_table_name],
    :id       => seq_key_from(id),
    :jsonpath => config[:sequence_id_path],
    :keep_all => config[:keep_all_messages] == "true",
    :config   => config
  )
end
seq_id() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 139
def seq_id
  @seq_id ||= Alephant::Sequencer::Sequencer.sequence_id_from(
    message, config[:sequence_id_path]
  )
end
seq_key_from(id) click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 135
def seq_key_from(id)
  "#{id}/#{opt_hash}"
end
store(component, view, location, storage_opts = {}) click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 77
def store(component, view, location, storage_opts = {})
  logger.info(
    event:        'StoreBeforeRender',
    component:    component,
    view:         view,
    location:     location,
    storage_opts: storage_opts
  )

  render = view.render

  logger.info(
    event:        'StoreAfterRender',
    component:    component,
    view:         view,
    location:     location,
    storage_opts: storage_opts
  )

  storage.put(location, render, view.content_type, storage_opts).tap do
    logger.info(
      "event"          => "MessageStored",
      "location"       => location,
      "view"           => view,
      "render"         => render.force_encoding("utf-8"),
      "contentType"    => view.content_type,
      "storageOptions" => storage_opts,
      "messageId"      => message.id,
      "method"         => "#{self.class}#store"
    )
  end

  lookup.write(component, options, seq_id, location).tap do
    logger.info(
      "event"      => "LookupLocationUpdated",
      "component"  => component,
      "options"    => options,
      "sequenceId" => seq_id,
      "location"   => location,
      "method"     => "#{self.class}#write"
    )
  end
end
views() click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 145
def views
  @views ||= renderer.views
end
write(component, view) click to toggle source
# File lib/alephant/publisher/queue/writer.rb, line 59
def write(component, view)
  seq_for(component).validate(message) do
    store(
      component,
      view,
      location_for(component),
      :msg_id => message.id
    )
  end.tap do
    logger.info(
      "event"     => "MessageWritten",
      "component" => component,
      "view"      => view,
      "method"    => "#{self.class}#write"
    )
  end
end