Skip to content

Instantly share code, notes, and snippets.

View MittalPatel-BTC's full-sized avatar

Mittal Patel MittalPatel-BTC

View GitHub Profile
@MittalPatel-BTC
MittalPatel-BTC / routes_with_namespace.rb
Created April 19, 2020 16:29
You can also define the routes without CRUD action using namespace as well.
namespace :users do
get 'charge'
get 'refund'
get 'bill'
end
@MittalPatel-BTC
MittalPatel-BTC / routes_without_crud_action.rb
Created April 19, 2020 16:28
Below is the output of the routes.
charge_users GET /users/charge(.:format) users#charge
refund_users GET /users/refund(.:format) users#refund
bill_users GET /users/bill(.:format) users#bill
@MittalPatel-BTC
MittalPatel-BTC / routes_with_only_method.rb
Created April 19, 2020 16:23
Below is the method of defining routes without CRUD using only block.
resources :users, only: [] do
collection do
get 'charge'
get 'refund'
get 'bill'
end
end
@MittalPatel-BTC
MittalPatel-BTC / routes_wothout_crud.rb
Last active April 19, 2020 16:27
Below is the method of defining routes without CRUD action using except block.
resources :users, except: [:new, :create, :edit, :update, :destroy] do
collection do
get 'charge'
get 'refund'
get 'bill'
end
end
@MittalPatel-BTC
MittalPatel-BTC / routes_output.rb
Last active April 19, 2020 16:20
Below is the output of CRUD routes.
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
@MittalPatel-BTC
MittalPatel-BTC / user_controller.rb
Created April 19, 2020 16:17
User controller where I have define my methods.
class UserController < ApplicationController
def charge
end
def refund
end
def bill
end
end
@MittalPatel-BTC
MittalPatel-BTC / refund_event_handler.rb
Created August 25, 2019 16:43
Define the service for the handling charge refund request.
# app/services/stripe/refund_event_handler.rb
# Stripe event handler for handling webhook
module Stripe
# This will inherite the eventHandler main class
class RefundEventHandler < EventHandler
def handle_charge_refund_created(event)
# your code goes here
end
end
end
@MittalPatel-BTC
MittalPatel-BTC / dispute_event_handler.rb
Created August 25, 2019 16:42
Define the service for the handling charge dispute request
# app/services/stripe/dispute_event_handler.rb
# Stripe event handler for handling webhook
module Stripe
# This will inherite the eventHandler main class
class DisputeEventHandler < EventHandler
def handle_charge_dispute_created(event)
# your code goes here
end
end
end
@MittalPatel-BTC
MittalPatel-BTC / stripe_event_handler.rb
Created August 25, 2019 16:39
Stripe event handler service
# app/services/stripe/event_handler.rb
# Stripe module
module Stripe
# stripe main class EventHandler
class EventHandler
def call(event)
method = 'handle_' + event.type.tr('.', '_')
send method, event
rescue JSON::ParserError => e
render json: { status: 400, error: 'Invalid payload' }
@MittalPatel-BTC
MittalPatel-BTC / event_handler.rb
Last active August 25, 2019 16:23
Service to handle webhook incoming request.
# Stripe module
module Stripe
# stripe main class EventHandler
class EventHandler
def call(event)
method = 'handle_' + event.type.tr('.', '_')
send method, event
rescue JSON::ParserError => e
render json: { status: 400, error: 'Invalid payload' }
Raven.capture_exception(e)