Using url_helpers inside lib class in rails 4

My rails application uses the lib class to receive emails. The email receiver class parses the email message and sends it to the application controller using an HTTP response. The code for this is shown like this:

  uri = 'http://localhost:3000/incoming_mail'
  body = {'from'=> from, 'to' => to, 'subject' => subject, 'message' => message}

  response = Net::HTTP::post_form(URI.parse(uri), body)

      

The problem is I don't want to give the full URL. Is there a way to use "incoming_mail_path" instead of "localhost: 3000 / incoming_mail"? I tried to add:

include Rails.application.routes.url_helpers

      

But that doesn't work and gives the following error:

 <class:EmailReceiver>': uninitialized constant EmailReceiver::Rails (NameError)

      

Can anyone suggest a solution for this.

I am posting the whole class here (updated class with include statement):

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'=> from, 'to' => to, 'subject' => subject, 'message' => message}

      response = Net::HTTP::post_form(URI.parse(uri), body)

    end
  end


end

handler = EmailReceiver.new
handler.submit(STDIN.read)

      

+3


source to share


2 answers


Debugging:

After reading your comments, I realized that you are using it as a ruby ​​script that Rails doesn't even recognize.

Before you figure out how to include all the file requirements. I tried to run the file through rails environment (while the server was already running):

cat sample.email | bundle exec rails runner "eval(File.read 'lib/email_receiver.rb')"

      

I got error for incoming_mail_url

:

 Missing host to link to! Please provide the :host parameter, 
set default_url_options[:host], or set :only_path to true (ArgumentError)

      

incoming_mail_path

Completed successfully so far /incoming_mail

(this is not what you want).




Output:

This means that whatever you do, as long as you don't run the file from the server (like initializers), then the host will never exist.

When you run this module from your server, it will loop route

through url_helpers

which you enabled.


Alternative suggestion:

The Griddler gem is a Rails engine that provides an endpoint for services that convert incoming emails to HTTP POST requests. It parses those POSTs and gives the inline email object to the class you implemented.

+1


source


To solve your immediate problem, you can try to disable the include statement. Right now, the interpreter seems to think "Rails" is a class in the EmailReceiver namespace. if you add :: to the include statement, it should refer to "Rails" at the top level you want.

It should work, but there might be something else wrong in your setup that forces you to use this otherwise unnecessary workaround



Edit What I meant by adding "::" to clarify

include ::Rails.application.routes.url_helpers

      

0


source







All Articles