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

#initialize(options = {}) ⇒ Data



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chupa-text/data.rb', line 82

def initialize(options={})
  @uri = nil
  @body = nil
  @size = nil
  @path = nil
  @mime_type = nil
  @attributes = Attributes.new
  @source = nil
  @screenshot = nil
  @need_screenshot = true
  @expected_screenshot_size = [200, 200]
  @max_body_size = nil
  @timeout = nil
  @limit_cpu = nil
  @limit_as = nil
  @options = options || {}
  source_data = @options[:source_data]
  if source_data
    merge!(source_data)
    @source = source_data
  end
end

Instance Attribute Details

#attributesAttributes (readonly)



50
51
52
# File 'lib/chupa-text/data.rb', line 50

def attributes
  @attributes
end

#bodyString?



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

def body
  @body
end

#expected_screenshot_sizeArray<Integer, Integer>



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

def expected_screenshot_size
  @expected_screenshot_size
end

#limit_asNumeric, ...



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

def limit_as
  @limit_as
end

#limit_cpuNumeric, ...



76
77
78
# File 'lib/chupa-text/data.rb', line 76

def limit_cpu
  @limit_cpu
end

#max_body_sizeInteger?



69
70
71
# File 'lib/chupa-text/data.rb', line 69

def max_body_size
  @max_body_size
end

#need_screenshot=(value) ⇒ Bool (writeonly)



63
64
65
# File 'lib/chupa-text/data.rb', line 63

def need_screenshot=(value)
  @need_screenshot = value
end

#pathString?



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

def path
  @path
end

#screenshotScreenshot?



59
60
61
# File 'lib/chupa-text/data.rb', line 59

def screenshot
  @screenshot
end

#sizeInteger?



36
37
38
# File 'lib/chupa-text/data.rb', line 36

def size
  @size
end

#sourceData?



55
56
57
# File 'lib/chupa-text/data.rb', line 55

def source
  @source
end

#timeoutNumeric, ...



72
73
74
# File 'lib/chupa-text/data.rb', line 72

def timeout
  @timeout
end

#uriURI?



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

def uri
  @uri
end

Instance Method Details

#[](name) ⇒ Object



171
172
173
# File 'lib/chupa-text/data.rb', line 171

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

#[]=(name, value) ⇒ Object



175
176
177
# File 'lib/chupa-text/data.rb', line 175

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

#extensionString?



197
198
199
200
201
202
203
204
# File 'lib/chupa-text/data.rb', line 197

def extension
  return nil if @uri.nil?
  if @uri.is_a?(URI::HTTP) and @uri.path.end_with?("/")
    "html"
  else
    File.extname(@uri.path).downcase.gsub(/\A\./, "")
  end
end

#initialize_copy(object) ⇒ Object



105
106
107
108
109
# File 'lib/chupa-text/data.rb', line 105

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

#merge!(data) ⇒ void

This method returns an undefined value.

Merges metadata from data.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chupa-text/data.rb', line 116

def merge!(data)
  self.uri = data.uri
  self.path = data.path
  data.attributes.each do |name, value|
    self[name] = value
  end
  if data.mime_type
    self["source-mime-types"] ||= []
    self["source-mime-types"].unshift(data.mime_type)
  end
  self.need_screenshot = data.need_screenshot?
  self.expected_screenshot_size = data.expected_screenshot_size
  self.max_body_size = data.max_body_size
  self.timeout = data.timeout
  self.limit_cpu = data.limit_cpu
  self.limit_as = data.limit_as
end

#mime_typeString?



183
184
185
# File 'lib/chupa-text/data.rb', line 183

def mime_type
  @mime_type || guess_mime_type
end

#mime_type=(type) ⇒ Object



190
191
192
# File 'lib/chupa-text/data.rb', line 190

def mime_type=(type)
  @mime_type = type
end

#need_screenshot?Bool



219
220
221
# File 'lib/chupa-text/data.rb', line 219

def need_screenshot?
  @need_screenshot
end

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

Yields:

  • (StringIO.new(body))


161
162
163
# File 'lib/chupa-text/data.rb', line 161

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

#peek_body(size) ⇒ Object



165
166
167
168
169
# File 'lib/chupa-text/data.rb', line 165

def peek_body(size)
  _body = body
  return nil if _body.nil?
  _body[0, size]
end

#text?Bool



208
209
210
# File 'lib/chupa-text/data.rb', line 208

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

#text_plain?Bool



214
215
216
# File 'lib/chupa-text/data.rb', line 214

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

#to_utf8_body_dataObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/chupa-text/data.rb', line 223

def to_utf8_body_data
  b = nil
  if @max_body_size
    open do |input|
      b = input.read(@max_body_size)
    end
  else
    b = body
  end
  return self if b.nil?

  converter = UTF8Converter.new(b)
  utf8_body = converter.convert
  if @max_body_size.nil? and b.equal?(utf8_body)
    self
  else
    TextData.new(utf8_body, source_data: self)
  end
end