0

I have some code to make API requests through Full contact to resolve domains for a list of company names, and output these in a csv table. I found the code was terminating whenever it hit a response code other than 200 or 202.

I have the following rescue block written:

def get_parse_all
        ORG_ARRAY.each do |company_name|
            begin
                org_info = get_org_info(company_name)
            rescue
                next
            end
            parse_org_info(org_info)
        end
    end

The issue is, I can't figure out how to still include the skipped company names (due to bad response code)in the output. I am getting a list of the successful calls, but I can't tell which ones were skipped and why.

I have tried to puts "error" before next, but it doesn't appear in the output csv. And if I remove next, I get a nil:NilClass (NoMethodError)

I've read some documentation but I am new to this and I'm quite stuck. If someone could point me in the right direction, that would be greatly appreciated!

Thank you in advance for any help :)

Belle
  • 11
  • 5
  • Do not use `rescue` without specifying which exception you are rescuing. This will rescue **everything** like NoMethodError and makes debugging impossible. – max Apr 20 '20 at 15:35

1 Answers1

0

In this case, it sounds like what you're looking to do is iterate over an array and transform each element to another value. The result would be another array, and each element within it would either be org_info or an error.

For this, you would use map instead of each. Remember that each does not return the result of the block, e.g. ORG_ARRAY.each do will always return ORG_ARRAY no matter what you do in the block.

def get_parse_all
  ORG_ARRAY.map do |company_name|
    begin
      parse_org_info(get_org_info(company_name))
    rescue => e
      e
    end
  end
end

As mentioned in a comment, you should also avoid using "naked rescue" - rescue a more specific error instead of any error at all.

max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • Thanks, this makes sense, but my output should be an array of arrays, with each array including the company name, and domain found. Under rescue I write ``` puts e.message``` which is showing up in my terminal but not in my CSV output. so it still seems to be skipping these errors? – Belle Apr 20 '20 at 19:06
  • Ok, you need to give an example of the expected output - one that includes both error and success cases - and also say what is the result format of `parse_org_info` – max pleaner Apr 20 '20 at 22:05
  • Hi Max, thank you for your help. I think I just discovered the problem I thought I had was not the problem I have. I think I need to add a column to the output with the company name I used to search bc the errors I'm getting are a result of full contact not finding that company in their database. So the three columns I need are: Searched Company | Matched Company from Full Contact| Domain of Matched Company – Belle Apr 21 '20 at 14:49
  • @Belle ok, well if you figured it out you can make your own answer so the question isn't solved .... if it's not solved then you need to add more info, which I mentioned in my previous comment – max pleaner Apr 21 '20 at 17:31