Quantcast
Viewing latest article 32
Browse Latest Browse All 58

Write Ruby API’s with Poncho

As we said in a previous post: WE LOVE STRIPE!

The team at Stripe has open sourced a RubyGem that makes REST API’s easy to create, Poncho. It’s compatible with any rack-based framework — like Rails or Sinatra. Best of all, it has a slew of (simple to use) features:

Installation is simple, just install the poncho gem (or add gem poncho to your Gemfile). After installation, you can setup your API:

class ChargeResource < Poncho::Resource
  param :amount, :type => :integer
end

class ChargeCreateMethod < Poncho::JSONMethod
  param :amount, :type => :integer, :required => true

  def invoke
    charge = Charge.new
    charge.amount = param(:amount)
    charge.save

    ChargeResource.new(charge)
  end
end

post '/charges', &ChargeCreateMethod

The above code, which was pulled from the documentation is using Sinatra, but you could easily swap Rails out:

match '/charges' => ChargeCreateMethod, :via => :post

The documentation is thorough, and does a great job at explaining how to use Poncho. You can also view the source or discuss this on HackerNews.

The post Write Ruby API’s with Poncho appeared first on The Changelog.


Viewing latest article 32
Browse Latest Browse All 58

Trending Articles