From c84b341d053227297757e01c97773d060e681729 Mon Sep 17 00:00:00 2001 From: Aaron Stone Date: Sat, 15 Mar 2014 21:07:55 -0700 Subject: [PATCH] Use String#start_with? instead of index == 0 --- code/main.rb | 2 +- code/queue.rb | 62 +++++++++++++++++++++--------------------- code/relay_script.rb | 6 ++-- code/rq.rb | 6 ++-- code/rule_processor.rb | 4 +-- 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/code/main.rb b/code/main.rb index e1ca1cc..010bd55 100644 --- a/code/main.rb +++ b/code/main.rb @@ -238,7 +238,7 @@ def flash_now(type, msg) end end - if hostnames.any? {|h| prms['dest'].index(h) == 0} + if hostnames.any? { |h| prms['dest'].start_with?(h) } q_name = params[:name] else if (prms['relay_ok'] == 'yes') && (the_method != 'single_que') diff --git a/code/queue.rb b/code/queue.rb index 4354901..676f27f 100644 --- a/code/queue.rb +++ b/code/queue.rb @@ -807,7 +807,7 @@ def clone_msg(msg) new_basename = @queue_path + "/prep/" + new_msg['msg_id'] if File.directory?(old_basename + "/attach/") - ents = Dir.entries(old_basename + "/attach/").reject {|i| i.index('.') == 0 } + ents = Dir.entries(old_basename + "/attach/").reject { |i| i.start_with?('.') } if not ents.empty? # simple check for attachment dir old_attach_path = old_basename + '/attach/' @@ -859,7 +859,7 @@ def get_message(params, state, # Now check for attachments if options[:read_message] && options[:check_attachments] && File.directory?(basename + "/attach/") cwd = Dir.pwd - ents = Dir.entries(basename + "/attach/").reject {|i| i.index('.') == 0 } + ents = Dir.entries(basename + "/attach/").reject { |i| i.start_with?('.') } if not ents.empty? msg['_attachments'] = { } ents.each do |ent| @@ -894,7 +894,7 @@ def gen_full_dest(msg) } # IF message already has full remote dest... - if msg['dest'].index('http:') == 0 + if msg['dest'].start_with?('http:') res['dest'] = msg['dest'] q_name = msg['dest'][/\/q\/([^\/]+)/, 1] res['queue'] = q_name; @@ -914,7 +914,7 @@ def attach_msg(msg) #TODO: deal with symlinks # simple early check, ok, now check for pathname - return [false, "Invalid pathname, must be normalized #{msg['pathname']} (ie. must start with /"] unless msg['pathname'].index("/") == 0 + return [false, "Invalid pathname, must be normalized #{msg['pathname']} (ie. must start with /"] unless msg['pathname'].start_with?("/") return [false, "No such file #{msg['pathname']} to attach to message"] unless File.exists?(msg['pathname']) return [false, "Attachment currently cannot be a directory #{msg['pathname']}"] if File.directory?(msg['pathname']) return [false, "Attachment currently cannot be read: #{msg['pathname']}"] unless File.readable?(msg['pathname']) @@ -930,7 +930,7 @@ def attach_msg(msg) name = msg['name'] || File.basename(msg['pathname']) # Validate - that it does not have any '/' chars or a '.' prefix - if (name.index(".") == 0) + if name.start_with?(".") return [false, "Attachment name as a dot-file not allowed: #{name}"] end # Unsafe char removal @@ -1031,12 +1031,12 @@ def load_messages # prep just has message ids basename = @queue_path + '/prep/' - @prep = Dir.entries(basename).reject {|i| i.index('.') == 0 } + @prep = Dir.entries(basename).reject { |i| i.start_with?('.') } @prep.sort!.reverse! # run msgs just put back into que basename = @queue_path + '/run/' - messages = Dir.entries(basename).reject {|i| i.index('.') == 0 } + messages = Dir.entries(basename).reject { |i| i.start_with?('.') } messages.each do |mname| begin File.rename(basename + mname, @queue_path + '/que/' + mname) @@ -1048,7 +1048,7 @@ def load_messages # que has actual messages copied basename = @queue_path + '/que/' - messages = Dir.entries(basename).reject {|i| i.index('.') == 0 } + messages = Dir.entries(basename).reject { |i| i.start_with?('.') } messages.sort!.reverse! @@ -1155,7 +1155,7 @@ def handle_status_read(msg) due,future,new_dest = parts[1].split('-',3) new_due = Time.now.to_i + due.to_i - if new_dest.index('http') == 0 + if new_dest.start_with?('http') que_name = 'relay' else que_name = new_dest @@ -1179,7 +1179,7 @@ def handle_status_read(msg) attachments = [] if File.directory?(basename + "/attach/") - ents = Dir.entries(basename + "/attach/").reject {|i| i.index('.') == 0 } + ents = Dir.entries(basename + "/attach/").reject { |i| i.start_with?('.') } if not ents.empty? # Cool, lets normalize the paths full_path = File.expand_path(basename + "/attach/") @@ -1634,33 +1634,33 @@ def handle_request(sock) log("REQ [ #{packet} ]") - if packet.index('ping ') == 0 + if packet.start_with?('ping ') resp = [ "pong" ].to_json send_packet(sock, resp) return end - if packet.index('uptime ') == 0 + if packet.start_with?('uptime ') resp = [(Time.now - @start_time).to_i, ].to_json send_packet(sock, resp) return end - if packet.index('config ') == 0 + if packet.start_with?('config ') # Sadly there's no struct-to-hash method until Ruby 2.0 resp = [ 'ok', Hash[@config.each_pair.to_a]].to_json send_packet(sock, resp) return end - if packet.index('status') == 0 + if packet.start_with?('status') @status.update! resp = [ @status.admin_status, @status.oper_status ].to_json send_packet(sock, resp) return end - if packet.index('shutdown') == 0 + if packet.start_with?('shutdown') resp = [ 'ok' ].to_json send_packet(sock, resp) shutdown! @@ -1675,7 +1675,7 @@ def handle_request(sock) return end - if packet.index('create_message') == 0 + if packet.start_with?('create_message') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1692,7 +1692,7 @@ def handle_request(sock) return end - if packet.index('single_que') == 0 + if packet.start_with?('single_que') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1713,7 +1713,7 @@ def handle_request(sock) return end - if packet.index('num_messages') == 0 + if packet.start_with?('num_messages') status = { } status['prep'] = @prep.length status['que'] = @que.length @@ -1721,14 +1721,14 @@ def handle_request(sock) status['pause'] = [] status['done'] = RQ::HashDir.num_entries(@queue_path + "/done") status['relayed'] = RQ::HashDir.num_entries(@queue_path + "/relayed/") - status['err'] = Dir.entries(@queue_path + "/err/").reject {|i| i.index('.') == 0 }.length + status['err'] = Dir.entries(@queue_path + "/err/").reject { |i| i.start_with?('.') }.length resp = status.to_json send_packet(sock, resp) return end - if packet.index('messages') == 0 + if packet.start_with?('messages') json = packet.split(' ', 2)[1] options = JSON.parse(json) if not options.has_key?('state') @@ -1747,7 +1747,7 @@ def handle_request(sock) elsif options['state'] == 'relayed' status = RQ::HashDir.entries(@queue_path + "/relayed/", options['limit']) elsif options['state'] == 'err' - status = Dir.entries(@queue_path + "/err/").reject {|i| i.index('.') == 0 } + status = Dir.entries(@queue_path + "/err/").reject { |i| i.start_with?('.') } else status = [ "fail", "invalid 'state' field (#{options['state']})"] end @@ -1757,7 +1757,7 @@ def handle_request(sock) return end - if packet.index('prep_message') == 0 + if packet.start_with?('prep_message') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1773,7 +1773,7 @@ def handle_request(sock) return end - if packet.index('attach_message') == 0 + if packet.start_with?('attach_message') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1797,7 +1797,7 @@ def handle_request(sock) return end - if packet.index('delete_attach_message') == 0 + if packet.start_with?('delete_attach_message') # Params: msg_id, attachment_name json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1823,7 +1823,7 @@ def handle_request(sock) return end - if packet.index('commit_message') == 0 + if packet.start_with?('commit_message') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1847,7 +1847,7 @@ def handle_request(sock) return end - if packet.index('get_message ') == 0 + if packet.start_with?('get_message ') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1875,7 +1875,7 @@ def handle_request(sock) return end - if packet.index('get_message_state ') == 0 + if packet.start_with?('get_message_state ') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1899,7 +1899,7 @@ def handle_request(sock) return end - if packet.index('get_message_status ') == 0 + if packet.start_with?('get_message_status ') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1930,7 +1930,7 @@ def handle_request(sock) return end - if packet.index('delete_message') == 0 + if packet.start_with?('delete_message') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1953,7 +1953,7 @@ def handle_request(sock) return end - if packet.index('run_message') == 0 + if packet.start_with?('run_message') json = packet.split(' ', 2)[1] options = JSON.parse(json) @@ -1982,7 +1982,7 @@ def handle_request(sock) return end - if packet.index('clone_message') == 0 + if packet.start_with?('clone_message') json = packet.split(' ', 2)[1] options = JSON.parse(json) diff --git a/code/relay_script.rb b/code/relay_script.rb index 7947da4..030420b 100755 --- a/code/relay_script.rb +++ b/code/relay_script.rb @@ -108,7 +108,7 @@ def file_md5(path) # If none of the hostnames on this system are in the dest, # this is a remote queue delivery -if not hostnames.any? {|h| dest.index(h) == 0} +if not hostnames.any? { |h| dest.start_with?(h) } remote_delivery = true end @@ -216,7 +216,7 @@ def file_md5(path) # Idempotently attach any attachments if File.exists?('../attach') log("attempting sending attach") - entries = Dir.entries('../attach').reject { |e| e.index('.') == 0 } + entries = Dir.entries('../attach').reject { |e| e.start_with?('.') } fnames = entries.select { |e| File.file?("../attach/#{e}") } fnames.each do @@ -362,7 +362,7 @@ def file_md5(path) # Idempotently attach any attachments if File.exists?('../attach') - entries = Dir.entries('../attach').reject { |e| e.index('.') == 0 } + entries = Dir.entries('../attach').reject { |e| e.start_with?('.') } fnames = entries.select { |e| File.file?("../attach/#{e}") } fnames.each do diff --git a/code/rq.rb b/code/rq.rb index b5b18df..dba0722 100644 --- a/code/rq.rb +++ b/code/rq.rb @@ -60,7 +60,7 @@ def cmd_sendmesg(args) q_name = args['dest'] raise RQ::RqMissingArgument if not q_name - if q_name.index('http:') == 0 + if q_name.start_with?('http:') raise RQ::RqCannotRelay if !args.has_key?('relay-ok') # throw :halt, [404, 'Sorry - cannot relay message'] q_name = 'relay' end @@ -83,7 +83,7 @@ def cmd_prepmesg(args) q_name = args['dest'] raise RQ::RqMissingArgument if not q_name - if q_name.index('http:') == 0 + if q_name.start_with?('http:') raise RQ::RqCannotRelay if !args.has_key?('relay-ok') # throw :halt, [404, 'Sorry - cannot relay message'] q_name = 'relay' end @@ -217,7 +217,7 @@ def cmd_single_que(args) q_name = args['dest'] raise RQ::RqMissingArgument if not q_name - #if (q_name.index('http:') == 0) && args.has_key?('relay-ok') + #if q_name.start_with?('http:') && args.has_key?('relay-ok') # q_name = 'relay' #else # throw :halt, [404, 'Sorry - cannot relay message'] diff --git a/code/rule_processor.rb b/code/rule_processor.rb index f222598..76f0fb4 100755 --- a/code/rule_processor.rb +++ b/code/rule_processor.rb @@ -153,7 +153,7 @@ def first_match(o) def txform_host(old, new) # if new has full address, we just use that - if new.index("http") == 0 + if new.start_with?("http") return new end @@ -162,7 +162,7 @@ def txform_host(old, new) new += ':3333' end - if old.index("http") == 0 # if a standard full msg_id + if old.start_with?("http") # if a standard full msg_id # just swap out the host parts = old.split('/q/', 2) "http://#{new}/q/#{parts[1]}"