Module: Contentful::Management::Resource

Included in:
ApiKey, ApiUsage, Array, Asset, ContentType, EditorInterface, Entry, Environment, Field, File, Link, Locale, Location, Organization, PersonalAccessToken, PreviewApiKey, Role, Snapshot, Space, SpaceMembership, UIExtension, Upload, UsagePeriod, User, Validation, Webhook, WebhookCall, WebhookHealth
Defined in:
lib/contentful/management/resource.rb,
lib/contentful/management/resource/fields.rb,
lib/contentful/management/resource/archiver.rb,
lib/contentful/management/resource/publisher.rb,
lib/contentful/management/resource/refresher.rb,
lib/contentful/management/resource/array_like.rb,
lib/contentful/management/resource/field_aware.rb,
lib/contentful/management/resource/asset_fields.rb,
lib/contentful/management/resource/entry_fields.rb,
lib/contentful/management/resource/all_published.rb,
lib/contentful/management/resource/environment_aware.rb,
lib/contentful/management/resource/system_properties.rb

Overview

Include this module to declare a class to be a contentful resource. This is done by the default in the existing resource classes

You can define your own classes that behave like contentful resources: See examples/custom_classes.rb to see how.

Take a look at examples/resource_mapping.rb on how to register them to be returned by the client by default

See Also:

  • examples/custom_classes.rb Custom Class as Resource
  • examples/resource_mapping.rb Mapping a Custom Class

Defined Under Namespace

Modules: AllPublished, Archiver, ArrayLike, AssetFields, ClassMethods, EntryFields, EnvironmentAware, FieldAware, Fields, Publisher, Refresher, SystemProperties

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Shared instance of the API client



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

def client
  @client
end

#propertiesObject (readonly)

rubocop:enable Style/DoubleNegation



30
31
32
# File 'lib/contentful/management/resource.rb', line 30

def properties
  @properties
end

#raw_objectObject (readonly)

rubocop:enable Style/DoubleNegation



30
31
32
# File 'lib/contentful/management/resource.rb', line 30

def raw_object
  @raw_object
end

#requestObject (readonly)

rubocop:enable Style/DoubleNegation



30
31
32
# File 'lib/contentful/management/resource.rb', line 30

def request
  @request
end

Instance Method Details

#array?Boolean

Returns true for resources that behave like an array

Returns:

  • (Boolean)


99
100
101
# File 'lib/contentful/management/resource.rb', line 99

def array?
  false
end

#default_localeObject

Get default_locale from client



136
137
138
# File 'lib/contentful/management/resource.rb', line 136

def default_locale
  client.default_locale
end

#destroytrue, Contentful::Management::Error

Destroys a resource.

Returns:



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

def destroy
  ResourceRequester.new(client, self.class).destroy(
    space_id: space.id,
    environment_id: environment_id,
    resource_id: id
  )
end

#environment_idObject

Returns the Environment ID



131
132
133
# File 'lib/contentful/management/resource.rb', line 131

def environment_id
  nil
end

#fieldsObject

Resources that don’t include Fields or AssetFields return nil for #fields



116
117
118
# File 'lib/contentful/management/resource.rb', line 116

def fields
  nil
end

#nested_locale_fields?Boolean

By default, fields come flattened in the current locale. This is different for syncs

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/contentful/management/resource.rb', line 104

def nested_locale_fields?
  # rubocop:disable Style/DoubleNegation
  !!@nested_locale_fields
  # rubocop:enable Style/DoubleNegation
end

#resource?true

Returns:

  • (true)


126
127
128
# File 'lib/contentful/management/resource.rb', line 126

def resource?
  true
end

#saveContentful::Management::Resource

Creates or updates a resource.



77
78
79
# File 'lib/contentful/management/resource.rb', line 77

def save
  update({})
end

#sysObject

Resources that don’t include SystemProperties return nil for #sys



111
112
113
# File 'lib/contentful/management/resource.rb', line 111

def sys
  nil
end

#update(attributes) ⇒ Contentful::Management::Resource

Updates a resource.

Parameters:

  • attributes (Hash)

Returns:

See Also:

  • README for more information on how to create each resource


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/contentful/management/resource.rb', line 62

def update(attributes)
  headers = self.class.create_headers(client, attributes, self)
  headers = headers.merge(update_headers)

  ResourceRequester.new(client, self.class).update(
    self,
    update_url_attributes,
    query_attributes(attributes),
    headers
  )
end