Change status based on your requests sent

This commit is contained in:
Bernie Chiu 2013-09-25 17:46:32 +08:00
parent 70de1cea9d
commit 8247f077df
1 changed files with 29 additions and 13 deletions

View File

@ -28,15 +28,28 @@ class Panel::Ask::BackEnd::AskRepliesController < OrbitBackendController
Resque.enqueue(SendAskReplyMail, @ask_reply.ask_question.id)
end
status_stack = []
new_status = ""
params[:school][:depts].each do |dns|
next if dns.empty?
begin
send_request(dns, @ask_question, @ask_reply)
rescue URI::InvalidURIError
next
status_stack << send_request(dns, @ask_question, @ask_reply)
rescue
status_stack << "fail"
end
end unless params[:school].nil?
success_count = status_stack.count("success")
failed_count = status_stack.count("fail")
if failed_count == 0 && success_count > 0
new_status = "轉介其他單位"
elsif failed_count > 0 && success_count > 0
new_status = "部份傳送失敗 (Timeout)"
else
new_status = "傳送失敗 (Timeout)"
end
@ask_reply.update_attributes(status: new_status)
redirect_to panel_ask_back_end_ask_questions_path, notice: t('ask.reply_success')
end
@ -49,6 +62,7 @@ class Panel::Ask::BackEnd::AskRepliesController < OrbitBackendController
uri = URI.parse("http://" + dep_dns + uri_path)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
body = {
# 'auth_token' => '3kjlfksjDFJ',
'name' => current_question.name,
@ -60,20 +74,22 @@ class Panel::Ask::BackEnd::AskRepliesController < OrbitBackendController
'content' => current_question.content,
}
#binding.pry
2.times do
status = ""
3.times do
request.set_form_data(body)
request.add_field 'X-Auth-Token', '3kjlfksjDFJ'
response = http.request(request)
if response.code != "200"
new_status = "部份傳送失敗"
current_reply.update_attributes(status: new_status)
elsif response.code == "200" && (current_reply.status != "部份傳送失敗")
new_status = "轉介其他單位"
current_reply.update_attributes(status: new_status)
if response.code == "200"
status = "success"
break
end
else
status = "fail"
end
end
puts "Response #{response.code} #{response.message}: #{response.body}"
return status
# puts "Response #{response.code} #{response.message}: #{response.body}"
# flash[:notice] = response.body
# flash[:notice] = "Response #{response.code} #{response.message}: #{response.body}"
end