0

I want to try and run my code, even if I encounter an error. I'll state where I believe I get the error below and what the error is:

require 'rubygems'
require 'simple_oauth'
require 'cloudsight'
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'open-uri'
require 'openssl'
require 'hpricot'


#ALCHEMY

 file='C:\\Users\\ENTER USERNAME\\Desktop\\cloudsight.txt'
 f = File.open(file, "r")
 f.each_line {|line|

 tstart = 'name"=>"'
 tstop = '"'
 term = line[/#{tstart}(.*?)#{tstop}/m, 1]

 url = 'http://access.alchemyapi.com/calls'
 service = '/text/TextGetRankedTaxonomy'
 apikey = '?apikey=ENTER ALCHEMY API KEY'
 thething = '&text='
 termencoded = URI::encode(term)
 fullurl = url + service + apikey + thething + termencoded

 sleep 1
 opener = open(fullurl, 'Accept-Encoding' => '') {|f| f.read } 
 #print opener

I think I get the error at this point.

 lstart = '<label>/'
 lstop = '</label>'
 label = opener[/#{lstart}(.*?)#{lstop}/m, 1]

 sleep 1
 cstart = '<score>'
 cstop = '</score>'
 confidence = opener[/#{cstart}(.*?)#{cstop}/m, 1]

 #data = label + ',' + confidence + ',' + line
 print label
 print confidence
 print "\n"

 }

This is the error I seem to get:

C:/Ruby21/lib/ruby/2.1.0/uri/common.rb:304:in `escape': undefined method `gsub' for nil:NilClass (NoMethodError)

Any ideas as to how I can pass the error/escape it/ or make a string come up instead?

CDub
  • 13,146
  • 4
  • 51
  • 68
semiflex
  • 1,176
  • 3
  • 25
  • 44
  • Will this work? label = opener[/#{lstart}(.*?)#{lstop}/m, 1] rescue '' and this confidence = opener[/#{cstart}(.*?)#{cstop}/m, 1] rescue '' – Zahid Sep 17 '15 at 15:54
  • Where are you using gsub? The code you posted doesn't include it. – grenierm5 Sep 17 '15 at 15:58
  • then I get the following: C:/Users/USERNAME/Desktop/tes453:45: syntax error, unexpected tINTEGER, expecting keyword_do or '{' or '(' C:/Users/USERNAME/Desktop/tes453:51: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' Do you know how I can get around this? – semiflex Sep 17 '15 at 15:59
  • I don't think I'm using gsub – semiflex Sep 17 '15 at 16:04

2 Answers2

0

I'm guessing the error is coming from this line:

termencoded = URI::encode(term)

And is probably happening because term is nil. Add some debugging to see what term is set to before that line is called. If you're okay with term being blank you could change that line to this:

termencoded = URI::encode(term.to_s)

Whether or not that's acceptable for your program I can't say.

I wouldn't try to suppress the error though. Something isn't working right and you should figure out what it is and how to handle it.

Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
  • term seems to already be a string. I added your code anyway and I seem to get the same results. What I'm doing is retrieving data using the alchemy api. Sometimes the the data is there, in which case I receive it. If it's not there, this error throws up. – semiflex Sep 17 '15 at 16:38
  • Interesting. Does the stacktrace not point you to the line in your code that the error originates on? – Philip Hallstrom Sep 17 '15 at 17:45
0

I solved the problem. Turns out it's an issue on alchemys side in this instance. The taxonomy search just wasn't able to find a term

semiflex
  • 1,176
  • 3
  • 25
  • 44