Class: RCAP::CAP_1_2::Resource

Inherits:
Base::Resource show all
Defined in:
lib/rcap/cap_1_2/resource.rb

Overview

A Resource object is valid if

  • it has a resource description

  • it has a mime_type

Constant Summary

DEREF_URI_ELEMENT_NAME =
'derefUri'
DEREF_URI_XPATH =
"cap:#{ DEREF_URI_ELEMENT_NAME }"
DEREF_URI_YAML =
"Derefrenced URI Data"
DEREF_URI_KEY =
'deref_uri'

Constants inherited from Base::Resource

Base::Resource::DIGEST_ELEMENT_NAME, Base::Resource::DIGEST_KEY, Base::Resource::DIGEST_XPATH, Base::Resource::DIGEST_YAML, Base::Resource::MIME_TYPE_ELEMENT_NAME, Base::Resource::MIME_TYPE_KEY, Base::Resource::MIME_TYPE_XPATH, Base::Resource::MIME_TYPE_YAML, Base::Resource::RESOURCE_DESC_ELEMENT_NAME, Base::Resource::RESOURCE_DESC_KEY, Base::Resource::RESOURCE_DESC_XPATH, Base::Resource::RESOURCE_DESC_YAML, Base::Resource::SIZE_ELEMENT_NAME, Base::Resource::SIZE_KEY, Base::Resource::SIZE_XPATH, Base::Resource::SIZE_YAML, Base::Resource::URI_ELEMENT_NAME, Base::Resource::URI_KEY, Base::Resource::URI_XPATH, Base::Resource::URI_YAML, Base::Resource::XML_ELEMENT_NAME, Base::Resource::XPATH

Instance Attribute Summary (collapse)

Attributes inherited from Base::Resource

#digest, #mime_type, #resource_desc, #size, #uri

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Base::Resource

#calculate_hash_and_size, #decoded_deref_uri, #initialize, #inspect, #size_in_kb, #to_s, #to_xml

Constructor Details

This class inherits a constructor from RCAP::Base::Resource

Instance Attribute Details

- (String) deref_uri

Dereferenced URI - contents of URI Base64 encoded

Returns:

  • (String)

    Dereferenced URI - contents of URI Base64 encoded



10
11
12
# File 'lib/rcap/cap_1_2/resource.rb', line 10

def deref_uri
  @deref_uri
end

Class Method Details

+ (Resource) from_h(resource_hash)

Parameters:

  • resource_hash (Hash)

Returns:



96
97
98
99
100
# File 'lib/rcap/cap_1_2/resource.rb', line 96

def self.from_h( resource_hash )
  super.tap do |resource|
    resource.deref_uri = resource_hash[ DEREF_URI_KEY ].strip if resource_hash[ DEREF_URI_KEY ]
  end
end

+ (Resource) from_xml_element(resource_xml_element)

Parameters:

  • resource_xml_element (REXML::Element)

Returns:



54
55
56
57
58
# File 'lib/rcap/cap_1_2/resource.rb', line 54

def self.from_xml_element( resource_xml_element )
  super.tap do |resource|
    resource.deref_uri = RCAP.xpath_text( resource_xml_element, DEREF_URI_XPATH, resource.xmlns )
  end
end

+ (Resource) from_yaml_data(resource_yaml_data)

Parameters:

  • resource_yaml_data (Hash)

Returns:



76
77
78
79
80
# File 'lib/rcap/cap_1_2/resource.rb', line 76

def self.from_yaml_data( resource_yaml_data )
  super.tap do |resource|
    resource.deref_uri = resource_yaml_data[ DEREF_URI_YAML ].strip if resource_yaml_data[ DEREF_URI_YAML ]
  end
end

Instance Method Details

- (Array(Integer,String)) dereference_uri!

Retrieves the content at uri and stores it in deref_uri as Base64 encoded text. It will also calculate the Base::Resource#digest on the encoded data using SHA1 and set the Base::Resource#size.

This uses the open-uri Ruby API to open and read the content. This method may throw an exception due to any number of network related issue so please handle accordingly.

Returns an array containing the size (in bytes) and SHA-1 hash.

Returns:



41
42
43
44
45
# File 'lib/rcap/cap_1_2/resource.rb', line 41

def dereference_uri!
  content = URI.parse( self.uri ).read
  @deref_uri = Base64.encode64( content )
  self.calculate_hash_and_size
end

- (Hash) to_h

Returns:

  • (Hash)


85
86
87
88
89
90
91
92
# File 'lib/rcap/cap_1_2/resource.rb', line 85

def to_h
  RCAP.attribute_values_to_hash( [ RESOURCE_DESC_KEY, @resource_desc ],
                                 [ URI_KEY,           @uri],
                                 [ MIME_TYPE_KEY,     @mime_type],
                                 [ DEREF_URI_KEY,     @deref_uri],
                                 [ SIZE_KEY,          @size ],
                                 [ DIGEST_KEY,        @digest ])
end

- (REXML::Element) to_xml_element

Returns:

  • (REXML::Element)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rcap/cap_1_2/resource.rb', line 19

def to_xml_element
  xml_element = super
  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( DEREF_URI_ELEMENT_NAME ).add_text( @deref_uri ) if @deref_uri
  xml_element.add_element( DIGEST_ELEMENT_NAME ).add_text( @digest )       if @digest
  xml_element
end

- (String) to_yaml(options = {})

Parameters:

  • options (Hash) (defaults to: {})

Returns:



64
65
66
67
68
69
70
71
72
# File 'lib/rcap/cap_1_2/resource.rb', line 64

def to_yaml( options = {} )
  RCAP.attribute_values_to_hash(
    [ RESOURCE_DESC_YAML, @resource_desc ],
    [ URI_YAML,           @uri ],
    [ MIME_TYPE_YAML,     @mime_type ],
    [ DEREF_URI_YAML,     @deref_uri ],
    [ SIZE_YAML,          @size ],
    [ DIGEST_YAML,        @digest ]).to_yaml( options )
end

- (String) xmlns

Returns:



48
49
50
# File 'lib/rcap/cap_1_2/resource.rb', line 48

def xmlns
  Alert::XMLNS
end