Class: Doze::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/doze/entity.rb

Overview

A simple wrapper class for an entity, which is essentially a lump of binary data together with some metadata about it, most importantly a MediaType, but also potentially a character encoding and a content language.

A key feature is that the binary data may be specified lazily via a block. This is so that content negotiation can demand the data only once it’s decided which (if any) of many proferred entities it wants to respond with.

TODO: handle character encodings here in a nicer 1.9-compatible way TODO: maybe allow a stream for lazy_binary_data too

Direct Known Subclasses

HTML, JSON, YAML, Serialization::Entity

Defined Under Namespace

Classes: HTML, JSON, YAML

Constant Summary collapse

DEFAULT_TEXT_ENCODING =
'iso-8859-1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media_type, options = {}, &lazy_binary_data) ⇒ Entity

Returns a new instance of Entity.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/doze/entity.rb', line 26

def initialize(media_type, options={}, &lazy_binary_data)
  @binary_data = options[:binary_data]
  @binary_data_stream = options[:binary_data_stream]
  @binary_data_length = options[:binary_data_length]
  @lazy_binary_data = options[:lazy_binary_data] || lazy_binary_data

  @media_type = media_type
  @media_type_params = options[:media_type_params] || {}
  @encoding   = options[:encoding] || (DEFAULT_TEXT_ENCODING if @media_type.major == 'text')
  @language   = options[:language]
  @extra_content_headers = options[:extra_content_headers] || {}
end

Instance Attribute Details

#binary_data_lengthObject (readonly)

Returns the value of attribute binary_data_length.



16
17
18
# File 'lib/doze/entity.rb', line 16

def binary_data_length
  @binary_data_length
end

#encodingObject (readonly)

Returns the value of attribute encoding.



16
17
18
# File 'lib/doze/entity.rb', line 16

def encoding
  @encoding
end

#extra_content_headersObject (readonly)

Used when constructing a HTTP response from this entity For when you need to specify extra entity-content-specific HTTP headers to be included when responding with this entity. A teensy bit of an abstraction leak, but helpful for awkward cases.



22
23
24
# File 'lib/doze/entity.rb', line 22

def extra_content_headers
  @extra_content_headers
end

#languageObject (readonly)

Returns the value of attribute language.



16
17
18
# File 'lib/doze/entity.rb', line 16

def language
  @language
end

#media_typeObject (readonly)

Returns the value of attribute media_type.



16
17
18
# File 'lib/doze/entity.rb', line 16

def media_type
  @media_type
end

#media_type_paramsObject (readonly)

Returns the value of attribute media_type_params.



16
17
18
# File 'lib/doze/entity.rb', line 16

def media_type_params
  @media_type_params
end

Instance Method Details

#binary_dataObject



45
46
47
48
49
50
51
52
# File 'lib/doze/entity.rb', line 45

def binary_data
  @binary_data ||= if @lazy_binary_data
    @lazy_binary_data.call
  elsif @binary_data_stream
    @binary_data_stream.rewind if @binary_data_stream.respond_to?(:rewind)
    @binary_data_stream.read
  end
end

#binary_data_streamObject



39
40
41
42
43
# File 'lib/doze/entity.rb', line 39

def binary_data_stream
  @binary_data_stream ||= if @binary_data
    StringIO.new(@binary_data)
  end
end

#etagObject

This is a ‘strong’ etag in that it’s sensitive to the exact bytes of the entity. Note that etags are per-entity, not per-resource. (even weak etags, which we don’t yet support; ‘weak’ appears to refer to time-based equivalence for the same entity, rather than equivalence of all entity representations of a resource.)

May return nil. Default implementation is an MD5 digest of the entity data.


59
60
61
# File 'lib/doze/entity.rb', line 59

def etag
  Digest::MD5.hexdigest(binary_data)
end