Skip to content

Instantly share code, notes, and snippets.

View mscoutermarsh's full-sized avatar

Mike Coutermarsh mscoutermarsh

View GitHub Profile
require 'pony'
require "openssl"
require "net/smtp"
Net::SMTP.class_eval do
private
def do_start(helodomain, user, secret, authtype)
raise IOError, 'SMTP session already started' if @started
if RUBY_VERSION > "1.8.6"
check_auth_args user, secret # for rails 1.8.7
@mscoutermarsh
mscoutermarsh / push.pl
Created January 29, 2012 19:31
perl script... automate commit and push to github/heroku
#perl script to automate git push
`git add . >/dev/null`;
print "add\n";
print "Commit Message:";
$cmsg = <>;
`git commit -m '$cmsg' >/dev/null`;
print "\npushing to github...\n";
print "#################################\n";
`git push > /dev/null`;
print "#################################\n";
# Copy and paste this to the rails console to test your email settings
class MyMailer < ActionMailer::Base
def msg
mail(
# Option 2
:from => "mailer@taskk.it",
:to => "coutermarsh.mike@gmail.com",
:subject => "hey dude"
)
@mscoutermarsh
mscoutermarsh / gist:3927973
Created October 21, 2012 18:21
Send user email in their timezone
# Set zone to the users time zone
Time.zone = user.time_zone
# convert time to UTC
user_time_to_utc = Time.zone.parse('2012-10-22 09:25:00').utc
puts "Scheduling for #{user.login} at #{user_time_to_utc}"
# Schedule the sidekiq job
SendPromoEmail.perform_at(user_time_to_utc,user.id)
@mscoutermarsh
mscoutermarsh / api_key.rb
Created November 24, 2012 20:20
API Key model
# This is how we grant access to the API.
# User logs in via the API. Access_token is granted and returned to them.
# On subsequent logins they use the access_token rather than their credentials.
class ApiKey < ActiveRecord::Base
attr_accessible :access_token, :expires_at, :user_id, :active, :application
before_create :generate_access_token
before_create :set_expiration
belongs_to :user
@mscoutermarsh
mscoutermarsh / createapikeys.rb
Last active October 13, 2015 04:47
api key migration
class CreateApiKeys < ActiveRecord::Migration
def change
create_table :api_keys do |t|
t.string :access_token, null: false
t.integer :user_id, null: false
t.boolean :active, null: false, default: true
t.datetime :expires_at
t.timestamps
end
@mscoutermarsh
mscoutermarsh / apihelpers.rb
Created November 24, 2012 23:03
Authentication helpers
helpers do
def authenticate!
error!('Unauthorized. Invalid or expired token.', 401) unless current_user
end
def current_user
# find token. Check if valid.
user_token = params[:token]
token = ApiKey.where(:access_token => user_token).first
if token && !token.expired?
@mscoutermarsh
mscoutermarsh / api_auth.rb
Created November 25, 2012 01:40
Grape API authentication methods
# /api/auth
resource :auth do
desc "Creates and returns access_token if valid login"
params do
requires :login, :type => String, :desc => "Username or email address"
requires :password, :type => String, :desc => "Password"
end
post :login do
@mscoutermarsh
mscoutermarsh / root.rb
Created November 26, 2012 02:53
Grape - Root API
module API
class Root < Grape::API
prefix "api"
version 'v1', :using => :header, :vendor => 'vendor'
format :json
error_format :json
# load the rest of the API
mount API::Auth
mount API::Tasks
@mscoutermarsh
mscoutermarsh / auth.rb
Created November 27, 2012 00:18
Grape API authentication methods
module API
class Auth < Grape::API
# /api/auth
resource :auth do
#
# auth code goes here!
#
desc "Returns pong if logged in correctly."