So I am fairly new to Ruby and this may very well be answered elsewhere but I have searched high and low with nothing to show.
I am defining a module for a RESTful API framework and mostly things have gone well apart from trying to get a string from a .txt file into the actual API end point (before parameters)
MY code is this:
require 'rest-client'
module Delete
def delete
file = File.open("ST.txt", "r")
sT = file.read,
file = File.open("cR.txt","r")
cR = file.read
begin
return RestClient.post({'https://testing.ixaris.com/paymentpartner/virtualcards/{cR}/delete'},
{ },
{:A => sT,})
rescue => e
return e.response
end
end
end
The the first "ST.txt" into the "A parameter" works fine but I can't seem to get the "cR" string into the "{cR}" part of the end point.
Any help would be hugely appreciated!
I'm not quite sure which line the thing is on so I'm going to try for both.
For only one line files (with a guarantee it's only one line):
cr = File.read('cR.txt')
Which converts the entire content of the file into a string which is fine as there is only one line.
For multiline files you might want to try:
cr =
File.open('cR.txt', 'r') do |f|
break unless f.each_line do |line|
break if line =~ /\d*/ # regex for matching any numbers or whatever you want to use to match with like ==,>,<,>=,<=,!=
end
end
Also, note that ruby implicitly returns from everything so unless you need to return explicitly from the middle of a method i.e. an if statement, you don't need to add return to everything.
Also, if you do a file.read
, make sure to file.close
when you're done with it, otherwise it won't be cleaned up till the garbage-collector runs and that could waste system resources; which is the advantage of doing a open file with a block as it automatically closes the file when the block is done.