Class: Tango::LinkStack

Inherits:
Object
  • Object
show all
Defined in:
lib/tango/link_stack.rb

Overview

Stack of links to be crawled

Author:

  • Mckomo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_link) ⇒ LinkStack

Returns a new instance of LinkStack.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tango/link_stack.rb', line 13

def initialize( base_link )
  
  if base_link !~ URI::regexp
    raise ArgumentError, "'#{base_link}' is not valid website URL."
  end
  
  # Parse base link
  url = URI( base_link )
  
  @host = "#{url.scheme}://#{url.host}:#{url.port}" # Extract host from base link
  @links = []                                       # Container for links (without host part)
  @shifted = 0                                      # Shifted links counter
  
  # Extract path (with query) from base link and append it as initial link
  path = url.query ? "#{url.path}?#{url.query}" : url.path 
  append( path )
  
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/tango/link_stack.rb', line 11

def host
  @host
end

Returns the value of attribute links.



11
12
13
# File 'lib/tango/link_stack.rb', line 11

def links
  @links
end

#shiftedObject (readonly)

Returns the value of attribute shifted.



11
12
13
# File 'lib/tango/link_stack.rb', line 11

def shifted
  @shifted
end

Instance Method Details

#append(links) ⇒ Array|String

Append link/s to stack

Returns:

  • (Array|String)


45
46
47
48
49
50
51
# File 'lib/tango/link_stack.rb', line 45

def append( links )    
  if links.is_a? String
    @links << links
  elsif links.is_a? Array
    @links += links 
  end   
end

#has_links?Boolean

Check if link stack still has links

Returns:

  • (Boolean)


56
57
58
# File 'lib/tango/link_stack.rb', line 56

def has_links?
  ! @links.empty?
end

#shiftString

Shift link from stack and get referrer content

Returns:

  • (String)


35
36
37
38
39
# File 'lib/tango/link_stack.rb', line 35

def shift
  return unless has_links?
  @shifted += 1 
  @links.shift
end