Class: Nazrin::DocumentClient

Inherits:
Object
  • Object
show all
Defined in:
lib/nazrin/document_client.rb

Defined Under Namespace

Classes: InvalidBatchOperationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Nazrin.config) ⇒ DocumentClient

Returns a new instance of DocumentClient.



7
8
9
10
11
12
13
14
# File 'lib/nazrin/document_client.rb', line 7

def initialize(config=Nazrin.config)
  @client = Aws::CloudSearchDomain::Client.new(
    endpoint: config.document_endpoint,
    region: config.region,
    access_key_id: config.access_key_id,
    secret_access_key: config.secret_access_key,
    logger: config.logger)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/nazrin/document_client.rb', line 5

def client
  @client
end

Instance Method Details

#add_document(id, field_data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nazrin/document_client.rb', line 16

def add_document(id, field_data)
  ActiveSupport::Deprecation.warn 'config.debug_mode is deprecated. Use config.mode = \'sandbox\' instead.' and return nil if Nazrin.config.debug_mode
  return nil if Nazrin.config.mode == 'sandbox'
  client.upload_documents(
    documents: [
      {
        type: 'add',
        id: id,
        fields: field_data
      }
    ].to_json,
    content_type: 'application/json')
end

#batch(operations) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nazrin/document_client.rb', line 43

def batch(operations)
  ActiveSupport::Deprecation.warn 'config.debug_mode is deprecated. Use config.mode = \'sandbox\' instead.' and return nil if Nazrin.config.debug_mode
  return nil if Nazrin.config.mode == 'sandbox'

  documents = operations.each_with_object([]) do |(type, tuple), arr|
    case type.to_sym
    when :add
      tuple.each do |id, field_data|
        arr.push(
          type: 'add',
          id: id,
          fields: field_data
        )
      end
    when :delete
      tuple.each do |id|
        arr.push(
          type: 'delete',
          id: id
        )
      end
    else
      raise(
        InvalidBatchOperationError,
        "`#{type}` is not a valid batch operation"
      )
    end
  end

  client.upload_documents(
    documents: documents.to_json,
    content_type: 'application/json'
  )
end

#delete_document(id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nazrin/document_client.rb', line 30

def delete_document(id)
  ActiveSupport::Deprecation.warn 'config.debug_mode is deprecated. Use config.mode = \'sandbox\' instead.' and return nil if Nazrin.config.debug_mode
  return nil if Nazrin.config.mode == 'sandbox'
  client.upload_documents(
    documents: [
      {
        type: 'delete',
        id: id
      }
    ].to_json,
    content_type: 'application/json')
end