Skip to content

Instantly share code, notes, and snippets.

View cybersamx's full-sized avatar

Samuel Chow cybersamx

  • Los Angeles, Ca
  • 15:25 (UTC -07:00)
View GitHub Profile
@cybersamx
cybersamx / ssh-key-generation.md
Last active July 3, 2023 17:38
Generate a new SSH key pair

Mac and Linux

  • Open Terminal

  • Check if you already have a SSH keypair generated. Do the following:

    $ ls -la ~/.ssh/id_rsa*
    

If the files exist, you already have SSH installed. IMPORTANT: But if you wish to regenerate the SSH key pair, at least back up your old SSH keys.

@cybersamx
cybersamx / sed_match_n_replace
Last active August 29, 2015 14:16
Ways for sed to match and replace lines
# Remove all occurences of string_to_match
sed -i '/string_to_match/d' file
# Match and add new string line
sed -i '/string_to_match/a new_string' file
# Match string and replace the entire line with string line
sed -i '/string_to_match/c\new_string' file
# Match the string and replace with sring
@cybersamx
cybersamx / logstash_forwarder_on_ubuntu.md
Last active December 27, 2018 02:18
Install Logstash Forwarder on Ubuntu

Installation

Add an apt repository and install the logstash-forwarder package.

$ sudo sh -c 'echo "deb http://packages.elasticsearch.org/logstashforwarder/debian stable main" > /etc/apt/sources.list.d/logstash.list'
$ wget -O - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install logstash-forwarder
@cybersamx
cybersamx / rails_notice_vs_flash
Created October 22, 2014 01:25
Rails notice vs flash
# The mechanism for embedding a notification in a Rails controller is different for render and redirect.
def ping1
flash[:error] = 'Warning...'
render action: 'pong'
end
def ping2
redirect_to pong_path, notice: 'Warning...'
end
@cybersamx
cybersamx / edit.html.erb
Last active August 29, 2015 14:07
Rails select_tag and data validation
<%= form_for @my_model, html: { class: 'form-horizontal' } do |f| %>
<%= f.label :id, class: 'control-label %>
<div class="controls">
<%= f.select(:id, MyModel.all.pluck(:name, :id), include_blank: 'Select') %>
</div>
<% end %>
@cybersamx
cybersamx / Install_SwaggerUI.md
Created September 29, 2014 22:56
Installing SwaggerUI

Steps for installing SwaggerUI on the Mac

  1. Clone SwaggerUI project
$ git clone git@github.com:wordnik/swagger-ui.git
  1. Install nginx (optioinal)
@cybersamx
cybersamx / script_to_run_in_rails.rb
Last active July 26, 2023 18:40
Load and execute a Ruby script in Rails console
# Say you need to run this script which is not part of your Rails app...
# Do the following:
#
# 1. Assume we put this script file in <Rails app root>/tmp
# 2. $ cd <Rails app root>
# 3. $ rails console
# 4. ruby > load "#{Rails.root}/tmp/script_to_run_in_rails.rb"
# 5. ruby > MyClass.echo
# Hello
# => nil
@cybersamx
cybersamx / capybara_cheat_sheet.rb
Last active June 26, 2022 08:32
Capybara (visit/page) cheat sheet
# Navigation
visit('/resources')
visit(resource_path(resource))
# Clicking buttons and hyperlinks
click_button('Submit Button')
click_link('Link Text')
click_link('Link id')
click('Link or button')
@cybersamx
cybersamx / activerecord_exotic_queries.rb
Created August 19, 2014 21:18
Rails ActiveRecord exotic data queries
# Unique values of a column
Address.uniq.pluck(:state)
@cybersamx
cybersamx / curl_commands.sh
Last active August 29, 2015 14:05
Commonly used CURL commands for testing/debugging
# GET a JSON result.
curl -i -H "Accept: Application/json" -H "Content-Type: application/json" http://localhost/sessions.json
# POST a JSON result.
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" -d '{"id":"IDVALUE","name":"Sam"}' http://localhost/sessions.json
# Use cookies (write/read).
curl -c cookies.txt -b cookies.txt http://localhost/users.json
# Follow redirection.