Skip to content

Instantly share code, notes, and snippets.

@amitsaxena
amitsaxena / gist:9cb0712e572c39a41edc
Last active October 24, 2019 12:37
CustomURL: Launch app if app is installed, else open an alternate URL (Android all browsers)
<script type="text/javascript">
var custom = "myapp://custom_url";
var alt = "http://mywebsite.com/alternate/content";
var g_intent = "intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end";
var timer;
var heartbeat;
var iframe_timer;
function clearTimers() {
clearTimeout(timer);
@amitsaxena
amitsaxena / get_s3_file_sizes_recursively.rb
Last active August 29, 2015 13:58
Get file size of all the files at a given path on Amazon S3 - recursively, and dump in a CSV
# Using aws-sdk gem
@access_key = "access_key"
@secret_key = "secret_access_key"
@bucket = "my_bucket"
AWS.config(
:access_key_id => @access_key,
:secret_access_key => @secret_key
)
@amitsaxena
amitsaxena / gist:8625016
Created January 25, 2014 22:59
Custom URL: Launch app if app is installed, else open an alternate URL (iOS Safari only)
<script type="text/javascript">
var timer;
window.addEventListener("pageshow", function(evt){
clearTimeout(timer);
}, false);
window.addEventListener("pagehide", function(evt){
clearTimeout(timer);
}, false);
@amitsaxena
amitsaxena / gist:8601424
Last active October 18, 2019 21:51
CustomURL: Launch app if app is installed, else open an alternate URL (iOS all browsers)
<script type="text/javascript">
var timer;
var heartbeat;
var lastInterval;
function clearTimers() {
clearTimeout(timer);
clearTimeout(heartbeat);
}
@amitsaxena
amitsaxena / gist:6977506
Last active February 7, 2017 18:33
How to get object creation timestamp from BSON object id using mongoid, and do range queries on it.
# To get the created at timestamp
sample_object.id.generation_time
# To do range queries
start = Moped::BSON::ObjectId.from_time(Time.now.beginning_of_day)
finish = Moped::BSON::ObjectId.from_time(Time.now.end_of_day)
MyModel.where(:id => {'$gt' => start, '$lt' => finish}).count
@amitsaxena
amitsaxena / gist:5209033
Last active December 15, 2015 05:29
Get a recursive listing of all files at at a given Amazon S3 path
credentials = YAML.load_file("#{Rails.root}/config/s3.yml")
@access_key = credentials[Rails.env]["access_key_id"]
@secret_key = credentials[Rails.env]["secret_access_key"]
@bucket = credentials[Rails.env]["bucket"]
AWS.config(
:access_key_id => @access_key,
:secret_access_key => @secret_key
)
@s3 = AWS::S3.new
Ruby aws-sdk has poor documentation, and I wasn't able to locate it as well. Below is a function that I have created to check whether a file has read permission or not. Modify it as per your needs:
def check_if_public_read(object)
object.acl.grants.each do |grant|
begin
if(grant.grantee.uri == "http://acs.amazonaws.com/groups/global/AllUsers")
return true if ([:read, :full_control].include?(grant.permission.name))
end
rescue
end
class Link < ActiveRecord::Base
validate :url_presence
def url_presence
errors.add(:base, "#{Link.url_label(linktype)} can't be blank.") if url.blank?
end
def self.url_label(linktype)
{"web" => "Web URL", "video" => "Video URL", "audio" => "Audio URL", "ecommerce" => "Shop URL",
"page" => "Page number", "email" => "Email"}[linktype] || "URL"
def url_for_oauth_code(options = {})
# for permissions, see http://developers.facebook.com/docs/authentication/permissions
permissions = options[:permissions]
scope = permissions ? "&scope=#{permissions.is_a?(Array) ? permissions.join(",") : permissions}" : ""
display = options.has_key?(:display) ? "&display=#{options[:display]}" : ""
callback = options[:callback] || @oauth_callback_url
raise ArgumentError, "url_for_oauth_code must get a callback either from the OAuth object or in the options!" unless callback