Skip to content

Instantly share code, notes, and snippets.

View sahidursuman's full-sized avatar

Sahidur Rahman Suman sahidursuman

View GitHub Profile
@sahidursuman
sahidursuman / pixelpeeper_with_timeouts_and_caching.rb
Created May 31, 2018 08:59 — forked from derwiki/pixelpeeper_with_timeouts_and_caching.rb
Example thin JSON API client in Ruby, with one second global timeouts and local API response Redis caching
require 'httparty'
class PixelPeeper
include HTTParty
base_uri 'www.pixel-peeper.com'
default_timeout 1 # hard timeout after 1 second
def api_key
ENV['PIXELPEEPER_API_KEY']
end
@sahidursuman
sahidursuman / pixel_peeper_integration.rb
Created May 31, 2018 08:58 — forked from derwiki/pixel_peeper_integration.rb
Example of using the lightweight PixelPeeper API.
def example_pictures_for(gear)
pp = PixelPeeper.new
if gear.pp_lens_id.present?
pp.examples(lens_id: gear.pp_lens_id)
elsif gear.pp_camera_id.present?
pp.examples(camera_id: gear.pp_camera_id)
else
[]
end.take(8)
end
@sahidursuman
sahidursuman / gmail-invoice-parse.rb
Created May 31, 2018 08:58 — forked from derwiki/gmail-invoice-parse.rb
Ruby script utilizing the ruby-gmail gem that allows for searching and downloading Gmail messages by label. This implementation is looking for invoice emails and parsing the amount of money that was transferred.
require 'gmail'
label = 'transactional/payment'
Gmail.new(username, password) do |gmail|
puts "Emails that match label '#{ label }'"
lender_profit = 0
gmail.mailbox(label).emails.each do |email|
message = email.message
next if message.subject =~ /^Re: /
next if message.subject =~ /^Fwd: /
@sahidursuman
sahidursuman / gmail-invoice-parse.rb
Created May 31, 2018 08:58 — forked from derwiki/gmail-invoice-parse.rb
Ruby script utilizing the ruby-gmail gem that allows for searching and downloading Gmail messages by label. This implementation is looking for invoice emails and parsing the amount of money that was transferred.
require 'gmail'
label = 'transactional/payment'
Gmail.new(username, password) do |gmail|
puts "Emails that match label '#{ label }'"
lender_profit = 0
gmail.mailbox(label).emails.each do |email|
message = email.message
next if message.subject =~ /^Re: /
next if message.subject =~ /^Fwd: /
def pay_lender!(token = nil)
fail unless lender.stripe_recipient_id || token
recipient = lender.stripe_recipient_id ||
create_recipient!(token, lender.name).id
Stripe::Transfer.create(
amount: (lender_profit * 100).round,
currency: "usd",
recipient: lender.stripe_recipient_id,
statement_description: "CameraLends Lending"
)
@sahidursuman
sahidursuman / bulk_insert.rb
Created May 31, 2018 08:57 — forked from derwiki/bulk_insert.rb
Comparing runtime characteristics of individual transactions, one transaction, and one statement.
def insert_many
1000.times { SeoRank.create! }
end
start = Time.now
ActiveRecord::Base.transaction { insert_many }
puts "N transactions: #{ Time.now - start }"
start = Time.now
ActiveRecord::Base.transaction { insert_many }
Class.new(ActiveSupport::LogSubscriber) do
def sql(event)
return
query = event.payload[:sql]
if query =~ /SELECT/
Rails.logger.info "Traced query: #{query}"
Rails.logger.info '!stacktrace!begin'
# Rails.logger.info Rails.backtrace_cleaner.clean(caller).join("\n")
Rails.logger.info Rails.backtrace_cleaner.clean(caller).first
Rails.logger.info '!stacktrace!end'
@sahidursuman
sahidursuman / s3.sh
Created May 31, 2018 08:56 — forked from derwiki/s3.sh
PUT to S3 via cURL, Bash -- no external dependencies
#!/bin/bash
# usage ./s3.sh filename.ext remote/path/filename.ext
S3_KEY="REDACTED"
S3_SECRET="REDACTED"
S3_BUCKET="REDACTED"
REMOTE_PATH=$2
date=$(date +"%a, %d %b %Y %T %z")
acl="x-amz-acl:public-read"
@sahidursuman
sahidursuman / review-apps-postdeploy.sh
Created May 31, 2018 08:56 — forked from derwiki/review-apps-postdeploy.sh
`postdeploy` script for Heroku Review Apps that replaces the default Postgres DB with a MySQL DB from ClearDB.
#!/app/bin/ruby
require 'platform-api'
cleardb_url = ENV['CLEARDB_DATABASE_URL']
if cleardb_url.nil? || cleardb_url == ''
puts "Error: CLEARDB_DATABASE_URL not set"
exit -1
end
@sahidursuman
sahidursuman / parser.rb
Created May 31, 2018 08:56 — forked from derwiki/parser.rb
Using `parser` to look for `Resque.enqueue` calls inside a `ActiveRecord::Base.transaction` block.
require 'parser/current'
class Processor < AST::Processor
attr_accessor :verbose
def initialize(*args)
super
self.verbose = false
end