Class: Dato::Site::Repo::Item

Inherits:
Base
  • Object
show all
Defined in:
lib/dato/site/repo/item.rb

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Dato::Site::Repo::Base

Instance Method Details

#all(filters = {}, deserialize_response = true) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dato/site/repo/item.rb', line 39

def all(filters = {}, deserialize_response = true)
  items_per_page = 500

  base_response = client.request(
    :get, '/items', filters.dup.merge('page[limit]' => items_per_page)
  )

  extra_pages = (
    base_response[:meta][:total_count] / items_per_page.to_f
  ).ceil - 1

  extra_pages.times do |page|
    base_response[:data] += client.request(
      :get,
      '/items',
      filters.dup.merge(
        'page[offset]' => items_per_page * (page + 1),
        'page[limit]' => items_per_page
      )
    )[:data]
  end

  if deserialize_response
    JsonApiDeserializer.new.deserialize(base_response)
  else
    base_response
  end
end

#create(resource_attributes) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dato/site/repo/item.rb', line 9

def create(resource_attributes)
  body = JsonApiSerializer.new(
    type: :item,
    attributes: resource_attributes.keys - [:item_type, :id],
    relationships: {
      item_type: { collection: false, type: 'item_type' }
    },
    required_relationships: %i(item_type)
  ).serialize(resource_attributes)

  post_request '/items', body
end

#destroy(item_id) ⇒ Object



72
73
74
# File 'lib/dato/site/repo/item.rb', line 72

def destroy(item_id)
  delete_request "/items/#{item_id}"
end

#find(item_id) ⇒ Object



68
69
70
# File 'lib/dato/site/repo/item.rb', line 68

def find(item_id)
  get_request "/items/#{item_id}"
end

#update(item_id, resource_attributes) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dato/site/repo/item.rb', line 22

def update(item_id, resource_attributes)
  resource_attributes = resource_attributes.except(
    :id,
    :updated_at,
    :is_valid,
    :item_type,
    :last_editor
  )

  body = JsonApiSerializer.new(
    type: :item,
    attributes: resource_attributes.keys
  ).serialize(resource_attributes, item_id)

  put_request "/items/#{item_id}", body
end