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 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, #to_yaml_data

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


8
9
10
# File 'lib/rcap/cap_1_2/resource.rb', line 8

def deref_uri
  @deref_uri
end

Class Method Details

.from_h(resource_hash) ⇒ Resource

Parameters:

  • resource_hash (Hash)

Returns:


93
94
95
96
97
# File 'lib/rcap/cap_1_2/resource.rb', line 93

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

.from_xml_element(resource_xml_element) ⇒ Resource

Parameters:

  • resource_xml_element (REXML::Element)

Returns:


51
52
53
54
55
# File 'lib/rcap/cap_1_2/resource.rb', line 51

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:


73
74
75
76
77
# File 'lib/rcap/cap_1_2/resource.rb', line 73

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

#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:


38
39
40
41
42
# File 'lib/rcap/cap_1_2/resource.rb', line 38

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

#to_hHash

Returns:

  • (Hash)

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

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)

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

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:


61
62
63
64
65
66
67
68
69
# File 'lib/rcap/cap_1_2/resource.rb', line 61

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

#xmlnsString

Returns:


45
46
47
# File 'lib/rcap/cap_1_2/resource.rb', line 45

def xmlns
  Alert::XMLNS
end