Class: Provet::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/provet/base.rb

Instance Method Summary collapse

Instance Method Details

#all(query = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/provet/base.rb', line 13

def all(query = {})
  data = list(page: 1, **query)

  return [] unless data['results'].is_a?(Array) && data['results'].any? && data['num_pages'].is_a?(Integer)

  num_pages = data['num_pages']
  return data['results'] if num_pages <= 1

  results = data['results']
  (2..num_pages).each do |page_index|
    data = list(page: page_index, **query)
    results += data['results']
  end
  results
end

#collection_pathObject



65
66
67
# File 'lib/provet/base.rb', line 65

def collection_path
  File.join('/', endpoint_name, '/')
end

#collection_urlObject



61
62
63
# File 'lib/provet/base.rb', line 61

def collection_url
  File.join(self.class.base_uri, collection_path)
end

#create(body) ⇒ Object



33
34
35
# File 'lib/provet/base.rb', line 33

def create(body)
  post(collection_path, body)
end

#destroy(id, hard: !soft_deletable?)) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/provet/base.rb', line 41

def destroy(id, hard: !soft_deletable?)
  if hard
    delete(resource_path(id))
  else
    patch(resource_path(id), archive_payload)
  end
end

#find(id) ⇒ Object



29
30
31
# File 'lib/provet/base.rb', line 29

def find(id)
  get(resource_path(id))
end

#list(query = {}) ⇒ Object



9
10
11
# File 'lib/provet/base.rb', line 9

def list(query = {})
  get(collection_path, query: query)
end

#really_destroy!(id) ⇒ Object



49
50
51
52
53
# File 'lib/provet/base.rb', line 49

def really_destroy!(id)
  raise MethodNotAllowedError unless soft_deletable?

  destroy(id, hard: true)
end

#resource_path(id) ⇒ Object



73
74
75
# File 'lib/provet/base.rb', line 73

def resource_path(id)
  File.join(collection_path, id.to_s, '/')
end

#resource_url(id) ⇒ Object



69
70
71
# File 'lib/provet/base.rb', line 69

def resource_url(id)
  File.join(self.class.base_uri, resource_path(id))
end

#restore(id) ⇒ Object



55
56
57
58
59
# File 'lib/provet/base.rb', line 55

def restore(id)
  raise MethodNotAllowedError unless soft_deletable?

  patch(resource_path(id), restore_payload)
end

#update(id, body, verb: :put) ⇒ Object



37
38
39
# File 'lib/provet/base.rb', line 37

def update(id, body, verb: :put)
  send(verb, resource_path(id), body)
end