Skip to content

Instantly share code, notes, and snippets.

View aaripurna's full-sized avatar
💣
(_T_)

nawa aripurna aaripurna

💣
(_T_)
View GitHub Profile
require "our_app"
require "we_can_use_our_middleware"
Rails.application.routes.draw do
# we can mount directly
mount OurNewApp => "/posts"
# using Rack::Builder to add middleware
mount Rack::Builder.new {
# inside config/application.rb
require "path_to_rack_app/or/gem_name"
module ExampleWeb
class Application < Rails::Application
# ... other code
config.middleware.use OurNewMiddleware
# ... other code
require "rack"
require "puma"
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello World']] }
class ApiKeyValidator
def initialize(app)
@app = app
end
@aaripurna
aaripurna / middleware.rb
Last active June 23, 2022 13:57
Rack Middleware
class ApiKeyValidator
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.has_header?('X-API-KEY') && request.get_header('X-API-KEY') == ENV['API_KEY']
return @app.call(env)
@aaripurna
aaripurna / application.rb
Created June 23, 2022 12:55
This is a Valid Rack Syntac
# This is a valid syntax
class App
def call(env)
[200, {'Content-Type' => 'text/plain'}, ['Hello World']]
end
end
# This is a valid syntax (singleton)
class App
abstract class Baz
abstract def call(args : String) : String
end
class Bar < Baz
def call(args : String) : String
"#{args} + bar"
end
end
@aaripurna
aaripurna / deep-match.rs
Created July 23, 2021 16:53
depp nessted match mattern
match connect_database() {
Ok(mut conn) => match conn.fetch_user() {
Ok(user) => conn.get_post(user.id) {
Ok(post) => conn.get_comments(post.id) {
Ok(_comments) => println!("We got the coments"),
Err(_) => println!("its error, no comment")
}
Err(_) => println!("Can't find post")
}
Err(_) => println!("Can't find user")
@aaripurna
aaripurna / error.rs
Created July 23, 2021 16:27
error handling rust
fn some_operation() -> Result<str, Error> {
if succeded() {
Ok("We did it")
} else {
Err(Error::from("this not working"))
}
}
fn main() {
match some_operation() {
@aaripurna
aaripurna / pattern_matching.rs
Created July 23, 2021 16:08
Struct pattern patching
// Destructuring a struct
Person {
name: person_name,
age: person_age,
...
} = person;
println!("{}", person_name);
@aaripurna
aaripurna / nullable.rs
Created July 23, 2021 15:38
Null handling in rust
// Sometimes will retun None
fn some_operation() -> Option<String> {
// Some operation that may return None
}
fn write_text() {
match some_iperation() {
Some(text) => println!("{}", text),
None => println!("There is nothing")
}