Ruby将对象作为另一个对象内的属性调用
我有这样的想法:Ruby将对象作为另一个对象内的属性调用,ruby,object,instance,Ruby,Object,Instance,我有这样的想法: d1 = RequestService.new('env', 'user', 'password') req_obj = d1.getRequest('0000-0000') d1.addAttachment(req_obj, 'file_location') 好像没用d1。添加附件不接受req\u obj。如果我使用d1.addAttachment('000-000','file_location'),那么它可以完美地工作。如果我在addAttachment中使用req_o
d1 = RequestService.new('env', 'user', 'password')
req_obj = d1.getRequest('0000-0000')
d1.addAttachment(req_obj, 'file_location')
好像没用<代码>d1。添加附件不接受req\u obj
。如果我使用d1.addAttachment('000-000','file_location'),那么它可以完美地工作。如果我在addAttachment中使用req_obj作为属性,它将失败并出现错误:
request.rb:67:in `+': no implicit conversion of Request into String (TypeError)
from request.rb:67:in `addAttachment'
from tes.rb:9:in `<main>'
request.rb:67:in`+':没有将请求隐式转换为字符串(TypeError)
来自请求。rb:67:“添加附件”中
来自tes.rb:9:in`'
代码为:
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
类请求
def初始化(id)
$id=id
结束
def至美国
$id.chomp
结束
结束
类RequestService@user,:password=>@password,:accept=>'application/json'))
#创建请求对象以将请求id设置为有效的cx票证号
如果response.code==200
req_obj=请求。新(req_id)
返回请求对象
结束
营救
放置“请求id”+请求id+“不存在”
出口
结束
结束
def添加附件(请求id、文件位置)
#定义局部变量
请求id=请求id
文件位置=文件位置
#如果存在请求,请继续添加附件
f=Base64.encode64(File.read(File_loc))
文件大小=文件大小(文件位置)
file\u name=file.basename(file\u loc)
有效负载=[{“contentType”=>“text/plain”,“size”=>文件大小,“fileName”=>文件名,“data”=>f.chomp}]。发送到json
obj1=WsApi.new(@uri、@user、@password)
restap=obj1.restapi
response=restap['requests/'+req_id+'/attachments'].post(有效负载:username=>@user;password=>@password;content_type=>'application/json',:accept=>'application/json')
如果response.code==200
放入“成功添加附件”
其他的
作出回应
结束
结束
结束
addAttachment
需要的是字符串而不是对象,您只需调用到req\u对象上的方法即可。Ruby不会为您这样做。您可以使用“requests/{req\u id}/attachments”
而不是使用“requests/'+req\u id+'/attachments'
。也许你应该将改写为_s
方法。什么是“不工作”呢?你收到错误信息了吗?你能发布RequestService的代码吗?如果我发布d1.addAttachment('000-000','file_location'),那么它工作得很好。如果我在addAttachment中使用req_obj作为属性,那么它失败了。您必须发布addAttachment
方法的定义。如果失败,则显示堆栈跟踪。只添加了代码和应能正常工作的错误,但是否确实要使用$id
?您应该使用@id
。同样在ruby中,我们只使用camel case类。并注意您的代码格式。请我们需要阅读这篇文章。