class ContentfulLite::Entry
Attributes
content_type_id[R]
The id for the content type of this entry
Public Class Methods
field_reader(*attrs, default: nil, localizable: false)
click to toggle source
Defines a field existing on the content type. This macro registers the accessor for that field @param attrs [Array<Symbol,String>] The field names @param default [Object, nil] The default value to return if field is not present on API response @param localizable [Boolean] If the field is marked as localizable @example Defines two string localizable localized fields
field_reader :first_name, :last_name, localizable: true
@see github.com/JuulLabs-OSS/contentful_lite#creating-your-model-classes-with-macros
# File lib/contentful_lite/entry.rb, line 34 def self.field_reader(*attrs, default: nil, localizable: false) attrs.each do |k| define_method(k) do |locale: nil| field = fields(locale: localizable ? locale : default_locale)[k.to_s] field.nil? ? default : field end end end
new(raw)
click to toggle source
@param raw [Hash] raw response from Contentful API @api private
Calls superclass method
ContentfulLite::CommonData::new
# File lib/contentful_lite/entry.rb, line 12 def initialize(raw) super(raw) @content_type_id = raw['sys']['contentType']['sys']['id'] @localized_fields.values.each do |fields| fields.transform_values! { |value| build_link(value) } end end
Public Instance Methods
contentful_link()
click to toggle source
Gets the URL to view/edit the entry on Contentful webapp @return [String]
# File lib/contentful_lite/entry.rb, line 22 def contentful_link "https://app.contentful.com/spaces/#{space_id}/entries/#{id}" end
Private Instance Methods
build_link(value)
click to toggle source
# File lib/contentful_lite/entry.rb, line 45 def build_link(value) return value.map!{ |element| build_link(element) } if value.is_a?(Array) return value unless value.is_a?(Hash) && value.fetch('sys', {}).fetch('type', '') == 'Link' ContentfulLite::Link.new(value) end