Class: Saper::Items::Document

Inherits:
Saper::Item show all
Defined in:
lib/saper/items/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Saper::Item

#==, [], exists?, inherited, #serialize, subclasses, try, #type, type

Constructor Details

#initialize(body, uri = nil, headers = {}) ⇒ Document


26
27
28
29
30
# File 'lib/saper/items/document.rb', line 26

def initialize(body, uri = nil, headers = {})
  @uri     = (uri.nil? ? nil : uri.to_s)
  @body    = body
  @headers = headers
end

Instance Attribute Details

#bodyObject (readonly)


6
7
8
# File 'lib/saper/items/document.rb', line 6

def body
  @body
end

#headersObject (readonly)


12
13
14
# File 'lib/saper/items/document.rb', line 12

def headers
  @headers
end

#uriObject (readonly)


9
10
11
# File 'lib/saper/items/document.rb', line 9

def uri
  @uri
end

Class Method Details

.new(body, uri = nil, headers = {}) ⇒ Object


15
16
17
18
19
20
21
22
23
# File 'lib/saper/items/document.rb', line 15

def self.new(body, uri = nil, headers = {})
  unless body.is_a?(String)
    raise(Errors::InvalidItem)
  end
  unless headers.is_a?(Hash)
    raise(Errors::InvalidItem)
  end
  super
end

Instance Method Details

#charsetObject


43
44
45
# File 'lib/saper/items/document.rb', line 43

def charset
  content_type.include?("charset=") ? content_type.split("charset=").last : nil
end

#mimeObject


38
39
40
# File 'lib/saper/items/document.rb', line 38

def mime
  content_type.split(";").first
end

#sizeObject


33
34
35
# File 'lib/saper/items/document.rb', line 33

def size
  body.size
end

#to_htmlObject


53
54
55
# File 'lib/saper/items/document.rb', line 53

def to_html
  HTML.new(self)
end

#to_jsonObject


63
64
65
# File 'lib/saper/items/document.rb', line 63

def to_json
  JSON.new(body)
end

#to_markdownObject


68
69
70
# File 'lib/saper/items/document.rb', line 68

def to_markdown
  Markdown.new(to_html)
end

#to_nativeObject


73
74
75
# File 'lib/saper/items/document.rb', line 73

def to_native
  body
end

#to_sObject


77
78
79
# File 'lib/saper/items/document.rb', line 77

def to_s
  to_native
end

#to_textObject


48
49
50
# File 'lib/saper/items/document.rb', line 48

def to_text
  Text.new(body)
end

#to_xmlObject


58
59
60
# File 'lib/saper/items/document.rb', line 58

def to_xml
  XML.new(body)
end