2017-11-08 2 views
1

나는이 이메일 링크가 있습니다. uri.tomailto 링크에서 본문과 제목을 찾는 방법은 무엇입니까? 내가 <code>to</code>, <code>subject</code>, <code>body</code>을 찾고 싶은</p> <p><code>mailto:[email protected]?&subject=test&body=type%20your&body=message%20here</code></p> <p>:

하지만 난 subjectbody을 추출 할 수 없습니다 : 그와에 :

uri = URI('mailto:[email protected]?&subject=test&body=type%20your&body=message%20here') 

<URI::MailTo mailto:[email protected]?&subject=test&body=type%20your&body=message%20here>

내가 가진 :

는 사실은 내가 사용합니다.

어떻게하는지 알고 계십니까?

답변

3

당신은 배열의 배열을 반환 URI::MailTo#headers를 사용할 수 있습니다

uri.headers 
#=> [[], ["subject", "test"], ["body", "type%20your"], ["body", "message%20here"]] 

그러나 귀하의 이메일 링크가 약간 깨진. 그것은 다음과 같아야합니다 준다

uri = URI('mailto:[email protected]?subject=test&body=type%20your%0D%0Amessage%20here') 
#         ^      ^
#        no '&' here    newline as %0D%0A 

:

headers = uri.headers.to_h 
#=> {"subject"=>"test", "body"=>"type%20your%0D%0Amessage%20here"} 
:

uri.headers.assoc('subject').last 
#=> "test" 

또는 해시로 변환 : assoc을 통해 액세스 할 수 있습니다

uri.headers 
#=> [["subject", "test"], ["body", "type%20your%0D%0Amessage%20here"]] 

디코딩 된 값 :

URI.decode_www_form_component(headers['body']) 
#=> "type your\r\nmessage here" 
+0

은 내가 순간을 위해 그것을 할 방법은, 실제로 당신의 대답 주셔서 감사하지만 추한와 나는 형식에 따라 매우 불안정 생각합니다. Deplus, 우리는 훌륭한 도구 (URI)를 가지고 있으며 더 간단하다고 생각합니다. 나는'uri.subject'와'uri.body'을 할 수 있기를 원합니다. –

+0

@HugoBarthelemy'URI'는'decode_www_form_component'를 제공하지만 당신이 직접 호출해야한다고 생각합니다. 그에 따라 내 대답을 업데이트했습니다. – Stefan

+0

감사합니다 @ 스탄;) –