Class: ChupaText::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/chupa-text/data.rb

Direct Known Subclasses

InputData, TextData, VirtualFileData

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeData

Returns a new instance of Data.



54
55
56
57
58
59
60
61
62
# File 'lib/chupa-text/data.rb', line 54

def initialize
  @uri = nil
  @body = nil
  @size = nil
  @path = nil
  @mime_type = nil
  @attributes = Attributes.new
  @source = nil
end

Instance Attribute Details

#attributesAttributes (readonly)

Returns The attributes of the data.

Returns:



47
48
49
# File 'lib/chupa-text/data.rb', line 47

def attributes
  @attributes
end

#bodyString?

Returns The content of the data, nil if the data doesn't have any content.

Returns:

  • (String, nil)

    The content of the data, nil if the data doesn't have any content.



29
30
31
# File 'lib/chupa-text/data.rb', line 29

def body
  @body
end

#pathString?

Returns The path associated with the content of the data, nil if the data doesn't associated with any file.

The path may not be related with the original content. For example, "/tmp/XXX.txt" may be returned for the data of "http://example.com/XXX.txt".

This value is useful to use an external command to extract text and meta-data.

Returns:

  • (String, nil)

    The path associated with the content of the data, nil if the data doesn't associated with any file.

    The path may not be related with the original content. For example, "/tmp/XXX.txt" may be returned for the data of "http://example.com/XXX.txt".

    This value is useful to use an external command to extract text and meta-data.



44
45
46
# File 'lib/chupa-text/data.rb', line 44

def path
  @path
end

#sizeInteger?

Returns The byte size of the data, nil if the data doesn't have any content.

Returns:

  • (Integer, nil)

    The byte size of the data, nil if the data doesn't have any content.



33
34
35
# File 'lib/chupa-text/data.rb', line 33

def size
  @size
end

#sourceData?

Returns The source of the data. For example, text data (hello.txt) in archive data (hello.tar) have the archive data in #source.

Returns:

  • (Data, nil)

    The source of the data. For example, text data (hello.txt) in archive data (hello.tar) have the archive data in #source.



52
53
54
# File 'lib/chupa-text/data.rb', line 52

def source
  @source
end

#uriURI?

Returns The URI of the data if the data is for remote or local file, nil if the data isn't associated with any URIs.

Returns:

  • (URI, nil)

    The URI of the data if the data is for remote or local file, nil if the data isn't associated with any URIs.



25
26
27
# File 'lib/chupa-text/data.rb', line 25

def uri
  @uri
end

Instance Method Details

#[](name) ⇒ Object



84
85
86
# File 'lib/chupa-text/data.rb', line 84

def [](name)
  @attributes[name]
end

#[]=(name, value) ⇒ Object



88
89
90
# File 'lib/chupa-text/data.rb', line 88

def []=(name, value)
  @attributes[name] = value
end

#extensionString?

Returns Normalized extension as String if #uri is not nil, nil otherwise. The normalized extension uses lower case like pdf not PDF.

Returns:

  • (String, nil)

    Normalized extension as String if #uri is not nil, nil otherwise. The normalized extension uses lower case like pdf not PDF.



110
111
112
113
# File 'lib/chupa-text/data.rb', line 110

def extension
  return nil if @uri.nil?
  File.extname(@uri.path).downcase.gsub(/\A\./, "")
end

#initialize_copy(object) ⇒ Object



64
65
66
67
68
# File 'lib/chupa-text/data.rb', line 64

def initialize_copy(object)
  super
  @attributes = @attributes.dup
  self
end

#mime_typeString?

Returns:

  • (String)

    The MIME type of the data. If MIME type isn't set, guesses MIME type from path and body.

  • (nil)

    If MIME type isn't set and it can't guess MIME type from path and body.



96
97
98
# File 'lib/chupa-text/data.rb', line 96

def mime_type
  @mime_type || guess_mime_type
end

#mime_type=(type) ⇒ Object

Parameters:

  • type (String, nil)

    The MIME type of the data. You can unset MIME type by nil. If you unset MIME type, MIME type is guessed from path and body of the data.



103
104
105
# File 'lib/chupa-text/data.rb', line 103

def mime_type=(type)
  @mime_type = type
end

#open {|StringIO.new(body)| ... } ⇒ Object

Yields:

  • (StringIO.new(body))


80
81
82
# File 'lib/chupa-text/data.rb', line 80

def open
  yield(StringIO.new(body))
end

#text?Bool

Returns true if MIME type is "text/XXX", false otherwise.

Returns:

  • (Bool)

    true if MIME type is "text/XXX", false otherwise.



117
118
119
# File 'lib/chupa-text/data.rb', line 117

def text?
  (mime_type || "").start_with?("text/")
end

#text_plain?Bool

Returns true if MIME type is "text/plain", false otherwise.

Returns:

  • (Bool)

    true if MIME type is "text/plain", false otherwise.



123
124
125
# File 'lib/chupa-text/data.rb', line 123

def text_plain?
  mime_type == "text/plain"
end