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 collapse

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

Methods included from Validation

#errors, included, #valid?, #validate

Constructor Details

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

Instance Attribute Details

#deref_uriString

Returns Dereferenced URI - contents of URI Base64 encoded.

Returns:

  • (String)

    Dereferenced URI - contents of URI Base64 encoded


7
8
9
# File 'lib/rcap/cap_1_1/resource.rb', line 7

def deref_uri
  @deref_uri
end

Class Method Details

.from_h(resource_hash) ⇒ Resource

Parameters:

  • resource_hash (Hash)

Returns:


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

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

.from_xml_element(resource_xml_element) ⇒ Resource

Parameters:

  • resource_xml_element (REXML::Element)

Returns:


47
48
49
50
51
# File 'lib/rcap/cap_1_1/resource.rb', line 47

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

.from_yaml_data(resource_yaml_data) ⇒ Resource

Parameters:

  • resource_yaml_data (Hash)

Returns:


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

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

#dereference_uri!Array(Integer,String)

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:


34
35
36
37
38
# File 'lib/rcap/cap_1_1/resource.rb', line 34

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

#to_hHash

Returns:

  • (Hash)

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

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

#to_xml_elementREXML::Element

Returns:

  • (REXML::Element)

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

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(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

#to_yaml(options = {}) ⇒ String

Parameters:

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

Returns:


66
67
68
# File 'lib/rcap/cap_1_1/resource.rb', line 66

def to_yaml(options = {})
  to_yaml_data.to_yaml(options)
end

#to_yaml_dataObject


55
56
57
58
59
60
61
62
# File 'lib/rcap/cap_1_1/resource.rb', line 55

def to_yaml_data
  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])
end

#xmlnsString

Returns:


41
42
43
# File 'lib/rcap/cap_1_1/resource.rb', line 41

def xmlns
  Alert::XMLNS
end