<commit_before>require File.expand_path('../config/boot', __FILE__)

# Bad Request (used for a request that isn't formed correctly)
error 400 do
	{errors: [{ message: 'This resource requires you post JSON data with the "to", "subject", and "body" attributes.' }] }.to_json
end

# Wrong HTTP Verb
error 405 do
	{errors: [{ message: 'This resource only responses to a POST request. Please refer to the documentation' }] }.to_json
end

before '/api/*' do
	content_type :json
	if request.request_method != 'POST'
		halt 405
	end
end

module Mailers

	def email(request_data)
		mail_options = {
			:to 			=> request_data["to"],
			:from 		=> '<EMAIL>',
			:subject 	=> request_data["subject"],
			:body 		=> request_data["body"],
			:via			=> :smtp,
  		:via_options => {
  			:address => 'smtp.gmail.com', # 'smtp.mandrillapp.com'
  			:enable_starttls_auto => true,
  			:port => '587',
  			:user_name => ENV['GMAIL_USER'], # ENV['MANDRILL_USER'],
  			:password => ENV['<PASSWORD>'], # ENV['MANDRILL_API_KEY'],
  			:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
  			:domain => "bryansbriefcase.com" 
  		}
		}
		Pony.mail(mail_options)
	end

end

helpers Mailers

get '/hello' do
	'What up dawg'
end

post '/api/nr_mailer' do
	request.body.rewind
	json_data = request.body.read
	if json_data != ''
		begin
			request_data = JSON.parse(json_data)
		rescue JSON::ParserError
			halt 400
		end
	else
		halt 400
	end
	mail_response = email(request_data)
	{:emails => [{ :status => 'Mail Sent', :details => mail_response }] }.to_json
end<commit_msg>refactored the post request to be more concise<commit_after>require File.expand_path('../config/boot', __FILE__)

# Bad Request (used for a request that isn't formed correctly)
error 400 do
	{errors: [{ message: 'This resource requires you post JSON data with the "to", "subject", and "body" attributes.' }] }.to_json
end

# Wrong HTTP Verb
error 405 do
	{errors: [{ message: 'This resource only responses to a POST request. Please refer to the documentation' }] }.to_json
end

# ensures that the content type is JSON and the Request is POST
before '/api/*' do
	content_type :json
	if request.request_method != 'POST'
		halt 405
	end
end

# Mailers in their own module so it can be tested
module Mailers

	def email(request_data)
		mail_options = {
			:to 			=> request_data["to"],
			:from 		=> '<EMAIL>',
			:subject 	=> request_data["subject"],
			:body 		=> request_data["body"],
			:via			=> :smtp,
  		:via_options => {
  			:address => 'smtp.gmail.com', # 'smtp.mandrillapp.com'
  			:enable_starttls_auto => true,
  			:port => '587',
  			:user_name => ENV['GMAIL_USER'], # ENV['MANDRILL_USER'],
  			:password => ENV['<PASSWORD>'], # ENV['MANDRILL_API_KEY'],
  			:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
  			:domain => "bryansbriefcase.com" 
  		}
		}
		Pony.mail(mail_options)
	end

end

helpers Mailers

get '/hello' do
	'What up dawg'
end

post '/api/nr_mailer' do
	request.body.rewind
	json_data = request.body.read

	begin
		request_data = JSON.parse(json_data)
	rescue JSON::ParserError
		halt 400
	end

	mail_response = email(request_data)
	{:emails => [{ :status => 'Mail Sent', :details => mail_response }] }.to_json
end
{'fraction_non_alphanumeric': 0.13977832512315272, 'fraction_numerical': 0.010775862068965518, 'mean_word_length': 3.7920353982300883, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6822999', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1178, 'n_words': 324}