class Provet::Base

Public Instance Methods

all(query = {}) click to toggle source
# 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_path() click to toggle source
# File lib/provet/base.rb, line 65
def collection_path
  File.join('/', endpoint_name, '/')
end
collection_url() click to toggle source
# File lib/provet/base.rb, line 61
def collection_url
  File.join(self.class.base_uri, collection_path)
end
create(body) click to toggle source
# File lib/provet/base.rb, line 33
def create(body)
  post(collection_path, body)
end
destroy(id, hard: !soft_deletable?) click to toggle source
# 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) click to toggle source
# File lib/provet/base.rb, line 29
def find(id)
  get(resource_path(id))
end
list(query = {}) click to toggle source
# File lib/provet/base.rb, line 9
def list(query = {})
  get(collection_path, query: query)
end
really_destroy!(id) click to toggle source
# File lib/provet/base.rb, line 49
def really_destroy!(id)
  raise MethodNotAllowedError unless soft_deletable?

  destroy(id, hard: true)
end
resource_path(id) click to toggle source
# File lib/provet/base.rb, line 73
def resource_path(id)
  File.join(collection_path, id.to_s, '/')
end
resource_url(id) click to toggle source
# File lib/provet/base.rb, line 69
def resource_url(id)
  File.join(self.class.base_uri, resource_path(id))
end
restore(id) click to toggle source
# 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) click to toggle source
# File lib/provet/base.rb, line 37
def update(id, body, verb: :put)
  send(verb, resource_path(id), body)
end

Protected Instance Methods

archive_payload() click to toggle source
# File lib/provet/base.rb, line 83
def archive_payload
  { archived: 1 }.to_json
end
default_options() click to toggle source
# File lib/provet/base.rb, line 115
def default_options
  { headers: headers }
end
default_query() click to toggle source
# File lib/provet/base.rb, line 119
def default_query
  {}
end
delete(path) click to toggle source
# File lib/provet/base.rb, line 111
def delete(path)
  self.class.delete(path, default_options)
end
endpoint_name() click to toggle source
# File lib/provet/base.rb, line 131
def endpoint_name
  raise 'Must be overriden by subclass'
end
get(path, query: {}) click to toggle source
# File lib/provet/base.rb, line 91
def get(path, query: {})
  options = default_options
  options[:query] ||= default_query
  options[:query].merge!(query) if query.any?

  self.class.get(path, options)
end
headers() click to toggle source
# File lib/provet/base.rb, line 123
def headers
  {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'Authorization' => "Token #{Provet.token}"
  }
end
patch(path, body) click to toggle source
# File lib/provet/base.rb, line 107
def patch(path, body)
  self.class.patch(path, default_options.merge(body: body))
end
post(path, body) click to toggle source
# File lib/provet/base.rb, line 99
def post(path, body)
  self.class.post(path, default_options.merge(body: body))
end
put(path, body) click to toggle source
# File lib/provet/base.rb, line 103
def put(path, body)
  self.class.put(path, default_options.merge(body: body))
end
restore_payload() click to toggle source
# File lib/provet/base.rb, line 87
def restore_payload
  { archived: 0 }.to_json
end
soft_deletable?() click to toggle source
# File lib/provet/base.rb, line 79
def soft_deletable?
  false
end