简体   繁体   English

Ruby调用对象作为另一个对象内的属性

[英]Ruby calling object as an attribute inside another object

I have something like this: 我有这样的事情:

d1 = RequestService.new('env', 'user', 'password')
req_obj = d1.getRequest('0000-0000')
d1.addAttachment(req_obj, 'file_location')

Seems like it is not working. 似乎无法正常工作。 d1.addAttachment is not taking req_obj . d1.addAttachment没有使用req_obj If I do d1.addAttachment('000-000', 'file_location'), then it works perfectly. 如果我执行d1.addAttachment('000-000','file_location'),则它可以完美运行。 if I use req_obj as an attribute inside addAttachment it fails with error: 如果我将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>'

code is: 代码是:

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

addAttachment is expecting a String not an object, you just need to call to_s method on req_object . addAttachment期望String不是对象,您只需要在req_object上调用to_s方法。 Ruby wont do that for you. Ruby不会为您这样做。

you can use "requests/#{req_id}/attachments" instead of using 'requests/' + req_id + '/attachments' . 您可以使用"requests/#{req_id}/attachments"代替使用'requests/' + req_id + '/attachments' And maybe you should overwrite the to_s method. 也许您应该覆盖to_s方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM