Class: Deta::BaseResource

Inherits:
Resource show all
Defined in:
lib/deta/resources/base.rb

Constant Summary collapse

@@api_url =
"https://database.deta.sh/v1"

Instance Attribute Summary

Attributes inherited from Resource

#client, #resource_name

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Deta::Resource

Instance Method Details

#delete(key = nil) ⇒ Object



20
21
22
# File 'lib/deta/resources/base.rb', line 20

def delete(key = nil)
  BaseObject.new delete_request([@@api_url, client.project_id, resource_name, "items", key].join("/")).body
end

#fetch(query: nil, limit: nil, last: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/deta/resources/base.rb', line 40

def fetch(query: nil, limit: nil, last: nil)
  request = {}
  request[:limit] = limit
  request[:last] = last
  if query.is_a?(Array)
    request[:query] = query
  else
    request[:query] = [query]
  end

  BaseObject.new post_request([@@api_url, client.project_id, resource_name, "query"].join("/"), body: request).body
end

#get(key = nil) ⇒ Object



16
17
18
# File 'lib/deta/resources/base.rb', line 16

def get(key = nil)
  BaseObject.new get_request([@@api_url, client.project_id, resource_name, "items", key].join("/")).body
end

#insert(**attributes) ⇒ Object



24
25
26
27
# File 'lib/deta/resources/base.rb', line 24

def insert(**attributes)
  item = {item: attributes}
  BaseObject.new post_request([@@api_url, client.project_id, resource_name, "items"].join("/"), body: item).body
end

#put(attributes) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/deta/resources/base.rb', line 5

def put(attributes)
  items = {}
  if attributes.is_a?(Array)
    items["items"] = attributes
  else
    items["items"] = [attributes]
  end

  BaseObject.new put_request([@@api_url, client.project_id, resource_name, "items"].join("/"), body: items).body
end

#update(key = nil, set: nil, increment: nil, append: nil, prepend: nil, delete: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/deta/resources/base.rb', line 29

def update(key = nil, set: nil, increment: nil, append: nil, prepend: nil, delete: nil)
  request = {}
  request[:set] = set if set
  request[:increment] = increment if increment
  request[:append] = append if append
  request[:prepend] = prepend if prepend
  request[:delete] = delete if delete
  
  BaseObject.new patch_request([@@api_url, client.project_id, resource_name, "items", key].join("/"), body: request).body
end