我有这样的事情:
d1 = RequestService.new('env', 'user', 'password')
req_obj = d1.getRequest('0000-0000')
d1.addAttachment(req_obj, 'file_location')
好像它不起作用。 d1.addAttachment
没有req_obj
。如果我执行d1.addAttachment(' 000-000',' file_location'),那么它可以完美地运行。如果我使用req_obj作为addAttachment中的属性,则会失败并显示错误:
request.rb:67:in `+': no implicit conversion of Request into String (TypeError)
from request.rb:67:in `addAttachment'
from tes.rb:9:in `<main>'
代码是:
class Request
def initialize(id)
$id = id
end
def to_s
$id.chomp
end
end
class RequestService < WsApi
def initialize(environment, user, password)
@environment = environment
@user = user
@password = password
if @environment == 'staging'
@uri = 'some url'
elsif @environment == 'production'
@uri = ''
else
puts 'Enter valid environment - staging / production'
end
end
def getRequest(request_id)
# define local variables
req_id = request_id
begin
# Create object of ws_api_base class to do a get function on the request_id to check if id exists or not
obj1 = WsApi.new(@uri, @user, @password)
restap = obj1.restapi
response = restap['requests/' + req_id ].get(:username => @user, :password => @password, :accept => 'application/json')
# Create a request object to set request id as valid cx ticket number
if response.code === 200
req_obj = Request.new(req_id)
return req_obj
end
rescue
puts 'Request id ' + req_id + ' does not exists'
exit
end
end
def addAttachment(request_id, file_location)
# define local variables
req_id = request_id
file_loc = file_location
# If request exists proceed with add attachment
f = Base64.encode64(File.read(file_loc))
file_size = File.size(file_loc)
file_name = File.basename(file_loc)
payload = [{"contentType" => "text/plain","size" => file_size,"fileName" => file_name,"data" => f.chomp}].to_json
obj1 = WsApi.new(@uri, @user, @password)
restap = obj1.restapi
response = restap['requests/' + req_id + '/attachments'].post(payload, :username => @user, :password => @password, :content_type => 'application/json', :accept => 'application/json')
if response.code === 200
puts "Attachment added successfuly"
else
puts response
end
end
end
答案 0 :(得分:0)
addAttachment
期待String不是对象,您只需要在to_s
上调用req_object
方法。 Ruby不会为你做那件事。
答案 1 :(得分:0)
您可以使用"requests/#{req_id}/attachments"
代替'requests/' + req_id + '/attachments'
。也许你应该覆盖to_s
方法。