Skip to content

Instantly share code, notes, and snippets.

@ogryzek
Last active August 3, 2019 06:14
Show Gist options
  • Save ogryzek/9673941 to your computer and use it in GitHub Desktop.
Save ogryzek/9673941 to your computer and use it in GitHub Desktop.
Sinatra and Ruby Gems

#Sinatra

Ruby Gems

Ruby Gems
There are over 72,000 gems. There's is probably a gem available for whatever it is you want to do. Let's try installing a gem called "snooby". In your terminal, try gem install snooby.
If you are having difficulty with installing gems, try installing a ruby version management tool, such as RVM

1.) Find a gem that allows you to print something in IRB in color.

gem install colorize
irb
> require "colorize"
> puts "Hello CodeCore".colorize(:red)

Let's get started with Sinatra

Sinatra is just another gem, just like colorize, or rails. You'll see and use many gems. Let's install it.

gem install sinatra

Make a new directory, and create a file inside of it. Open the file, and type in the following.

# app.rb
require 'sinatra'

get "/" do
  "Hellow World"
end

Browsers

Browser understand Text, HTML, CSS, and JavaScript (let's forget about other things like HTTP requests, etc. for now). Any of these things, we can combine. But keep in mind, it does not understand Ruby. You can send those things to the client, but don't send ruby.

Another thing to unsderstand is that the web only works through request responses. So, you make a request, you get a response back. The web uses HTTP which stands for Hypertext Transfer Protocol. You don't have to understand this fully, but you should know that HTTP is stateless, this means, you make a request, it's a brand new request, you make a resonse, it's a brand new resoponse. Request -> Response, and you display that. If you make another request, it it a brand new request.

Cookies are sent. Each time, they are sent. So, if you are logged in, each time there is a request-response, that cookie may be sent, but if you think you are in a 'state' of being logged in, it's an illusion. The reality is, it is always listening for requests.

How would my server know to listen to a request? It doesn't. It has to have something to know that it's listening for something. In this case, we ran our ruby file, and we got a response that it was listening on port 4567.

And the server listens to what are different ports. Most web servers listen to port 80, that's the default. But, anyway, you have to have a server that's software running on a computer, listening for requests. It takes requests, it generates a response and sends it back to the client. The client's browser displays that HTML.

If you have something like Webrick, Sinatra, ERB, how will you know what code to execute in order to generate back the HTML, CSS, JavaScript for the client to read? This is where we use the HTTP protocol. The standard that SInatra, and Rails use are very similar. They use the exact same way of deciding routing, of deciding where to go.

REST

The verbing in HTTP, there are many, but we care about five of them. These five are Get, Post, Put/Patch, and Delete. These are the five we care about. Now, the path could be anything we want. It could be just /, or /contact, it could be anything. And then there are parameters. We might think of a hash, where we get a user's name, and the value can be the parameters, {name: "______"}

If you type in a URL in the browser and hit enter, that's a Get request.
A Post request is a standard when you enter a form.

# app.rb
require 'sinatra'

get "/" do              # This is the path. We are 'getting' the root path
  "Hello World"
end

If I want to build a form, how would I do this?

# app.rb
require 'sinatra'

get "/" do
  "Hello World"
end

get "/contact" do
  "<h1>Contact Us</h1>"
end

get "/thankyou" do
  "<h1>Thank You</h1>"
end

Install a gem call rerun gem install rerun Then type in rerun. Then open your file with rerun instead of ruby, like so rerun app.rb.

Now, go inside your app's directory (I called mine 'sinatra'), and make a directory called 'views'. Make a new file in 'views' called 'index.erb'

# index.erb
<html>

<head>
  <title>This is my title</title>

  <meta charset="UTF-8">
  
</head>
<body>

<h1>Welcome to my first Sinatra App</h1>

<p>Hello there!</p>

</body>
</html>

Now, update your app.rb get "/" do block to access the index you just created.

# app.rb
require 'sinatra'

get "/" do
  erb :index
end

get "/contact" do
  "<h1>Contact Us</h1>"
end

get "/thankyou" do
  "<h1>Thank You</h1>"
end

Now, if we want to creat a default template, we can do so with erb using <% yield %>. I can add some referencs to my app.rb

# app.rb
require 'sinatra'

get "/" do
  erb :index, layout: :default
end

get "/contact" do
  erb :contact, layout: :default
end

get "/thankyou" do
  "<h1>Thank You</h1>"
end
# views/default_template.erb
<html>

<head>
  <title>This is my title</title>

  <meta charset="UTF-8">
  
</head>
<body>

 <%= yield %>

</body>
</html>
# index.erb
<h1>Welcome to my first Sinatra App</h1>

<p>Hello there!</p>

Let's build out a contact form..

<h1>Contact Us</h1>

<form action="/contact" method="post">

  <label for="name">Your Name</label>
  <input type="text" id="name" name="name">
  <br>
  <label for="email">Email</label>
  <input type="email" id="email" name="email">
  <br>
  <label for="message">Message</label>
  <textarea rows="5" id="message" name="message"></textarea>
  <br>
  <input type="submit" value="Contact Me">
</form>

Change out the thank you message...

# app.rb
require 'sinatra'

get "/" do
  erb :index, layout: :default
end

get "/contact" do
  erb :contact, layout: :default
end

get "/thankyou" do
  "Thank you #{params[:name]} for contacting us"
end

Sending Email from Sinatra (or Ruby)

The gem we are going to use today is called Pony. gem install pony. At the top of your app.rb require 'pony'

# app.rb
require 'sinatra'
require 'pony'        # require the pony gem on your contact form.

get "/" do
  erb :index, layout: :default
end

get "/contact" do
  erb :contact, layout: :default
end

get "/thankyou" do
  Pony.email(to: "[email protected]",
             from: params[:email],
             subject: "You got a new message from #{params[:name]}",
             :body => params[:message],
             :via => :smtp,
             :via_options => {
             :address        => 'smtp.gmail.com',
             :port           => '587',
             :user_name      => 'answerawesome',
             :password       => 'Sup3r$ecret',
             :authentication => :plain, # :plain, :login, :cram_md5, no auth by default
             :domain         => "awesomeanswer.com" # the HELO domain provided by the client to the server
    }
  )
  "Thank you #{params[:name]} for contacting us"
end
@Yo555
Copy link

Yo555 commented Aug 3, 2019

ddd

@Yo555
Copy link

Yo555 commented Aug 3, 2019


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment