Class: Doze::Entity
- Inherits:
-
Object
- Object
- Doze::Entity
- 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
Defined Under Namespace
Constant Summary collapse
- DEFAULT_TEXT_ENCODING =
'iso-8859-1'
Instance Attribute Summary collapse
-
#binary_data_length ⇒ Object
readonly
Returns the value of attribute binary_data_length.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#extra_content_headers ⇒ Object
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.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#media_type ⇒ Object
readonly
Returns the value of attribute media_type.
-
#media_type_params ⇒ Object
readonly
Returns the value of attribute media_type_params.
Instance Method Summary collapse
- #binary_data ⇒ Object
- #binary_data_stream ⇒ Object
-
#etag ⇒ Object
This is a ‘strong’ etag in that it’s sensitive to the exact bytes of the entity.
-
#initialize(media_type, options = {}, &lazy_binary_data) ⇒ Entity
constructor
A new instance of Entity.
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, ={}, &lazy_binary_data) @binary_data = [:binary_data] @binary_data_stream = [:binary_data_stream] @binary_data_length = [:binary_data_length] @lazy_binary_data = [:lazy_binary_data] || lazy_binary_data @media_type = media_type @media_type_params = [:media_type_params] || {} @encoding = [:encoding] || (DEFAULT_TEXT_ENCODING if @media_type.major == 'text') @language = [:language] @extra_content_headers = [:extra_content_headers] || {} end |
Instance Attribute Details
#binary_data_length ⇒ Object (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 |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
16 17 18 |
# File 'lib/doze/entity.rb', line 16 def encoding @encoding end |
#extra_content_headers ⇒ Object (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 |
#language ⇒ Object (readonly)
Returns the value of attribute language.
16 17 18 |
# File 'lib/doze/entity.rb', line 16 def language @language end |
#media_type ⇒ Object (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_params ⇒ Object (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_data ⇒ Object
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_stream ⇒ Object
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 |
#etag ⇒ Object
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 |