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
63
# File 'lib/contentful/management/space.rb', line 52

def self.create(attributes)
  default_locale = attributes[:default_locale] || client.default_locale
  request = Request.new(
      '',
      {'name' => attributes.fetch(:name), defaultLocale: default_locale},
      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.



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

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.



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

def content_types
  SpaceContentTypeMethodsFactory.new(self)
end

#destroyObject

Destroys a space. Returns true if succeed.



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

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.



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

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.



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

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.



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

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.



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

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.



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

def webhooks
  SpaceWebhookMethodsFactory.new(self)
end