Skip to content

Instantly share code, notes, and snippets.

View shinsaka's full-sized avatar
✍️
Writing python 🔥

Manabu Shinsaka shinsaka

✍️
Writing python 🔥
View GitHub Profile
@shinsaka
shinsaka / parse.rb
Last active April 18, 2017 10:35 — forked from tatsuru/parse.rb
ec2 RI parser
#!/usr/bin/env ruby
require 'json'
def parse(uri)
JSON.parse `curl -s #{uri}`.gsub(/\/\*.*\*\/\s*callback\(\{/m,'{').gsub("\);", '').gsub(/([a-zA-Z]+):/, '"\1":')
end
puts %w(
platform
@shinsaka
shinsaka / rds_ri_prices_parser.rb
Last active September 17, 2019 04:14
AWS RDS RI pricing parser
#!/usr/bin/env ruby
require 'json'
def parse(uri)
JSON.parse `curl -s #{uri}`.gsub(/\/\*(?:(?!\*\/).)*\*\//m, '').strip.gsub(/^callback\(/, '').gsub(/\);/, '').gsub(/([a-zA-Z]+[0-9]*):/, '"\1":')
end
puts %w(
platform
@shinsaka
shinsaka / replace comment block
Created February 2, 2016 03:30
Replace C like comment block sample
text = "
/*
* multi-line comment block
*/
void main() {
/* single-line comment block */
}"
puts text.gsub(/\/\*(?:(?!\*\/).)*\*\//m, '')
@shinsaka
shinsaka / redirect_correct_hostname_middleware.py
Created May 30, 2016 12:35
redirects to correct hostname if requested hostname and settings.CORRECT_HOST does not match
from django.http import HttpResponsePermanentRedirect
from django.conf import settings
class RedirectCorrectHostname(object):
"""
redirects to correct hostname if requested hostname and settings.CORRECT_HOST does not match
(at heroku domain redirects to custom domain)
"""
def process_request(self, request):
if not getattr(settings, 'CORRECT_HOST', None):
@shinsaka
shinsaka / ajax_required_decorator.py
Last active June 9, 2016 15:36
Decorator for determing whether the request is Ajax, in Django-views.
def ajax_required(ret_unexcepted):
"""
Decorator for determing whether the request is Ajax, in Django-views.
e.g.) in views.py
from django.http import HttpResponseBadRequest
from utils.decorators import ajax_required
@ajax_requirxed(HttpResponseBadRequest())
def index(request):
@shinsaka
shinsaka / testListObjectFromS3.rb
Last active September 3, 2017 09:08
S3 list_objets "max_keys" behavior test with AWS SDK V2 for Ruby
require 'aws-sdk'
def get_object_list(max_num)
options = {
region: 'ap-northeast-1',
access_key_id: 'AKI*****************',
secret_access_key: '****************************************'
}
Aws::S3::Client.new(options).list_objects(
bucket: '<YOUR-BUCKET-NAME>',
@shinsaka
shinsaka / jsongenerator_sample.cls
Created September 19, 2017 03:35
using JSONGenerator with Apex/SFDC
Hoge p = new Hoge();
p.code = '0001';
p.name = 'hogename1';
p.items = new List<HogeItem>{
new HogeItem('0001-01', 'item001'),
new HogeItem('0001-02', 'item002')
};
JSONGenerator jg = JSON.createGenerator(false);
jg.writeObject(p);
@shinsaka
shinsaka / getvalue.py
Created November 1, 2018 01:30
Get value from list of dict that has "Key" and "Value" key.
def getValue(key, items):
"""
Get value from list of dict that has "Key" and "Value" key.
"""
values = [x['Value'] for x in items if 'Key' in x and 'Value' in x and x['Key'] == key]
return values[0] if values else None
# 使用例
items = [{'Key': 'Name', 'Value': 'apple'},
@shinsaka
shinsaka / get_events.py
Last active March 28, 2024 06:06
Get all event messages of a group and stream from CloudWatch Logs AWS
import boto3
from datetime import datetime
def get_events(log_group_name, log_stream_name, region_name=None):
"""
Get all event messages of a group and stream from CloudWatch Logs AWS
"""
client = boto3.client('logs', region_name=region_name)
@shinsaka
shinsaka / AmazonConnectAgentEventFromKinesisStream.js
Last active September 3, 2021 05:04
Logging Sample for Amazon Connect Agent Event Stream by Lambda
exports.handler = async (event) => {
event.Records.map(({kinesis}) => {
const payload = Buffer.from(kinesis.data, 'base64').toString();
outputlog(JSON.parse(payload));
});
return {statusCode: 200};
};
function outputlog({EventType, EventTimestamp, CurrentAgentSnapshot}) {
if (EventType === 'HEART_BEAT') return;