2

我正在与一个团队合作,在他们注册 Web 应用程序时检查用户的电子邮件输入。如果使用 HTTParty 通过以下 API 调用未找到用户的电子邮件,则不允许用户注册。我们正在获取函数中首先出现的任何语法的 method_errors。例如,在下面的方法中,“include”作为未定义的方法错误出现。

 def email_checker
            include HTTParty   
            default_params :output => 'json'
            format :json
            base_uri 'app.close.io'
            basic_auth 'insert_api_code_here', ' '
            response = HTTParty.get('/api/v1/contact/')

        @email_database = []
        response['data'].each do |x|
          x['emails'].each do |contact_info|
              @email_database << contact_info['email']
            end
            end

            unless @email_database.include? :email 
              errors.add :email, 'According to our records, your email has not been found!'
            end

  end 

更新:所以我们使用 HTTParty 的内联版本,我们的注册控制器(使用设计)看起来像这样:

class RegistrationsController < Devise::RegistrationsController

      def email_checker(email)
        YAML.load(File.read('config/environments/local_env.yml')).each {|k, v|  ENV[k.to_s] = v}
        api_options = {
          query: => {:output => 'json'},
          format: :json,
          base_uri: 'app.close.io',
          basic_auth: ENV["API_KEY"], ' '
      }

        response = HTTParty.get('/api/v1/contact/', api_options)
        @email_database = []
        response['data'].each do |x| 
          x['emails'].each do |contact_info|
              @email_database << contact_info['email']
            end
        end

        unless @email_database.include? email 
            return false
        else
            return true 
        end
      end


    def create
        super 
        if email_checker == false 
            direct_to 'users/sign_up'
            #and return to signup with errors
        else 
            User.save!
        end
    end
end 

我们收到语法错误:“语法错误,意外 =>”我们搞砸了格式吗?

4

1 回答 1

1

使用 HTTParty 有两种不同的方法,您正在尝试同时使用这两种方法。选一个 :)。

基于类的方法看起来像这样:

class CloseIo
  include HTTParty   
  default_params :output => 'json'
  format :json
  base_uri 'app.close.io'
  basic_auth 'insert_api_code_here', ' '
end

class UserController
  def email_checker
    response = CloseIo.get('/api/v1/contact/')
    # ... the rest of your stuff
  end
end

内联版本看起来像这样

class UserController
  def email_checker
    api_options = {
      query: :output => 'json',
      format: :json,
      base_uri: 'app.close.io',
      basic_auth: 'insert_api_code_here'
    }
    response = HTTParty.get('/api/v1/contact/', api_options)
    # ... do stuff
  end
end
于 2014-08-12T00:31:40.067 回答