Class: HtmlToKit::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/htmltokit/source.rb

Defined Under Namespace

Classes: InvalidHTML, InvalidURL

Instance Method Summary collapse

Constructor Details

#initialize(url_file_or_html) ⇒ Source

Returns a new instance of Source.



7
8
9
# File 'lib/htmltokit/source.rb', line 7

def initialize(url_file_or_html)
  @value = url_file_or_html
end

Instance Method Details

#file? ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/htmltokit/source.rb', line 15

def file?
  @is_file ||= @value.is_a?(File) || @value.is_a?(Tempfile)
end

#html ⇒ Object

Raises:



28
29
30
31
32
33
34
35
36
# File 'lib/htmltokit/source.rb', line 28

def html
  raise InvalidHTML if url?

  if html?
    @value
  else
    @value.read
  end
end

#html? ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/htmltokit/source.rb', line 19

def html?
  @is_html ||= !(url? || file?)
end

#url ⇒ Object

Raises:



23
24
25
26
# File 'lib/htmltokit/source.rb', line 23

def url
  raise InvalidURL unless url?
  @value
end

#url? ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/htmltokit/source.rb', line 11

def url?
  @is_url ||= @value.is_a?(String) && @value.match?(/\Ahttp/)
end