Skip to content

Commit

Permalink
Use String#start_with? instead of index == 0
Browse files Browse the repository at this point in the history
  • Loading branch information
sodabrew committed Mar 16, 2014
1 parent e11f233 commit c84b341
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion code/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
62 changes: 31 additions & 31 deletions code/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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;
Expand All @@ -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'])
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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!

Expand Down Expand Up @@ -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
Expand All @@ -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/")
Expand Down Expand Up @@ -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!
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -1713,22 +1713,22 @@ 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
status['run'] = @run.length
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')
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions code/relay_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions code/rq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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']
Expand Down
4 changes: 2 additions & 2 deletions code/rule_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]}"
Expand Down

0 comments on commit c84b341

Please sign in to comment.