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 を使用するインライン バージョンを使用し、登録コントローラー (devise で動作) は次のようになります。

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 を使用するには 2 つの異なる方法があり、両方を使用しようとしています。一つを選ぶ :)。

クラスベースのメソッドは次のようになります。

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 に答える