title: Using url_helpers inside lib class in rails 4
body: <p>In my rails app a lib class is used to recieve emails. The email reciever class parses the email and post it to a application controller using a HTTP response. The code for this is as shown :</p>
  
  <pre><code>  uri = 'http://localhost:3000/incoming_mail'
    body = {'from'=&gt; from, 'to' =&gt; to, 'subject' =&gt; subject, 'message' =&gt; message}
  
    response = Net::HTTP::post_form(URI.parse(uri), body)
  </code></pre>
  
  <p>The problem is I don't want to specify the complete URL. Is there any way to use 'incoming_mail_path' instead of 'localhost:3000/incoming_mail' ?
  I tried to add :</p>
  
  <pre><code>include Rails.application.routes.url_helpers
  </code></pre>
  
  <p>But this is not working and gives the following error :</p>
  
  <pre><code> &lt;class:EmailReceiver&gt;': uninitialized constant EmailReceiver::Rails (NameError)
  </code></pre>
  
  <p>Can anyone please suggest a solution for this.</p>
  
  <p>I am posting the entire class here(Updated the class with include statement):</p>
  
  <pre><code>require 'mail'
  require 'net/https'
  require 'net/http'
  require 'uri'
  
  
  
  
  class EmailReceiver 
  
    include Rails.application.routes.url_helpers
    attr_accessor :url
    def initialize
    end
  
    def submit(content)
      mail    = Mail.read_from_string(content)
      body    = mail.body.decoded
      from    = mail.from.first
      to      = mail.to.first
      subject = mail.subject
  
  
      if mail.multipart?
        part = mail.parts.select { |p| p.content_type =~ /text\/plain/ }.first rescue nil
        unless part.nil?
          message = part.body.decoded
        end
      else
        message = mail.decoded
      end
  
      unless message.nil?
  
        uri = incoming_mail_url
        #uri = 'http://localhost:3000/incoming_mail'
        body = {'from'=&gt; from, 'to' =&gt; to, 'subject' =&gt; subject, 'message' =&gt; message}
  
        response = Net::HTTP::post_form(URI.parse(uri), body)
  
      end
    end
  
  
  end
  
  handler = EmailReceiver.new
  handler.submit(STDIN.read)
  </code></pre>