Class: Surfliner::MetadataConsumer::Solr::MessageHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/surfliner/metadata_consumer/solr/message_handler.rb

Overview

A message handler that delegates to sub-handlers based on the resource status provided in the message.

Direct Known Subclasses

DeleteHandler, IndexHandler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ MessageHandler

Initializes a new MessageHandler.

Parameters:



51
52
53
# File 'lib/surfliner/metadata_consumer/solr/message_handler.rb', line 51

def initialize(payload)
  @payload = payload
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



10
11
12
# File 'lib/surfliner/metadata_consumer/solr/message_handler.rb', line 10

def payload
  @payload
end

Class Method Details

.handle(payload_json) ⇒ Object

Delegates payload handling to the appropriate handler.

Parameters:

  • payload_json (String)

    JSON message payload



44
45
46
# File 'lib/surfliner/metadata_consumer/solr/message_handler.rb', line 44

def handle(payload_json)
  handler_for(payload_json).handle
end

.handler_for(payload_json) ⇒ #handle

Returns the appropriate handler based on the resource status provided in the message.

Parameters:

  • payload_json (String)

    JSON message payload

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/surfliner/metadata_consumer/solr/message_handler.rb', line 29

def handler_for(payload_json)
  payload = Payload.new(payload_json)

  case payload.status
  when :published, :updated
    IndexHandler.new(payload)
  when :unpublished, :deleted
    DeleteHandler.new(payload)
  else
    raise ArgumentError, "Couldn't handle message with payload status: #{payload.status}"
  end
end

Instance Method Details

#handleObject

This method is abstract.

Implementations should handle the Payload provided in the initializer.

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/surfliner/metadata_consumer/solr/message_handler.rb', line 57

def handle
  raise NotImplementedError
end

#solr_connectionRSolr::Client

Connects to the configured Solr instance.

Returns:

  • (RSolr::Client)

    The Solr connection.



14
15
16
17
18
19
20
21
22
23
# File 'lib/surfliner/metadata_consumer/solr/message_handler.rb', line 14

def solr_connection
  @solr_connection ||= begin
    solr_host = ENV.fetch("SOLR_HOST")
    solr_port = ENV.fetch("SOLR_PORT")
    solr_collection_name = ENV.fetch("SOLR_COLLECTION_NAME")

    solr_url = "http://#{solr_auth}#{solr_host}:#{solr_port}/solr/#{solr_collection_name}"
    RSolr.connect(url: solr_url)
  end
end