Instantly share code, notes, and snippets.

View 0001_sidekiq.rb
# Sidekiq interaction and startup script
commands:
create_post_dir:
command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_sidekiq.sh":
mode: "000755"
owner: root
group: root
View upload-images-s3-sdk-ruby
Using gem aws-sdk for a ror application for uploading images to s3
Uploading images to a fixed bucket with different folders for each object or application.
The s3 keeps a limitation on the number of buckets creattion whereas there is no
limitation for content inside a bucket.
This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public
so that the images uploaded are directly accessible. The input it takes is the image complete path
where it is present, folder in which it should be uploaded and user_id for whom it should
be uploaded.
View estimate.rb
# Main reference was lascarides' post at http://stackoverflow.com/questions/14743447/getting-pdf-from-wickedpdf-for-attachment-via-carrierwave
# estimate.rb
# ...
has_attached_file :pdf,
storage: :s3,
s3_credentials: {
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
bucket: ENV['AWS_BUCKET']
View upgrade_es_on_semaphoreci.sh
sudo service elasticsearch stop
if ! [ -e .semaphore-cache/elasticsearch-2.4.4.deb ]; then (cd .semaphore-cache; curl -OL https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.4.4/elasticsearch-2.4.4.deb); fi
echo Y | sudo dpkg -i .semaphore-cache/elasticsearch-2.4.4.deb
sudo sh -c "echo 'script.inline: true' >> /etc/elasticsearch/elasticsearch.yml"
sudo sh -c "echo 'script.groovy.sandbox.enabled: true' >> /etc/elasticsearch/elasticsearch.yml"
sudo sh -c "echo 'index.number_of_replicas: 0' >> /etc/elasticsearch/elasticsearch.yml"
sudo sh -c "echo 'index.number_of_shards: 1' >> /etc/elasticsearch/elasticsearch.yml"
sudo service elasticsearch start
sleep 10 && curl -XGET 'localhost:9200'
View semaphore-es.sh
#!/bin/bash
# Install a custom ElasticSearch version - https://www.elastic.co/products/elasticsearch
#
# To run this script on SemaphoreCI, add the following command to your project's build setup:
# \curl -sSL <RAW_URL_FOR_THIS_SCRIPT> | bash -s
#
ELASTICSEARCH_VERSION="0.90.13"
ELASTICSEARCH_PORT="9200"
ELASTICSEARCH_DIR="$SEMAPHORE_PROJECT_DIR/elasticsearch"
ELASTICSEARCH_WAIT_TIME="15"
View form_user.rb
class FormUser < User
attr_accessor :current_password
validates_presence_of :email, if: :email_required?
validates_uniqueness_of :email, allow_blank: true, if: :email_changed?
validates_format_of :email, with: Devise.email_regexp, allow_blank: true, if: :email_changed?
validates_presence_of :password, if: :password_required?
validates_confirmation_of :password, if: :password_required?
validates_length_of :password, within: Devise.password_length, allow_blank: true
View gcal_chk.rb
# coding: utf-8
require 'rest-client'
require 'json'
require 'date'
require 'gmail'
require 'yaml'
#google-api-clientはv0.6.4が必要です
require "google/api_client"
View contact_importer.rb
require 'oauth'
require 'oauth2'
class ContactImporter
def initialize(importer)
@importer = importer
end
def authorize_url
case @importer
View token.rb
# The OAuth access token provided by the Google API expires in 60 minutes. After expiration,
# you must exchange a refresh token for a new access token. Unfortunately, the the Google API
# ruby gem does not include a method for refreshing access tokens.
# You can read up on how to refresh an access token here:
# https://developers.google.com/accounts/docs/OAuth2WebServer#refresh
# This Token model implements that process. It's based off of a Token model that can be created
# by running:
# rails g model Token token:text refresh_token:string expires_at:datetime
View get_attachment_id.rb
# Copyright (C) 2012 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,