Generating a QR code in rails - ruby-on-rails

I want to generate QR codes in ruby on rails, to run in the background of my website written in rails. Saw this http://code.google.com/p/qrcode-rails/ but cannot work out how I could get this to work for me. Basically in RoR I want to:
Pass a generator a string, my unique code, a 20 character length number (e.g. 32032928889998887776) and have an image generated with the name 'code'_qr.jpg and saved in a resource folder to be attached to an email that my program will send out.
How would I do this, does anyone know?
And while I'm asking (not so important that I get this answer now) but how would I implement QR code reading in, to get that code back, from a web cam? Thanks.

If you just need to write the data from the URL to a file, you can open up a stream, read from the file, and simply write the data to disk -- just remember to use the same extension (.jpg in this instance.)
Note that you could also simply send the link in the email (or post it as an inline image in the email.) If you really, really want to write it to disk and send it as attachment in your production system, the first-class solution for Ruby image processing is ruby-vips or ImageMagick.
Finally, since it's a disk operation, you're going to want to do it outside the normal web request cycle -- you're probably best off farming the operation out with delayed_job, or at the very least triggering the process with an AJAX request. Both of these give you the advantage that you can present a progress bar for the operation.

Related

Download, Unzip, and Parse a file in Ruby/Rails

I'm working with the very frustrating Bing Ads Api (SOAP) and while I've successfully executed the majority of SOAP requests I need, the last one is giving me trouble.
The team there tells me that to get an ad campaigns stats (clicks, impressions, conversions, etc.) I need to Request a report be generated (pass it the parameters), then take the reportID from the response and "poll" the report with another SOAP request, which yields a download URL for a zip file.
I've successfully done all the above and the download URL (which is only good for 5mins) looks like this:
https://download.api.bingads.microsoft.com/ReportDownload/Download.aspx?q=k471B%2fhtf62jwhaelHhu0EqMSfWCvWSpOOBRu76%2bUC%2bgATLEobf%2bMYiVKX0CBOr52d95ViPXJeKbvAbnb%2bSK%2bGumYlSYQT80kTtt5waa5z%2fmbeXT%2fPFqde95DFR1%2b4yQgekl5T6gKipbMFcQJOn5aGYmtI1ALcREIwJRA%2bi%2b3jOE55Cl69TAzBOUWvB73NAKX6S0Y7zF%2bERnSu7TJnJfmqHopWihGtkeMzoqqwsJVgVDEKz84RrPPaDOs2pxg3qE%2bLSrEwu2cpa7bP%2f9t%2fjUVtIgiZMbMjzSf73VnAUSpYNz
When I go to that URL, its starts to download a zip file that once unzipped, does contain the XML that I need to parse to report to users of the web app I'm creating.
My question is - What is the best way to get at that XML consistently within the app? This really seems like an arduous approach for the app to take considering all the above would have to take place every time a user loads the Bing page, or changes the date range, but they tell me its the only way to do it.
The path I've been heading down is to get the report with HTTParty and then unzip with RubyZip (have been unsuccessful because of undefined conversion error issues) but I'm unsure what to do from there. Store in a database (maybe temporarily)?
Any help would be greatly appreciated.
If there is no better way to use the API, cache the results (in your db or on the filesystem) and refresh the data using a rake task that will run periodically. If you do this, consider adding an option for the user to request an immediate refresh.
Use a background task to download the zip file and then process it, something like delayed_job or resque could be used to start the job with the URL.

Sending data to form, but cant work out encrypted post data - work around

Im trying to send some data to a form on a site were im a member using cURL, but when i look at the headers being sent, they seem to have been encrypted.
Is there a way i can get around this by making the computer / server visit the site and actual add the data to the inputs on the form and then hit submit, so that it would generate the correct data and post the form ?
You have got a few options:
reverse engineer the JavaScript that does the encryption (or possibly just encoding) process
get a browser engine (e.g. the Gecko engine), and add some scripting to it to fill in the forms and push the submit button - of course you would need JavaScript support within the page itself
parse the HTML using an HTML parser, feed the JavaScript in it to a JavaScript runtime with the correct libraries, fill in the "form" and hit the submit button
It's probably easiest to go for the first option. The JavaScript must be in the open to be able to be executed in the browser. But it may take some time to reverse-engineer as it is likely obfuscated.
You can use a framework to automate user interaction on the web pages, like Selenium.
This would enable you to not bother reverse engineering anything.
Selenium has binding in various languages, including Python and java.
Provided the javascript is visible on the website in question, you should be able to simply copy and paste their encryption routines to prepare the headers exactly as they do
A hacky fix if you can isolate the function that encodes the data you type in the form - is to use something like PyV8 to execute the JS inside python.
Use AutoHotKeyIt and actually have it use the Browser Normally. It can read from files, and do repetitive tasks infinitely. Also you can push a flag to make it only happen within that application, which means you can have it minimized and yet still preform the action.
You seem to be having issues with the problem of them encrypting the headers and such, so why not simply use that too your advantage? Your still pushing the same data in, but now your working around their system. With little to no side effect too you.

MD5 in ActionScript

I am trying to build a web based flash application. I am quite new to flash. I would like to develop it in two forms - demo and paid version. For this application to act as a paid version I need to have some kind of serial key. In order to achieve this I googled and came across something like this
MD5(MD5(thisuri)+thisuri)
I think 'thisuri' points to the current url page but I don't know how to get that url and I don't know whether '+' acts as a character or an operator.
Can you please help me?
It seems that a library exists in AS3.0 : as3corelib
An ActionScript 3 Library that contains a number of classes and utilities for working with ActionScript? 3. These include classes for MD5 and SHA 1 hashing, Image encoders, and JSON serialization as well as general String, Number and Date APIs.
To use it, just download the zip file, decompress it and copy the contents of "src" directory to the root of your project.
Then in your actionscript code, simply do the following :
import com.adobe.crypto.MD5;
var hash:String = MD5.hash(”test”);
source in french
To add to #Julien's recommendation of using as3corelib, you will also need the advice from this post to get the current url: Get Current Browser URL - ActionScript 3
Somehow I think there's a more elegant way to get the url, but I don't remember it.
While this may provide you with some basic check for the paid version, a determined hacker will easily fool this algorithm. For example, I could fool the environment into thinking that its being served from a domain that you've registered as part of the "paid" version. Also, since the client has the flash code, they can decompile the binary and potentially see the algorithm you're using. Depending on what you're offering in the app, this extreme case may or may not be acceptable to you.
Look into more secure authentication mechanisms if you're serious about security.

How to get text files onto iPhone?

I am making an app where the user will probably want to import a custom settings file.
It is a tuning fork app, and there are more tuning systems in existence than you can shake a stick at. I will supply a settings file that contains maybe half a dozen common tunings. this will satisfy 99% of customers, but those who want to go the extra mile I would like to provide the option of complete customisation.
Now it doesn't make sense to make an elaborate UI design. it would take users hours of painstaking way looking up numbers on Google or using their calculator, and keying in various tuning systems.
also what if they reset their phone?
what if they want to transmit this data to the iPad?
what if they want to send their configuratino to a friend?
what if I am maintaining a wiki which holds 300 different tuning systems, and they would like to grab half a dozen?
the best solution path I can see is to have the user create a text configuration file, with a simple format. something like this:
theme: "A3"
comment: "An octave below concert pitch (ie A4 440Hz)"
presets: {
A3 220Hz=220.0
}
// http://en.wikipedia.org/wiki/Guitar_tuning
theme: "Guitar Standard Tuning"
comment:"EADGBE using 12-TET tuning"
presets {
E2=82.41
A2=110.00
D3=146.83
G3=196.00
B3=246.94
E4=329.63
}
theme: "Bass Guitar Standard Tuning"
comment: "EADG using 12-TET tuning"
presets: {
E1=41.204
A2=55.000
D3=73.416
G3=97.999
}
Question is, how to get this on to their iPhone?
I don't want to rely on iTunes. maybe they will take their phone on holiday with them, and not their computer. maybe they don't even have a Mac, and they don't have iTunes on their PC. maybe they don't have a PC. maybe they just hate iTunes?
I don't have the resources to set up server space and actually host something myself.
first question: does there exists some place on the Internet I can deposit a text file and retrieve it with a minimal URL?
second question: I would also like to provide a mechanism for the user to load the file directly from their computer, ( say for example they are off-line ) -- how to do this?
Making a file in that format seems like an awful lot of hard work for a user.
It also seems like it would be a pretty big headache for you having to parse that data as the user will undoubtably only rarely get the format correct.
If I was designing this app I would make a UI for the settings. By making the UI you decouple the app from a computer completely as you no longer have to make a text file.
The only potential reasonable solution which is not viable as you do not want to set up a web service would be to create a small web front end where a user can edit their pref's through a custom form and then save the results. Then you can serve up the info by giving them a url, but this still seems a lot harder than just doing it on the device.
Remember that you will also have to implement UI anyway or the user will be forced to use a computer to configure their app - which quickly results in your app being canned for an easier alternative.
If this is still a route you wish to continue down then see this post for filetype association:.
If I paste something into Pastie, eg
http://pastie.org/2197740
I can get the raw text back by copying the URL of the ' download ' link.
(NOTE: cannot use the 'raw' link, as that still displays an HTML file containing JavaScript)
EDIT: Using http://min.us you can actually deposit a textfile, and then pass the URL -- which you get by copying the link from the ' download ' button on the page ).
EDIT: gist.github seems to be the perfect tool for this -- you can write a text file in the browser, and save it. and pastes typically persist forever. just need to throw the URL through bit.ly to shorten it.

rails 3: how to add (or perhaps substitute) a text string to an existing PDF document?

Users of our app need to print a PDF document we have pre-created, but have a placeholder string in the PDF template "YOUR_NAME_HERE" be replaced with their name. (Or, alternatively, we could no use a placeholder and add a new string with a certain font/style at a certain X,Y offset.)
Doing full PDF creation is overkill, since ALL we need to do is add their name to the PDF doc.
To make it more fun, we're hosted on Heroku which does not have local file storage, so we need to create the final PDF as something displayed in their browser that can (hopefully) be saved to local disk.
Does anyone know of a technique that would let us easily add (or replace) text to an existing PDF document?
I'm not finding anything for editing PDFs in ruby. I would just look into using something like prawn to generate them, even if that is a bit overkill when only a few words are different between each.
If efficiency is an issue, you could convert the pre-made part into a PNG and then just add the text on top. It feels dirty, but it'd probably be quicker than full generation and I don't know what other options you have, since it doesn't seem like anyone has implemented a true PDF editor in ruby yet.
As far as local storage, keep in mind that you do have write access to tmp/ on Heroku, so you can use that as long as you're only going to use the file during a single request.

Resources