Class: RCAP::Base::Resource
- Inherits:
-
Object
- Object
- RCAP::Base::Resource
- Includes:
- Validation
- Defined in:
- lib/rcap/base/resource.rb
Direct Known Subclasses
Constant Summary
- XML_ELEMENT_NAME =
'resource'
- MIME_TYPE_ELEMENT_NAME =
'mimeType'
- SIZE_ELEMENT_NAME =
'size'
- URI_ELEMENT_NAME =
'uri'
- DIGEST_ELEMENT_NAME =
'digest'
- RESOURCE_DESC_ELEMENT_NAME =
'resourceDesc'
- XPATH =
"cap:#{ XML_ELEMENT_NAME }"
- MIME_TYPE_XPATH =
"cap:#{ MIME_TYPE_ELEMENT_NAME }"
- SIZE_XPATH =
"cap:#{ SIZE_ELEMENT_NAME }"
- URI_XPATH =
"cap:#{ URI_ELEMENT_NAME }"
- DIGEST_XPATH =
"cap:#{ DIGEST_ELEMENT_NAME }"
- RESOURCE_DESC_XPATH =
"cap:#{ RESOURCE_DESC_ELEMENT_NAME }"
- RESOURCE_DESC_YAML =
"Resource Description"
- URI_YAML =
"URI"
- MIME_TYPE_YAML =
"Mime Type"
- SIZE_YAML =
"Size"
- DIGEST_YAML =
"Digest"
- RESOURCE_DESC_KEY =
'resource_desc'
- URI_KEY =
'uri'
- MIME_TYPE_KEY =
'mime_type'
- SIZE_KEY =
'size'
- DIGEST_KEY =
'digest'
Instance Attribute Summary (collapse)
-
- (String) digest
SHA-1 hash of contents of resource.
- - (String) mime_type
-
- (String) resource_desc
Resource Description.
-
- (Integer) size
Expressed in bytes.
-
- (String) uri
Resource location.
Class Method Summary (collapse)
- + (Resource) from_h(resource_hash)
- + (Resource) from_xml_element(resource_xml_element)
- + (Resource) from_yaml_data(resource_yaml_data)
Instance Method Summary (collapse)
-
- (nil, Array(Integer,String)) calculate_hash_and_size
Calculates the SHA-1 hash and size of the contents of #deref_uri.
-
- (nil, String) decoded_deref_uri
The decoded contents of #deref_uri if present otherwise nil.
-
- (Resource) initialize {|_self| ... }
constructor
A new instance of Resource.
- - (String) inspect
-
- (Float) size_in_kb
If size is defined returns the size in kilobytes.
- - (Hash) to_h
-
- (String) to_s
Returns a string representation of the resource of the form.
- - (String) to_xml
- - (REXML::Element) to_xml_element
- - (String) to_yaml(options = {})
Constructor Details
- (Resource) initialize {|_self| ... }
A new instance of Resource
39 40 41 |
# File 'lib/rcap/base/resource.rb', line 39 def initialize yield( self ) if block_given? end |
Instance Attribute Details
- (String) digest
SHA-1 hash of contents of resource
15 16 17 |
# File 'lib/rcap/base/resource.rb', line 15 def digest @digest end |
- (String) mime_type
9 10 11 |
# File 'lib/rcap/base/resource.rb', line 9 def mime_type @mime_type end |
- (String) resource_desc
Resource Description
7 8 9 |
# File 'lib/rcap/base/resource.rb', line 7 def resource_desc @resource_desc end |
- (Integer) size
Expressed in bytes
11 12 13 |
# File 'lib/rcap/base/resource.rb', line 11 def size @size end |
- (String) uri
Resource location
13 14 15 |
# File 'lib/rcap/base/resource.rb', line 13 def uri @uri end |
Class Method Details
+ (Resource) from_h(resource_hash)
157 158 159 160 161 162 163 164 165 |
# File 'lib/rcap/base/resource.rb', line 157 def self.from_h( resource_hash ) self.new do |resource| resource.resource_desc = RCAP.strip_if_given( resource_hash[ RESOURCE_DESC_KEY ]) resource.uri = RCAP.strip_if_given( resource_hash[ URI_KEY ]) resource.mime_type = RCAP.strip_if_given( resource_hash[ MIME_TYPE_KEY ]) resource.size = RCAP.to_i_if_given( resource_hash[ SIZE_KEY ]) resource.digest = RCAP.strip_if_given( resource_hash[ DIGEST_KEY ]) end end |
+ (Resource) from_xml_element(resource_xml_element)
56 57 58 59 60 61 62 63 64 |
# File 'lib/rcap/base/resource.rb', line 56 def self.from_xml_element( resource_xml_element ) resource = self.new do |resource| resource.resource_desc = RCAP.xpath_text( resource_xml_element, RESOURCE_DESC_XPATH, resource.xmlns ) resource.uri = RCAP.xpath_text( resource_xml_element, URI_XPATH, resource.xmlns ) resource.mime_type = RCAP.xpath_text( resource_xml_element, MIME_TYPE_XPATH, resource.xmlns ) resource.size = RCAP.xpath_text( resource_xml_element, SIZE_XPATH, resource.xmlns ).to_i resource.digest = RCAP.xpath_text( resource_xml_element, DIGEST_XPATH, resource.xmlns ) end end |
+ (Resource) from_yaml_data(resource_yaml_data)
130 131 132 133 134 135 136 137 138 |
# File 'lib/rcap/base/resource.rb', line 130 def self.from_yaml_data( resource_yaml_data ) self.new do |resource| resource.resource_desc = resource_yaml_data[ RESOURCE_DESC_YAML ] resource.uri = resource_yaml_data[ URI_YAML ] resource.mime_type = resource_yaml_data[ MIME_TYPE_YAML ] resource.size = resource_yaml_data[ SIZE_YAML ] resource.digest = resource_yaml_data[ DIGEST_YAML ] end end |
Instance Method Details
- (nil, Array(Integer,String)) calculate_hash_and_size
Calculates the SHA-1 hash and size of the contents of #deref_uri. Returns an array containing the size (in bytes) and SHA-1 hash if #deref_uri is present otherwise returns nil.
71 72 73 74 75 76 77 |
# File 'lib/rcap/base/resource.rb', line 71 def calculate_hash_and_size if @deref_uri @digest = Digest::SHA1.hexdigest( @deref_uri ) @size = @deref_uri.bytesize [ @size, @digest ] end end |
- (nil, String) decoded_deref_uri
The decoded contents of #deref_uri if present otherwise nil.
82 83 84 |
# File 'lib/rcap/base/resource.rb', line 82 def decoded_deref_uri Base64.decode64( @deref_uri ) if @deref_uri end |
- (String) inspect
100 101 102 |
# File 'lib/rcap/base/resource.rb', line 100 def inspect [ @resource_desc, @uri, @mime_type, @size ? format( "%.1fKB", self.size_in_kb ) : nil ].compact.join(' - ') end |
- (Float) size_in_kb
If size is defined returns the size in kilobytes
88 89 90 91 92 |
# File 'lib/rcap/base/resource.rb', line 88 def size_in_kb if @size @size.to_f/1024 end end |
- (Hash) to_h
147 148 149 150 151 152 153 |
# File 'lib/rcap/base/resource.rb', line 147 def to_h RCAP.attribute_values_to_hash( [ RESOURCE_DESC_KEY, @resource_desc ], [ URI_KEY, @uri], [ MIME_TYPE_KEY, @mime_type], [ SIZE_KEY, @size ], [ DIGEST_KEY, @digest ]) end |
- (String) to_s
Returns a string representation of the resource of the form
resource_desc
108 109 110 |
# File 'lib/rcap/base/resource.rb', line 108 def to_s @resource_desc end |
- (String) to_xml
95 96 97 |
# File 'lib/rcap/base/resource.rb', line 95 def to_xml self.to_xml_element.to_s end |
- (REXML::Element) to_xml_element
44 45 46 47 48 49 50 51 52 |
# File 'lib/rcap/base/resource.rb', line 44 def to_xml_element xml_element = REXML::Element.new( XML_ELEMENT_NAME ) xml_element.add_element( RESOURCE_DESC_ELEMENT_NAME ).add_text( @resource_desc ) xml_element.add_element( MIME_TYPE_ELEMENT_NAME ).add_text( @mime_type ) if @mime_type xml_element.add_element( SIZE_ELEMENT_NAME ).add_text( @size.to_s ) if @size xml_element.add_element( URI_ELEMENT_NAME ).add_text( @uri ) if @uri xml_element.add_element( DIGEST_ELEMENT_NAME ).add_text( @digest ) if @digest xml_element end |
- (String) to_yaml(options = {})
120 121 122 123 124 125 126 |
# File 'lib/rcap/base/resource.rb', line 120 def to_yaml( = {} ) RCAP.attribute_values_to_hash( [ RESOURCE_DESC_YAML, @resource_desc ], [ URI_YAML, @uri ], [ MIME_TYPE_YAML, @mime_type ], [ SIZE_YAML, @size ], [ DIGEST_YAML, @digest ]).to_yaml( ) end |