Class: MossRuby
- Inherits:
-
Object
- Object
- MossRuby
- Defined in:
- lib/moss_ruby.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#port ⇒ Object
Returns the value of attribute port.
-
#server ⇒ Object
Returns the value of attribute server.
-
#userid ⇒ Object
Returns the value of attribute userid.
Class Method Summary collapse
Instance Method Summary collapse
- #check(files_dict, callback = nil) ⇒ Object
- #extract_results(uri, min_pct = 10, callback = nil) ⇒ Object
-
#initialize(userid, server = "moss.stanford.edu", port = 7690) ⇒ MossRuby
constructor
A new instance of MossRuby.
- #upload_file(moss_server, file, id = 0) ⇒ Object
Constructor Details
#initialize(userid, server = "moss.stanford.edu", port = 7690) ⇒ MossRuby
Returns a new instance of MossRuby.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/moss_ruby.rb', line 49 def initialize(userid, server = "moss.stanford.edu", port = 7690) @options = { max_matches: 10, directory_submission: false, show_num_matches: 250, experimental_server: false, comment: "", language: "c" } @server = server @port = port @userid = userid end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
33 34 35 |
# File 'lib/moss_ruby.rb', line 33 def @options end |
#port ⇒ Object
Returns the value of attribute port.
32 33 34 |
# File 'lib/moss_ruby.rb', line 32 def port @port end |
#server ⇒ Object
Returns the value of attribute server.
31 32 33 |
# File 'lib/moss_ruby.rb', line 31 def server @server end |
#userid ⇒ Object
Returns the value of attribute userid.
30 31 32 |
# File 'lib/moss_ruby.rb', line 30 def userid @userid end |
Class Method Details
.add_base_file(hash, file) ⇒ Object
39 40 41 42 |
# File 'lib/moss_ruby.rb', line 39 def self.add_base_file ( hash, file ) hash[:base_files] << file hash end |
.add_file(hash, file) ⇒ Object
44 45 46 47 |
# File 'lib/moss_ruby.rb', line 44 def self.add_file ( hash, file ) hash[:files] << file hash end |
.empty_file_hash ⇒ Object
35 36 37 |
# File 'lib/moss_ruby.rb', line 35 def self.empty_file_hash { base_files: Array.new, files: Array.new } end |
Instance Method Details
#check(files_dict, callback = nil) ⇒ Object
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/moss_ruby.rb', line 75 def check(files_dict, callback=nil) # Chech that the files_dict contains valid filenames files_dict[:base_files].each do |file_search| if Dir.glob(file_search).length == 0 raise "Unable to locate base file(s) matching #{file_search}" end end if files_dict[:files].length == 0 return end files_dict[:files].each do |file_search| if Dir.glob(file_search).length == 0 raise "Unable to locate base file(s) matching #{file_search}" end end # Connect to the server callback.call('Connecting to MOSS') unless callback.nil? moss_server = TCPTimeout::TCPSocket.new @server, @port, connect_timeout: 10, write_timeout: 9, read_timeout: 300 begin # Send header details callback.call(' - Sending configuration details') unless callback.nil? moss_server.write "moss #{@userid}\n" moss_server.write "directory #{@options[:directory_submission] ? 1 : 0 }\n" moss_server.write "X #{@options[:experimental_server] ? 1 : 0}\n" moss_server.write "maxmatches #{@options[:max_matches]}\n" moss_server.write "show #{@options[:show_num_matches]}\n" # Send language option moss_server.write "language #{@options[:language]}\n" callback.call(' - Checking language') unless callback.nil? line = gets(moss_server) if line.strip() != "yes" moss_server.write "end\n" raise "Invalid language option." end count = 1 processing = files_dict[:base_files] processing.each do |file_search| callback.call(" - Sending base files #{count} of #{processing.count} - #{file_search}") unless callback.nil? files = Dir.glob(file_search) file_count = 1 files.each do |file| callback.call(" - Base file #{file_count} of #{files.count} - #{file}") unless callback.nil? upload_file moss_server, file file_count += 1 end count += 1 end idx = 1 count = 1 processing = files_dict[:files] processing.each do |file_search| callback.call(" - Sending files #{count} of #{processing.count} - #{file_search}") unless callback.nil? files = Dir.glob(file_search) file_count = 1 files.each do |file| callback.call(" - File #{idx} = #{file_count} of #{files.count} - #{file}") unless callback.nil? upload_file moss_server, file, idx idx += 1 file_count += 1 end end callback.call(" - Waiting for server response") unless callback.nil? moss_server.write "query 0 #{@options[:comment]}\n" result = gets(moss_server) moss_server.write "end\n" return result.strip() ensure moss_server.close end end |
#extract_results(uri, min_pct = 10, callback = nil) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/moss_ruby.rb', line 156 def extract_results(uri, min_pct = 10, callback = nil) result = Array.new begin match = -1 match_file = Array.new data = Array.new to_fetch = get_matches(uri, min_pct, callback) to_fetch.each do |id| match += 1 callback.call("Checking match #{match + 1} (id #{id})") unless callback.nil? # read the two match files match_url = "#{uri}/match#{id}-top.html" match_top = URI.open(match_url).read().encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '?') callback.call(" - checking match #{match} percents") unless callback.nil? top = read_pcts match_top next if Integer(top[:pct0]) < min_pct && Integer(top[:pct1]) < min_pct callback.call(" - fetching #{match} html") unless callback.nil? match_file[0] = URI.open("#{uri}/match#{id}-0.html").read().encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '?') match_file[1] = URI.open("#{uri}/match#{id}-1.html").read().encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '?') # puts match_top # puts "---FILE0\n\n" # puts match_file[0] # puts "---FILE1\n\n" # puts match_file[1] callback.call(" - extracting data for #{match}") unless callback.nil? data[0] = read_data match_file[0] data[1] = read_data match_file[1] callback.call(" - adding #{match} result") unless callback.nil? result << [ { filename: data[0][:filename], html: strip_a("<PRE>#{data[0][:html]}</PRE>"), pct: Integer(top[:pct0]), url: match_url, part_url: "#{uri}/match#{id}-0.html" }, { filename: data[1][:filename], html: strip_a("<PRE>#{data[1][:html]}</PRE>"), pct: Integer(top[:pct1]), url: match_url, part_url: "#{uri}/match#{id}-1.html" } ] end rescue OpenURI::HTTPError #end when there are no more matches -- indicated by 404 when accessing matches-n-top.html end result end |
#upload_file(moss_server, file, id = 0) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/moss_ruby.rb', line 63 def upload_file (moss_server, file, id = 0) filename = file.strip.encode('UTF-8', invalid: :replace, undef: :replace, replace: '').gsub /[^\w\-\/.]/, '_' content = IO.read(file) size = content.bytes.length if size > 0 moss_server.write "file #{id} #{@options[:language]} #{size} #{filename}\n" moss_server.write content end end |