Class: RCAP::Base::Resource
- Inherits:
-
Object
- Object
- RCAP::Base::Resource
- Includes:
- Validation
- Defined in:
- lib/rcap/base/resource.rb
Direct Known Subclasses
Constant Summary collapse
- 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
-
#digest ⇒ String
SHA-1 hash of contents of resource.
- #mime_type ⇒ String
-
#resource_desc ⇒ String
Resource Description.
-
#size ⇒ Integer
Expressed in bytes.
-
#uri ⇒ String
Resource location.
Class Method Summary collapse
- .from_h(resource_hash) ⇒ Resource
- .from_xml_element(resource_xml_element) ⇒ Resource
- .from_yaml_data(resource_yaml_data) ⇒ Resource
Instance Method Summary collapse
-
#calculate_hash_and_size ⇒ nil, Array(Integer,String)
Calculates the SHA-1 hash and size of the contents of #deref_uri.
-
#decoded_deref_uri ⇒ nil, String
The decoded contents of #deref_uri if present otherwise nil.
-
#initialize {|_self| ... } ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ String
-
#size_in_kb ⇒ Float
If size is defined returns the size in kilobytes.
- #to_h ⇒ Hash
-
#to_s ⇒ String
Returns a string representation of the resource of the form resource_desc.
- #to_xml ⇒ String
- #to_xml_element ⇒ REXML::Element
- #to_yaml(options = {}) ⇒ String
- #to_yaml_data ⇒ Object
Methods included from Validation
#errors, included, #valid?, #validate
Constructor Details
#initialize {|_self| ... } ⇒ Resource
Returns 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
#digest ⇒ String
Returns SHA-1 hash of contents of resource.
15 16 17 |
# File 'lib/rcap/base/resource.rb', line 15 def digest @digest end |
#resource_desc ⇒ String
Returns Resource Description.
7 8 9 |
# File 'lib/rcap/base/resource.rb', line 7 def resource_desc @resource_desc end |
#size ⇒ Integer
Returns Expressed in bytes.
11 12 13 |
# File 'lib/rcap/base/resource.rb', line 11 def size @size end |
#uri ⇒ String
Returns Resource location.
13 14 15 |
# File 'lib/rcap/base/resource.rb', line 13 def uri @uri end |
Class Method Details
.from_h(resource_hash) ⇒ Resource
161 162 163 164 165 166 167 168 169 |
# File 'lib/rcap/base/resource.rb', line 161 def self.from_h(resource_hash) 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 |
.from_xml_element(resource_xml_element) ⇒ Resource
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 = 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 |
.from_yaml_data(resource_yaml_data) ⇒ Resource
134 135 136 137 138 139 140 141 142 |
# File 'lib/rcap/base/resource.rb', line 134 def self.from_yaml_data(resource_yaml_data) 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
#calculate_hash_and_size ⇒ nil, Array(Integer,String)
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 |
#decoded_deref_uri ⇒ nil, String
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 |
#inspect ⇒ String
100 101 102 |
# File 'lib/rcap/base/resource.rb', line 100 def inspect [@resource_desc, @uri, @mime_type, @size ? format('%.1fKB', size_in_kb) : nil].compact.join(' - ') end |
#size_in_kb ⇒ Float
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 |
#to_h ⇒ Hash
151 152 153 154 155 156 157 |
# File 'lib/rcap/base/resource.rb', line 151 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 |
#to_s ⇒ String
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 |
#to_xml ⇒ String
95 96 97 |
# File 'lib/rcap/base/resource.rb', line 95 def to_xml to_xml_element.to_s end |
#to_xml_element ⇒ REXML::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 |
#to_yaml(options = {}) ⇒ String
128 129 130 |
# File 'lib/rcap/base/resource.rb', line 128 def to_yaml( = {}) to_yaml_data.to_yaml() end |
#to_yaml_data ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/rcap/base/resource.rb', line 118 def to_yaml_data RCAP.attribute_values_to_hash([RESOURCE_DESC_YAML, @resource_desc], [URI_YAML, @uri], [MIME_TYPE_YAML, @mime_type], [SIZE_YAML, @size], [DIGEST_YAML, @digest]) end |