Skip to content

Instantly share code, notes, and snippets.

View tinhnq's full-sized avatar

Tinh Nguyen tinhnq

  • Viet Nam
View GitHub Profile
@tinhnq
tinhnq / vultr_dokku_rails.md
Created November 11, 2017 04:21 — forked from shirren/vultr_dokku_rails.md
How to deploy a Rails 4/5 app on Vultr or Digital Ocean using a stock Ubuntu image with Dokku 0.6.5v

Rails/Dokku Deployment Procedure

This procedure has been tested on Vultr, and Digital Ocean. In theory it should work on any VPS as long as the linux instance is Ubuntu. There might be some subtle differences for other linux variants.

  1. Create a new compute instance on Vultr using an 64-bit ubuntu image
  • From the available compute instance sizes, pick at a minimum a server with 1024Mb of memory. Compute instances with smaller sizes will fail on deploy
  1. ssh on to the server root@ipaddress
  • If this is your first deploy onto Vultr ensure you've created a ssh key pair firstly. Then add your public key during the compute instance creation procedure
  • Now wait for Vultr to provision an instance of your new image
  1. Install Dokku 0.6.5
@tinhnq
tinhnq / curl.md
Created December 6, 2017 07:01 — forked from subfuzion/curl.md
curl POST examples

Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.

-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).

-c, --cookie-jar <file name> File to save response cookies to.

@tinhnq
tinhnq / running_app_in_production_locally.markdown
Created February 24, 2018 10:40 — forked from rwarbelow/running_app_in_production_locally.markdown
How to Run a Rails App in Production Locally
  1. Add gem 'rails_12factor' to your Gemfile. This will add error logging and the ability for your app to serve static assets.
  2. bundle
  3. Run RAILS_ENV=production rake db:create db:migrate db:seed
  4. Run rake secret and copy the output
  5. From the command line: export SECRET_KEY_BASE=output-of-rake-secret
  6. To precompile your assets, run rake assets:precompile. This will create a folder public/assets that contains all of your assets.
  7. Run RAILS_ENV=production rails s and you should see your app.

Remember to clobber your assets (rake assets:clobber) and re-precompile (rake assets:precompile) if you make changes.

@tinhnq
tinhnq / nonAccentVietnamese.js
Created February 4, 2020 10:24 — forked from jarvisluong/nonAccentVietnamese.js
Converting standard Vietnamese Characters to non-accent ones. Example: hải -> hai
function nonAccentVietnamese(str) {
str = str.toLowerCase();
// We can also use this instead of from line 11 to line 17
// str = str.replace(/\u00E0|\u00E1|\u1EA1|\u1EA3|\u00E3|\u00E2|\u1EA7|\u1EA5|\u1EAD|\u1EA9|\u1EAB|\u0103|\u1EB1|\u1EAF|\u1EB7|\u1EB3|\u1EB5/g, "a");
// str = str.replace(/\u00E8|\u00E9|\u1EB9|\u1EBB|\u1EBD|\u00EA|\u1EC1|\u1EBF|\u1EC7|\u1EC3|\u1EC5/g, "e");
// str = str.replace(/\u00EC|\u00ED|\u1ECB|\u1EC9|\u0129/g, "i");
// str = str.replace(/\u00F2|\u00F3|\u1ECD|\u1ECF|\u00F5|\u00F4|\u1ED3|\u1ED1|\u1ED9|\u1ED5|\u1ED7|\u01A1|\u1EDD|\u1EDB|\u1EE3|\u1EDF|\u1EE1/g, "o");
// str = str.replace(/\u00F9|\u00FA|\u1EE5|\u1EE7|\u0169|\u01B0|\u1EEB|\u1EE9|\u1EF1|\u1EED|\u1EEF/g, "u");
// str = str.replace(/\u1EF3|\u00FD|\u1EF5|\u1EF7|\u1EF9/g, "y");
// str = str.replace(/\u0111/g, "d");
@tinhnq
tinhnq / get-latest-tag-on-git.sh
Created February 11, 2020 07:13 — forked from rponte/get-latest-tag-on-git.sh
Getting latest tag on git repository
# The command finds the most recent tag that is reachable from a commit.
# If the tag points to the commit, then only the tag is shown.
# Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object
# and the abbreviated object name of the most recent commit.
git describe
# With --abbrev set to 0, the command can be used to find the closest tagname without any suffix:
git describe --abbrev=0
# other examples
@tinhnq
tinhnq / benchmark.rb
Created March 10, 2020 08:17 — forked from panthomakos/benchmark.rb
Benchmark Your Bundle
#!/usr/bin/env ruby
require 'benchmark'
REGEXPS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
/^Missing API definition file in (.+)$/i,
/^cannot load such file -- (.+)$/i,
]
@tinhnq
tinhnq / application_helper.rb
Created August 11, 2020 08:06 — forked from dnagir/application_helper.rb
Render a different variant in rails
module ApplicationHelper
def with_variant(new_variant, &block)
old_variants = lookup_context.variants
begin
lookup_context.variants = [new_variant]
return block.call
ensure
lookup_context.variants = old_variants
end
end
@tinhnq
tinhnq / call_template.rb
Created August 11, 2020 08:50 — forked from juggy/call_template.rb
Render a complete page in rails 3 without controller
# create the template
template = PageOfflineTemplate.new
template.quote = quote
template.pages = quote.build_pages
# Here I render a template with layout to a string then a PDF
pdf = PDFKit.new template.render_to_string(:template=>"quotes/review.html.haml")
require 'nokogiri'
require 'open-uri'
# Get a Nokogiri::HTML:Document for the page we're interested in...
doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))
# Do funky things with it using Nokogiri::XML::Node methods...
####