Class: RCAP::Base::Resource

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/rcap/base/resource.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#errors, included, #valid?, #validate

Constructor Details

#initialize {|_self| ... } ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • attributes (Hash{Symbol => Object})

Yields:

  • (_self)

Yield Parameters:



39
40
41
# File 'lib/rcap/base/resource.rb', line 39

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#digestString

Returns SHA-1 hash of contents of resource.

Returns:

  • (String)

    SHA-1 hash of contents of resource



15
16
17
# File 'lib/rcap/base/resource.rb', line 15

def digest
  @digest
end

#mime_typeString

Returns:



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

def mime_type
  @mime_type
end

#resource_descString

Returns Resource Description.

Returns:

  • (String)

    Resource Description



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

def resource_desc
  @resource_desc
end

#sizeInteger

Returns Expressed in bytes.

Returns:



11
12
13
# File 'lib/rcap/base/resource.rb', line 11

def size
  @size
end

#uriString

Returns Resource location.

Returns:

  • (String)

    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

Parameters:

  • resource_hash (Hash)

Returns:



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

Parameters:

  • resource_xml_element (REXML::Element)

Returns:



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

Parameters:

  • resource_yaml_data (Hash)

Returns:



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_sizenil, 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.

Returns:



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_urinil, String

The decoded contents of #deref_uri if present otherwise nil.

Returns:



82
83
84
# File 'lib/rcap/base/resource.rb', line 82

def decoded_deref_uri
  Base64.decode64(@deref_uri) if @deref_uri
end

#inspectString

Returns:



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_kbFloat

If size is defined returns the size in kilobytes

Returns:



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_hHash

Returns:

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

Returns a string representation of the resource of the form

resource_desc

Returns:



108
109
110
# File 'lib/rcap/base/resource.rb', line 108

def to_s
  @resource_desc
end

#to_xmlString

Returns:



95
96
97
# File 'lib/rcap/base/resource.rb', line 95

def to_xml
  to_xml_element.to_s
end

#to_xml_elementREXML::Element

Returns:

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

Parameters:

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

Returns:



128
129
130
# File 'lib/rcap/base/resource.rb', line 128

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

#to_yaml_dataObject



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