I want to print my logfile data into html.erb file - ruby

def cookies_conscent_log
logger = Logger.new("conscent.log")
logger.debug "IP Address: " + #country_ip + " | Country: " + #country_name + " | Consent status : " + #deliveries + " | Cookie type : " + #a + " | Time and date: #{Time.now.strftime("[%Y_%m_%d %H:%M:%S]")}". values printing in log file
logger.debug "------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
end
This is my controller method code used to get the values from Ajax call which I am printing in the log file. I am using Logger in rails to print the log file. The same data in my log file needs to be print on html.erb file. I want to display logfile data in tabular format that is why I am trying to get it in HTML file from log file. Is there any way to print logfile data into html.erb file.

Related

Unable To Send Multiline Plaintext Email With Mailgun using Ruby

I am using two files to send messages with Mailgun. They are:
email_sender.rb
message_text.rb
The code for the first one is:
require './message_text.rb'
fromLabel = "Email Guy"
fromAddress = "digital#mail.*****.com"
toAddress = "info#*****.net"
subject = "An Invitation"
cmd = "curl -s --user 'api:key-*****' https://api.mailgun.net/v3/mail.*****.com/messages -F from='" + fromLabel + " <" + fromAddress + ">' -F to='" +toAddress + "' -F subject='" + subject + "' -F text='" + $message + "'"
wasGood = system (cmd)
The code for the second file is:
$message = "Line One Text."
+ "\n" + "\n" + "And Line Two Text!"
When I test sending an email, the message that arrives in my test account inbox is as follows.
Line One Text.
If you run the code with ruby -w, that is: with warnings enabled, it warns: warning: possibly useless use of + in void context, with the according line number, pointing to:
$message = "Line One Text."
+ "\n" + "\n" + "And Line Two Text!"
Which is a polite way of Ruby saying: "well, it's not a syntax error, but it does not make sense to me."
Try it with
$message = "Line One Text.
And Line Two Text!" # or: "Line One Text.\n\nAnd Line Two Text!"
So I got it working by putting everything on a single line.
$message = "Line One Text!" + "\n" + "\n" + "Line Two Text!"

Path Tcl and Bash

i have a problem with path in a tcl file i tried to use
source " /tmp/mob.tcl "
and this path in bash file :
/opt/ns-allinone-2.35/ns-2.35/indep-utils/cmu-scen-gen/setdest/setdest -v 1 -n $n -p 10 -M 64 -t 100 -x 250 -y 250 >> /tmp/mob.tcl
but terminal give me this error :
couldn't read file " /tmp/mob.tcl ": no such file or directory
while executing
"source.orig { /tmp/mob.tcl }"
someone can help me please
Learn to believe the error messages you get ;-).
couldn't read file " /tmp/mob.tcl ": no such file or directory
This is because you have submitted a string with the first letter (and the last) of the path being a " " char, i.e.
source " /tmp/mob.tcl "
try submitting
source "/tmp/mob.tcl"
IHTH

How to provide flags to ruby script like -v , -o and place code accordingly?

I have created one ruby script that I want to run with some flags on console say -v flag prints output on console and -o stores output in new file with file name I am taking from console using gets()
My code has following structure:
puts "Enter filename to analyze:\n\n"
filename = gets().chomp
puts "Provide filename to store result in new text file:\n\n"
output = gets().chomp
filesize = File.size(filename)
puts "File size in Bytes:\n#{filesize.to_i}\n"
pagecontent = filesize - 20
puts "\n\nData:\n#{pagecontent}\n\n"
File.open(filename,'r') do |file|
#whole process with few do..end in between that I want to do in 2 different #ways.
#If I provide -v flag on console result of this code should be displayed on console
#and with -o flag it should be stored in file with filename provided on console #stored in output variable declared above
end
end
Use stdlib OptionParser

Rename method of Ruby; escaping colon

How do you escape a colon, when renaming files in Ruby?
I have following code (names is a hash with data already filled in):
new_filename = ""
counter = 0
Dir.glob(folder_path + "/*").each do |f|
numbering = names.index(names.values.sort[counter])
new_filename = numbering + " - " + names.values.sort[counter]
puts "New file name: " + new_filename
File.rename(f, folder_path + "/" + new_filename + File.extname(f))
counter += 1
end
puts "Renaming complete."
The output of new_filename is correct, e.g. "Foo - Bar: Foo.txt". When it renames the file, the file has following format: "Foo - Bar/ Foo.txt".
I tried escaping with the colon with a backslash, but doesn't seem to work, because my output then looks like this: "Foo - Bar/\ Foo.txt".
Is is possible to have a colon in a string for renaming files?
FYI - in NTFS a colon identifies a separate stream of the same file... "Foo Bar: Foo.txt" identifies file "Foo Bar", stream " Foo.txt". Reference "Alternate Data Streams" (currently http://support.microsoft.com/kb/105763). AFIK this feature is not really widely used, though I have seen it used to tag files with thrid-party data (I use it to store a file's sha1 for dupe identification under the stream *:sha1).

renaming files using variables in Ruby

How do you use variables to rename files in Ruby?
File.rename("text1.txt", "text2.txt")
The above example is fine when using irb, but I writing a script where both var1 and var2 are unknown to me.
for example:
script_dir = File.expand_path File.dirname(__FILE__)
Dir.chdir(script_dir)
Dir.glob('Cancer1-1.pencast').each do |pencast|
pencast_title = File.basename(File.basename(pencast), '.*')
i = 1
audio_title = File.basename(`unzip -l #{pencast} | grep .aac | awk '{print $4;}' | awk 'NR=='#{i}''`)
audio_path = `unzip -l #{pencast} | grep .aac | awk '{print $4;}' | awk 'NR=='#{i}''`
audio_extension = File.extname(File.basename(audio_path))
new_name = "#{pencast_title}-#{i}#{audio_extension}"
File.rename(audio_title, new_name)
does not work...
but if i use puts var1 I see the file name I want.
The error I get is:
prog_test.rb:12:in `rename': No such file or directory - audio-0.aac (Errno::ENOENT)
or Cancer1-1-1.aac
from prog_test.rb:12
from prog_test.rb:5:in `each'
from prog_test.rb:5
but the file audio-0.aac is there... I'm looking at it.
I am certain I have located the problem:
it seems to be adding a variable to another variable. This is a simplified example that produces the same output:
audio_title = "audio-0.aac"
fullPath = File::SEPARATOR + "Users" + File::SEPARATOR + "name" + File::SEPARATOR + "Desktop" + File::SEPARATOR + audio_title
newname = File::SEPARATOR + "Users" + File::SEPARATOR + "name" + File::SEPARATOR + "Desktop" + File::SEPARATOR + "audio1.aac"
puts fullPath
puts newname
File.rename(fullPath, newname)
OUTPUT :
/Users/name/Desktop/audio-0.aac
/Users/name/Desktop/audio1.aac
prog_test.rb:22:in `rename': No such file or directory - /Users/name/Desktop/audio-0.aac or /Users/name/Desktop/audio1.aac (Errno::ENOENT)
from prog_test.rb:22
You should be passing the full file path to File.rename, not just the basename
I am not sure what is going on in your example inside File.basename() , but imagine the following:
fullPath = "C:" + File::SEPARATOR + "Folder" + File::SEPARATOR + "File.txt" # C:\Folder\File.txt
basename = File.basename(fullPath) # File
newFileName = "File.bak"
File.rename(basename, newFileName)
# How can Ruby possibly know which directory to find the above file in, or where to put it? - It will just look in the current working directory
So instead, you need to pass the full path to File.rename, like so:
fullPath = "C:" + File::SEPARATOR + "Folder" + File::SEPARATOR + "File.txt" # C:\Folder\File.txt
directory = File.dirname(fullPath) # C:\Folder
newFileName = "File.bak"
File.rename(fullPath, directory + File::SEPARATOR + newFileName)

Resources