当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - API lookup by invalid email is failing because of < sign?

问题描述:

I have about 100,000 email addresses in a CSV file. Each one is associated with a member ID.

I have an API call that allows me to look up a member ID by email, however, it does not allow me to look up by improperly formatted emails. To solve this, I am trying to skip over specific errors: The API returns a 400 error when sent an improperly formatted email, so this is my code:

url = HTTParty.get("#{base_uri}#{call}",

:basic_auth => auth,

:headers => {'Accept' => 'application/json' } )

if url.code == 400

puts "400"

next

else

.....

This works for the most part. It seems to cycle through the list doing exactly what it should until it reaches a certain email, at which point it fails every time. The email it is failing on is formatted with a leading < sign:

<[email protected]

When it reaches this, the script stops and does not puts "400". It fails at the API call and doesn't do anything past that, and I can't figure out why.

As a side note, when I try to type the email here on StackOverflow outside of the code parameter, it automatically hides everything past the < sign. Could this have something to do with it? I feel like the < sign is being communicated in the code as something I am unaware of.

I am using Ruby 1.9.3.

网友答案:

You need to learn some technologies: SMTP and email addresses, and HTML and how browsers display it.

The email it is failing on is formatted with a leading < sign:

<[email protected]

That's the start of a legitimate email address, so you need to learn about the myriads of ways addresses can be formatted. The spec isn't straightforward, nor are there simple ways of checking for valid ones, so I'd recommend starting with Wikipedia's "Email address" article and jumping off from there into the RFCs, of which, RFC-5322 covers addresses. The best answer is always to try it and see; Send a message asking for a response with a known token you've generated, to that address if you want to know whether it's valid. If you get a response from the user containing that token then you know it's valid.

Since it's the API that's failing it's pretty obvious it's not written to meet the internet standards. Find a better API to use.

when I try to type the email here on StackOverflow outside of the code parameter, it automatically hides everything past the < sign.

< marks the beginning of a HTML tag, so your browser turns off output until it determines the tag has ended. Usually that occurs when the matching > is found, but there are additional heuristics a browser can use to decide when to give up.

If you don't want the browser to do that then you have to encode < as &lt; or use its numeric entity equivalent.

网友答案:

I don't know the rest of your code, but if you had this in a method, you could add this:

def do_work
  url = HTTParty.get("#{base_uri}#{call}",
    :basic_auth => auth,
    :headers => {'Accept' => 'application/json' } )
  if url.code == 400 
    puts "400"
    next
  else
    # more code...
rescue => e
  puts e
end

Catch any exception that may be bubbling out, it likely has a description of the error. The < character is an opening bracket for an HTML/XML tag, so it may well be inspecting that value and failing.

You can also just wrap a particular chunk of code in a begin rescue end block:

begin
  # sketchy code here...
rescue => e
  puts e
end
分享给朋友:
您可能感兴趣的文章:
随机阅读: