<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Ruby and Rails Blog</description><title>Ruby Italian Style</title><generator>Tumblr (3.0; @rubyitalianstyle)</generator><link>http://rubyitalianstyle.it/</link><item><title>Rails Phone Country Codes</title><description>&lt;p&gt;Today I needed to add a select box for the internationals phone country codes.
&lt;br/&gt;This is also known as &lt;a href="http://en.wikipedia.org/wiki/E.164"&gt;E.164&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;I wasn&amp;#8217;t able to find a library for that, except for: &lt;a href="https://github.com/hexorx/countries"&gt;https://github.com/hexorx/countries&lt;/a&gt; that I used as a base and extended this way:
&lt;/p&gt;&lt;pre&gt;&lt;code class="ruby"&gt;
c = ISO3166::Country::Data.map{ |_,v| v["country_code"].to_i }
codes = c.uniq.sort.map{ |v| "+#{v}" }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can use the array in an options_for_select helper&lt;/p&gt;</description><link>http://rubyitalianstyle.it/post/43178670420</link><guid>http://rubyitalianstyle.it/post/43178670420</guid><pubDate>Sat, 16 Feb 2013 00:00:25 +0100</pubDate><category>ruby</category><category>country_codes</category></item><item><title>Developer Love</title><description>&lt;img src="http://24.media.tumblr.com/c5f5a3556aac2cc495ac87fd366598a1/tumblr_mia8dr9z6U1rpitubo1_500.jpg"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;Developer Love&lt;/p&gt;</description><link>http://rubyitalianstyle.it/post/43175055249</link><guid>http://rubyitalianstyle.it/post/43175055249</guid><pubDate>Fri, 15 Feb 2013 23:13:03 +0100</pubDate><category>developer</category><category>love</category><category>ruby</category></item><item><title>Paypal Recurring Payments</title><description>&lt;p&gt;There is a time when every SaaS need to get his money, and maybe you choose to get them month by month.&lt;/p&gt;
&lt;p&gt;This is a post about the steps that I took to integrate Paypal recurring payments, and the problems that I run into.&lt;/p&gt;

&lt;h1 style="margin:20px 0  10px;"&gt;Part 1. Find the right library&lt;/h1&gt;

&lt;p&gt;The most common and known library for payment, and the one I was using for instant payment with Paypal is the ActiveMerchant gem, an huge container of all the payment gateway of the galaxy.&lt;/p&gt;
&lt;p&gt;The gem is great, until you realize that you need recurring payments&amp;#8230;.
&lt;br/&gt;
Some users provided forks with their own implementatios, but there wans&amp;#8217;t a clean solution, and some messy code inside a huge library for me was not the way to go.&lt;/p&gt;

&lt;p&gt;
My Choice:
&lt;br/&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class="ruby"&gt;
gem "paypal-recurring", "~&amp;gt; 1.1.0"
&lt;/code&gt;&lt;/pre&gt;


&lt;h1 style="margin:20px 0  10px;"&gt;Part 2. Understanding Paypal&lt;/h1&gt;

&lt;p&gt;
Before the integration of the gem is important to understand what we are going to do and why.
&lt;br/&gt;
I attach an image from the Paypal developer website, that explain all the steps needed for a recurring payment:
&lt;/p&gt;

&lt;div style="text-align:center;margin-top:15px;"&gt;
&lt;img src="http://media.tumblr.com/tumblr_meh4am4ECQ1r7dv14.gif"/&gt;&lt;br/&gt;&lt;i&gt;Image credit of Paypal, source: &lt;a href="https://www.x.com/developers/paypal/documentation-tools/express-checkout/integration-guide/ECRecurringPayments"&gt;Paypal Developer Network&lt;/a&gt;&lt;/i&gt;
&lt;/div&gt;

&lt;ol style="margin:15px 0 0 25px;"&gt;&lt;li&gt;We ask Paypal for an address where we can send our customer, with a return and an error url&lt;/li&gt;
&lt;li&gt;Paypal give us the address, and we redirect the customer there&lt;/li&gt;
&lt;li&gt;Customer signup and/or login to Paypal, and confirm the amout that we want to bill montly.&lt;/li&gt;
&lt;li&gt;If the customer agree is redirected to the return url we provided. Paypal return us a token and an id to identify the customer and the transaction&lt;/li&gt;
&lt;li&gt;We show the customer a review of the subscription&lt;/li&gt;
&lt;li&gt;If the customer agree we instantly bill the amount and we create a recurring profile for the future payments.&lt;/li&gt;
&lt;/ol&gt;&lt;h1 style="margin:20px 0  10px;"&gt;Part 3. Integration&lt;/h1&gt;

&lt;p&gt;
&lt;b&gt;WARN: &lt;/b&gt;&lt;i&gt;The files in this section are only examples, the code miss some basic security, that can be easly added when integrated in your project.
&lt;/i&gt;
&lt;/p&gt;

&lt;p&gt;
Using the examples provided by the gem, and a little help from a recent Railscasts I ended up with this integration.&lt;/p&gt;

&lt;p&gt;
&lt;b&gt;Subscription model&lt;/b&gt;: here you can manage the relations with the customers. One customer has many subscriptions, and a subscription belongs to a plan.
&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
class Subscription &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
Through the &lt;i&gt;paypal&lt;/i&gt; method we can access the &lt;i&gt;PaypalPayment&lt;/i&gt; model, that manage the code between the gem &amp;#8220;paypal-recurring&amp;#8221; and our code.
&lt;/p&gt;

&lt;p&gt;
The &lt;i&gt;PaypalPayment&lt;/i&gt; module is as follow:
&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
class PaypalPayment
  def initialize(subscription)
    @subscription = subscription
  end
  
  def checkout_details
    process :checkout_details
  end
  
  def checkout_url(options)
    process(:checkout, options).checkout_url
  end
  
  def make_recurring
    payment = process(:request_payment)
    if payment.approved? &amp;amp;&amp;amp; payment.completed?
      # Set the subscription as paid
    end
    recurring = process(:create_recurring_profile, {period: :monthly, frequency: 1, start_at: Time.zone.now + 30.days})
    @subscription.update_attribute(:paypal_profile_id, recurring.profile_id)
  end
  
  def cancel_recurring
    ppr = PayPal::Recurring.new(:profile_id =&amp;gt; @subscription.paypal_profile_id)
    ppr.cancel
  end

private

  def process(action, options = {})
    options = options.reverse_merge(
      token: @subscription.paypal_token,
      payer_id: @subscription.paypal_payer_id,
      description: @subscription.name,
      amount: @subscription.amount,
      currency: "USD",
      ipn_url: "http://www.example.com" + "/paypal/notifications/#{@subscription.id}"
    )
    response = PayPal::Recurring.new(options).send(action)
    raise response.errors.inspect if response.errors.present?
    response
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;
&lt;i&gt;Checkout details&lt;/i&gt;: return some details about the customer that is subscribing to our website, like name and email address.
&lt;br/&gt;&lt;i&gt;Checkout url&lt;/i&gt;: return the url of paypal where we need to redirect the customer to give us the permission to create a recurring payment.
&lt;br/&gt;&lt;i&gt;Make recurring&lt;/i&gt;: Create an instant payment, and a recurring profile. Note: The first month is paid with the instant payment, so the recurring start from the next month.
&lt;br/&gt;&lt;i&gt;Cancel recurring&lt;/i&gt;: Cancel the recurring profile
&lt;/p&gt;

&lt;p&gt;Everything is glued in the controller&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
class PaymentsController  :notification

  before_filter :load_subscription

  def checkout
    redirect_to @subscription.paypal.checkout_url(
      return_url: "http://www.example.com" + "/paypal/review/#{@subscription.id}",
      cancel_url: "http://www.example.com" + "/paypal/cancel/#{@subscription.id}"
    )
  end

  def review
    @subscription.update_attributes(paypal_token: params["token"], paypal_payer_id: params["PayerID"])
  end

  def complete
    @subscription.paypal.make_recurring
  end

  def cancel
  end

  # Manage the paypal IPN
  #
  def notification
    notify = PaypalNotification.new(request.raw_post)
    if notify.acknowledge
      begin
        if notify.complete? &amp;amp;&amp;amp; @subscription.amount == notify.amount
          # Mark as paid
        else
          # Mark as error and investigate
        end
      rescue =&amp;gt; e
        # Mark as error and investigate
        raise
      end
    end

    render :nothing =&amp;gt; true
  end

  private

    def load_subscription
      @subscription = Subscription.find(params[:id])
    end

end
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 style="margin:20px 0 10px;"&gt;Part 4. Final note&lt;/h1&gt;
&lt;p&gt;This steps should be enough to integrate paypal into your app. Paypal also provide a sandbox in wich you can create fake account and test that everything is working as expected.&lt;/p&gt;
&lt;p&gt;Payment is mostly done in Paypal and model code, the views can be styled to reflect your website aspect and don&amp;#8217;t require specific code.&lt;/p&gt;

&lt;p&gt;On another post I will show how to manage the IPN with a custom library.
&lt;/p&gt;


&lt;p&gt;&lt;i&gt;The code in this post can be found in this Gist: &lt;a href="https://gist.github.com/4197985"&gt;https://gist.github.com/4197985&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;</description><link>http://rubyitalianstyle.it/post/37217199474</link><guid>http://rubyitalianstyle.it/post/37217199474</guid><pubDate>Wed, 05 Dec 2012 00:14:00 +0100</pubDate></item><item><title>Carrierwave resize based on orientation</title><description>&lt;p&gt;Today I had the need to create a thumbnail, from an image uploaded with Carrierwave, based on the image orientation, landscape or portrait.

It was very simple, and I&amp;#8217;m going to share with you how I&amp;#8217;ve done it.

My application is a Rails 3.1, with Carrierwave and MiniMagick.

Place inside the initializers folder a file, containing a custom Carrierwave processor

&lt;b&gt;config/initializers/carierwave.rb&lt;/b&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
module CarrierWave
  module MiniMagick
    def from_orientation(portrait, landscape)
      manipulate! do |img|
        if img[:width] &amp;gt; img[:height] # Landscape
          width, height = landscape
        else # Portrait
          width, height = portrait
        end
        img.resize "#{width}x#{height}&amp;gt;"
        img
      end
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;br/&gt;
you may need to restart your Rails application after adding this file.
&lt;br/&gt;&lt;br/&gt;
After that you can use the processor in one of your uploader:
&lt;pre&gt;&lt;code class="ruby"&gt;
 version :thumb do
    process :from_orientation =&amp;gt; [[184, 130], [140, 197]]
  end
&lt;/code&gt;&lt;/pre&gt;
&lt;br/&gt;
Simple as that.&lt;/p&gt;</description><link>http://rubyitalianstyle.it/post/26381795226</link><guid>http://rubyitalianstyle.it/post/26381795226</guid><pubDate>Tue, 03 Jul 2012 02:03:13 +0200</pubDate></item><item><title>Chef Cookbook Redis</title><description>&lt;p&gt;In this days I&amp;#8217;m implementing an analytics system for social media with Redis, so I&amp;#8217;ve updated my Chef cookbook repo.&lt;/p&gt;

&lt;p&gt;In the &amp;#8220;readme&amp;#8221; you can find detailed instruction to test the cookbook with Vagrant.&lt;/p&gt;

&lt;p&gt;The repo can be found on Github: &lt;a href="https://github.com/McRipper/Chef-Cookbooks"&gt;&lt;a href="https://github.com/McRipper/Chef-Cookbooks"&gt;https://github.com/McRipper/Chef-Cookbooks&lt;/a&gt;&lt;/a&gt;.&lt;/p&gt;</description><link>http://rubyitalianstyle.it/post/20986242775</link><guid>http://rubyitalianstyle.it/post/20986242775</guid><pubDate>Fri, 13 Apr 2012 00:19:50 +0200</pubDate></item><item><title>Ruby namespaced methods</title><description>&lt;p&gt;Today I was writing a library to access some API, in the design process I started wondering if in Ruby is possible to use some sort of namespacing on class.&lt;/p&gt;

&lt;p&gt;All that I wanted was concatenating methods, like you do on active record objects. I show you the solution that I found:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
module Test
  module Foo

    def bar
      @namespace = "bar"
      self
    end

    def bar_quack
      puts "Quack!"
    end
  end
end

module Test
  class Base

    def initialize
      puts "Init!"
    end

    def method_missing(method, *args)
      if @namespace
        self.send("#{@namespace}_#{method}", *args)
      else
        super
      end
    end

    include Test::Foo

  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this way you can do this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
test = Test::Base.new
#=&amp;gt; Init!
test.bar
#=&amp;gt;
test.bar_quack
#=&amp;gt; Quack!
test.bar.quack
#=&amp;gt; Quack!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The code works by implementing a method missing in the Base class, and a namespace method in the module that you want namespaced. All the method in the module must have the format: namespace_method.&lt;br/&gt;Now when you call the namespace method an instance variable is set with the namespace, and self is returned.
Because the method cannot be found method missing is triggered, and is the instance variable is set we send the method with the namestace.&lt;/p&gt;

&lt;p&gt;This is a quick way that I foud to achive a prettier way to access mehods. Bye!&lt;/p&gt;</description><link>http://rubyitalianstyle.it/post/19783190339</link><guid>http://rubyitalianstyle.it/post/19783190339</guid><pubDate>Fri, 23 Mar 2012 15:54:00 +0100</pubDate><category>ruby</category><category>namespace</category><category>methods</category><category>method_missing</category></item><item><title>Ruby 1.9.3 + Evernote + Thrift</title><description>&lt;p&gt;Today I started studing the Evernote Api for a new project.&lt;/p&gt;
&lt;p&gt;The first thing after reading the website was searching for the gem that provide a wrapper over the Thrift interface of Evernote.&lt;/p&gt;
&lt;p&gt;Simple and easy, just add the gem to the Gemfile, bundle install and write a small piece of code inside the project to test api key&lt;/p&gt;&lt;p&gt;Nobody ever said life was easy&amp;#8230; The program exit with and error
&lt;/p&gt;&lt;pre style="overflow:auto;"&gt;&lt;code class="ruby"&gt;
1.9.3-p0/lib/ruby/gems/1.9.1/gems/thrift-0.8.0/lib/thrift/transport/http_client_transport.rb:47:in `initialize': can't convert nil into String (TypeError)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;WTF? The error seems to be in the Thrift gem&amp;#8230; Let&amp;#8217;s start searching on Google.
Nobody seems to have the same problem, it must be my fault! Then the idea&amp;#8230; Try with another ruby version, I use rbenv to change my ruby version&amp;#8230; 1.8.7&amp;#8230; No error!&lt;/p&gt;

&lt;p&gt;Some Google digging later I find that the problem is with enconding in the new Ruby. A fork of Thrift with the fix is on Github. Good!&lt;/p&gt;

&lt;p&gt;Another test and another error: cannot change encoding on frozen String! This seems simple, let&amp;#8217;s add a dup on the string&amp;#8230; and Done!&lt;/p&gt;

&lt;p&gt;Now the example work again with ruby 1.9.3-p0 and 1.9.3-p125&lt;/p&gt;

&lt;p&gt;How I make the change? I fork the github project, make the fix and do a pull request&amp;#8230; No way! Thrift Github repo contains code for a lot of languages, and I can&amp;#8217;t understand how to specify in bundle how to use it!&lt;/p&gt;

&lt;p&gt;The idea&amp;#8230; &amp;#8220;RECREATE ALL THE PRJECTS&amp;#8221;. Long story short I&amp;#8217;ve recreated the repo for the project. To get yourself up and running without hassle add this to your Gemfile:&lt;/p&gt;

&lt;pre style="overflow:auto;"&gt;&lt;code class="ruby"&gt;
gem "evernote", "~&amp;gt; 1.2.0", :git =&amp;gt; "git://github.com/McRipper/evernote.git"
gem "thrift", :git =&amp;gt; "git://github.com/McRipper/thrift-1.9.3.git"
gem "thrift_client", "~&amp;gt; 0.8.1", :git =&amp;gt; "git://github.com/McRipper/thrift_client.git"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I wrote this post to share with the community the hours I&amp;#8217;ve spent figuring out this mess. The solution is very ugly and quick, but I think that in the future the various gems will be fixed and this hack can be removed!&lt;/p&gt;</description><link>http://rubyitalianstyle.it/post/19728996112</link><guid>http://rubyitalianstyle.it/post/19728996112</guid><pubDate>Thu, 22 Mar 2012 13:08:00 +0100</pubDate></item><item><title>Ruby Inject for sum</title><description>&lt;p&gt;Ruby is a great language because some tasks can be achieved in different ways.&lt;/p&gt;
&lt;p&gt;The way you choose can be the most readable and beautiful.&lt;/p&gt;
&lt;p&gt;Today I want to show you different ways to do a sum on values returned from different methods.&lt;/p&gt;

&lt;pre&gt;
&lt;code class="ruby"&gt;
def negative
  25
end

def positive
  25
end

def neutral
 50
end
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;The result that we want is this: &lt;/p&gt;

&lt;pre&gt;
&lt;code class="ruby"&gt;
  (negative + positive + neutral)
  # =&amp;gt; 100
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;First way: collect all the values in an array and then inject + on it. (&lt;a href="http://apidock.com/ruby/Enumerable/inject"&gt;http://apidock.com/ruby/Enumerable/inject&lt;/a&gt;)&lt;/p&gt;

&lt;pre&gt;
&lt;code class="ruby"&gt;
  %w(negative positive neutral).collect{ |m| send(m) }.inject(:+)
  # =&amp;gt; 100
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;Or: pass to the inject method the values that we want &lt;i&gt;sum&lt;/i&gt; to be initialized, in this case 0,  and a second variable &lt;i&gt;m&lt;/i&gt; as the method name.&lt;/p&gt;

&lt;pre&gt;
&lt;code class="ruby"&gt;
  %w(negative positive neutral).inject(0) {|sum, m| sum + send(m) }
  # =&amp;gt; 100
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s all!&lt;/p&gt;</description><link>http://rubyitalianstyle.it/post/19113372320</link><guid>http://rubyitalianstyle.it/post/19113372320</guid><pubDate>Sun, 11 Mar 2012 12:21:00 +0100</pubDate></item><item><title>Rails 3 Javascript asset</title><description>&lt;p&gt;To keep my Js code clean with the Rails 3 asset pipeline I remove the require tree from the main application js file, then to load only the file that correspond to the correct controller I created this helper:&lt;/p&gt;

&lt;p&gt;Helper&lt;/p&gt;
&lt;pre&gt;
&lt;code class="ruby"&gt;
def js_controller&lt;br/&gt;
  js = []&lt;br/&gt;
  path = controller.controller_path.split("/")&lt;br/&gt;
  path.shift&lt;br/&gt;
  while path.size &amp;gt; 0&lt;br/&gt;
    js &amp;lt;&amp;lt; (["frontend"] &amp;lt;&amp;lt; path).join("/")&lt;br/&gt;
    path.pop&lt;br/&gt;
  end&lt;br/&gt;
  return js.reverse&lt;br/&gt;
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Layout&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
-js_controller.each do |js|&lt;br/&gt;
    =javascript_include_tag js
&lt;/code&gt;&lt;/pre&gt;</description><link>http://rubyitalianstyle.it/post/17963797192</link><guid>http://rubyitalianstyle.it/post/17963797192</guid><pubDate>Mon, 20 Feb 2012 21:18:00 +0100</pubDate></item></channel></rss>
