Class: Rummageable::Implementation

Inherits:
Object
  • Object
show all
Defined in:
lib/rummageable/implementation.rb

Instance Method Summary collapse

Instance Method Details

#amend(link, amendments) ⇒ Object



15
16
17
18
# File 'lib/rummageable/implementation.rb', line 15

def amend(link, amendments)
  validate_structure amendments
  RestClient.post url_for(link), amendments
end

#commitObject



24
25
26
27
# File 'lib/rummageable/implementation.rb', line 24

def commit
  url = Rummageable.rummager_host + Rummageable.path_prefix + "/commit"
  RestClient.post url, {}
end

#delete(link) ⇒ Object



20
21
22
# File 'lib/rummageable/implementation.rb', line 20

def delete(link)
  RestClient.delete url_for(link), content_type: :json, accept: :json
end

#index(documents) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/rummageable/implementation.rb', line 3

def index(documents)
  documents = [documents].flatten
  documents.each do |document|
    validate_structure document
  end
  url = Rummageable.rummager_host + Rummageable.path_prefix + "/documents"
  0.step(documents.length - 1, CHUNK_SIZE).each do |i|
    body = JSON.dump(documents[i, CHUNK_SIZE])
    RestClient.post url, body, content_type: :json, accept: :json
  end
end

#validate_structure(hash, parents = []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rummageable/implementation.rb', line 29

def validate_structure(hash, parents=[])
  hash.each do |key, value|
    full_key = parents + [key]
    case value
    when Array
      value.each do |el|
        validate_structure el, full_key
      end
    when Hash
      validate_structure value, full_key
    else
      raise InvalidDocument unless VALID_KEYS.include?(full_key)
    end
  end
end