Class: RCAP::CAP_1_1::Resource

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

Overview

A Resource object is valid if

  • it has a resource description

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



9
10
11
# File 'lib/rcap/cap_1_1/resource.rb', line 9

def deref_uri
  @deref_uri
end

Class Method Details

+ (Resource) from_h(resource_hash)

Parameters:

  • resource_hash (Hash)

Returns:



91
92
93
94
95
# File 'lib/rcap/cap_1_1/resource.rb', line 91

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

+ (Resource) from_xml_element(resource_xml_element)

Parameters:

  • resource_xml_element (REXML::Element)

Returns:



50
51
52
53
54
# File 'lib/rcap/cap_1_1/resource.rb', line 50

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:



71
72
73
74
75
# File 'lib/rcap/cap_1_1/resource.rb', line 71

def self.from_yaml_data( resource_yaml_data )
  super.tap do |resource|
    resource.deref_uri = RCAP.strip_if_given( 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:



37
38
39
40
41
# File 'lib/rcap/cap_1_1/resource.rb', line 37

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)


80
81
82
83
84
85
86
87
# File 'lib/rcap/cap_1_1/resource.rb', line 80

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)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rcap/cap_1_1/resource.rb', line 15

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:



60
61
62
63
64
65
66
67
# File 'lib/rcap/cap_1_1/resource.rb', line 60

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:



44
45
46
# File 'lib/rcap/cap_1_1/resource.rb', line 44

def xmlns
  Alert::XMLNS
end