How to confirm partial line of text in Ruby - ruby-on-rails

I'm writing a test to confirm that a csv file has hit my downloads folder. As the title of the csv file is set to include the date and time of the download, it's impractical to keep changing the name of the file in my feature. Example filename: fleet_123456_20140707_103015.csv
Can I include in my ruby code, something that will just confirm that the "fleet_123456" is present as it's the only generic part of the name that will appear on every download?
At the moment I have:
Then /^I should get a download with the filename "(.*?)"$/ do |file_name|
page.response_headers['Content-Disposition'].should include("filename=\"#{file_name}\"")
end
I'm thinking that the "#{file_name}\"") needs tweeking, just not sure where.
Any help would be great, thank you

You asked:
Can I include in my ruby code, something that will just confirm that the "fleet_123456" is present as it's the only generic part of the name that will appear on every download?
Yes, you can. One way would be to replace the include matcher with a regex-based one. For example, instead of
page.response_headers['Content-Disposition'].should include("filename=\"#{file_name}\"")
you could write
page.response_headers['Content-Disposition'].should match(/filename="fleet_[\d_]+.csv"/)
, which would match "fleet_123456" followed by any combination of numbers and underscores, followed ".csv". Another possibility, if you want to be a little more specific, is
page.response_headers['Content-Disposition'].should match(/filename="fleet_123456_\d+_\d+.csv"/)
which matches the specific arrangement of groups of numbers separated by underscores. You can read about regular expressions in Ruby here and play with them here.

Related

how to force long URL/text to wrap in table cell when generating PDF in rails?

I am using princely(https://github.com/mbleigh/princely) to generate PDF in rails. I have long url in one table cell. It will extend the margin when I generate the PDF. In html,"word-break: break-all;" works well. But this rule "word-break: break-all;" doesn't work in PDF. Any body have any idea to wrap the long text when generating PDF?
Since princely is converting the ERB to PDF i believe we can use the truncate helper function of rails to make the link shorter.
= link_to truncate("The anchor you want to place", :length => 5), 'http://yoururl'
I was confronted with a similar issues when printing. Aside from the technical problem, you need to ask yourself some design questions: If you were generating a web page, a user would click the link. But if you're generating a PDF, I assume the goal is to print it. In which case, someone needs to type in this long URL.
How likely are the users going to be to type in a long url? If it only makes sense in the context of a live webpage, maybe you want to eliminate that content from the PDF.
If they do need to visit the destination, but they don't need to see the exact URL, then it may make more sense to shorten the URL.
If you want to shorten the URL, you can implement a URL redirection service on your Rails app. I like the following bit of code to generate the short URL code because they are all keyboard-friendly (they don't contain confusing characters, and don't require lots of shifting or switching of keyboards):
def generate_short_murl
a = [('a'..'k'),('m'..'z')].map{|i| i.to_a}.flatten
n = [('2'..'9')].map{|i| i.to_a}.flatten
(0...4).map{ a[rand(a.length)] }.join + (0...3).map{ n[rand(n.length)] }.join
end

Should I use unique ids or the whole sentences when localizing software?

I need to translate a website to a couple languages, and I've already read how to do it:
Mark strings for translation
Generate messages file
Translate messages file
The problem is, if I use whole sentences as the message ids, in english, for example, then if later I decide to modify the text, I'll have to change it in the code and on each message file . Or I could just change the english translation, but then my english message file will look weird, with translations from english to english which do not match.
Example:
Original: "I don't know what to do."
Translation: "I'm not sure what to do."
An alternative is to use unique message ids such as:
Original: "INDECISION_MESSAGE"
Translation: "I'm not sure what to do."
The advantage is that I can change translations without changing the id and things will still be consistent. But then there is no easy way for a translator to know what the message should be like as there is no context except by looking at the code.
What would you use?
In PHP people normally use the first approach you mentioned. In ASP.NET the second.
I think it's more of a personal taste and a matter of the framework that you use.
Personally I prefer the second option. Since you normally already have the ID/translation pair somewhere in a list, the translator can just take that list to translate.
What framework do you use?

What is the term for the last part of the path of a URL

What term do you give the part of the url after the last slash, but before the query? It seems most places people call it "the last part of the path" e.g. here but that is just so... wordy.
E.g. "something" in this url:
http://www.example.com/path/to/something?param=foo
Update:
I was hoping there was a well-known answer that I was not aware of, but it seems there is still some debate so, that's my answer right there. Guess I'll just keep calling it "the part of the path after the last slash". But I'll leave this question open anway, in case someone makes a convincing argument that gets lots of upvotes.
I'm a bit late to the party, but where I work we call it the slug, as mentioned here for example: https://prettylinks.com/2018/03/url-slugs/
protocol://server.domain/path?query
The path element (and it appears there is no 'defacto' definition) in my mind is the path to the resource on the server. No matter whether the resource is a file (blah.html) or a folder (/path/) it still instructs the server to use the path to find the resource.
Now there appears to be another definition at good/bad ol' Wikipedia here which states that it is usually "http://server/path/program?query_string" where the end resource is defined as 'program' but I think this is incorrect (is a folder a program?)
So.. perhaps its should be
protocol://server.domain/path[/resource.*]?query
? /../../ I traverse...
If URL is like /path/to/file.html or example.com/path/to/something.php?param=foo
then I think we can call it filename as mentioned at http://httpd.apache.org/docs/2.2/mod/directive-dict.html
In my experience it is called a query string whenever it is instructing action on the website. Code is frequently written to address the query string, which would prompt customized results on the page.
Otherwise, I agree it would be called a file name, if the path was referencing a file.
I saw a few uncertain suggestions here and there, including "filename", "basename". The Qt project has these names in their lexicon, so it can serve as a standard.
Namely check out the QFileInfo class.
They have standardized the names as the following:
QFileInfo fi("/tmp/archive.tar.gz");
fi.fileName(); // "archive.tar.gz"
fi.baseName(); // "archive"
fi.completeBaseName(); // "archive.tar"
fi.suffix(); // "gz"
fi.completeSuffix(); // "tar.gz"
This is a specification for local files however, so in this standard, there is a departure from basic URI terminology here:
fi.path(); // "/tmp"
fi.filePath(); // "/tmp/archive.tar.gz"
Where as in QUrl, you have path & fileName
QUrl("file:file.txt").path(); // "file.txt"
QUrl("/home/user/file.txt").path(); // "/home/user/file.txt"
QUrl("http://www.example.com/test/123").path(); // "/test/123"
and
QUrl("file:file.txt").fileName(); // "file.txt"
QUrl("/home/user/file.txt").fileName(); // "file.txt"
QUrl("http://www.example.com/test/123").fileName(); // "123"
If I were building a library, I personally would adopt QFileInfo terminology and apply to both URI and local files, and hope the entire rest of the world has enough sense to follow me.
I found the terms "last segment" or "last chunk" quite accurate and succinct.

Extracting email addresses in an html block in ruby/rails

I am creating a parser that wards off against spamming and harvesting of emails from a block of text that comes from tinyMCE (so it may or may not have html tags in it)
I've tried regexes and so far this has been successful:
/\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i
problem is, i need to ignore all email addresses with mailto hrefs. for example:
test#mail.com
should only return the second email add.
To get a background of what im doing, im reversing the email addresses in a block so the above example would look like this:
moc.liam#tset
problem with my current regex is that it also replaces the one in href. Is there a way for me to do this with a single regex? Or do i have to check for one then the other? Is there a way for me to do this just by using gsub or do I have to use some nokogiri/hpricot magicks and whatnot to parse the mailtos? Thanks in advance!
Here were my references btw:
so.com/questions/504860/extract-email-addresses-from-a-block-of-text
so.com/questions/1376149/regexp-for-extracting-a-mailto-address
im also testing using this:
http://rubular.com/
edit
here's my current helper code:
def email_obfuscator(text)
text.gsub(/\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i) { |m|
m = "<span class='anti-spam'>#{m.reverse}</span>"
}
end
which results in this:
<a target="_self" href="mailto:<span class='anti-spam'>moc.liamg#tset</span>"><span class="anti-spam">moc.liamg#tset</span></a>
Another option if lookbehind doesn't work:
/\b(mailto:)?([A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4})\b/i
This would match all emails, then you can manually check if first captured group is "mailto:" then skip this match.
Would this work?
/\b(?<!mailto:)[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i
The (?<!mailto:) is a negative lookbehind, which will ignore any matches starting with mailto:
I don't have Ruby set up at work, unfortunately, but it worked with PHP when I tested it...
Why not just store all the matched emails in an array and remove any duplicates? You can do this easily with the ruby standard library and (I imagine) it's probably quicker/more maintainable than adding more complexity to your regex.
emails = ["email_one#example.com", "email_one#example.com", "email_two#example.com"]
emails.uniq # => ["email_one#example.com", "email_two#example.com"]

ASP.Net MVC FileStreamResult, valid chars for FileDownloadName

I have an action method that returns a FileStreamResult, the download works fine, the problem is that although I set the FileDownloadName property of the result object, some of the files are downloaded with another name (specifically the last part of the address of the page I'm working on. e.g. in the page "http://localhost:5479/Items/Edit/277" it will download a file called "277").
This happens when the name of the file contains special chars (e.g. "San José.jpg"), but it works just fine when the name doesn't have such chars (e.g. "San Jose.jpg").
So, my question is, how do I allow the user to download a file with special chars in its name? or, if it isn't possible, is there a method that removes all the special chars from a string or do I have to create one?
Thanks
Actually I just found a way to fix this.
Basically what I have to do is use the HttpUtility.UrlEncode method to convert the name of the file, when the file is downloaded it will get almost the same name as the original file (the difference being the spaces replaced with a plus (+) sign).
Hope this helps someone else.
I have the same Problem. A better solution is HttpUtility.UrlPathEncode(...) since it doesn't replace space by '+'.

Resources