Write Ruby API's with Stripe's Poncho (a DSL for REST interfaces)
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:
- Standard Params: integer, array, boolean, etc
- Custom Params
- Standard Validators: presence, format, length, etc
- Custom Validators
- Method Filters
- And more...
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.
Sign in or Join to comment or subscribe