Skip to content

Commit

Permalink
Adds support for Tags API
Browse files Browse the repository at this point in the history
  • Loading branch information
rubydog committed Apr 22, 2022
1 parent 4b9e4a4 commit b1d173f
Show file tree
Hide file tree
Showing 28 changed files with 4,370 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Master
* Changed CI/CD vendor
* Added support for Tags API

## 3.3.0
* Added getter/setter for EditorInterface#sidebar
Expand Down
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,50 @@ Destroying a locale:
blog_post_locale.destroy
```

### Tags

Retrieving all tags from the environment:

```ruby
tags = environment.tags.all
```

Retrieving one tag by ID from the environment:

```ruby
tag = environment.tags.find(tag_id)
```

Creating a tag:

```ruby
environment.tags.create(name: 'tag name', id: 'tagID')
```

Updating a tag:

```ruby
tag.update(name: 'new name')
```

Destroying a tag:

```ruby
tag.destroy
```

Tagging an entry:

```ruby
entry.update(_metadata: {"tags": [{ "sys": { "type": "Link", "linkType": "Tag", "id": "fooTag" } }]})
```

Tagging an asset:

```ruby
asset.update(_metadata: {"tags": [{ "sys": { "type": "Link", "linkType": "Tag", "id": "fooTag" } }]})
```

### Roles

Retrieving all roles from the space:
Expand Down
4 changes: 2 additions & 2 deletions lib/contentful/management/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.create_attributes(client, attributes)
fields[:description] = { locale => attributes[:description] } if attributes[:description]
fields[:file] = { locale => attributes[:file].properties } if attributes[:file]

{ fields: fields }
{ fields: fields, metadata: attributes[:_metadata] }
end

# @private
Expand Down Expand Up @@ -122,7 +122,7 @@ def query_attributes(attributes)
self.description = attributes[:description] || description
self.file = attributes[:file] || file

{ fields: fields_for_query }
{ fields: fields_for_query, metadata: attributes[:_metadata] }
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/contentful/management/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'contentful/management/response'
require 'contentful/management/resource_builder'

require 'contentful/management/client_tag_methods_factory'
require 'contentful/management/client_role_methods_factory'
require 'contentful/management/client_user_methods_factory'
require 'contentful/management/client_space_methods_factory'
Expand Down Expand Up @@ -194,6 +195,15 @@ def assets(space_id, environment_id)
ClientAssetMethodsFactory.new(self, space_id, environment_id)
end

# Allows manipulation of tags in context of the current client
# Allows listing all tags for client, creating new and finding one by ID.
# @see _ README for details.
#
# @return [Contentful::Management::ClientTagMethodsFactory]
def tags(space_id, environment_id)
ClientTagMethodsFactory.new(self, space_id, environment_id)
end

# Allows manipulation of content types in context of the current client
# Allows listing all content types for client, creating new and finding one by ID.
# @see _ README for details.
Expand Down
11 changes: 11 additions & 0 deletions lib/contentful/management/client_tag_methods_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative 'client_association_methods_factory'

module Contentful
module Management
# Wrapper for Tag API for usage from within Client
# @private
class ClientTagMethodsFactory
include Contentful::Management::ClientAssociationMethodsFactory
end
end
end
6 changes: 3 additions & 3 deletions lib/contentful/management/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def self.create_attributes(client, attributes)
end

client.register_dynamic_entry(content_type.id, DynamicEntry.create(content_type, client))

{ fields: fields_for_create }
{ fields: fields_for_create, metadata: attributes[:_metadata] }
end

# @private
Expand Down Expand Up @@ -183,7 +182,8 @@ def snapshots
protected

def query_attributes(attributes)
{ fields: Contentful::Management::Support.deep_hash_merge(fields_for_query, fields_from_attributes(attributes)) }
{ metadata: attributes.delete(:_metadata),
fields: Contentful::Management::Support.deep_hash_merge(fields_for_query, fields_from_attributes(attributes)) }
end

private
Expand Down
10 changes: 10 additions & 0 deletions lib/contentful/management/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_relative 'environment_content_type_methods_factory'
require_relative 'environment_ui_extension_methods_factory'
require_relative 'environment_editor_interface_methods_factory'
require_relative 'environment_tag_methods_factory'

module Contentful
module Management
Expand Down Expand Up @@ -121,6 +122,15 @@ def editor_interfaces
EnvironmentEditorInterfaceMethodsFactory.new(self)
end

# Allows manipulation of tags in context of the current environment
# Allows listing all tags for the current environment, creating new and finding one by ID.
# @see _ README for details.
#
# @return [Contentful::Management::EnvironmentTagMethodsFactory]
def tags
EnvironmentTagMethodsFactory.new(self)
end

# Gets the environment ID
def environment_id
id
Expand Down
11 changes: 11 additions & 0 deletions lib/contentful/management/environment_tag_methods_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative 'environment_association_methods_factory'

module Contentful
module Management
# Wrapper for Tag API for usage from within Environment
# @private
class EnvironmentTagMethodsFactory
include Contentful::Management::EnvironmentAssociationMethodsFactory
end
end
end
1 change: 1 addition & 0 deletions lib/contentful/management/resource/refresher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def refresh_data(resource)
@properties = resource.instance_variable_get(:@properties)
@fields = resource.instance_variable_get(:@fields)
@sys = resource.instance_variable_get(:@sys).merge(locale: locale)
@_metadata = resource.instance_variable_get(:@_metadata)
self
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/contentful/management/resource_requester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def update(object, endpoint_options = {}, attributes = {}, headers = {})
object.refresh_data(put(endpoint_options, attributes, headers, object))
end

def destroy(endpoint_options = {})
delete(endpoint_options)
def destroy(endpoint_options = {}, attributes = {}, headers = {})
delete(endpoint_options, attributes, headers)
end

def archive(object, endpoint_options = {}, headers = {})
Expand Down
27 changes: 27 additions & 0 deletions lib/contentful/management/tag.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
require_relative 'resource'
require_relative 'resource/environment_aware'

module Contentful
module Management
# Resource Class for Tags
# https://www.contentful.com/developers/docs/references/content-management-api/#/reference/content-tags
class Tag
include Contentful::Management::Resource
include Contentful::Management::Resource::Refresher
include Contentful::Management::Resource::SystemProperties
include Contentful::Management::Resource::EnvironmentAware

property :name

# @private
def self.create_attributes(_client, attributes)
return {} if attributes.nil? || attributes.empty?

{
'name' => attributes.fetch(:name),
'sys' => {
'visibility' => attributes.fetch(:visibility, 'private'),
'id' => attributes.fetch(:id),
'type' => 'Tag'
}
}
end

def destroy
ResourceRequester.new(client, self.class).destroy(
{ space_id: space.id,
environment_id: environment_id,
resource_id: id },
{},
version: sys[:version]
)
end
end
end
end
Loading

0 comments on commit b1d173f

Please sign in to comment.