Skip to content

Instantly share code, notes, and snippets.

@tarot
tarot / DateFormat.cls
Last active February 19, 2020 16:09
Apexでhttp-date (RFC1123)
public class DateFormat {
private static final Map<String, Integer> MONTHS = new Map<String, Integer> {
'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6,
'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12
};
public static Datetime fromRFC1123(String x) {
String[] dateParts = x.split('[\\s:]');
Integer year = Integer.valueOf(dateParts[3]);
@IsTest
private class CalloutSampleTest {
public class CalloutMock implements HttpCalloutMock {
public HttpResponse respond(HttpRequest req) {
return new HttpResponse();
}
}
@IsTest
@IsTest
private class CalloutSampleTest {
public class CalloutMock implements HttpCallout.Service {
public HttpResponse respond(HttpRequest req) {
return new HttpResponse();
}
}
@IsTest
@tarot
tarot / a.rb
Created October 30, 2015 14:40
class Parent < ActiveRecord::Base
# t.string name
has_many :children
end
class Child < ActiveRecord::Base
# t.integer parent_id
# t.boolean hide_parent
belongs_to :parent
module Tokenizable
extend ActiveSupport::Concern
module ClassMethods
def from_token(token)
row = Rails.cache.read(token).try { |id| find_by_id(id) }
Rails.cache.write(token, row.id, expires_in: 2.hours.to_i) if row.present?
row
end
end
class ApplicationController < ActionController::Base
rescue_from StandardError, with: :error500
def error500
respond_to do |format|
format.json { render json: {message: 'Internal Server Error'}, status: :internal_server_error }
# ↓こいつのContent-Typeをtext/htmlに固定したい
format.any { render 'error500', formats: :html, status: :internal_server_error }
end
end
require 'json'
require 'json/pure/generator'
module JSON::Pure::Generator::GeneratorMethods
%i(Fixnum Bignum).each do |constant|
const_set constant, Integer
end
module Array
alias_method :pure_to_json, :to_json
@tarot
tarot / normalize_schemafile.rb
Created February 1, 2016 15:28
ridgepoleのSchemafileのカラムをソートするやつ(テーブルはソートされてる)
module SchemafileNormalizer
Table = Struct.new(:facing, :columns)
def self.normalize!(filename)
src = File.open(filename) { |io| io.read }
File.open(filename, 'w') { |io| io.write(sort_column(src)) }
end
def self.sort_column(src)
src.each_line.with_object([Table.new([], [])]) { |line, a|
@tarot
tarot / CalloutJob.cls
Created February 1, 2016 16:28
QueueableとCalloutとTest
public class CalloutJob implements Queueable, Database.AllowsCallouts {
public void execute(QueueableContext context) {
String endpoint = 'https://trust.salesforce.com/';
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(endpoint);
new Http().send(req);
}
@tarot
tarot / README.md
Created July 4, 2016 07:20 — forked from nicerobot/README.md
Mac OS X uninstall script for packaged install of node.js

To run this, you can try:

curl -ks https://gist.githubusercontent.com/nicerobot/2697848/raw/uninstall-node.sh | bash

I haven't tested this script doing it this way but i run a lot of my Gists like this so maybe this one'll work too.

Alternatively,

curl -ksO https://gist.githubusercontent.com/nicerobot/2697848/raw/uninstall-node.sh

chmod +x ./uninstall-node.sh