<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rails</title>
	<atom:link href="http://abhaynikam.blog.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://abhaynikam.blog.com</link>
	<description>Writing away with Blog.com</description>
	<lastBuildDate>Sat, 12 Mar 2016 10:27:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2-bleeding</generator>
	<atom:link rel='hub' href='http://abhaynikam.blog.com/?pushpress=hub'/>
		<item>
		<title>ROR: Sending mail via MailGun</title>
		<link>http://abhaynikam.blog.com/2016/03/12/ror-sending-mail-via-mailgun/</link>
		<comments>http://abhaynikam.blog.com/2016/03/12/ror-sending-mail-via-mailgun/#comments</comments>
		<pubDate>Sat, 12 Mar 2016 10:27:49 +0000</pubDate>
		<dc:creator>nikam.abhay1</dc:creator>
		
		<guid isPermaLink="false">http://abhaynikam.blog.com/?p=12</guid>
		<description><![CDATA[Introduction: Send mail is the basic requirement of every web application. To start with setting up mailer for your ruby on rails project using Mailgun we should have a account of Mailgun to deliver messages. To use Mailgun for sending mail &#8230; <a href="http://abhaynikam.blog.com/2016/03/12/ror-sending-mail-via-mailgun/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Introduction:</h2>
<p>Send mail is the basic requirement of every web application. To start with setting up mailer for your ruby on rails project using <strong>Mailgun </strong>we should have a account of Mailgun to deliver messages.</p>
<ul>
<li>To use Mailgun for sending mail visit: <a href="http://www.mailgun.com/">http://www.mailgun.com/</a> and create a account for your business requirement.</li>
</ul>
<h2>Creating a demo project:</h2>
<p style="padding-left: 30px">Creating a sample project to setup mailer:</p>
<blockquote>
<p style="padding-left: 30px">rails new sample_app</p>
<p style="padding-left: 30px"><span class="line"> rails g scaffold user name:string email:string </span></p>
<p style="padding-left: 30px"><span class="line">rake db:migrate</span></p>
</blockquote>
<p style="padding-left: 30px">now we have are web app ready to setup Action Mailer</p>
<h2 id="walkthrough-to-generating-a-mailer">Walkthrough to Generating a Mailer:</h2>
<p style="padding-left: 30px">To create a mailer:</p>
<blockquote>
<div class="line number1 index0 alt2"><code>rails generate mailer UserMailer</code></div>
<div class="line number2 index1 alt1"><code>create  app/mailers/user_mailer.rb</code></div>
<div class="line number3 index2 alt2"><code>create  app/mailers/application_mailer.rb</code></div>
<div class="line number4 index3 alt1"><code>invoke  erb</code></div>
<div class="line number5 index4 alt2"><code>create    app/views/user_mailer</code></div>
<div class="line number6 index5 alt1"><code>create    app/views/layouts/mailer.text.erb</code></div>
<div class="line number7 index6 alt2"><code>create    app/views/layouts/mailer.html.erb</code></div>
<div class="line number8 index7 alt1"><code>invoke  test_unit</code></div>
<div class="line number9 index8 alt2"><code>create    test/mailers/user_mailer_test.rb</code></div>
<div class="line number10 index9 alt1"><code>create    test/mailers/previews/user_mailer_preview.rb</code></div>
</blockquote>
<div class="line number10 index9 alt1" style="padding-left: 30px">Edit mailer file and a method to send mail to user:</div>
<blockquote>
<div class="line number10 index9 alt1" style="padding-left: 30px">
<div class="line number1 index0 alt2"><code>class</code><code>UserMailer &lt; ApplicationMailer</code></div>
<div class="line number1 index0 alt2">     # NOTE:  Add default from mailer id</div>
<div class="line number2 index1 alt1"><code>  </code><code>default from: </code><code>'notifications@example.com'</code></div>
<div class="line number4 index3 alt1"><code>  </code><code>def </code><code>welcome_email(user)</code></div>
<div class="line number7 index6 alt2"><code>    </code><code>mail(to: </code><code>user</code><code>.email, subject: </code><code>'Welcome to My Awesome Site'</code><code>)</code></div>
<div class="line number8 index7 alt1"><code>  </code><code>end</code></div>
<div class="line number9 index8 alt2"><code>end</code></div>
</div>
</blockquote>
<div class="line number10 index9 alt1" style="padding-left: 30px">now create a view for mailer , this template will used for the email, formatted in HTML.</div>
<blockquote>
<div class="line number10 index9 alt1" style="padding-left: 30px">File: app/views/user_mailer/welcome_email_view.html.erb</div>
<div class="line number10 index9 alt1" style="padding-left: 30px">
<div class="line number1 index0 alt2"><code>&lt;!DOCTYPE html&gt;</code></div>
<div class="line number2 index1 alt1"><code>&lt;html&gt;</code></div>
<div class="line number3 index2 alt2"><code>  </code><code>&lt;head&gt;</code></div>
<div class="line number4 index3 alt1"><code>    </code><code>&lt;meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /&gt;</code></div>
<div class="line number5 index4 alt2"><code>  </code><code>&lt;/head&gt;</code></div>
<div class="line number6 index5 alt1"><code>  </code><code>&lt;body&gt;</code></div>
<div class="line number7 index6 alt2"><code>    </code><code>&lt;h1&gt;Welcome to example.com, &lt;%= @user.name %&gt;&lt;/h1&gt;</code></div>
<div class="line number8 index7 alt1"><code>    </code><code>&lt;p&gt;</code></div>
<div class="line number9 index8 alt2"><code>      </code><code>You have successfully signed up to example.com,</code></div>
<div class="line number10 index9 alt1"><code>      </code><code>your username is: &lt;%= @user.login %&gt;.&lt;br&gt;</code></div>
<div class="line number11 index10 alt2"><code>    </code><code>&lt;/p&gt;</code></div>
<div class="line number12 index11 alt1"><code>    </code><code>&lt;p&gt;</code></div>
<div class="line number13 index12 alt2"><code>      </code><code>To login to the site, just follow this link: &lt;%= @url %&gt;.</code></div>
<div class="line number14 index13 alt1"><code>    </code><code>&lt;/p&gt;</code></div>
<div class="line number15 index14 alt2"><code>    </code><code>&lt;p&gt;Thanks for joining and have a great day!&lt;/p&gt;</code></div>
<div class="line number16 index15 alt1"><code>  </code><code>&lt;/body&gt;</code></div>
<div class="line number17 index16 alt2"><code>&lt;/html&gt;</code></div>
</div>
</blockquote>
<div class="line number10 index9 alt1" style="padding-left: 30px">now we are all set to send mail. We need a method where we can trigger or send mail.</div>
<blockquote>
<div class="line number10 index9 alt1" style="padding-left: 30px">
<div class="line number1 index0 alt2"><code>class</code><code>UsersController &lt; ApplicationController</code></div>
<div class="line number2 index1 alt1"><code>  </code><code># POST /users</code></div>
<div class="line number3 index2 alt2"><code>  </code><code># POST /users.json</code></div>
<div class="line number4 index3 alt1"><code>  </code><code>def</code><code>create</code></div>
<div class="line number5 index4 alt2"><code>    </code><code>@user</code><code>= User.</code><code>new</code><code>(params[</code><code>:user</code><code>])</code></div>
<div class="line number7 index6 alt2"><code>    </code><code>respond_to </code><code>do</code><code>|format|</code></div>
<div class="line number8 index7 alt1"><code>      </code><code>if</code><code>@user</code><code>.save</code></div>
<div class="line number9 index8 alt2"><code>        </code><code># Tell the UserMailer to send a welcome email after save</code></div>
<div class="line number10 index9 alt1"><code>        </code><code>UserMailer.welcome_email(</code><code>@user</code><code>).deliver_now</code></div>
<div class="line number12 index11 alt1"><code>        </code><code>format.html { redirect_to(</code><code>@user</code><code>, notice: </code><code>'User was successfully created.'</code><code>) }</code></div>
<div class="line number13 index12 alt2"><code>        </code><code>format.json { render json: </code><code>@user</code><code>, status: </code><code>:created</code><code>, location: </code><code>@user</code><code>}</code></div>
<div class="line number14 index13 alt1"><code>      </code><code>else</code></div>
<div class="line number15 index14 alt2"><code>        </code><code>format.html { render action: </code><code>'new'</code><code>}</code></div>
<div class="line number16 index15 alt1"><code>        </code><code>format.json { render json: </code><code>@user</code><code>.errors, status: </code><code>:unprocessable_entity</code><code>}</code></div>
<div class="line number17 index16 alt2"><code>      </code><code>end</code></div>
<div class="line number18 index17 alt1"><code>    </code><code>end</code></div>
<div class="line number19 index18 alt2"><code>  </code><code>end</code></div>
<div class="line number20 index19 alt1"><code>end</code></div>
</div>
</blockquote>
<div class="line number20 index19 alt1"></div>
<h2 class="line number20 index19 alt1">Send mail using Gmail:</h2>
<p class="line number20 index19 alt1" style="padding-left: 30px">To send mail using gmail we should  have set some configuration. Before we proceed we need to save sensitive information such as username and password as environment variables. We will do so by using the gem <code>figaro.</code></p>
<blockquote>
<p class="line number20 index19 alt1" style="padding-left: 30px"># File: /config/application.yml</p>
<p class="line number20 index19 alt1" style="padding-left: 30px"><span class="line"><span class="n">username</span><span class="p">:</span> <span class="s1">&#8216;username@gmail.com&#8217;</span> </span></p>
<p class="line number20 index19 alt1" style="padding-left: 30px"><span class="line"><span class="n">password</span><span class="p">:</span> <span class="s1">&#8216;Gmail password&#8217;</span></span></p>
</blockquote>
<p class="line number20 index19 alt1" style="padding-left: 30px">Set config to send mail using SMTP:</p>
<blockquote>
<div class="container">
<div class="container">
<div class="line number1 index0 alt2"># File: /config/environments/production.rb</div>
<div class="line number1 index0 alt2"></div>
<div class="line number1 index0 alt2"><code>config.action_mailer.delivery_method = </code><code>:smtp</code></div>
<div class="line number2 index1 alt1"><code>config.action_mailer.smtp_settings = {</code></div>
<div class="line number3 index2 alt2"><code>  </code><code>address:              </code><code>'smtp.gmail.com'</code><code>,</code></div>
<div class="line number4 index3 alt1"><code>  </code><code>port:                 </code><code>587</code><code>,</code></div>
<div class="line number5 index4 alt2"><code>  </code><code>domain:               </code><code>'example.com'</code><code>,</code></div>
<div class="line number6 index5 alt1"><code>  </code><code>user_name:            ENV['username']</code><code>,</code></div>
<div class="line number7 index6 alt2"><code>  </code><code>password:             </code><code>ENV['password']</code><code>,</code></div>
</div>
</div>
<div class="container">
<div class="line number8 index7 alt1"><code>  </code><code>authentication:       </code><code>'plain'</code><code>,</code></div>
<div class="line number9 index8 alt2"><code>  </code><code>enable_starttls_auto: </code><code>true</code></div>
<div class="line number9 index8 alt2"><code>}</code></div>
</div>
</blockquote>
<div class="container">
<div class="line number9 index8 alt2">When a new user is created we are sending out an email via the<span style="font-family: monospace"> welcome_email</span> method in mailer User<code>Mailer.</code></div>
</div>
<div class="line number9 index8 alt2"></div>
<h2 class="line number9 index8 alt2">Sending Email via MailGun:</h2>
<p>As discussed above sending mail via mailgun require a mailgun account and its credentials so that we can send mail via mailgun smtp. <a href="https://mailgun.com/">Create a account with mailgun</a> and we are all set to send mail.</p>
<blockquote><p># File: /config/application.yml</p>
<p><span class="line"><span class="n">api_key</span><span class="p">:</span> <span class="s1">&#8216;API Key&#8217;</span> </span></p>
<p><span class="line"><span class="ss">domain</span><span class="p">:</span> <span class="s1">&#8216;Domain&#8217;</span> </span></p>
<p><span class="line"><span class="ss">username</span><span class="p">:</span> <span class="s1">&#8216;Default SMTP Login&#8217;</span> </span></p>
<p><span class="line"><span class="ss">password</span><span class="p">:</span> <span class="s1">&#8216;Default Password&#8217;</span> </span></p>
<p><span class="line"><span class="n">username</span><span class="p">:</span> <span class="s1">&#8216;username@gmail.com&#8217;</span> </span></p>
<p><span class="line"><span class="n">password</span><span class="p">:</span> <span class="s1">&#8216;gmail password&#8217;</span></span></p></blockquote>
<p>Now add config for mailgun:</p>
<blockquote><p>config.action_mailer.delivery_method = :smtp<br />
# SMTP settings for mailgun<br />
config.action_mailer.smtp_settings = {<br />
port:                       587,<br />
address:                &#8221;smtp.mailgun.org&#8221;,<br />
domain:                 ENV['domain'],<br />
user_name:         ENV['username'],<br />
password:             ENV['password'],<br />
authentication:  :plain,<br />
}</p></blockquote>
<p>&nbsp;</p>
<h2>Sending mail via Mailgun API:</h2>
<p style="padding-left: 30px">We need to first add a gem for using mailgun API:</p>
<blockquote><p># NOTE: File: gemfile.rb</p>
<p>gem &#8216;mailgun-ruby&#8217;, &#8216;~&gt;1.0.2&#8242;, require: &#8216;mailgun&#8217;</p>
<p>bundle install</p></blockquote>
<p>Setup welcome method for sending mail via API:</p>
<blockquote><p>def sample_email(user)<br />
@user = user<br />
mg_client = Mailgun::Client.new ENV['api_key']<br />
message_params = { from: ENV['username'],<br />
to: @user.email,<br />
subject: &#8216;Sample Mail using Mailgun API&#8217;,<br />
text: &#8216;This mail is sent using Mailgun API via mailgun-ruby&#8217;</p>
<p>}<br />
mg_client.send_message ENV['domain'], message_params<br />
end</p></blockquote>
<p>&nbsp;</p>
<h2>Conclusion:</h2>
<p>We have setup ActionMailer to send welcome mail via gmail, mailgun and mailgun API. Now are basic mailer is setup and we are good to send mail.</p>
]]></content:encoded>
			<wfw:commentRss>http://abhaynikam.blog.com/2016/03/12/ror-sending-mail-via-mailgun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ROR: Setting up devise and OmniAuth for Facebook</title>
		<link>http://abhaynikam.blog.com/2016/03/05/ror-setting-up-devise-and-omniauth-for-facebook/</link>
		<comments>http://abhaynikam.blog.com/2016/03/05/ror-setting-up-devise-and-omniauth-for-facebook/#comments</comments>
		<pubDate>Sat, 05 Mar 2016 13:05:09 +0000</pubDate>
		<dc:creator>nikam.abhay1</dc:creator>
		
		<guid isPermaLink="false">http://abhaynikam.blog.com/?p=9</guid>
		<description><![CDATA[Introduction I am a newbie to ruby on rails and I learned how to authenticate a user using devise and oauth so I thought i could share this with all of you. Before starting with how to start with user &#8230; <a href="http://abhaynikam.blog.com/2016/03/05/ror-setting-up-devise-and-omniauth-for-facebook/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2><span style="text-decoration: underline"><em><strong>Introduction</strong></em></span></h2>
<blockquote><p>I am a newbie to ruby on rails and I learned how to authenticate a user using devise and oauth so I thought i could share this with all of you. Before starting with how to start with user authentication using oauth for Facebook. We have some prerequisite that you should now and have in your system.</p>
<ul>
<li>You should have ROR setup in your system(obviously you do have that why you here) and if you don&#8217;t visit <a href="https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-on-ubuntu-12-04-lts-precise-pangolin-with-rvm">https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-on-ubuntu-12-04-lts-precise-pangolin-with-rvm</a></li>
<li>For user authentication from facebook, you need to have you app registered on facebook (<a href="https://developers.facebook.com/">https://developers.facebook.com/</a>).</li>
</ul>
</blockquote>
<p><em><span style="text-decoration: underline"><strong>Creating new app</strong></span></em></p>
<blockquote>
<h3>First, create a rails app:</h3>
<p style="padding-left: 30px"><code>rails new oauth_demo</code></p>
<h3>You need to add oauth and devise gem to application:</h3>
<p style="padding-left: 30px"><code><br />
# Devise gem<br />
gem 'devise'<br />
# OmniAuth gem<br />
gem 'omniauth'<br />
gem 'omniauth-facebook'<br />
</code></p>
<h3>Install the gem by:</h3>
<p style="padding-left: 30px"><code>bundle install</code></p>
<p>Okay, now you are set to start to setup your user authentication using devise.</p></blockquote>
<h2><em><span style="text-decoration: underline"><strong>Setting up devise with oauth</strong></span></em></h2>
<blockquote><p>To start with we need to install devise and create some view for devise(devise has a generators which build this file in no time for you).</p>
<p style="padding-left: 30px"><code><br />
rails generate devise:install<br />
rails generate devise user<br />
rake db:migrate<br />
rails generate devise:views users</code></p>
<p style="padding-left: 30px"><code><br />
</code></p>
<p>Add  <code>apps/controller/application_controller.rb </code>before filter to authenticate user by devise if (s)he is logged in the system.</p>
<p style="padding-left: 30px"><code>before_filter :authenticate_user!</code></p>
<p>Run the rails server using <code>rails s</code> and hit <a>http://localhost:3000/users/sign_in</a> and check whether you sign in form or not.</p>
<h2><span style="text-decoration: underline"><em><strong>Now, OAUTH</strong></em></span></h2>
<p>For oauth using facebook, as mentioned above you need to register your project to facebook.<br />
In <code>config/devise.rb</code> you need to add:</p>
<p style="padding-left: 30px"><code> config.omniauth :facebook, 'APP_ID', 'APP_SECRET'<br />
</code></p>
<p>Adding callback after successfully signed in. After sign in we have to add a callback methods which would be added in CallbackController. For that we need to change the routes to:</p>
<p style="padding-left: 30px"><code> devise_for :users, controllers: { omniauth_callbacks: 'callbacks' }</code></p>
<p>This callback will basically get a auth json from facebook which we&#8217;ll have the required data need for authentication.</p>
<p>create a <code>apps/controller/callbacks_controller.rb</code> to handle the callback from facebook where you get the json from facebook.</p>
<p style="padding-left: 30px"><code> class CallbacksController &lt; Devise::OmniauthCallbacksController<br />
def facebook<br />
@user = User.from_omniauth(request.env["omniauth.auth"])<br />
sign_in_and_redirect @user<br />
end<br />
end</code></p>
<p>Now using that json we can create a user</p>
<p style="padding-left: 30px"><code><br />
def self.from_omniauth(auth)<br />
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|<br />
user.provider = auth.provider<br />
user.uid = auth.uid<br />
user.email = auth.info.email<br />
user.password = Devise.friendly_token[0,20]<br />
end<br />
end<br />
</code></p>
</blockquote>
<pre><strong>Start your rails server and your good to go</strong></pre>
<h2><em><span style="text-decoration: underline"><strong>Conclusion</strong></span></em></h2>
<blockquote><p>We can have authentication using facebook, twitter, etc. so that user can log in with any of the social sites. If we are using twitter, facebook, etc. We need to care of few things like user can log in first using facebook and next time using something else. We need to manage that.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://abhaynikam.blog.com/2016/03/05/ror-setting-up-devise-and-omniauth-for-facebook/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
