My First Sinatra App

Posted by Tyler Jones on May 29, 2020

​ I felt my understanding of ruby and Sinatra had grown so much but again I was a little overwhelmed about how little I knew of the app structure used in Sinatra applications. I can’t describe what it felt like to open a blank document and form an app structure. Outside of the curriculum I never spent time creating my own validations either which, led to more blank spots in my understanding of Sinatra. I’d still say its easier to make a small rails app than the previous projects api gem.

When thinking about the app structure I felt that it was easier to build a rough draft similar to corneal’s

├── config.ru
├── Gemfile
├── Gemfile.lock
├── Rakefile
├── README
├── app
│   ├── controllers
│   │   └── application_controller.rb
│   ├── models
│   └── views
│       ├── layout.erb
│       └── welcome.erb
├── config
│   ├── initializers
│   └── environment.rb
├── db
│   └── migrate
├── lib
│   └── .gitkeep
└── public
|   ├── images
|   ├── javascripts
|   └── stylesheets
|       └── main.css
└── spec
    ├── application_controller_spec.rb
    └── spec_helper.rb

I typed out:

user
	has_many collections
	validates username && password
		vars{
		username
		password
		about
		}
collection
	belongs_to user
	has_many items
		vars{
		name
		description
		items
		}
item
	belongs_to collection
	validates image? # if possible
		vars{
		name
		condition
		price? #maybe? maybe not..
		image? #if i can validate
		}	

Typing my app out in sudo will now be apart of every project I do. This notepad.md took 5mins to make and I used it constantly until I finalized my migrations AND completed my app structure (at least a few days worth of coding). However, thanks to the amazing gem that corneal is, I had the basics down pretty quickly. I was also able to use a few great templates to get my CONTRIBUTING.md (see here), LICENSE.md (see here) and README.md(see here) to exponentially save time while following GitHub’s community guidelines.

​ One of the biggest challenges I faced with this project is that it can be difficult to find help and articles for coding with Sinatra. nearly all questions I had can be answered with Ruby On Rails but not necessarily with Sinatra. For example, I needed to validate a user input for an image URL to make sure a user can only input specific types of images. The first gem I came across looks amazing and does everything I need but I couldn’t find any docs for Carrierwave that helped with the use of Sinatra. To be fair I don’t know a lot about rails currently or to ‘convert’ the coding/terminology to Sinatra. Paperclip seemed a little advanced (though it did have non-rails instruction) it also did a lot more than needed for my small project. I stumbled on this Stack Overflow article for a simple validation regex:

validates :image_url, allow_blank: true, format: {
  with: %r{\.gif|jpg|png}i, #I added an |jepg| for and extra validation
  message: 'must be a url for gif, jpg, or png image.'
}

I also spent some time figuring out why my servers were dying and how rackup and shotgun run local servers. I noticed that (after quite some time I should add) whenever I would accidentally force stop my server ctrl^z or reset/restart my database I would get Input/output error @ io_writev - <STDERR>. Finding out how ports work and use processes was a game changer. If you look hard enough at the task manager you’ll see a suspended Ruby process Untitledthat is holding up that port so restarting the server without closing the process a new one cannot startup.