Class: NumRu::GDir::HtDir

Inherits:
Object
  • Object
show all
Defined in:
lib/numru/htdir.rb

Constant Summary collapse

Regexp_URL =
/^(http:\/\/)?([\w\-\.]+)(:(\d+))?(\/[\w\-\.\/_~]*)?/
Regexp_File =
/<img.*alt=\"\[(.*)\]\".*href=\"([^\"]+)\"[^>]*>([^<]*)/i
File_Type =
{'DIR'=>'directory','TXT'=>'text'}
@@proxy_host =
@@proxy_port = nil
@@dods_interpret =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, proxy = nil) ⇒ HtDir

Returns a new instance of HtDir.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/numru/htdir.rb', line 65

def initialize(url, proxy=nil)
	url += '/' if /\/$/ !~ url    # treat URLs that always end with '/'
  @url = url
	if Regexp_URL =~ @url
	  @host = $2
	  @port = ( $4 ? $4.to_i : 80 )
	  @path = $5
	else
	  raise "Invalid URL: "+url
	end
	if proxy
	  if Regexp_URL =~ proxy
	    @proxy_host = $2
	    @proxy_port = ( $4 ? $4.to_i : 8080 )
	  else
	    raise "Invalid Proxy URL: "+proxy
	  end
	else
	  @proxy_host = @@proxy_host
	  @proxy_port = @@proxy_port
	end
	@http = Net::HTTP.new(@host, @port, @proxy_host, @proxy_port)
	res = @http.get(@path)
	@body = res.body
	@files = Array.new
	@body.each do |line|
	  if Regexp_File =~ line
	    fltyp = File_Type[$1]
	    url = $2
	    label = $3
	    if @@dods_interpret
 if /Parent Directory/ =~ label
		url = '..'
 else
		url = File.basename( url.sub(/http:\//,'') )
 end
 if ( fltyp != 'text' and /\.(\w+)\.html$/ =~ url and
		   /#{$1}$/ =~ label )
		# It must be a dods file url with a '.html' suffix
		url.sub!(/\.html$/,'')
 end
	    end
	    @files.push( {:file_type=>fltyp, :url=>url} )
	  end
	end
end

Class Method Details

.dods_interpretObject



48
# File 'lib/numru/htdir.rb', line 48

def dods_interpret; @@dods_interpret; end

.dods_interpret=(t_or_f) ⇒ Object



49
50
51
# File 'lib/numru/htdir.rb', line 49

def dods_interpret=(t_or_f)
  @@dods_interpret=t_or_f
end

.proxy=(proxy) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/numru/htdir.rb', line 55

def proxy=(proxy)
  if Regexp_URL =~ proxy
    @@proxy_host = $2
    @@proxy_port = ( $4 ? $4.to_i : 8080 )
  else
    raise "Invalid Proxy URL: "+proxy
  end
end

.proxy_hostObject



54
# File 'lib/numru/htdir.rb', line 54

def proxy_host; @@proxy_host; end

.proxy_portObject



53
# File 'lib/numru/htdir.rb', line 53

def proxy_port; @@proxy_port; end

Instance Method Details

#bodyObject



112
113
114
# File 'lib/numru/htdir.rb', line 112

def body
	@body
end

#closeObject



142
143
144
# File 'lib/numru/htdir.rb', line 142

def close
	@http.finish if @http.active?
end

#collectObject



122
123
124
125
126
127
128
# File 'lib/numru/htdir.rb', line 122

def collect
	ary = Array.new
	@files.each do |fl|
	  ary.push( yield(fl[:url]) )
	end
	ary
end

#eachObject



116
117
118
119
120
# File 'lib/numru/htdir.rb', line 116

def each
	@files.each do |fl|
	  yield(fl[:url])
	end
end

#each_dirObject



130
131
132
133
134
# File 'lib/numru/htdir.rb', line 130

def each_dir
	@files.each do |fl|
	  yield(fl[:url]) if fl[:file_type] == 'directory'
	end
end

#each_fileObject



136
137
138
139
140
# File 'lib/numru/htdir.rb', line 136

def each_file
	@files.each do |fl|
	  yield(fl[:url]) if fl[:file_type] != 'directory'
	end
end

#rewindObject



146
147
148
# File 'lib/numru/htdir.rb', line 146

def rewind
	# do nothing
end