Giter Club home page Giter Club logo

week-06-sinatra's Introduction

Week 6 Sinatra Heroku Example

Outline

  • Developing, testing and deploying web services
    • Rack and Sinatra
    • Rack::Test
    • Heroku
  • Exercise
  • Exploration
    • Travis Continuous Integration

Developing, Testing and Deploying web services

Rack

Rack is an minimal interface for developing web applications. Rails, Sinatra, and many other web frameworks are written on top of Rack. If you want the simplest possible web application you can use the rack gem and a file named config.ru that contains the following code:

app = lambda do |env|
  body = "Hello, World!"
  [200, {"Content-Type" => "text/plain", "Content-Length" => body.length.to_s}, [body]]
end

run app

To run the above application, simply use the command:

rackup

Your application is available at http://localhost:9292 once you see something like the following in your console:

[2011-11-08 09:06:12] INFO  WEBrick 1.3.1
[2011-11-08 09:06:12] INFO  ruby 1.9.2 (2011-07-09) [x86_64-darwin11.1.0]
[2011-11-08 09:06:12] INFO  WEBrick::HTTPServer#start: pid=4318 port=9292

Rack::Test

Rack::Test is a small, simple testing API for Rack apps. Rack uses mock objects to allow inspection of http requests and responses without actually starting your web application or making any client requests. Don't worry about understanding mocks right now, except to understand it is one more trick in the ruby toolbox that makes developing and testing easier.

Sinatra uses rack, so this is a rack application.

# lib/my_app.rb
require 'sinatra'
get '/' do
  'Hello World!'
end

Our spec file simply includes our code and rack/test, and then provides some required set-up.

# spec/my_app_spec.rb
require 'my_app'
require 'rack/test'

describe "my app" do
  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  ...
end

In the spec file's examples we use rack-test to simulate, or mock, a GET request against the server. Note: the get method used here is from rack-test, not sinatra!

  get "/"

The rack-test code then creates last_request and last_response objects that we can interrogate.

last_request.env['REQUEST_METHOD'].should == 'GET'
last_response.body.should match(/Hello World/)
last_response.status.should == 200

Heroku

Heroku is a cloud application platform that makes deploying web applications trivial (and free).

  1. Create a Heroku account
  2. Install the heroku gem with gem install heroku
  3. Make a config.ru in the root-directory that runs your app
  4. Create the app on heroku with heroku create <your-appname>
  5. Push to it heroku with git push heroku master
  6. Open the app in a browser at http://your-heroku-app-name.heroku.com or type heroku open

Example

Exercise

Review the sinatra documentation.

  1. Use this repository to make your own, called yourname-week-06-sinatra .
  2. Complete the two pending specs. One of them describes a feature that you will need to write.
  3. (optional) Add yet another feature and a spec for it.
  4. Deploy the app to Heroku.

Exploration

A distributed build system for the open source community.

This service automatically runs your tests everytime you push your code to github. While you should always run your tests manually before you commit, this continuous integration service can be very useful when you have long running tests and/or many contributors.

In order to build your Ruby project on Travis CI, your repository should have a Rakefile with the default task being a test task.

As well, Travis makes it easy to test your project against multiple versions and flavors of Ruby. Take a look at the file named .travis.yml in this project and notice the flavors of ruby that will be used to run tests.

Once you have that set up, you can include your live build status in your Readme file, like so: build status

week-06-sinatra's People

Contributors

bfaloona avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

week-06-sinatra's Issues

Should you redirect on a 404?

This question relates to the last test in the rspec file. Is it proper to explicitly redirect to a url such as http://www.mysite.com/404.html if the page the user requested does not exist? If so, how do you coordinate this with the Sinatra not_found method? Should I stick the redirect in there? The behavior I'm seeing is that not_found will render the template with name "404," but it doesn't redirect you to a page named "404."

Why don't the routes appear to be working?

When I try to run lib/app.rb from the command line, the server starts, but the routes defined in app.rb are ignored (I get the default Sinatra page). I start the app from the root directory like this:

ruby lib/app.rb

How do we turn in this assignment?

I didn't fork this repo because we're pushing the Sinatra app to Heroku. Are you going to look for the URL [your github name]-week-06-sinatra.heroku.com?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.