Class: MossRuby

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#optionsObject (readonly)

Returns the value of attribute options.



33
34
35
# File 'lib/moss_ruby.rb', line 33

def options
  @options
end

#portObject

Returns the value of attribute port.



32
33
34
# File 'lib/moss_ruby.rb', line 32

def port
  @port
end

#serverObject

Returns the value of attribute server.



31
32
33
# File 'lib/moss_ruby.rb', line 31

def server
  @server
end

#useridObject

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_hashObject



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



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
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/moss_ruby.rb', line 86

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, File::FNM_CASEFOLD).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, File::FNM_CASEFOLD).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 = TCPSocket.new @server, @port
  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 = moss_server.gets
    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::FNM_CASEFOLD)
      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::FNM_CASEFOLD)
      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 = moss_server.gets

    moss_server.write "end\n"
    return result.strip()
  ensure
    moss_server.close
  end
end

#extract_results(uri, min_pct = 10, callback = nil) ⇒ Object



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
216
217
218
219
220
221
# File 'lib/moss_ruby.rb', line 167

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_top = open("#{uri}/match#{id}-top.html").read

      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] = open("#{uri}/match#{id}-0.html").read
      match_file[1] = open("#{uri}/match#{id}-1.html").read

      # 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])
        },
        {
          filename:  data[1][:filename], 
          html:      strip_a("<PRE>#{data[1][:html]}</PRE>"),
          pct:     Integer(top[:pct1])            
        }
      ]
    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
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/moss_ruby.rb', line 63

def upload_file (moss_server, file, id = 0)
  filename = file.strip.tap do |name|
    name.gsub! /[^\w\-\/.]/, '_'
  end

  content = IO.read(filename)
  size = content.length

  if size > 0 
    moss_server.write "file #{id} #{@options[:language]} #{size} #{file}\n"
    moss_server.write content
  end
  puts "File upload size #{filename} #{size}"
  begin
    line = moss_server.read_nonblock(100)
    puts "--------------------------------------------"
    puts " From MOSS: #{line}"
    puts "--------------------------------------------"
  rescue IO::WaitReadable
    #expected
  end
end