Skip to content

Instantly share code, notes, and snippets.

View mataki's full-sized avatar

Akihiro Matsumura mataki

  • SonicGarden
  • Kanagawa, Japan
  • X @mat_aki
View GitHub Profile
@mataki
mataki / user.rb
Last active December 19, 2015 09:19
mailchimp sync sample
class User < ActiveRecord::Base
def subscribe_mailchimp_list(client = Gibbon.new)
if ENV["MAILCHIMP_LIST_ID"]
logger.info "add #{self.email} to list #{ENV["MAILCHIMP_LIST_ID"]}"
client.list_subscribe(id: ENV["MAILCHIMP_LIST_ID"],
email_address: self.email,
double_optin: false,
send_welcome: false,
update_existing: true,
merge_vers: {:FNAME => self.first_name,
@mataki
mataki / file0.rb
Last active December 18, 2015 13:48
AWS の S3 のファイルを別の bucket にコピーする方法 ref: http://qiita.com/mat_aki/items/90205144c3f93758e357
require 'right_aws'
aws_access_key_id, aws_secret_access_key = "key_id", "access_key"
old_bucket_name = 'from-bucket'
new_bucket_name = "to-bucket"
s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key)
bucket = s3.bucket(old_bucket_name)
puts "Copy S3 files from '#{old_bucket_name}' to '#{new_bucket_name}'"
@mataki
mataki / factory_girl_massassignment.rb
Created June 5, 2013 05:36
factory girl は mass assignment を回避してデータを作れるか? YES
require "active_record"
require "sqlite3"
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
ActiveRecord::Base.establish_connection('test')
class CreateAllTables < ActiveRecord::Migration
def self.up
create_table :locales do |t|
t.string :name
require "rubygems"
require "capybara-webkit"
require "mini_magick"
class WebkitScreenshot
def initialize
@browser = Capybara::Driver::Webkit::Browser.new
end
def browser
@mataki
mataki / facebook_test_user.rb
Created June 27, 2011 06:03
FbGraphでテストユーザ作成
# -*- coding: utf-8 -*-
require "rubygems"
require "fb_graph"
app_id = ""
csecret = ""
app = FbGraph::Application.new(app_id, :secret => csecret)
# ユーザ作って友達にするとか
@mataki
mataki / rails2311_csrf_path.rb
Created February 9, 2011 07:11
path for csrf risk on rails 2.3.11
module ActionController
module RequestForgeryProtection
protected
def verify_authenticity_token
verified_request? || handle_unverified_request
end
# Process logout
def handle_unverified_request
reset_session
$(document).ajaxSend(function(e, xhr, options) {
var token = $("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
});
@mataki
mataki / heroku_autoscalling.rb
Created December 2, 2010 06:33
Auto scalling dynos on heroku using NewRelic
=begin
Need to install gems heroku, newrelic_rpm
$ gem install heroku newrelic_rpm
Set your apps setting
app_name : heroku's app_name of auto scaling
license_key : NewRelic api key. You can get heroku's NewRelic admin console. "App setting" and "Agent configuration"
execute with cron every minutes
$ ruby ./adjust_dynos_with_newrelic.rb
require "net/http"
require "uri"
http = Net::HTTP.new("www.youroom.local", "3000")
http.send_request('CONNECT', "/")
# Group -has_many-> Participation
# Group.joined ==> defined named_scope
# Group.user_id_not_null ==> genarated named_scope by searchlogic
g = Group.find(1)
# SQL) SELECT * FROM `groups` WHERE (`groups`.`id` = 1)
g.participations
# SQL) SELECT * FROM `participations` WHERE (`participations`.group_id = 1)