We are working on changing a Rails 3.2 app to use the Mailgun http api for handling email campaigns. Currently we are using ActionMailer for building out our templates and sending over smtp but since we want to use some of the more advanced features offered by Mailgun we would rather not deal with all the MIME headers and instead use the REST api.
To render our haml template as a string for the HTTP POST it looks like we either need to use some sort of controller or commit a 'design smell' and invoke the action view from within a model. Being that the email will be executed by a background worker it doesn't really make sense to call it from a controller; is there some other method that we are missing?
I wrote a Rails Action Mailer adapter for mailgun. This means you can use the standard Rails approach for sending emails and you don't have to invent a custom wrapper for doing it.
Regards
From Haml doc:
template = File.read('path_to/your_haml_file')
haml_engine = Haml::Engine.new(template)
your_variables = { }
html = haml_engine.render(Object.new, your_variables)
Related
This question already has answers here:
Where do API calls go in a ruby on rails MVC framework project?
(3 answers)
Closed 7 years ago.
My task is to take some data from users. The next part is to make permanent requests to an API of a third party site and to process responses from it. I don't know where this part should be placed: model, controller, or module? The final part of the app will send statuses to users' emails.
Processing user input from an HTTP request is usually done in the controller.
Send a request to the rails server including the user input.
The request will be routed to the appropriate controller action. In the controller action, form an HTTP request to an external API and include the user input in the request using something like RestClient.
Finally, you will send an email to the user and include the request statuses by calling the deliver! method on a mailer class.
Example:
class UsersController < ApplicationController
def controller_action
#user_input = params[:query]
# Build the external API request URI.
# Using www.icd10api.com as an example.
url = Addressable::URI.new(
scheme: "http",
host: "www.icd10api.com",
query_values: {code: #user_input, r: "json", desc: "long"})
# Perform the external request and parse the response
resp = JSON.parse(RestClient.get(url.to_s))
# Finally, deliver the email.
UserMailer.statuses_email(resp).deliver!
# Return status code
render status: 200
end
end
You can always refactor your code into a module, but I only do this if it's used in 3+ locations.
If you're using this as more than a demo app, I would refer to the link in Andrew CP Kelley's comment:
Where do API calls go in a ruby on rails MVC framework project?
References:
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
You also might want to look into concerns if you're using rails 4+:
How to use concerns in Rails 4
I usually wrap it inside a module or a class and place the file into folder
app/models/services/
Here is the folder I place all things are kind of service, e.g. The logic to requests to API and proceed responses from it.
I'm a Rails noob, but I'd like to use it as a backend for an Ember application with Ember Data. Unfortunately, I have some unknown unknowns.
The RESTAdapter documentation says:
Comments for a post can be loaded by post.get('comments'). The REST
adapter will send a GET request to /comments?ids[]=1&ids[]=2&ids[]=3.
It will generate similar urls if you use something like App.Post.find({title: "Some Title"}), in about the format you'd expect: /posts?title=Some+Title
Is there some option, or gem I can use to handle that sort of simple query, or do I have to go parse parameters in my controllers manually?
To clarify, I'm aware that I can tell my Rails controller to return a set like:
#comments = Comment.find(params[:ids])
respond_with(#comments)
But it seems like querying on ids or accessible attributes like that would be a common enough use case for REST APIs that something would be built in, or have a gem written to handle it.
Can anyone point me in the right direction?
Thanks.
This might be helpful in your case:
https://github.com/ernie/ransack/
Or
https://github.com/ernie/squeel
My app wants to send an email using the custom mailers. I went through the doc http://grails.org/Mail+from+Grails which I find pretty incomplete. I followed the steps mentioned in the alternative mailer, but I get an exception
NullPointerException occurred when processing request: [GET] url/sendEmail
Cannot invoke method sendNewEmail() on null object.
My controller looks like this
XXXMailer paMailer
paMailer.sendNewEmail()
The web page you linked to is merely a proposal which may have never been implemented and hasn't been updated in 3 years. The most popular way to to send email from a Grails app is with the mail plugin. The docs are comprehensive, and it's very easy to use.
XXXMailer seems to be a service, or any other automatically wired thing. So, you have to define it as field, and only then used it from an method, like:
class MyController {
XXXMailer paMailer //now it will be filled with real instance
def myAction() {
paMailer.sendNewEmail()
}
}
I want to use ExtDirect for a 3rd party extjs user interface in Rails 3. So I have started to update the active-direct gem to work with Rails 3. Here is the updated version: https://github.com/stonegao/active-direct
At the moment my modified active direct plugin/gem works with models. I'm able to do this in JavaScript:
App.namespace.Project.all({params},callback_function);
That's great.
Now I want to use some special Rails 3 controllers (that act like a service).
In my Extdirect JS is this:
App.mynamespace.MyProject_Controller_V1_workspaceController.getStatus
This response also comes to my extjs router. No I want call this controller action and get the response.
I can't use #app.call(env) with a changed request_uri because I have no match in routes.rb
Is it possible to call this controller action
Specification of extdirect: http://tinyurl.com/4y3nc44
Thanks skeller1
yes, it is!
just do something like this:
status,headers,response=ProjectsController.action("index").call(#env)
so you can call the index method of the ProjectsController.
beware: ExtDirect accepts all types in the HTTP_ACCEPT http header field.
so you can do a
#env['HTTP_ACCEPT'] = "application/json"
before your request to an action to set your wanted response type.
Within Rails, how can I listen for new mail coming into the boxes of certain accounts. I want to implement a simple "post-by-email" service, which gets the new content of the email and makes a blog post or sth.
You could try this gem.
I wrote a post explaining some of the options. A later post also covered my way of testing these options. The mail gem is great for parsing. You just have to decide the best option for you to optain the messages.
http://steve.dynedge.co.uk/2010/09/07/incoming-email-in-rails-3-choosing-the-right-approach/
I use this code to parse my emails.
class Receiver < ActionMailer::Base
def self.parse(email)
reply_separator = /(.*?)\s?== ADD YOUR REPLY ABOVE THIS LINE ==/m
comment_text = reply_separator.match(email.body.to_s)
# ...
end
end
The email object here is just a Mail::Message object which I get from using the gmail gem to read an inbox. If you're not using GMail then you should be able to use plain ol' vanilla Mail gem to connect to the mail server and then get the Mail::Message objects that way.