In my current project, I had to store some temporary data for a user and let a few background
processors have access to it. I wrote something small with a dependency on Redis which does the job.
It allows me to use current_user.tmp[:token_id] = "somedata here" and then access it
in the background processor using user.tmp[:token_id] which I think is pretty neat.
Moreover, since my use case needed this for temporary storage, I set it to auto expire in 1 day.
If yours is different you could change that piece of code.
Many SaaS apps allow users to host their websites under their root domain, e.g. GitHub allows you to host your sites using GitHub Pages under the .github.io domain.
Here is a list of subdomains which you should reserve while building your own SaaS product.
I usually put this data in a /data/reserved_subdomains file and then use it like below:
Here is a simple script which can cleanup directories older than x days on your server
It is useful for freeing up space by removing temporary directories on your server
# usage # # deletes dirs inside /opt/builds which are older than 3 days # delete-old-dirs.sh /opt/builds 3 # cron entry to run this every hour # 0 * * * * /usr/bin/delete-old-dirs.sh /opt/builds 2 >> /tmp/delete.log 2>&1 # cron entry to run this every day # 0 0 * * * /usr/bin/delete-old-dirs.sh /opt/builds 2 >> /tmp/delete.log 2>&1
if ! [ $# -eq 2 ] then cat <<EOS Invalid arguments Usage: delete-old-dirs.sh /root/directory/to-look/for-temp-dirs days-since-last-modification e.g. > delete-old-dirs.sh /opt/builds 3 EOS exit 1 fi
root=$1 ctime=$2
for dir in $(find $root -mindepth 1 -maxdepth 1 -type d -ctime +"$ctime") do # --format %n:filename, %A:access rights, %G:Group name of owner, %g: Group ID of owner, %U: User name of owner, %u: User ID of owner, %y: time of last data modification echo"removing: $(stat --format="%n %A %G(%g) %U(%u) %y""$dir")" rm -rf "$dir" done
When working with Static Site Blogs, you end up creating files with very long
names for your blog posts. For example, this very post has a filename
source/_posts/how-to-open-the-most-recent-file-created-in-vim.md.
Now finding this exact file in hundreds of other files and opening them is a
pain. Here is a small script which I wrote by piecing together stuff from the
internet.
1 2 3 4 5 6 7 8 9
# takes 1 argument functionlatest(){ # finding latest file from the directory and ignoring hidden files echo $(find $1 -type f -printf"%T@|%p\n" | sort -n | grep -Ev '^\.|/\.' | tail -n 1 | cut -d '|' -f2) }
tmux is an awesome terminal multiplexer. I have been
an Xmonad user about 4 years, and everytime I heard about tmux in the past I
used to think that my window manager was powerful and I din’t need another
terminal manager. But, tmux is much more than that.
If you spend a lot of time on your terminal, I urge you to take some time to
learn tmux, you’ll be surprised by it. Anyway, the point of this post is to show
you its scriptability.
I hacked together the following script from various sources online.
This script is to manage my workspace for Zammu(Zammu an
awesome continuous delivery app that I am currently working on, Go check it out
at https://zammu.in/). Zammu is a rails app, it is architected to use a bunch of
microservices, so to start any meaningful work I need to fire up those agents
too. Doing this manually is very tedious, with tmux I have one command to do it:
I just run tmz and it does the following:
Opens my editor with my TODO file in the first window.
Opens a pry console in the second window.
Creates a split pane in the second window with a bash terminal, also runs
the git log command, git status command and launches a browser with my
server’s url.
Creates a third window with rails server in the first pane, sidekiq in
the second pane, foreman start in the third pane which starts all the
agents and a guard agent for livereload in a tiny 2 line pane.
Finally it switches to the first window and puts me in my editor.
This has been saving me a lot of time, I hope you find it useful.
I have similar workspace setter uppers for my communication (mutt,
rainbowstream, irssi) and other projects.
I just ran the command history | grep '2016-02-17' | wc and it gave me 591 3066 23269
That is 591 commands in 3066 words and 23269 characters and that’s just the terminal.
Do yourself a favor and use tmux.
I have also created a short screencast for it, check it out.
# open these only if we don't already have a session # if we do just attach to that session if [ $? != 0 ] then # -n => name of window tmux new-session -d -s ${SESSION_NAME} -c ${ROOT_DIR} -n src
There are manygems
which do app configuration loading for ruby.
However, you don’t really need a gem to do environment loading. Here is a
snippet of code which does most of what you want.
1 2 3 4 5 6 7
classEnvLoader defload(path) YAML.load_file(path).each do|k, v| ENV[k] = v.to_s end end end
describe EnvLoader do describe '#load'do it 'imports stuff into ENV'do temp = "/tmp/#{Time.now.to_i}" File.write(temp, <<-EOS.strip_heredoc) SECRET: This is awesome FOO:33 EOS
EnvLoader.new.load(temp) expect(ENV['FOO']).to eq('33') expect(ENV['SECRET']).to eq("This is awesome") end end end
Guard is an awesome rubygem which allows livereload among other things.
However, when I run guard in tmux it was crashing all my tmux sessions. I guess
that is because I am using Tmux 2.2 and Guard tries to use Tmux notifications
for notifying about stuff. So, an easy way to fix this problem is to use
libnotify for notifications. Just add this line to your Guardfile and you
should be good.
This is a bad practice, This goes completely opposite to what static site generators are. Static site generators are meant to spit out the required HTML
to run it from any basic webserver/webhost. Also, there is Github Pages which is an excellent host which provides hosting
for static content. Heck, it even supports building of websites automatically using the Jekyll static site generator.
The servers which come bundled with the static site generators are a conveneince to test your site locally and not something to be hosted on a production server.
If you are a figure with a big following, please don’t propagate bad practices. It may seem like a fun/clever exercise for you, but it in the end it sends the wrong message.
While writing code to show themes in Zammu, I had to show the same button in two places on the same page.
The easy way is to duplicate the code. But that causes problems with maintainability.
<divclass="col-md-8"> <dl>....</dl> <!-- <<<<<<<<<<<<<<<<<<<<<<<<< SECOND COPY --> <%= form_tag("/") do %> <buttonclass='btn btn-lg btn-primary push-top-10'>Looks good, let's clone this</button> <% end %> </div>
</div>
To remove duplication I just used a content_for and captured the code that had to be duplicated and used yield to spit it out in the two places.
The changed code is:
However, understanding how to use them is not very straightforward to new users. Let us try to build a simple static site generator to better understand the problem.
The problems with managing websites are the issues of publishing, duplication and maintenance. If your website has multiple web pages, then more than 70% of the structure between the pages
is the same. This includes the styling, header, footer, navigation. If you write the html for your pages manually, things become difficult when you need to make changes.
That is why we have static generators to make things more maintainable.
The simplest way to build our generator would be to put the common stuff in one file and the changing content in other files.
For our example we’ll put the common markup in a file called layout.html and the page specific content in their own pages in a pages folder.
Now with the structure out of the way, we need to decide how we are going to notate the ‘changeable area’ or ‘placeholders’ in the layout.
I am using a dumb way to notate placeholder, we’ll use _PAGE_TITLE for the title and _PAGE_CONTENT for the page’s content. So our layout looks like this:
# this generates a static site into a public folder for the current directory
# create the folder FileUtils.mkdir_p "public"
# read the layout layout = File.read("layout.html")
# read the pages Dir["pages/*html"].each do|page_filepath| page = File.read(page_filepath) # replace the page title and page content title = File.basename(page_filepath) # we'll use the filename as the title rendered_page = layout.gsub("_PAGE_TITLE", title) rendered_page = rendered_page.gsub("_PAGE_CONTENT", page)
# write it out File.write("public/#{title}", rendered_page) puts "generated #{title}" end
You can just drag and drop files in your terminal to get the full path of the file. Then you can do whatever you want with it, e.g. in my case I had downloaded a google verification file, I just dragged it to my terminal and edited the command to scp it over to my server. This can be very useful when you have unwieldy filenames.
These days APIs are everythere which is a good thing. However, many APIs are very tedious. You can tell if your API is easy to use by looking at how simple it is to curl it.
echo"> appending submodule archives" # for each of git submodules append to the root archive git submodule foreach --recursive 'git archive --verbose --prefix=repo/$path/ --format tar master --output $ROOT_ARCHIVE_DIR/repo-output-sub-$sha1.tar'
if [[ $(ls repo-output-sub*.tar | wc -l) != 0 ]]; then # combine all archives into one tar echo"> combining all tars" tar --concatenate --file repo-output.tar repo-output-sub*.tar
# remove sub tars echo"> removing all sub tars" rm -rf repo-output-sub*.tar fi
# gzip the tar echo"> gzipping final tar" gzip --force --verbose repo-output.tar
echo"> moving output file to $OUTPUT_FILE" mv repo-output.tar.gz $OUTPUT_FILE
caseself when Hash root = OpenStruct.new(self) self.each_with_object(root) do|(k,v), o| o.send("#{k}=", v.to_ostruct) end root when Array self.map do|v| v.to_ostruct end else self end