This file contains 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
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 { |
This file contains 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
# 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 |
This file contains 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
require "rack" | |
require "puma" | |
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello World']] } | |
class ApiKeyValidator | |
def initialize(app) | |
@app = app | |
end | |
This file contains 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
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) |
This file contains 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
# 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 |
This file contains 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
abstract class Baz | |
abstract def call(args : String) : String | |
end | |
class Bar < Baz | |
def call(args : String) : String | |
"#{args} + bar" | |
end | |
end |
This file contains 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
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") |
This file contains 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
fn some_operation() -> Result<str, Error> { | |
if succeded() { | |
Ok("We did it") | |
} else { | |
Err(Error::from("this not working")) | |
} | |
} | |
fn main() { | |
match some_operation() { |
This file contains 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
// Destructuring a struct | |
Person { | |
name: person_name, | |
age: person_age, | |
... | |
} = person; | |
println!("{}", person_name); |
This file contains 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
// 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") | |
} |
NewerOlder