In ruby you can find security levels for your code.
The Ruby security level is represented by the $SAFE global variable.
The value ranges from minimum value 0 to maximum value 4.
No checking of the use of externally supplied (tainted) data is performed. This is Ruby’s default mode.
Ruby disallows the use of tainted data by potentially dangerous operations.
Ruby prohibits the loading of program files from globally writable locations.
All newly created objects are considered tainted.
Ruby effectively partitions the running program in two. Nontainted objects may not be modified. Typically, this will be used to create a sandbox: the program sets up an environment using a lower$SAFE level, then resets $SAFE to 4 to prevent subsequent changes to that environment.
In ruby you can find regexp for emails. If you want to use it, you have to require uri
library and call URI::MailTo::EMAIL_REGEXP constant.
The global DATA variable in ruby allows us to access the text at the end of our file
listed after the __END__ block. This can be surprisingly useful, for instance if we
need to extract information from a rather large data blob.
Ruby defines curry for Method and Proc, allowing procs to return partially applied procs when they get called with fewer than the required number of arguments.
Note: While Proc#curry has been around since Ruby 1.9, Method#curry was only added in Ruby 2.2.0. For versions before 2.2.0, you will first need to convert your method object to a proc via Method#to_proc.
I have a problem. Each time I want add domain to my custom github project
I forget what I should do exactly. That’s why I want to post this SO answer in my blog.
I hope it would helpful not only for me.
To Setup a custom domain for your gh-pages project repo that handles
www.yourdomain.com and yourdomain.com (assumes you already have a gh-pages
branch on your repo) you need this:
From your project repo, gh-pages branch. Create a CNAME file with the contents yourdomain.com.
In your DNS manager, setup two CNAME records. One for the root apex (@) and one for www. Both point to YOURusername.github.io. If your DNS provider does NOT support ALIAS records on the root apex (@), simply create A records that point to 188.8.131.52 and 184.108.40.206.
Sometimes you need to preview your emails in development enviroment.
For this you can use use letter_opener gem.
Because hanami uses mail gem, letter_opener integration will be easy.
Firstly you need add gem to your Gemfile:
Secondly you need add new delivery method to your lib/repository_name.rb file:
After that when you deliver your email, letter_opener will generate html files that users will see in tmp/letter_opener directory.
Sometimes you need to build API serveice in your rails application.
Of course you can use popular solutions as grape or rails-api gems.
And also you can find real examples in gitlab or rubygems projects.
But today I’m going to tell you about other framework. It’s roda.
Roda was created by Jeremy Evans and it’s fast and simple ruby routing framework.
So why should you take a look on it?
There’s a list of advantages of this framework:
Roda provides simple way to working with big count of different plugins;
You can use any architecture with it;
So I mentioned that roda is fast. If you want to verify this, just check this benchmark repository and you’ll see that roda is really fast.
For example, I run benchmarks on locally and get this result:
Roda based on rack. That’s why this integration with rails will be very simple.
Firstly you need to add roda gem to your gemfile:
Secondly you need to create simple roda application.
Note that you need to use json and all_verbs plugins.
The first one needs to JSON responce and the second one provides all REST methods for your application (like put, patch, etc).
I puted my roda application in to lib/api/base.rb path but you can use whatever you waht.
After that you need to mount your application in config/routes.rb.
This is done as well as any other rack application:
And the last part, we need to split our application to different modules with different logic and routes.
For this we have to create required module. In an example I created Users module with all REST routes:
Finnaly you need to mount our module to roda application. It is very simple too.
That’s all. Now we have a simple roda API application which integrated to our rails app.
Problems / future
In the last part I want to list problems and ideas what I want to solve in future.
Roda doesn’t have a swagger integrating from the box. Now I’m thinking about using swagger-block gem for it.
Also roda doesn’t typecast your params. I know that grape uses virtus gem for this. And this feature you should realize by youself too.
On this blog post I wanted to show you that you’re not limited only to popular API server gems.
As you can see roda have amazing ideas and properties such as modulatiry, simplicity, speed and stability.
Also this framework has a simple way to integration in to your rails application.
Today I read a nice blog post by Kir Shatrov about Profiling RSpec examples with Stackprof
and I wondered if I could repeat this trick with minitest? This question is very important for me
because I ofen use minitest for my job and OSS projects. Also a lot of cool projects use minitest
(for example, sidekiq, rails, lotus, etc).
In reality, to do that in minitest as easy as in RSpec, and the only difference is that you need
to install a separate gem.