Skip to content

Instantly share code, notes, and snippets.

@raecoo
raecoo / awk-example.sh
Last active September 21, 2023 07:12
awk/grep commands for Rails log analysis
# Access number
cat production.log | grep "^Processing" | wc | awk '{print $1}'
# Each IP access number
cat production.log | grep “^Processing” | awk ‘{print $4}’ | uniq -c
# Independent IP number
cat production.log | grep "^Processing" | awk '{print $4}' | uniq | wc | awk '{print $1}'
cat production.log | grep “^Processing” | awk ‘{print $4}’ | uniq | wc -l
@raecoo
raecoo / google_api_client.rb
Last active August 29, 2015 14:07
User Google API Client with Rails
# gems
gem 'google-api-client'
gem 'signet'
# Rails.root/config/initializers/google_api_client.rb
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
$google_api_client = Google::APIClient.new(
:application_name => 'Example for Google API client',
@raecoo
raecoo / commands.sql
Last active August 29, 2015 14:07
updates mysql character for existing database
# References
# http://stackoverflow.com/questions/6115612/how-to-convert-an-entire-mysql-database-characterset-and-collation-to-utf-8
# http://www.a2hosting.com/kb/developer-corner/mysql/convert-mysql-database-utf-8
ALTER DATABASE [DB-NAME] CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE [TABLE-NAME] CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
@raecoo
raecoo / placeholder-text-color.css
Created October 17, 2014 07:52
customized text color to input placeholder
::-webkit-input-placeholder {
color: black;
}
:-moz-placeholder { /* Firefox 18- */
color: black;
}
::-moz-placeholder { /* Firefox 19+ */
color: black;
@raecoo
raecoo / image_size_validator.rb
Last active August 29, 2015 14:09
image size validator
# Refs: http://stackoverflow.com/questions/7527887/validate-image-size-in-carrierwave-uploader
class ImageSizeValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value.blank?
image = MiniMagick::Image.open(value.path)
checks = [
{ :option => :width,
:field => :width,
@raecoo
raecoo / random.string.rb
Created December 2, 2014 13:54
random string in ruby
# Only upper case alphabets [A-Z]
value = "";
8.times{value << (65 + rand(25)).chr} >>}
#or
(0...8).map{65.+(rand(26)).chr}.join
(0...8).map{ ('A'..'Z').to_a[rand(26)] }.join
# Lower case and upper case [a-zA-Z]
value = "";
8.times{value << ((rand(2)==1?65:97) + rand(25)).chr} >>}
@raecoo
raecoo / example.html
Last active August 29, 2015 14:12
how to defined the name of front-end selectors
<!--Normal CSS selector name-->
<div class="here-is-a-css-class-name">
....
</div>
<!--Noram JS selector name(CSS selector with prefix 'js-') to To avoid the javascript code was thrown error -->
<div class="js-action-name">
....
</div>
<!--Both CSS selector and JS selector work together-->
<div class="style-class-name js-action-name">
@raecoo
raecoo / byebug.rb
Created December 30, 2014 14:51
Use Byebug with Pow
# .powenv
export BYEBUGPORT=3001
# config/initializers/byebug.rb
if Rails.env.development? and ENV['BYEBUGPORT']
require 'byebug'
Byebug.start_server 'localhost', ENV['BYEBUGPORT'].to_i
end
# restart Pow process
# Big thank you to Scott Wheeler and BBG in the Shopify API Forums
# http://ecommerce.shopify.com/c/shopify-apis-and-technology/t/paginate-api-results-113066
module ShopifyAPI
class Base
RETRY_AFTER = 60
def self.find_all(params = {}, &block)
params[:limit] ||= 50
params[:page] = 1
@raecoo
raecoo / convert-utf8mb4.rb
Created March 18, 2015 01:47
convert character set to utf8mb4 used for support Emoji on MySQL
desc "Database related tasks"
namespace :database do
desc "Convert to utf8mb4"
task convert_to_utf8mb4: :environment do
connection = ActiveRecord::Base.connection
database = connection.current_database
connection.execute "ALTER DATABASE #{database} CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;"
puts "Converted #{database} character set"