This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/services/fetch_product_details.rb | |
| class FetchProductDetails | |
| def initialize(api: Integration::CachingProductApiProxy.new) | |
| @api = api | |
| end | |
| def call(sku) | |
| @api.find_product(sku) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/services/integration/caching_product_api_proxy.rb | |
| module Integration | |
| class CachingProductApiProxy | |
| def initialize(real_api: SlowProductApi.new, cache: Rails.cache) | |
| @real_api = real_api | |
| @cache = cache | |
| end | |
| def find_product(sku) | |
| @cache.fetch("product_api/#{sku}", expires_in: 5.minutes) do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/services/integration/slow_product_api.rb | |
| module Integration | |
| class SlowProductApi | |
| def find_product(sku) | |
| # Simulate external HTTP request | |
| sleep(2) | |
| { sku: sku, name: "Product #{sku}", price_cents: 1000 } | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| * Main Menu | |
| * Products | |
| - Laptops (/products/laptops) | |
| - Monitors (/products/monitors) | |
| * Account | |
| - Profile (/account/profile) | |
| - Settings (/account/settings) | |
| - Help (/help) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| root = MenuGroup.new("Main Menu") | |
| products = MenuGroup.new("Products") | |
| products.add(MenuItem.new("Laptops", "/products/laptops")) | |
| products.add(MenuItem.new("Monitors", "/products/monitors")) | |
| account = MenuGroup.new("Account") | |
| account.add(MenuItem.new("Profile", "/account/profile")) | |
| account.add(MenuItem.new("Settings", "/account/settings")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/controllers/payments_controller.rb | |
| class PaymentsController < ApplicationController | |
| def create | |
| order = current_user.orders.find(params[:order_id]) | |
| transaction = PaymentFacade.new.process(order, params[:payment_method_token]) | |
| if transaction.success? | |
| redirect_to order_path(order), notice: "Payment successful" | |
| else | |
| redirect_to order_path(order), alert: "Payment failed" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/facades/payment_facade.rb | |
| class PaymentFacade | |
| def initialize( | |
| charge_gateway: Payment::ChargeGateway.new, | |
| create_transaction_record: Payment::CreateTransactionRecord.new, | |
| send_receipt: Payment::SendReceipt.new | |
| ) | |
| @charge_gateway = charge_gateway | |
| @create_transaction_record = create_transaction_record | |
| @send_receipt = send_receipt |
NewerOlder