Class: Teapi::Documents

Inherits:
Object
  • Object
show all
Defined in:
lib/teapi/documents.rb

Overview

Manages documents

Class Method Summary collapse

Class Method Details

.bulk(type, created_or_updated, deleted) ⇒ Object

bulk updates a type

Parameters:

  • type (String)

    the document’s type

  • created_or_updated (Array[Hash])

    an array of documents to be created or updated

  • deleted (Array[Hash])

    an array of document [343, 9920,…]



35
36
37
38
# File 'lib/teapi/documents.rb', line 35

def self.bulk(type, created_or_updated, deleted)
  return if (created_or_updated.nil? || created_or_updated.length == 0) && (deleted.nil? || deleted.length == 0)
  Teapi.post(:documents, Oj.dump({type: type, deletes: deleted, upserts: created_or_updated}, mode: :compat, time_format: :ruby))
end

.create(type, doc, meta = nil) ⇒ Object

creates a new document belonging to the given type

Parameters:

  • type (String)

    the document’s type

  • doc (Hash)

    document to create

  • optional

    meta [Hash] meta data associated with the document



8
9
10
11
12
# File 'lib/teapi/documents.rb', line 8

def self.create(type, doc, meta = nil)
  d = {type: type, doc: doc}
  d[:meta] = meta unless meta.nil?
  Teapi.post(:documents, Oj.dump(d, mode: :compat, time_format: :ruby))
end

.delete(type, id) ⇒ Object

deletes the document, by its id, belonging to the given type

Parameters:

  • type (String)

    the document’s type

  • doc (Hash)

    the document to update



27
28
29
# File 'lib/teapi/documents.rb', line 27

def self.delete(type, id)
  Teapi.delete(:documents, Oj.dump({type: type, id: id}, mode: :compat, time_format: :ruby))
end

.update(type, doc, meta = nil) ⇒ Object

updates the document belonging to the given type

Parameters:

  • type (String)

    the document’s type

  • doc (Hash)

    the document to update

  • optional

    meta [Hash] meta data associated with the document



18
19
20
21
22
# File 'lib/teapi/documents.rb', line 18

def self.update(type, doc, meta = nil)
  d = {type: type, doc: doc}
  d[:meta] = meta unless meta.nil?
  Teapi.put(:documents, Oj.dump(d, mode: :compat, time_format: :ruby))
end