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, #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.



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

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.



53
54
55
56
57
58
# File 'lib/contentful/management/space.rb', line 53

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.



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

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.



110
111
112
# File 'lib/contentful/management/space.rb', line 110

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.



96
97
98
# File 'lib/contentful/management/space.rb', line 96

def content_types
  SpaceContentTypeMethodsFactory.new(self)
end

#destroyObject

Destroys a space. Returns true if succeed.



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

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.



117
118
119
# File 'lib/contentful/management/space.rb', line 117

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.



103
104
105
# File 'lib/contentful/management/space.rb', line 103

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.



72
73
74
75
76
77
78
79
# File 'lib/contentful/management/space.rb', line 72

def save
  if id.nil?
    new_instance = self.class.create(name: name, organization_id: organization)
    refresh_data(new_instance)
  else
    update(name: name, organization_id: organization)
  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.



63
64
65
66
67
68
# File 'lib/contentful/management/space.rb', line 63

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.



124
125
126
# File 'lib/contentful/management/space.rb', line 124

def webhooks
  SpaceWebhookMethodsFactory.new(self)
end