ruby on rails how to input text into a website after using Nokogiri to open it - ruby-on-rails

I'm writing a unit test for our app for the forget password mutation. The mutation will send the email with the new randomly generated password to the provided email address, and I want to test this. Right now I'm using this to access yopmail.com
doc = Nokogiri::HTML(open("https://yopmail.com/en/"))
puts doc.to_html
On yopmail.com, there's an input box where u input the disposable email to get to the inbox, I just want to know how I would input the disposable email that I generated in my code into yopmail using Nokogiri or whatever other means. Sorry if the explanation isn't clear, I'm not sure how to make this completely make sense. Thank you.

As TTD already mentioned, Nokogiri can only parse HTML / XML but you can't really control the input (although you can alter the HTML itself but that doesn't help for your problem).
What I would do is to avoid entering user input as it will also make your tests a lot more flaky. You can get your mailbox for instance with https://yopmail.com/?<name-of-your-inbox> so you don't have to enter any input.
Unfortunately, yopmail seems to not offer any API. If you're not bound to yopmail, you could for instance try https://rapidapi.com/Privatix/api/temp-mail which offers an API and it make it easier to parse the email results.

Related

Verifying Captcha

Q1 - I have been reading through the docs for reCaptcha, and looking at many different forum cases but I am not experienced with API calls at all - I am trying to add a captcha to my custom contact form but I am stuck on the verification step trying to figure out how/where to send the info for verification, and how/where to receive it so I know weather or not the user is verified.
(Side Question: Why is it necessary to validate the token generated by the captcha? Isn't it good enough that you can tell weather or not the puzzle/answer was solved?)
Before the closing head tag:
<script src='https://www.google.com/recaptcha/api.js'></script>
End of my form:
<div class="g-recaptcha" data-sitekey="my-site-key"></div>
I can see the string/token generated by a correct answer when I call:
grecaptcha.getResponse();
Now (as I understand it) I have to verify this string/token which is where I get stuck:
URL: https://www.google.com/recaptcha/api/siteverify
METHOD: POST
DOCS: https://developers.google.com/recaptcha/docs/verify
I am relatively decent with jQuery and vanilla JS but when it comes to API calls I have almost no experience, which is why at this point in the docs I am unsure of how to 1 - form an/the API call (for verification), 2 - where to make the API call from template files-wise, 3 - how to get the response back, or rather how the response comes back.
As I mentioned I am using a Bigcommerce store, and the Google reCaptcha documentation mentioned in several different areas that this step is done on the server-side (or should be). I know that I am fairly restricted in the template files that I can modify - I can view and modify the HTML/CSS/JS files but I have no access to any PHP.
Any help or push in the right direction would be greatly appreciated - at this point I am going in circles finding and trying to read/follow the same docs (Google and other) and forum cases. Thank you.
Trying to answer your questions one by one.
Client side Captcha's are discussed here, please check and note that considering the power of Java Script, client side captchas are not safe.
How reCAPTCHA works: Once someone include below script, google will verify
user.
https://www.google.com/recaptcha/api.js
Writing below attributes in the Form will send data to google first and
response will be added in final post of the current Form with
attribute named g-recaptcha-response :
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
How to validate reCAPTCHA One has to validate this g-recaptcha-response with google. [ NOTE: this is requried becaues
client can send any random value for attribute g-recaptcha-response
without going through Captcha ]
$verifyResponse =
file_get_contents('https://www.google.com/recaptcha/api/siteverify?
secret='.$YOUR_SECRET.'&response='.$_POST['g-recaptcha-response'])
/*allow_url_fopen must be ON if you want to use file_get_contents.
check it using phpinfo();*/
file_put_contents( "logfile", $verifyResponse, FILE_APPEND );
$responseData = json_decode($verifyResponse);
$register_result = 'Robot verification failed, please try again.';
if( $responseData->success )
{
$register_result = 'You are not a bot';
}
else $register_result = 'You are a bot.';
Captcha with HTML/JS/CSS reCaptcha will not work for you if you don't have PHP access.
Puzzles as Captcha Captcha Puzzles are also possible and such captcha's are also available but they are handled on server side.
The reCaptcha verification si to make sure that the answer you received does come from reCaptha server, that the user is not a robot, that it hasn't matured, and that it hasn't been used more than once.
All this is really important to the server running the app, more that the cliente showing the form in order to accept or reject the form to be processed.
This is why the validation has to be done on the server. The client is an unsecure environment that can be tricked so the server cannot trust it.
Besides, to do the validation you need a secret api key. As it is secret it can't be embeded into the client code.
If you don't have access to the php or cannot add and aditional php to do the validation, I don't think you can implement reCaptcha.
EDIT I
The bottom line is that client code (js, jQuery running in a browser) cannot be trusted to do any kind of validation from the server point of view.
For example suppose you implement an input element for the user to enter the result of the sum of two random integers between 0 and 9. Very simple.
In the javascript code in some place there is an:
if(a + b === c){
sendFormToServer();
}else{
reject();
}
Anyone using the browser's developer tools could bypass the "if" and directly call sendFormToServer(). And the server has no way of knowing it.

OCR validations with Rails building a business card scanner

My goal is to write a validation class for Rails that is capable of using an OCR recognised text from a business card and is able to detect string snippets and assign them to the correct attributes. I know this cannot be probably 100% perfect but I want to get as close as possible. Here is my approach so far:
I scan business cards via jquery's navigator.mediaDevices
I send the scanned image to a third party API Service, called OCRSpace (a gem is available here: https://github.com/suyesh/ocr_space)
I then get a unformatted array of recognised text snippets back, for example:
result = [['John Doe'], ['+49 160 123456'], ['Mainstr. 45a'], ['12345 Berlin'], ['CEO'], ['johndoe#business-website.de'], ['www.business-website.de']]
I then iterate through the array and do some checks, for example
Using the people library (https://github.com/mericson/people)
to split the name in firstname and lastname (additionally the title
or middlenames) Using the phonelib library
(https://github.com/daddyz/phonelib) to look up a valid phone number
and format it in an international string
Doing a basic regex check on the email address and store it
What I miss now is:
How can I find out what the name-string would possibly be? Right now I let the user choose it (in my example he defines "John Doe" as the name and then the library does the rest). I'm sure I would run into conflicts when using a regex as strings like "Main Street" would then also be recognized as a name?
How do I regex a combination of ZIP-Code and City name? I'm not a regex expert, do you know any good sources that would help? Couldn't find any so far except some regex-checkers in general.
In general: Do you like my approach or is this way too complicated? And do you know some best-practices that look better?
Don't consider this a full answer, but it was too much to make it a comment.
Your way of working seems Ok but I wouldn't use the OCR Service since there are other ways , Tesseract is the best known.
If you do and all the results are comparible presented it seems not too difficult since every piece of info has it's own characteristics.
You can identify the name part because it won't have numbers in it, the rest does, also you can expect to contain it "Mr." or "Mrs." or the such and not "Str.", "street" and so on. You could also use Google Maps to check for correct adresses, there are Ruby gems but have no experience with them.
Your people gem could also help.
You could guess all of this, present the results in you webpage and let the user confirm or adjust.
You could also RegExpr the post-city combination by looking fo a number and string combination in either order but you could also use a gem like ZipCodes to help.
I'm sorry, don't have the time now to test some Regular Expressions now and I don't publish code without testing.
Hope this was some help, success !

ResearchKit: Validate email

I'm attempting to create a form step where one of the form step items is an email input. For this I want to validate the email against certain domains i.e.
#gmail.com, #icloud.com, #me.com
I can see we have an email answer format in the form of this:
ORKEmailAnswerFormat()
However I can't see anywhere in this type that allows me to apply a validation regex. Looking into this I see we have the following
ORKAnswerFormat.textAnswerFormatWithValidationRegex(validationRegex, invalidMessage)
I suppose this is my best option? If so, would anyone know of a regex (my regex isn't the greatest!) in swift that would handle the 3 domains stated above?
I have something like this...(not the greatest i know!)
[A-Z0-9a-z._%+-]+#gmail.com
[A-Z0-9a-z._%+-]+#(?:icloud|me|gmail)\.com
(or, if you don't care about capturing:)
[A-Z0-9a-z._%+-]+#(icloud|me|gmail)\.com
Now I made two modifications. I escaped the . and I made it so that the other two domains are options.
I suggest that you convert the whole thing to lower case. I don't know Swift, but you may be able to use one of its functions or the i modifier:
(?i)[0-9a-z._%+-]+#(icloud|me|gmail)\.com

Testing email markup against myself not working

I've used this schema example and sent myself an email using the Email Markup App Script.
The mail is recieved but no Event card is shown. I've also validated it with the Google Markup Validator, and I'm not missing any required field. What am I doing wrong?
PD: Only "Action" markup worked for me.
#whitenoisedb I see that you've tried sending the lodgingreservation schema to yourself. If you're copying and pasting the example without modifying the checkinDate and checkoutDate attributes, your card will not generate until 2017.
"checkinDate": "2017-04-11T16:00:00-08:00",
"checkoutDate": "2017-04-13T11:00:00-08:00"
Use a current checkinDate (ex. 2015-12-15T16:00:00-08:00) instead.
If the dates are coming up soon and it is still not showing, there is a chance that this type of Google cards are not supported in your country

Rails to_s Mechanics

Hey guys this has been tripping me up quite a bit. So here is the general problem:
I am writing an application that requires users to enter their Summoner Names from league of legends. I do a pretty simple data scrape of a match and enter the data into my database. Unfortunately I am having some errors registering users with "special characters".
For this example I will use one problem user: RIÇK
As you can see RICK != RIÇK. So when I do the data scrub from the site I get the correct value which I push onto an array for later use.
Once I need the player names I pull from the array as follows: (player_names is the array)
#temp_player = User.find_by_username(player_names[i].to_s)
The problem is the users with any special characters are not being pulled. Should I not be using find_by? Is to_s changing my original values? I am really quite lost on what to do and would greatly appreciate any help / advice.
Thanks in advance,
Dan
I would like to thank Brian Kung for the link to the following: joelonsoftware.com/articles/Unicode.html It does a great job giving the bare minimum a programmer truly needs to understand.
For my particular issue I had used a HTML scraper to get the contents but which kept HTML entries throughout. When using these with my SQL lookups it was obvious that things were not being found. In order to fix this I used the HTMLEntities Gem to decode the text as follows (as soon as I put the into the array originally):
requires 'RubyGems' #without this cannot include htmlentries as a gem
requires 'HTMLEntries'
coder = HTMLEntries.new
line = '&lt;'
player_names.push(coder.decode(line))
The Takeaway
When working with text and if running into errors I would strongly recommend tracing the strings you are working with to the origin and truly understanding what encoding is being used in each process. By doing this you can easily find where things are going wrong.

Resources