Class: Dato::Items

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/items.rb

Constant Summary collapse

BASE_ITEM_URL =
"https://site-api.datocms.com/items"

Instance Method Summary collapse

Constructor Details

#initialize(api_token) ⇒ Items

Returns a new instance of Items.



5
6
7
8
9
# File 'lib/dato/items.rb', line 5

def initialize(api_token)
  @http_headers = HTTP.headers({"Authorization" => "Bearer #{api_token}",
                                 "Accept" => "application/json",
                                 "X-Api-Version" => 3})
end

Instance Method Details

#all(item_type_id:) ⇒ Object



15
16
17
# File 'lib/dato/items.rb', line 15

def all(item_type_id:)
  @http_headers.get("#{BASE_ITEM_URL}?filter[type]=#{item_type_id}")
end

#create(item_type_id:, attributes:, meta: nil) ⇒ Object



19
20
21
22
23
# File 'lib/dato/items.rb', line 19

def create(item_type_id:, attributes:, meta: nil)
  data = create_attributes(attributes, item_type_id)
  data[:meta] = meta if meta
  @http_headers.post(BASE_ITEM_URL, json: {data: data})
end

#destroy(item_id:) ⇒ Object



30
31
32
# File 'lib/dato/items.rb', line 30

def destroy(item_id:)
  @http_headers.delete("#{BASE_ITEM_URL}/#{item_id}")
end

#find(item_id:) ⇒ Object



11
12
13
# File 'lib/dato/items.rb', line 11

def find(item_id:)
  @http_headers.get("#{BASE_ITEM_URL}/#{item_id}")
end

#update(attributes:, item_id:) ⇒ Object



25
26
27
28
# File 'lib/dato/items.rb', line 25

def update(attributes:, item_id:)
  data = {type: "item", attributes: attributes, id: item_id}
  @http_headers.put("#{BASE_ITEM_URL}/#{item_id}", json: {data: data})
end