Class: Mbrao::Content

Inherits:
Object
  • Object
show all
Includes:
ContentPublicInterface
Defined in:
lib/mbrao/content.rb

Overview

Represents a parsed content, with its metadata.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from ContentPublicInterface

#enabled_for_locales?, #get_body, #get_more, #get_tags, #get_title

Constructor Details

- (Content) initialize(uid = nil)

Creates a new content.

Parameters:

  • uid (String) (defaults to: nil)

    The UID for this content.



124
125
126
# File 'lib/mbrao/content.rb', line 124

def initialize(uid = nil)
  @uid = uid
end

Instance Attribute Details

- (Author) author

Returns The post author.

Returns:

  • (Author)

    The post author.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mbrao/content.rb', line 107

class Content
  include Mbrao::ContentPublicInterface

  attr_accessor :uid
  attr_accessor :locales
  attr_accessor :title
  attr_accessor :body
  attr_accessor :tags
  attr_accessor :more
  attr_accessor :author
  attr_accessor :created_at
  attr_accessor :updated_at
  attr_accessor :metadata

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  end

  # Sets the `title` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def title=(value)
    @title = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `body` attribute.
  #
  # @param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using #get_body.
  def body=(value)
    @body = value.ensure_string
  end

  # Sets the `tags` attribute.
  #
  # @param value [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def tags=(value)
    @tags = if value.is_a?(Hash) then
      value.ensure_hash(:indifferent) { |v| v.ensure_array(nil, true, true, true, :ensure_string) }
    else
      value.ensure_array(nil, true, true, true, :ensure_string)
    end
  end

  # Sets the `more` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def more=(value)
    @more = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `author` attribute.
  #
  # @param value [Author|Hash|Object] The new value for the attribute.
  def author=(value)
    if value.is_a?(Mbrao::Author) then
      @author = value
    elsif value.is_a?(Hash) then
      value = value.ensure_hash(:indifferent)
      @author = Mbrao::Author.new(value["name"], value["email"], value["website"], value["image"], value["metadata"], value["uid"])
    else
      @author = Mbrao::Author.new(value.ensure_string)
    end
  end

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value)
    @updated_at = @created_at if !@updated_at
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata || {}
  end

  # Sets the `metadata` attribute.
  #
  # @param value [Hash] The new value for the attribute.
  def metadata=(value)
    if value.is_a?(Hash) then
      @metadata = value.ensure_hash(:indifferent)
    else
      @metadata = HashWithIndifferentAccess.new({raw: value})
    end
  end

  # Validates locales for attribute retrieval.
  #
  # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
  # @param content [Content|nil] An optional content to check for availability
  # @return [Array] The validated list of locales.
  def self.validate_locales(locales, content = nil)
    locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
    locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
    raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
    locales
  end

  # Creates a content with metadata and body.
  #
  # @param metadata [Hash] The metadata.
  # @param body [String] The body of the content.
  # @return [Content] A new content.
  def self.create(, body)
    rv = Mbrao::Content.new
    rv.body = body.ensure_string.strip
    (rv, ) if .is_a?(Hash)
    rv
  end

  private
    # Assigns metadata to a content
    #
    # @param content [Content] The content to manipulate.
    # @param metadata [Hash] The metadata to assign.
    # @return [Content] The content with metadata.
    def self.(content, )
       = .symbolize_keys!

      content.uid = .delete(:uid)
      content.title = .delete(:title)
      content.author = Mbrao::Author.create(.delete(:author))
      content.tags = .delete(:tags)
      content.more = .delete(:more)
      content.created_at = .delete(:created_at)
      content.updated_at = .delete(:updated_at)
      content.locales = extract_locales()
      content. = 

      content
    end

    # Extract locales from metadata.
    #
    # @param metadata [Hash] The metadata that contains the locales.
    # @return [Array] The locales.
    def self.extract_locales()
      locales = .delete(:locales)
      locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
      normalize_locales(locales)
    end

    # Normalize locales for further usage.
    #
    # @param locales [Array] The locales to normalize.
    # @return [Array] The normalized locales.
    def self.normalize_locales(locales)
      locales.flatten.collect(&:ensure_string).collect(&:strip).uniq
    end

    # Extracts a date and time from a value.
    #
    # @param value [String|DateTime|Fixnum] The value to parse.
    # @return [DateTime] The extract values.
    def extract_datetime(value)
      begin
        if value.is_a?(Date) || value.is_a?(Time) then
          value = value.to_datetime
        elsif value.to_float > 0 then
          value = Time.at(value.to_float).to_datetime
        elsif value && !value.is_a?(DateTime) then
          value = DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z")
        end

        value ? value.utc : nil
      rescue ArgumentError
        raise Mbrao::Exceptions::InvalidDate.new
      end
    end
end

- (String|HashWithIndifferentAccess) body

Returns The content’s body. Can be a String or an HashWithIndifferentAccess, if multiple contents are specified for multiple locales.

Returns:

  • (String|HashWithIndifferentAccess)

    The content’s body. Can be a String or an HashWithIndifferentAccess, if multiple contents are specified for multiple locales.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mbrao/content.rb', line 107

class Content
  include Mbrao::ContentPublicInterface

  attr_accessor :uid
  attr_accessor :locales
  attr_accessor :title
  attr_accessor :body
  attr_accessor :tags
  attr_accessor :more
  attr_accessor :author
  attr_accessor :created_at
  attr_accessor :updated_at
  attr_accessor :metadata

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  end

  # Sets the `title` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def title=(value)
    @title = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `body` attribute.
  #
  # @param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using #get_body.
  def body=(value)
    @body = value.ensure_string
  end

  # Sets the `tags` attribute.
  #
  # @param value [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def tags=(value)
    @tags = if value.is_a?(Hash) then
      value.ensure_hash(:indifferent) { |v| v.ensure_array(nil, true, true, true, :ensure_string) }
    else
      value.ensure_array(nil, true, true, true, :ensure_string)
    end
  end

  # Sets the `more` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def more=(value)
    @more = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `author` attribute.
  #
  # @param value [Author|Hash|Object] The new value for the attribute.
  def author=(value)
    if value.is_a?(Mbrao::Author) then
      @author = value
    elsif value.is_a?(Hash) then
      value = value.ensure_hash(:indifferent)
      @author = Mbrao::Author.new(value["name"], value["email"], value["website"], value["image"], value["metadata"], value["uid"])
    else
      @author = Mbrao::Author.new(value.ensure_string)
    end
  end

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value)
    @updated_at = @created_at if !@updated_at
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata || {}
  end

  # Sets the `metadata` attribute.
  #
  # @param value [Hash] The new value for the attribute.
  def metadata=(value)
    if value.is_a?(Hash) then
      @metadata = value.ensure_hash(:indifferent)
    else
      @metadata = HashWithIndifferentAccess.new({raw: value})
    end
  end

  # Validates locales for attribute retrieval.
  #
  # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
  # @param content [Content|nil] An optional content to check for availability
  # @return [Array] The validated list of locales.
  def self.validate_locales(locales, content = nil)
    locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
    locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
    raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
    locales
  end

  # Creates a content with metadata and body.
  #
  # @param metadata [Hash] The metadata.
  # @param body [String] The body of the content.
  # @return [Content] A new content.
  def self.create(, body)
    rv = Mbrao::Content.new
    rv.body = body.ensure_string.strip
    (rv, ) if .is_a?(Hash)
    rv
  end

  private
    # Assigns metadata to a content
    #
    # @param content [Content] The content to manipulate.
    # @param metadata [Hash] The metadata to assign.
    # @return [Content] The content with metadata.
    def self.(content, )
       = .symbolize_keys!

      content.uid = .delete(:uid)
      content.title = .delete(:title)
      content.author = Mbrao::Author.create(.delete(:author))
      content.tags = .delete(:tags)
      content.more = .delete(:more)
      content.created_at = .delete(:created_at)
      content.updated_at = .delete(:updated_at)
      content.locales = extract_locales()
      content. = 

      content
    end

    # Extract locales from metadata.
    #
    # @param metadata [Hash] The metadata that contains the locales.
    # @return [Array] The locales.
    def self.extract_locales()
      locales = .delete(:locales)
      locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
      normalize_locales(locales)
    end

    # Normalize locales for further usage.
    #
    # @param locales [Array] The locales to normalize.
    # @return [Array] The normalized locales.
    def self.normalize_locales(locales)
      locales.flatten.collect(&:ensure_string).collect(&:strip).uniq
    end

    # Extracts a date and time from a value.
    #
    # @param value [String|DateTime|Fixnum] The value to parse.
    # @return [DateTime] The extract values.
    def extract_datetime(value)
      begin
        if value.is_a?(Date) || value.is_a?(Time) then
          value = value.to_datetime
        elsif value.to_float > 0 then
          value = Time.at(value.to_float).to_datetime
        elsif value && !value.is_a?(DateTime) then
          value = DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z")
        end

        value ? value.utc : nil
      rescue ArgumentError
        raise Mbrao::Exceptions::InvalidDate.new
      end
    end
end

- (DateTime) created_at

Returns The post creation date and time. The timezone is always UTC.

Returns:

  • (DateTime)

    The post creation date and time. The timezone is always UTC.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mbrao/content.rb', line 107

class Content
  include Mbrao::ContentPublicInterface

  attr_accessor :uid
  attr_accessor :locales
  attr_accessor :title
  attr_accessor :body
  attr_accessor :tags
  attr_accessor :more
  attr_accessor :author
  attr_accessor :created_at
  attr_accessor :updated_at
  attr_accessor :metadata

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  end

  # Sets the `title` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def title=(value)
    @title = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `body` attribute.
  #
  # @param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using #get_body.
  def body=(value)
    @body = value.ensure_string
  end

  # Sets the `tags` attribute.
  #
  # @param value [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def tags=(value)
    @tags = if value.is_a?(Hash) then
      value.ensure_hash(:indifferent) { |v| v.ensure_array(nil, true, true, true, :ensure_string) }
    else
      value.ensure_array(nil, true, true, true, :ensure_string)
    end
  end

  # Sets the `more` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def more=(value)
    @more = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `author` attribute.
  #
  # @param value [Author|Hash|Object] The new value for the attribute.
  def author=(value)
    if value.is_a?(Mbrao::Author) then
      @author = value
    elsif value.is_a?(Hash) then
      value = value.ensure_hash(:indifferent)
      @author = Mbrao::Author.new(value["name"], value["email"], value["website"], value["image"], value["metadata"], value["uid"])
    else
      @author = Mbrao::Author.new(value.ensure_string)
    end
  end

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value)
    @updated_at = @created_at if !@updated_at
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata || {}
  end

  # Sets the `metadata` attribute.
  #
  # @param value [Hash] The new value for the attribute.
  def metadata=(value)
    if value.is_a?(Hash) then
      @metadata = value.ensure_hash(:indifferent)
    else
      @metadata = HashWithIndifferentAccess.new({raw: value})
    end
  end

  # Validates locales for attribute retrieval.
  #
  # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
  # @param content [Content|nil] An optional content to check for availability
  # @return [Array] The validated list of locales.
  def self.validate_locales(locales, content = nil)
    locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
    locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
    raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
    locales
  end

  # Creates a content with metadata and body.
  #
  # @param metadata [Hash] The metadata.
  # @param body [String] The body of the content.
  # @return [Content] A new content.
  def self.create(, body)
    rv = Mbrao::Content.new
    rv.body = body.ensure_string.strip
    (rv, ) if .is_a?(Hash)
    rv
  end

  private
    # Assigns metadata to a content
    #
    # @param content [Content] The content to manipulate.
    # @param metadata [Hash] The metadata to assign.
    # @return [Content] The content with metadata.
    def self.(content, )
       = .symbolize_keys!

      content.uid = .delete(:uid)
      content.title = .delete(:title)
      content.author = Mbrao::Author.create(.delete(:author))
      content.tags = .delete(:tags)
      content.more = .delete(:more)
      content.created_at = .delete(:created_at)
      content.updated_at = .delete(:updated_at)
      content.locales = extract_locales()
      content. = 

      content
    end

    # Extract locales from metadata.
    #
    # @param metadata [Hash] The metadata that contains the locales.
    # @return [Array] The locales.
    def self.extract_locales()
      locales = .delete(:locales)
      locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
      normalize_locales(locales)
    end

    # Normalize locales for further usage.
    #
    # @param locales [Array] The locales to normalize.
    # @return [Array] The normalized locales.
    def self.normalize_locales(locales)
      locales.flatten.collect(&:ensure_string).collect(&:strip).uniq
    end

    # Extracts a date and time from a value.
    #
    # @param value [String|DateTime|Fixnum] The value to parse.
    # @return [DateTime] The extract values.
    def extract_datetime(value)
      begin
        if value.is_a?(Date) || value.is_a?(Time) then
          value = value.to_datetime
        elsif value.to_float > 0 then
          value = Time.at(value.to_float).to_datetime
        elsif value && !value.is_a?(DateTime) then
          value = DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z")
        end

        value ? value.utc : nil
      rescue ArgumentError
        raise Mbrao::Exceptions::InvalidDate.new
      end
    end
end

- (Array) locales

Returns A list of locales for this content should be visible. An empty list means that there are no limitations.

Returns:

  • (Array)

    A list of locales for this content should be visible. An empty list means that there are no limitations.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mbrao/content.rb', line 107

class Content
  include Mbrao::ContentPublicInterface

  attr_accessor :uid
  attr_accessor :locales
  attr_accessor :title
  attr_accessor :body
  attr_accessor :tags
  attr_accessor :more
  attr_accessor :author
  attr_accessor :created_at
  attr_accessor :updated_at
  attr_accessor :metadata

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  end

  # Sets the `title` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def title=(value)
    @title = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `body` attribute.
  #
  # @param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using #get_body.
  def body=(value)
    @body = value.ensure_string
  end

  # Sets the `tags` attribute.
  #
  # @param value [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def tags=(value)
    @tags = if value.is_a?(Hash) then
      value.ensure_hash(:indifferent) { |v| v.ensure_array(nil, true, true, true, :ensure_string) }
    else
      value.ensure_array(nil, true, true, true, :ensure_string)
    end
  end

  # Sets the `more` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def more=(value)
    @more = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `author` attribute.
  #
  # @param value [Author|Hash|Object] The new value for the attribute.
  def author=(value)
    if value.is_a?(Mbrao::Author) then
      @author = value
    elsif value.is_a?(Hash) then
      value = value.ensure_hash(:indifferent)
      @author = Mbrao::Author.new(value["name"], value["email"], value["website"], value["image"], value["metadata"], value["uid"])
    else
      @author = Mbrao::Author.new(value.ensure_string)
    end
  end

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value)
    @updated_at = @created_at if !@updated_at
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata || {}
  end

  # Sets the `metadata` attribute.
  #
  # @param value [Hash] The new value for the attribute.
  def metadata=(value)
    if value.is_a?(Hash) then
      @metadata = value.ensure_hash(:indifferent)
    else
      @metadata = HashWithIndifferentAccess.new({raw: value})
    end
  end

  # Validates locales for attribute retrieval.
  #
  # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
  # @param content [Content|nil] An optional content to check for availability
  # @return [Array] The validated list of locales.
  def self.validate_locales(locales, content = nil)
    locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
    locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
    raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
    locales
  end

  # Creates a content with metadata and body.
  #
  # @param metadata [Hash] The metadata.
  # @param body [String] The body of the content.
  # @return [Content] A new content.
  def self.create(, body)
    rv = Mbrao::Content.new
    rv.body = body.ensure_string.strip
    (rv, ) if .is_a?(Hash)
    rv
  end

  private
    # Assigns metadata to a content
    #
    # @param content [Content] The content to manipulate.
    # @param metadata [Hash] The metadata to assign.
    # @return [Content] The content with metadata.
    def self.(content, )
       = .symbolize_keys!

      content.uid = .delete(:uid)
      content.title = .delete(:title)
      content.author = Mbrao::Author.create(.delete(:author))
      content.tags = .delete(:tags)
      content.more = .delete(:more)
      content.created_at = .delete(:created_at)
      content.updated_at = .delete(:updated_at)
      content.locales = extract_locales()
      content. = 

      content
    end

    # Extract locales from metadata.
    #
    # @param metadata [Hash] The metadata that contains the locales.
    # @return [Array] The locales.
    def self.extract_locales()
      locales = .delete(:locales)
      locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
      normalize_locales(locales)
    end

    # Normalize locales for further usage.
    #
    # @param locales [Array] The locales to normalize.
    # @return [Array] The normalized locales.
    def self.normalize_locales(locales)
      locales.flatten.collect(&:ensure_string).collect(&:strip).uniq
    end

    # Extracts a date and time from a value.
    #
    # @param value [String|DateTime|Fixnum] The value to parse.
    # @return [DateTime] The extract values.
    def extract_datetime(value)
      begin
        if value.is_a?(Date) || value.is_a?(Time) then
          value = value.to_datetime
        elsif value.to_float > 0 then
          value = Time.at(value.to_float).to_datetime
        elsif value && !value.is_a?(DateTime) then
          value = DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z")
        end

        value ? value.utc : nil
      rescue ArgumentError
        raise Mbrao::Exceptions::InvalidDate.new
      end
    end
end

- (Object) metadata

Gets metadata attribute.

Returns:

  • The metadata attribute.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mbrao/content.rb', line 107

class Content
  include Mbrao::ContentPublicInterface

  attr_accessor :uid
  attr_accessor :locales
  attr_accessor :title
  attr_accessor :body
  attr_accessor :tags
  attr_accessor :more
  attr_accessor :author
  attr_accessor :created_at
  attr_accessor :updated_at
  attr_accessor :metadata

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  end

  # Sets the `title` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def title=(value)
    @title = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `body` attribute.
  #
  # @param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using #get_body.
  def body=(value)
    @body = value.ensure_string
  end

  # Sets the `tags` attribute.
  #
  # @param value [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def tags=(value)
    @tags = if value.is_a?(Hash) then
      value.ensure_hash(:indifferent) { |v| v.ensure_array(nil, true, true, true, :ensure_string) }
    else
      value.ensure_array(nil, true, true, true, :ensure_string)
    end
  end

  # Sets the `more` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def more=(value)
    @more = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `author` attribute.
  #
  # @param value [Author|Hash|Object] The new value for the attribute.
  def author=(value)
    if value.is_a?(Mbrao::Author) then
      @author = value
    elsif value.is_a?(Hash) then
      value = value.ensure_hash(:indifferent)
      @author = Mbrao::Author.new(value["name"], value["email"], value["website"], value["image"], value["metadata"], value["uid"])
    else
      @author = Mbrao::Author.new(value.ensure_string)
    end
  end

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value)
    @updated_at = @created_at if !@updated_at
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata || {}
  end

  # Sets the `metadata` attribute.
  #
  # @param value [Hash] The new value for the attribute.
  def metadata=(value)
    if value.is_a?(Hash) then
      @metadata = value.ensure_hash(:indifferent)
    else
      @metadata = HashWithIndifferentAccess.new({raw: value})
    end
  end

  # Validates locales for attribute retrieval.
  #
  # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
  # @param content [Content|nil] An optional content to check for availability
  # @return [Array] The validated list of locales.
  def self.validate_locales(locales, content = nil)
    locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
    locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
    raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
    locales
  end

  # Creates a content with metadata and body.
  #
  # @param metadata [Hash] The metadata.
  # @param body [String] The body of the content.
  # @return [Content] A new content.
  def self.create(, body)
    rv = Mbrao::Content.new
    rv.body = body.ensure_string.strip
    (rv, ) if .is_a?(Hash)
    rv
  end

  private
    # Assigns metadata to a content
    #
    # @param content [Content] The content to manipulate.
    # @param metadata [Hash] The metadata to assign.
    # @return [Content] The content with metadata.
    def self.(content, )
       = .symbolize_keys!

      content.uid = .delete(:uid)
      content.title = .delete(:title)
      content.author = Mbrao::Author.create(.delete(:author))
      content.tags = .delete(:tags)
      content.more = .delete(:more)
      content.created_at = .delete(:created_at)
      content.updated_at = .delete(:updated_at)
      content.locales = extract_locales()
      content. = 

      content
    end

    # Extract locales from metadata.
    #
    # @param metadata [Hash] The metadata that contains the locales.
    # @return [Array] The locales.
    def self.extract_locales()
      locales = .delete(:locales)
      locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
      normalize_locales(locales)
    end

    # Normalize locales for further usage.
    #
    # @param locales [Array] The locales to normalize.
    # @return [Array] The normalized locales.
    def self.normalize_locales(locales)
      locales.flatten.collect(&:ensure_string).collect(&:strip).uniq
    end

    # Extracts a date and time from a value.
    #
    # @param value [String|DateTime|Fixnum] The value to parse.
    # @return [DateTime] The extract values.
    def extract_datetime(value)
      begin
        if value.is_a?(Date) || value.is_a?(Time) then
          value = value.to_datetime
        elsif value.to_float > 0 then
          value = Time.at(value.to_float).to_datetime
        elsif value && !value.is_a?(DateTime) then
          value = DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z")
        end

        value ? value.utc : nil
      rescue ArgumentError
        raise Mbrao::Exceptions::InvalidDate.new
      end
    end
end

- (Object) more

Returns the value of attribute more



115
116
117
# File 'lib/mbrao/content.rb', line 115

def more
  @more
end

- (Array|HashWithIndifferentAccess) tags

Returns The tags associated with the content. Can be an Array or an HashWithIndifferentAccess, if multiple tags set are specified for multiple locales.

Returns:

  • (Array|HashWithIndifferentAccess)

    The tags associated with the content. Can be an Array or an HashWithIndifferentAccess, if multiple tags set are specified for multiple locales.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mbrao/content.rb', line 107

class Content
  include Mbrao::ContentPublicInterface

  attr_accessor :uid
  attr_accessor :locales
  attr_accessor :title
  attr_accessor :body
  attr_accessor :tags
  attr_accessor :more
  attr_accessor :author
  attr_accessor :created_at
  attr_accessor :updated_at
  attr_accessor :metadata

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  end

  # Sets the `title` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def title=(value)
    @title = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `body` attribute.
  #
  # @param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using #get_body.
  def body=(value)
    @body = value.ensure_string
  end

  # Sets the `tags` attribute.
  #
  # @param value [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def tags=(value)
    @tags = if value.is_a?(Hash) then
      value.ensure_hash(:indifferent) { |v| v.ensure_array(nil, true, true, true, :ensure_string) }
    else
      value.ensure_array(nil, true, true, true, :ensure_string)
    end
  end

  # Sets the `more` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def more=(value)
    @more = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `author` attribute.
  #
  # @param value [Author|Hash|Object] The new value for the attribute.
  def author=(value)
    if value.is_a?(Mbrao::Author) then
      @author = value
    elsif value.is_a?(Hash) then
      value = value.ensure_hash(:indifferent)
      @author = Mbrao::Author.new(value["name"], value["email"], value["website"], value["image"], value["metadata"], value["uid"])
    else
      @author = Mbrao::Author.new(value.ensure_string)
    end
  end

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value)
    @updated_at = @created_at if !@updated_at
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata || {}
  end

  # Sets the `metadata` attribute.
  #
  # @param value [Hash] The new value for the attribute.
  def metadata=(value)
    if value.is_a?(Hash) then
      @metadata = value.ensure_hash(:indifferent)
    else
      @metadata = HashWithIndifferentAccess.new({raw: value})
    end
  end

  # Validates locales for attribute retrieval.
  #
  # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
  # @param content [Content|nil] An optional content to check for availability
  # @return [Array] The validated list of locales.
  def self.validate_locales(locales, content = nil)
    locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
    locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
    raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
    locales
  end

  # Creates a content with metadata and body.
  #
  # @param metadata [Hash] The metadata.
  # @param body [String] The body of the content.
  # @return [Content] A new content.
  def self.create(, body)
    rv = Mbrao::Content.new
    rv.body = body.ensure_string.strip
    (rv, ) if .is_a?(Hash)
    rv
  end

  private
    # Assigns metadata to a content
    #
    # @param content [Content] The content to manipulate.
    # @param metadata [Hash] The metadata to assign.
    # @return [Content] The content with metadata.
    def self.(content, )
       = .symbolize_keys!

      content.uid = .delete(:uid)
      content.title = .delete(:title)
      content.author = Mbrao::Author.create(.delete(:author))
      content.tags = .delete(:tags)
      content.more = .delete(:more)
      content.created_at = .delete(:created_at)
      content.updated_at = .delete(:updated_at)
      content.locales = extract_locales()
      content. = 

      content
    end

    # Extract locales from metadata.
    #
    # @param metadata [Hash] The metadata that contains the locales.
    # @return [Array] The locales.
    def self.extract_locales()
      locales = .delete(:locales)
      locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
      normalize_locales(locales)
    end

    # Normalize locales for further usage.
    #
    # @param locales [Array] The locales to normalize.
    # @return [Array] The normalized locales.
    def self.normalize_locales(locales)
      locales.flatten.collect(&:ensure_string).collect(&:strip).uniq
    end

    # Extracts a date and time from a value.
    #
    # @param value [String|DateTime|Fixnum] The value to parse.
    # @return [DateTime] The extract values.
    def extract_datetime(value)
      begin
        if value.is_a?(Date) || value.is_a?(Time) then
          value = value.to_datetime
        elsif value.to_float > 0 then
          value = Time.at(value.to_float).to_datetime
        elsif value && !value.is_a?(DateTime) then
          value = DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z")
        end

        value ? value.utc : nil
      rescue ArgumentError
        raise Mbrao::Exceptions::InvalidDate.new
      end
    end
end

- (String|HashWithIndifferentAccess) title

Returns The content’s title. Can be a String or an HashWithIndifferentAccess, if multiple title are specified for multiple locales.

Returns:

  • (String|HashWithIndifferentAccess)

    The content’s title. Can be a String or an HashWithIndifferentAccess, if multiple title are specified for multiple locales.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mbrao/content.rb', line 107

class Content
  include Mbrao::ContentPublicInterface

  attr_accessor :uid
  attr_accessor :locales
  attr_accessor :title
  attr_accessor :body
  attr_accessor :tags
  attr_accessor :more
  attr_accessor :author
  attr_accessor :created_at
  attr_accessor :updated_at
  attr_accessor :metadata

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  end

  # Sets the `title` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def title=(value)
    @title = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `body` attribute.
  #
  # @param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using #get_body.
  def body=(value)
    @body = value.ensure_string
  end

  # Sets the `tags` attribute.
  #
  # @param value [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def tags=(value)
    @tags = if value.is_a?(Hash) then
      value.ensure_hash(:indifferent) { |v| v.ensure_array(nil, true, true, true, :ensure_string) }
    else
      value.ensure_array(nil, true, true, true, :ensure_string)
    end
  end

  # Sets the `more` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def more=(value)
    @more = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `author` attribute.
  #
  # @param value [Author|Hash|Object] The new value for the attribute.
  def author=(value)
    if value.is_a?(Mbrao::Author) then
      @author = value
    elsif value.is_a?(Hash) then
      value = value.ensure_hash(:indifferent)
      @author = Mbrao::Author.new(value["name"], value["email"], value["website"], value["image"], value["metadata"], value["uid"])
    else
      @author = Mbrao::Author.new(value.ensure_string)
    end
  end

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value)
    @updated_at = @created_at if !@updated_at
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata || {}
  end

  # Sets the `metadata` attribute.
  #
  # @param value [Hash] The new value for the attribute.
  def metadata=(value)
    if value.is_a?(Hash) then
      @metadata = value.ensure_hash(:indifferent)
    else
      @metadata = HashWithIndifferentAccess.new({raw: value})
    end
  end

  # Validates locales for attribute retrieval.
  #
  # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
  # @param content [Content|nil] An optional content to check for availability
  # @return [Array] The validated list of locales.
  def self.validate_locales(locales, content = nil)
    locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
    locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
    raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
    locales
  end

  # Creates a content with metadata and body.
  #
  # @param metadata [Hash] The metadata.
  # @param body [String] The body of the content.
  # @return [Content] A new content.
  def self.create(, body)
    rv = Mbrao::Content.new
    rv.body = body.ensure_string.strip
    (rv, ) if .is_a?(Hash)
    rv
  end

  private
    # Assigns metadata to a content
    #
    # @param content [Content] The content to manipulate.
    # @param metadata [Hash] The metadata to assign.
    # @return [Content] The content with metadata.
    def self.(content, )
       = .symbolize_keys!

      content.uid = .delete(:uid)
      content.title = .delete(:title)
      content.author = Mbrao::Author.create(.delete(:author))
      content.tags = .delete(:tags)
      content.more = .delete(:more)
      content.created_at = .delete(:created_at)
      content.updated_at = .delete(:updated_at)
      content.locales = extract_locales()
      content. = 

      content
    end

    # Extract locales from metadata.
    #
    # @param metadata [Hash] The metadata that contains the locales.
    # @return [Array] The locales.
    def self.extract_locales()
      locales = .delete(:locales)
      locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
      normalize_locales(locales)
    end

    # Normalize locales for further usage.
    #
    # @param locales [Array] The locales to normalize.
    # @return [Array] The normalized locales.
    def self.normalize_locales(locales)
      locales.flatten.collect(&:ensure_string).collect(&:strip).uniq
    end

    # Extracts a date and time from a value.
    #
    # @param value [String|DateTime|Fixnum] The value to parse.
    # @return [DateTime] The extract values.
    def extract_datetime(value)
      begin
        if value.is_a?(Date) || value.is_a?(Time) then
          value = value.to_datetime
        elsif value.to_float > 0 then
          value = Time.at(value.to_float).to_datetime
        elsif value && !value.is_a?(DateTime) then
          value = DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z")
        end

        value ? value.utc : nil
      rescue ArgumentError
        raise Mbrao::Exceptions::InvalidDate.new
      end
    end
end

- (String) uid

Returns A unique ID for this post. This is only for client uses.

Returns:

  • (String)

    A unique ID for this post. This is only for client uses.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mbrao/content.rb', line 107

class Content
  include Mbrao::ContentPublicInterface

  attr_accessor :uid
  attr_accessor :locales
  attr_accessor :title
  attr_accessor :body
  attr_accessor :tags
  attr_accessor :more
  attr_accessor :author
  attr_accessor :created_at
  attr_accessor :updated_at
  attr_accessor :metadata

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  end

  # Sets the `title` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def title=(value)
    @title = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `body` attribute.
  #
  # @param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using #get_body.
  def body=(value)
    @body = value.ensure_string
  end

  # Sets the `tags` attribute.
  #
  # @param value [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def tags=(value)
    @tags = if value.is_a?(Hash) then
      value.ensure_hash(:indifferent) { |v| v.ensure_array(nil, true, true, true, :ensure_string) }
    else
      value.ensure_array(nil, true, true, true, :ensure_string)
    end
  end

  # Sets the `more` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def more=(value)
    @more = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `author` attribute.
  #
  # @param value [Author|Hash|Object] The new value for the attribute.
  def author=(value)
    if value.is_a?(Mbrao::Author) then
      @author = value
    elsif value.is_a?(Hash) then
      value = value.ensure_hash(:indifferent)
      @author = Mbrao::Author.new(value["name"], value["email"], value["website"], value["image"], value["metadata"], value["uid"])
    else
      @author = Mbrao::Author.new(value.ensure_string)
    end
  end

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value)
    @updated_at = @created_at if !@updated_at
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata || {}
  end

  # Sets the `metadata` attribute.
  #
  # @param value [Hash] The new value for the attribute.
  def metadata=(value)
    if value.is_a?(Hash) then
      @metadata = value.ensure_hash(:indifferent)
    else
      @metadata = HashWithIndifferentAccess.new({raw: value})
    end
  end

  # Validates locales for attribute retrieval.
  #
  # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
  # @param content [Content|nil] An optional content to check for availability
  # @return [Array] The validated list of locales.
  def self.validate_locales(locales, content = nil)
    locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
    locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
    raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
    locales
  end

  # Creates a content with metadata and body.
  #
  # @param metadata [Hash] The metadata.
  # @param body [String] The body of the content.
  # @return [Content] A new content.
  def self.create(, body)
    rv = Mbrao::Content.new
    rv.body = body.ensure_string.strip
    (rv, ) if .is_a?(Hash)
    rv
  end

  private
    # Assigns metadata to a content
    #
    # @param content [Content] The content to manipulate.
    # @param metadata [Hash] The metadata to assign.
    # @return [Content] The content with metadata.
    def self.(content, )
       = .symbolize_keys!

      content.uid = .delete(:uid)
      content.title = .delete(:title)
      content.author = Mbrao::Author.create(.delete(:author))
      content.tags = .delete(:tags)
      content.more = .delete(:more)
      content.created_at = .delete(:created_at)
      content.updated_at = .delete(:updated_at)
      content.locales = extract_locales()
      content. = 

      content
    end

    # Extract locales from metadata.
    #
    # @param metadata [Hash] The metadata that contains the locales.
    # @return [Array] The locales.
    def self.extract_locales()
      locales = .delete(:locales)
      locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
      normalize_locales(locales)
    end

    # Normalize locales for further usage.
    #
    # @param locales [Array] The locales to normalize.
    # @return [Array] The normalized locales.
    def self.normalize_locales(locales)
      locales.flatten.collect(&:ensure_string).collect(&:strip).uniq
    end

    # Extracts a date and time from a value.
    #
    # @param value [String|DateTime|Fixnum] The value to parse.
    # @return [DateTime] The extract values.
    def extract_datetime(value)
      begin
        if value.is_a?(Date) || value.is_a?(Time) then
          value = value.to_datetime
        elsif value.to_float > 0 then
          value = Time.at(value.to_float).to_datetime
        elsif value && !value.is_a?(DateTime) then
          value = DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z")
        end

        value ? value.utc : nil
      rescue ArgumentError
        raise Mbrao::Exceptions::InvalidDate.new
      end
    end
end

- (DateTime) updated_at

Returns The post creation date and time. Defaults to the creation date. The timezone is always UTC.

Returns:

  • (DateTime)

    The post creation date and time. Defaults to the creation date. The timezone is always UTC.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/mbrao/content.rb', line 107

class Content
  include Mbrao::ContentPublicInterface

  attr_accessor :uid
  attr_accessor :locales
  attr_accessor :title
  attr_accessor :body
  attr_accessor :tags
  attr_accessor :more
  attr_accessor :author
  attr_accessor :created_at
  attr_accessor :updated_at
  attr_accessor :metadata

  # Creates a new content.
  #
  # @param uid [String] The UID for this content.
  def initialize(uid = nil)
    @uid = uid
  end

  # Sets the `locales` attribute.
  #
  # @param value [Array] The new value for the attribute. A empty or "*" will be the default value.
  def locales=(value)
    @locales = value.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  end

  # Sets the `title` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def title=(value)
    @title = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `body` attribute.
  #
  # @param value [String] The new value for the attribute. Can contain locales restriction (like !en), which will must be interpreted using #get_body.
  def body=(value)
    @body = value.ensure_string
  end

  # Sets the `tags` attribute.
  #
  # @param value [Array|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def tags=(value)
    @tags = if value.is_a?(Hash) then
      value.ensure_hash(:indifferent) { |v| v.ensure_array(nil, true, true, true, :ensure_string) }
    else
      value.ensure_array(nil, true, true, true, :ensure_string)
    end
  end

  # Sets the `more` attribute.
  #
  # @param value [String|Hash] The new value for the attribute. If an Hash, keys must be a string with one or locale separated by commas. A empty or "*" will be the default value.
  def more=(value)
    @more = value.is_a?(Hash) ? value.ensure_hash(:indifferent, nil, :ensure_string) : value.ensure_string
  end

  # Sets the `author` attribute.
  #
  # @param value [Author|Hash|Object] The new value for the attribute.
  def author=(value)
    if value.is_a?(Mbrao::Author) then
      @author = value
    elsif value.is_a?(Hash) then
      value = value.ensure_hash(:indifferent)
      @author = Mbrao::Author.new(value["name"], value["email"], value["website"], value["image"], value["metadata"], value["uid"])
    else
      @author = Mbrao::Author.new(value.ensure_string)
    end
  end

  # Sets the `created_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def created_at=(value)
    @created_at = extract_datetime(value)
  end

  # Sets the `updated_at` attribute.
  #
  # @param value [String|DateTime|Fixnum] The new value for the attribute.
  def updated_at=(value)
    @updated_at = extract_datetime(value)
    @updated_at = @created_at if !@updated_at
  end

  # Gets metadata attribute.
  #
  # @return The metadata attribute.
  def 
    @metadata || {}
  end

  # Sets the `metadata` attribute.
  #
  # @param value [Hash] The new value for the attribute.
  def metadata=(value)
    if value.is_a?(Hash) then
      @metadata = value.ensure_hash(:indifferent)
    else
      @metadata = HashWithIndifferentAccess.new({raw: value})
    end
  end

  # Validates locales for attribute retrieval.
  #
  # @param locales [Array] A list of desired locales for an attribute. Can include `*` to match all. If none are specified, the default mbrao locale will be used.
  # @param content [Content|nil] An optional content to check for availability
  # @return [Array] The validated list of locales.
  def self.validate_locales(locales, content = nil)
    locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
    locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
    raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
    locales
  end

  # Creates a content with metadata and body.
  #
  # @param metadata [Hash] The metadata.
  # @param body [String] The body of the content.
  # @return [Content] A new content.
  def self.create(, body)
    rv = Mbrao::Content.new
    rv.body = body.ensure_string.strip
    (rv, ) if .is_a?(Hash)
    rv
  end

  private
    # Assigns metadata to a content
    #
    # @param content [Content] The content to manipulate.
    # @param metadata [Hash] The metadata to assign.
    # @return [Content] The content with metadata.
    def self.(content, )
       = .symbolize_keys!

      content.uid = .delete(:uid)
      content.title = .delete(:title)
      content.author = Mbrao::Author.create(.delete(:author))
      content.tags = .delete(:tags)
      content.more = .delete(:more)
      content.created_at = .delete(:created_at)
      content.updated_at = .delete(:updated_at)
      content.locales = extract_locales()
      content. = 

      content
    end

    # Extract locales from metadata.
    #
    # @param metadata [Hash] The metadata that contains the locales.
    # @return [Array] The locales.
    def self.extract_locales()
      locales = .delete(:locales)
      locales = locales.ensure_string.split(/\s*,\s*/) if !locales.is_a?(::Array)
      normalize_locales(locales)
    end

    # Normalize locales for further usage.
    #
    # @param locales [Array] The locales to normalize.
    # @return [Array] The normalized locales.
    def self.normalize_locales(locales)
      locales.flatten.collect(&:ensure_string).collect(&:strip).uniq
    end

    # Extracts a date and time from a value.
    #
    # @param value [String|DateTime|Fixnum] The value to parse.
    # @return [DateTime] The extract values.
    def extract_datetime(value)
      begin
        if value.is_a?(Date) || value.is_a?(Time) then
          value = value.to_datetime
        elsif value.to_float > 0 then
          value = Time.at(value.to_float).to_datetime
        elsif value && !value.is_a?(DateTime) then
          value = DateTime.strptime(value.ensure_string, "%Y%m%dT%H%M%S%z")
        end

        value ? value.utc : nil
      rescue ArgumentError
        raise Mbrao::Exceptions::InvalidDate.new
      end
    end
end

Class Method Details

+ (Content) create(metadata, body)

Creates a content with metadata and body.

Parameters:

  • metadata (Hash)

    The metadata.

  • body (String)

    The body of the content.

Returns:



231
232
233
234
235
236
# File 'lib/mbrao/content.rb', line 231

def self.create(, body)
  rv = Mbrao::Content.new
  rv.body = body.ensure_string.strip
  (rv, ) if .is_a?(Hash)
  rv
end

+ (Array) validate_locales(locales, content = nil)

Validates locales for attribute retrieval.

Parameters:

  • locales (Array)

    A list of desired locales for an attribute. Can include * to match all. If none are specified, the default mbrao locale will be used.

  • content (Content|nil) (defaults to: nil)

    An optional content to check for availability

Returns:

  • (Array)

    The validated list of locales.

Raises:



219
220
221
222
223
224
# File 'lib/mbrao/content.rb', line 219

def self.validate_locales(locales, content = nil)
  locales = locales.ensure_array(nil, true, true, true) {|l| l.ensure_string.strip }
  locales = (locales.empty? ? [Mbrao::Parser.locale] : locales)
  raise Mbrao::Exceptions::UnavailableLocalization.new if content && !content.enabled_for_locales?(locales)
  locales
end