Class: Contentful::Management::Space

Inherits:
Object
  • Object
show all
Includes:
Resource, Resource::Refresher, Resource::SystemProperties
Defined in:
lib/contentful/management/space.rb

Overview

Constant Summary

Constants included from Resource::SystemProperties

Resource::SystemProperties::SYS_COERCIONS

Constants included from Resource

Resource::COERCIONS

Instance Attribute Summary

Attributes included from Resource::SystemProperties

#sys

Attributes included from Resource

#client, #default_locale, #properties, #raw_object, #request

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource::Refresher

#refresh_data, #reload

Methods included from Resource::SystemProperties

included, #initialize, #inspect

Methods included from Resource

#array?, #fields, #initialize, #inspect, #nested_locale_fields?, #sys

Class Method Details

.allObject

Gets a collection of spaces. Returns a Contentful::Management::Array of Contentful::Management::Space.



28
29
30
31
32
33
34
35
# File 'lib/contentful/management/space.rb', line 28

def self.all
  request = Request.new('')
  response = request.get
  result = ResourceBuilder.new(response, {}, {})
  spaces = result.run
  client.update_dynamic_entry_cache_for_spaces!(spaces)
  spaces
end

.create(attributes) ⇒ Object

Create a space. Takes a hash of attributes with optional organization id if client has more than one organization. Returns a Contentful::Management::Space.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/contentful/management/space.rb', line 52

def self.create(attributes)
  request = Request.new(
      '',
      {'name' => attributes.fetch(:name)},
      id = nil,
      organization_id: attributes[:organization_id]
  )
  response = request.post
  result = ResourceBuilder.new(response, {}, {})
  result.run
end

.find(space_id) ⇒ Object

Gets a specific space. Takes an id of space. Returns a Contentful::Management::Space.



40
41
42
43
44
45
46
47
# File 'lib/contentful/management/space.rb', line 40

def self.find(space_id)
  request = Request.new("/#{ space_id }")
  response = request.get
  result = ResourceBuilder.new(response, {}, {})
  space = result.run
  client.update_dynamic_entry_cache_for_space!(space) if space.is_a? Space
  space
end

Instance Method Details

#assetsObject

Allows manipulation of assets in context of the current space Allows listing all assets of space, creating new and finding one by id. See README for details.



120
121
122
# File 'lib/contentful/management/space.rb', line 120

def assets
  SpaceAssetMethodsFactory.new(self)
end

#content_typesObject

Allows manipulation of content types in context of the current space Allows listing all content types of space, creating new and finding one by id. See README for details.



106
107
108
# File 'lib/contentful/management/space.rb', line 106

def content_types
  SpaceContentTypeMethodsFactory.new(self)
end

#destroyObject

Destroys a space. Returns true if succeed.



93
94
95
96
97
98
99
100
101
# File 'lib/contentful/management/space.rb', line 93

def destroy
  request = Request.new("/#{ id }")
  response = request.delete
  if response.status == :no_content
    return true
  else
    ResourceBuilder.new(response, {}, {}).run
  end
end

#entriesObject

Allows manipulation of entries in context of the current space Allows listing all entries for space and finding one by id. See README for details.



127
128
129
# File 'lib/contentful/management/space.rb', line 127

def entries
  SpaceEntryMethodsFactory.new(self)
end

#localesObject

Allows manipulation of locales in context of the current space Allows listing all locales of space, creating new and finding one by id. See README for details.



113
114
115
# File 'lib/contentful/management/space.rb', line 113

def locales
  SpaceLocaleMethodsFactory.new(self)
end

#saveObject

If a space is new, an object gets created in the Contentful, otherwise the existing space gets updated. See README for details.



82
83
84
85
86
87
88
89
# File 'lib/contentful/management/space.rb', line 82

def save
  if id
    update(name: name, organization_id: organization)
  else
    new_instance = self.class.create(name: name, organization_id: organization)
    refresh_data(new_instance)
  end
end

#update(attributes) ⇒ Object

Updates a space. Takes a hash of attributes with optional organization id if client has more than one organization. Returns a Contentful::Management::Space.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/contentful/management/space.rb', line 67

def update(attributes)
  request = Request.new(
      "/#{ id }",
      {'name' => attributes.fetch(:name)},
      id = nil,
      version: sys[:version],
      organization_id: attributes[:organization_id]
  )
  response = request.put
  result = ResourceBuilder.new(response, {}, {})
  refresh_data(result.run)
end

#webhooksObject

Allows manipulation of webhooks in context of the current space Allows listing all webhooks for space and finding one by id. See README for details.



134
135
136
# File 'lib/contentful/management/space.rb', line 134

def webhooks
  SpaceWebhookMethodsFactory.new(self)
end