2016-08-11 3 views
0

내 레일 애플리케이션에서 수신 이메일을 수신하고 특정 방식으로 구문 분석 할 수있는 기능이 필요합니다.Rails 이메일 구문 분석

incoming_controller.rb

class IncomingController < ApplicationController 
    skip_before_action :verify_authenticity_token, only: [:create] 
    skip_before_action :authenticate_user!, only: [:create] 
    def create 
    # Find the user 
    user = User.find_by(email: params[:sender]) 

    # Find the topic 
    topic = Topic.find_by(title: params[:subject]) 

    # Assign the url to a variable after retreiving it from 
    url = params["body-plain"] 

    # If the user is nil, create and save a new user 
    if user.nil? 
     user = User.new(email: params[:sender], password: "password") 
     user.save! 
    end 

    # If the topic is nil, create and save a new topic 
     if topic.nil? 
     topic = Topic.new(title: params[:subject], user: user) 

     topic.save! 
     end 

     bookmark = topic.bookmarks.build(user: user, url: url, description: "bookmark for #{url}") 

     bookmark.save! 

    # Assuming all went well. 
    head 200 
    end 
end 

나는 단지 3 개 값 = 사용자 추출 할 수있는이 컨트롤러를 사용하여 : 보낸 사람, 주제 : 제목과 URL을 "몸 일반".

전자 메일에 4 번째 값을 추가하여 a : 설명을 구문 분석 할 수 있습니까?

답변

1

params[:description] 구현은 이론적으로 당신의 방법에 사용되는 다른 params 항목과 동일한 작동합니다, 당신은 단지 확인해야 당신의 IncomingController#create 조치가 :description PARAM을 보내는 호출 어떤 것을.

컨트롤러 동작을 호출하는 매개 변수를 추가 할 수없는 경우 url에 현재 사용중인 params['body-plain']에 매개 변수를 추가 할 수 있습니까? 당신은 예를 들어, (사용 YAML) 직렬화 된 텍스트 형식을 사용하여 전자 메일 본문에서 여러 필드를 저장할 수 : 컨트롤러에서 다음

url: http://example.com 
description: I'm a description 

를, 당신은 다음과 같이 해당 필드를 구문 분석하는 것 :

class IncomingController < ApplicationController 
    require 'yaml' 
    def create 
    # ... 
    body_params = YAML.load(params['body-plain']) 
    url = body_params[:url] 
    description = body_params[:description] 
    # ... 
    end 
end 
+0

설명으로 인식 될 수 있도록 전자 메일의 텍스트를 어떻게 설정합니까? 예를 들어 전자 메일의 제목을 주제와 같게 설정합니다. 설명과 동일하게 이메일에서 무엇을 설정할 수 있습니까? – davefogo

+0

그건 컨트롤러를 호출하는 것에 달려 있습니다 ... 전자 메일을 해싱으로 구문 분석하고 컨트롤러 작업에 제출하는 설정을받는 전자 메일이 있다고 가정합니다. 이를 제어 할 수 있으면 해시에 param을 추가 할 수 있습니다. 그렇게 할 수 없다면 편집 된 답변에서 설명한 것처럼 이메일 본문에 추가 할 수 있습니다. – rmhunter

+0

당신이 대답에서 설명한 방법을 시도하고 있지만 url과 description은 params에 의해 선택되지 않습니다. 둘 다 nil로 읽습니다. – davefogo