Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Move fast, don't break your API

Amber Feng
September 30, 2014

Move fast, don't break your API

Amber Feng

September 30, 2014
Tweet

More Decks by Amber Feng

Other Decks in Programming

Transcript

  1. post '/v1/charges' do card_number = params[:card_number] amount = params[:amount] charge

    = create_charge(card_number, amount) json { id: charge.id, amount: charge.amount card_number: charge.redacted_card_number, success: charge.success } end
  2. post '/v1/charges' do ... unless card_number.length == 16 return {error:

    "Invalid card number."} end unless amount > 0 and amount <= CHARGE_MAX return {error: "Invalid amount."} end ... end
  3. curl https://stripe.com/v1/charges -u API_KEY -d card_number=4242424242424242 -d amount=100 => {

    id: "ch_xxx", amount: 100, card_number: "*4242", success: true }
  4. use ErrorHandler use Authenticator get '/v1/charges/:id' do user = env.user

    id = params[:id] unless user.get_charge(id) raise UserError.new("No charge #{id}!") end end
  5. class ChargeCreateMethod < AbstractAPIMethod required :amount, :integer required :card_number, :string

    resource ChargeAPIResource def execute create_charge(amount, card_number) end end
  6. class ChargeAPIResource < AbstractAPIResource required :id, :string required :amount, :integer

    required :card_number, :string required :success, :boolean def describe_card_number charge.redacted_card_number end end
  7. class ChargeCreateMethod < AbstractAPIMethod required :amount, :integer required :card_number, :string

    document :amount, "Amount, in cents." document :card_number, "The card number." ... end
  8. def execute if !user.gating(:allows_amount) && params[:amount] raise UserError.new("Invalid param.") end

    ... if !user.gating(:allows_amount) response.delete(:amount) end end