1

バックグラウンドでファイルを処理するために resque を使用しています。これは CSV ファイルですが、次のエラーが表示されます。uninitialized constant ImportFileHelper::CSV

私はしようとしましたがrequire 'csv'include CSVどちらもうまくいきません。

require 'csv'

module ImportFileHelper  
  HOST = ""
  USER_NAME = ""
  PASSWORD = ""

  def self.process_file(file_data, file_name)
    init
    @file_name = file_name
    begin
      csv = CSV.parse(file_data, :headers => true)
      csv.each do |row|
        @first_name = row["FirstName"]
        @last_name = row["LastName"]
        @email = row["Email"]
        @password = "ch@ngeM3!"
        @user_group_name = row["GroupName"].split(",")
        @store_name = row["StoreName"]
        @external_id = row["ExternalID"]

        add_user unless @first_name.nil? || @last_name.nil? || @email.nil? || @password.nil? || @first_name.empty? || @last_name.empty? || @email.empty?
      end
    rescue NoMethodError => no_method_error
      log_error_to_db no_method_error
    rescue IOError => error
      log_error_to_db error
      @errors << error.to_s
    rescue Exception => ex
      log_error_to_db ex
    end
    prep_soap_responses_for_output
  end

  def self.init
    HTTPI.log = false
    @body = { username: USER_NAME, password: PASSWORD }
    @errors = []
    @existing_users = []

    configure_savon
    get_all_groups
    get_all_stores
  end

  def self.prep_soap_responses_for_output
    [@existing_users, @errors]
  end

  def self.log_error_to_db(error)
    error.backtrace ||= "Not Available"
    if error.message.length > 250
      error_message = "There was an error"
    else
      error_message = error.message
    end
    ErrorLog.create(message: error_message, trace: error.backtrace, file_name: @file_name)
  end

  def self.get_store_id
    @store_id = @stores[@store_name.to_sym]
  end

  def self.get_all_stores
    @stores = {  }
    client = Savon::Client.new(HOST + "Storews.asmx?wsdl")
    body_data = { mall_id: 1, is_return_offline_store: :false }
    @body.merge! body_data

    begin
      response = client.request :get_store_list, body: @body

      if response
        hash_response = response.to_hash
        stores = hash_response[:get_store_list_response][:get_store_list_result][:store]

        stores.each do |s|
          store = { s[:name].to_sym => s[:store_id] }
          @stores.merge! store
        end
      end
    rescue Savon::Error => ex
      log_error_to_db error
      @errors << error.to_s
    end
  end

  def self.create_adbuilder_user_object
    AdbuilderUser.new(@first_name, @last_name, @email, @user_id, @store_id, @store_name, @user_group_name, @group_id, @external_id)
  end

  def self.configure_savon
    Savon.configure do |configure|
      configure.log = false
    end
  end

  def self.add_user
    body_data = { first_name: @first_name, last_name: @last_name, user_password: @password, email: @email, external_id: @external_id }
    @body.merge! body_data
    begin
      client = Savon::Client.new(HOST + "UserWS.asmx?wsdl")
      response = client.request :add_user, body: @body
      if response
        @user_id = response.body[:add_user_response][:add_user_result]

        if @user_group_name
          get_group_id
        end

        if @store_name
          @store_id = get_store_id
          unless @store_id.to_s =~ /^0$/
            adbuilder_user = create_adbuilder_user_object
            UserMailer.create_password(adbuilder_user).deliver if adbuilder_user
          end
        end
      end
    rescue Savon::Error => error
      log_error_to_db error

      if error.message == "(soap:Client) 3: A user with the same email login already exists. Please choose a different login."
        @existing_users << @email
      else
        @errors << error.to_s
      end
    rescue Exception => error
      log_error_to_db error
      @errors << error.message.to_s
    end
  end

  def self.get_group_id
    begin
      @user_group_name.each do |group_name|
        user_group_id = @groups_info[group_name.downcase.to_sym]
        add_user_to_group user_group_id if user_group_id
      end
    rescue Exception => error
      log_error_to_db error
      @errors << error.message.to_s
    end
  end

  def self.get_all_groups
    @groups_info = {}
    begin
      client = Savon::Client.new(HOST + "Usergroupws.asmx?wsdl")
      response = client.request :get_user_group_list, body: @body
      if response
        group = response.to_hash
        groups = group[:get_user_group_list_response][:get_user_group_list_result][:user_group]

        groups.each do |g|
          new_hash = { g[:name].gsub(/\s/, "_").downcase.to_sym => g[:user_group_id] }
          @groups_info.merge! new_hash
        end
      end
    rescue Savon::Error => error
      log_error_to_db
      @errors << error.to_s
    end
  end

  def self.add_user_to_group(group_id)
    body_data = { user_id: @user_id, user_group_id: group_id }
    @body.merge! body_data
    begin
      client = Savon::Client.new(HOST + "Usergroupws.asmx?wsdl")
      response = client.request :add_user_to_group, body: @body
    rescue Savon::Error => error
      log_error_to_db error
      @errors << error.to_s
    end
  end
end

したがって、これを回避するために、resque ジョブ ファイルで csv 解析を行っています。これで実行できるようになりました。ただし、これが最善の方法であるかどうかはわかりません。

class ProcessFile
  @queue = :rts_file_parser

  def self.perform(file_data, file_name)
    csv = CSV.parse(file_data, :headers => true)
    csv.each do |row|
      row_data = { first_name: row["FirstName"], last_name: row["LastName"], email: row["Email"], password: "ch@ngeM3!", user_group_name: row["GroupName"].split(","), store_name: row["StoreName"], external_id: row["ExternalID"] }

      ImportFileHelper.process_file file_name, row_data
    end
  end
end
4

2 に答える 2

2

(私のコメントを介して)答えを主張してもよろしいですか?

スコープ解決の問題のようです。

::CSVの代わりに試してくださいCSV

于 2013-10-11T20:58:55.510 に答える