Class: ContentBlockTools::ContentBlock
- Inherits:
-
Object
- Object
- ContentBlockTools::ContentBlock
- Defined in:
- lib/content_block_tools/content_block.rb
Overview
Defines a Content Block
Constant Summary collapse
- CONTENT_BLOCK_PREFIX =
"content_block_".freeze
Instance Attribute Summary collapse
-
#content_id ⇒ String
readonly
The content UUID for a block.
-
#details ⇒ Hash
readonly
A hash that contains the details of the content block.
-
#document_type ⇒ String
readonly
The document type of the content block - this will be used to work out which Presenter will be used to render the content block.
-
#embed_code ⇒ String
readonly
The embed_code used for a block containing optional field name.
-
#format ⇒ String
readonly
The format specifier from the embed code, used to control rendering output.
-
#title ⇒ String
readonly
A title for the content block.
Class Method Summary collapse
-
.from_embed_code(embed_code) ⇒ ContentBlock
Creates a ContentBlock instance from an embed code string by fetching the content item data from the Content Store API.
Instance Method Summary collapse
-
#initialize(content_id:, title:, document_type:, details:, embed_code:) ⇒ ContentBlock
constructor
A new instance of ContentBlock.
-
#render ⇒ String
Renders the content block to HTML using the appropriate component or presenter.
Constructor Details
#initialize(content_id:, title:, document_type:, details:, embed_code:) ⇒ ContentBlock
Returns a new instance of ContentBlock.
84 85 86 87 88 89 90 |
# File 'lib/content_block_tools/content_block.rb', line 84 def initialize(content_id:, title:, document_type:, details:, embed_code:) @content_id = content_id @title = title @document_type = document_type @details = details @embed_code = end |
Instance Attribute Details
#content_id ⇒ String (readonly)
The content UUID for a block
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/content_block_tools/content_block.rb', line 47 class ContentBlock CONTENT_BLOCK_PREFIX = "content_block_".freeze attr_reader :content_id, :title, :embed_code # Creates a ContentBlock instance from an embed code string by fetching # the content item data from the Content Store API. # # @param embed_code [String] The embed code string to parse and fetch content for # @example # ContentBlock.from_embed_code("{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}") # # @return [ContentBlock] A new ContentBlock instance populated with data from the Content Store # # @raise [ContentBlockTools::InvalidEmbedCodeError] if the embed code format is invalid # @raise [GdsApi::HTTPErrorResponse] if the API request fails # # @example Create a ContentBlock from an embed code # embed_code = "{{embed:content_block_email:123e4567-e89b-12d3-a456-426614174000}}" # content_block = ContentBlock.from_embed_code(embed_code) # content_block.title #=> "Contact Email" # content_block.document_type #=> "email" # # @see ContentBlockReference.from_string # @see GdsApi.content_store def self.() reference = ContentBlockReference.from_string() api_response = GdsApi.content_store.content_item(reference.content_store_identifier) new( content_id: api_response["content_id"], title: api_response["title"], document_type: api_response["document_type"], details: api_response["details"], embed_code:, ) end def initialize(content_id:, title:, document_type:, details:, embed_code:) @content_id = content_id @title = title @document_type = document_type @details = details @embed_code = end # Renders the content block to HTML using the appropriate component or presenter # # @return [String] A HTML representation of the content block # @see Renderer def render Renderer.new(self).render end def details @details.deep_symbolize_keys end def document_type @document_type.delete_prefix(CONTENT_BLOCK_PREFIX) end def format EmbedCode.new().format end end |
#details ⇒ Hash (readonly)
A hash that contains the details of the content block
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/content_block_tools/content_block.rb', line 47 class ContentBlock CONTENT_BLOCK_PREFIX = "content_block_".freeze attr_reader :content_id, :title, :embed_code # Creates a ContentBlock instance from an embed code string by fetching # the content item data from the Content Store API. # # @param embed_code [String] The embed code string to parse and fetch content for # @example # ContentBlock.from_embed_code("{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}") # # @return [ContentBlock] A new ContentBlock instance populated with data from the Content Store # # @raise [ContentBlockTools::InvalidEmbedCodeError] if the embed code format is invalid # @raise [GdsApi::HTTPErrorResponse] if the API request fails # # @example Create a ContentBlock from an embed code # embed_code = "{{embed:content_block_email:123e4567-e89b-12d3-a456-426614174000}}" # content_block = ContentBlock.from_embed_code(embed_code) # content_block.title #=> "Contact Email" # content_block.document_type #=> "email" # # @see ContentBlockReference.from_string # @see GdsApi.content_store def self.() reference = ContentBlockReference.from_string() api_response = GdsApi.content_store.content_item(reference.content_store_identifier) new( content_id: api_response["content_id"], title: api_response["title"], document_type: api_response["document_type"], details: api_response["details"], embed_code:, ) end def initialize(content_id:, title:, document_type:, details:, embed_code:) @content_id = content_id @title = title @document_type = document_type @details = details @embed_code = end # Renders the content block to HTML using the appropriate component or presenter # # @return [String] A HTML representation of the content block # @see Renderer def render Renderer.new(self).render end def details @details.deep_symbolize_keys end def document_type @document_type.delete_prefix(CONTENT_BLOCK_PREFIX) end def format EmbedCode.new().format end end |
#document_type ⇒ String (readonly)
The document type of the content block - this will be used to work out which Presenter will be used to render the content block. All supported document_types are documented in ContentBlockTools::ContentBlockReference::SUPPORTED_DOCUMENT_TYPES
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/content_block_tools/content_block.rb', line 47 class ContentBlock CONTENT_BLOCK_PREFIX = "content_block_".freeze attr_reader :content_id, :title, :embed_code # Creates a ContentBlock instance from an embed code string by fetching # the content item data from the Content Store API. # # @param embed_code [String] The embed code string to parse and fetch content for # @example # ContentBlock.from_embed_code("{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}") # # @return [ContentBlock] A new ContentBlock instance populated with data from the Content Store # # @raise [ContentBlockTools::InvalidEmbedCodeError] if the embed code format is invalid # @raise [GdsApi::HTTPErrorResponse] if the API request fails # # @example Create a ContentBlock from an embed code # embed_code = "{{embed:content_block_email:123e4567-e89b-12d3-a456-426614174000}}" # content_block = ContentBlock.from_embed_code(embed_code) # content_block.title #=> "Contact Email" # content_block.document_type #=> "email" # # @see ContentBlockReference.from_string # @see GdsApi.content_store def self.() reference = ContentBlockReference.from_string() api_response = GdsApi.content_store.content_item(reference.content_store_identifier) new( content_id: api_response["content_id"], title: api_response["title"], document_type: api_response["document_type"], details: api_response["details"], embed_code:, ) end def initialize(content_id:, title:, document_type:, details:, embed_code:) @content_id = content_id @title = title @document_type = document_type @details = details @embed_code = end # Renders the content block to HTML using the appropriate component or presenter # # @return [String] A HTML representation of the content block # @see Renderer def render Renderer.new(self).render end def details @details.deep_symbolize_keys end def document_type @document_type.delete_prefix(CONTENT_BLOCK_PREFIX) end def format EmbedCode.new().format end end |
#embed_code ⇒ String (readonly)
The embed_code used for a block containing optional field name
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/content_block_tools/content_block.rb', line 47 class ContentBlock CONTENT_BLOCK_PREFIX = "content_block_".freeze attr_reader :content_id, :title, :embed_code # Creates a ContentBlock instance from an embed code string by fetching # the content item data from the Content Store API. # # @param embed_code [String] The embed code string to parse and fetch content for # @example # ContentBlock.from_embed_code("{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}") # # @return [ContentBlock] A new ContentBlock instance populated with data from the Content Store # # @raise [ContentBlockTools::InvalidEmbedCodeError] if the embed code format is invalid # @raise [GdsApi::HTTPErrorResponse] if the API request fails # # @example Create a ContentBlock from an embed code # embed_code = "{{embed:content_block_email:123e4567-e89b-12d3-a456-426614174000}}" # content_block = ContentBlock.from_embed_code(embed_code) # content_block.title #=> "Contact Email" # content_block.document_type #=> "email" # # @see ContentBlockReference.from_string # @see GdsApi.content_store def self.() reference = ContentBlockReference.from_string() api_response = GdsApi.content_store.content_item(reference.content_store_identifier) new( content_id: api_response["content_id"], title: api_response["title"], document_type: api_response["document_type"], details: api_response["details"], embed_code:, ) end def initialize(content_id:, title:, document_type:, details:, embed_code:) @content_id = content_id @title = title @document_type = document_type @details = details @embed_code = end # Renders the content block to HTML using the appropriate component or presenter # # @return [String] A HTML representation of the content block # @see Renderer def render Renderer.new(self).render end def details @details.deep_symbolize_keys end def document_type @document_type.delete_prefix(CONTENT_BLOCK_PREFIX) end def format EmbedCode.new().format end end |
#format ⇒ String (readonly)
The format specifier from the embed code, used to control rendering output
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/content_block_tools/content_block.rb', line 47 class ContentBlock CONTENT_BLOCK_PREFIX = "content_block_".freeze attr_reader :content_id, :title, :embed_code # Creates a ContentBlock instance from an embed code string by fetching # the content item data from the Content Store API. # # @param embed_code [String] The embed code string to parse and fetch content for # @example # ContentBlock.from_embed_code("{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}") # # @return [ContentBlock] A new ContentBlock instance populated with data from the Content Store # # @raise [ContentBlockTools::InvalidEmbedCodeError] if the embed code format is invalid # @raise [GdsApi::HTTPErrorResponse] if the API request fails # # @example Create a ContentBlock from an embed code # embed_code = "{{embed:content_block_email:123e4567-e89b-12d3-a456-426614174000}}" # content_block = ContentBlock.from_embed_code(embed_code) # content_block.title #=> "Contact Email" # content_block.document_type #=> "email" # # @see ContentBlockReference.from_string # @see GdsApi.content_store def self.() reference = ContentBlockReference.from_string() api_response = GdsApi.content_store.content_item(reference.content_store_identifier) new( content_id: api_response["content_id"], title: api_response["title"], document_type: api_response["document_type"], details: api_response["details"], embed_code:, ) end def initialize(content_id:, title:, document_type:, details:, embed_code:) @content_id = content_id @title = title @document_type = document_type @details = details @embed_code = end # Renders the content block to HTML using the appropriate component or presenter # # @return [String] A HTML representation of the content block # @see Renderer def render Renderer.new(self).render end def details @details.deep_symbolize_keys end def document_type @document_type.delete_prefix(CONTENT_BLOCK_PREFIX) end def format EmbedCode.new().format end end |
#title ⇒ String (readonly)
A title for the content block
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/content_block_tools/content_block.rb', line 47 class ContentBlock CONTENT_BLOCK_PREFIX = "content_block_".freeze attr_reader :content_id, :title, :embed_code # Creates a ContentBlock instance from an embed code string by fetching # the content item data from the Content Store API. # # @param embed_code [String] The embed code string to parse and fetch content for # @example # ContentBlock.from_embed_code("{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}") # # @return [ContentBlock] A new ContentBlock instance populated with data from the Content Store # # @raise [ContentBlockTools::InvalidEmbedCodeError] if the embed code format is invalid # @raise [GdsApi::HTTPErrorResponse] if the API request fails # # @example Create a ContentBlock from an embed code # embed_code = "{{embed:content_block_email:123e4567-e89b-12d3-a456-426614174000}}" # content_block = ContentBlock.from_embed_code(embed_code) # content_block.title #=> "Contact Email" # content_block.document_type #=> "email" # # @see ContentBlockReference.from_string # @see GdsApi.content_store def self.() reference = ContentBlockReference.from_string() api_response = GdsApi.content_store.content_item(reference.content_store_identifier) new( content_id: api_response["content_id"], title: api_response["title"], document_type: api_response["document_type"], details: api_response["details"], embed_code:, ) end def initialize(content_id:, title:, document_type:, details:, embed_code:) @content_id = content_id @title = title @document_type = document_type @details = details @embed_code = end # Renders the content block to HTML using the appropriate component or presenter # # @return [String] A HTML representation of the content block # @see Renderer def render Renderer.new(self).render end def details @details.deep_symbolize_keys end def document_type @document_type.delete_prefix(CONTENT_BLOCK_PREFIX) end def format EmbedCode.new().format end end |
Class Method Details
.from_embed_code(embed_code) ⇒ ContentBlock
Creates a ContentBlock instance from an embed code string by fetching the content item data from the Content Store API.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/content_block_tools/content_block.rb', line 72 def self.() reference = ContentBlockReference.from_string() api_response = GdsApi.content_store.content_item(reference.content_store_identifier) new( content_id: api_response["content_id"], title: api_response["title"], document_type: api_response["document_type"], details: api_response["details"], embed_code:, ) end |