Skip to content

Instantly share code, notes, and snippets.

View mscoutermarsh's full-sized avatar

Mike Coutermarsh mscoutermarsh

View GitHub Profile
@mscoutermarsh
mscoutermarsh / lists.rb
Created November 27, 2012 00:20
Lists API methods
module API
class Lists < Grape::API
# /api/lists
# all of your methods go here!
end
end
@mscoutermarsh
mscoutermarsh / application.rb
Created December 15, 2012 18:35
CORS - rails/grape
# Add this to your application.rb
config.middleware.use Rack::Cors do
allow do
origins '*'
# location of your API
resource '/api/*', :headers => :any, :methods => [:get, :post, :options, :put]
end
end
@mscoutermarsh
mscoutermarsh / taskk.coffee
Created January 24, 2013 02:30
Example of using taskk.js
# Initialize the taskk api. With your api token. Get yours at https://api.taskk.it
taskk_api = new TaskkAPI('token')
# Test your API key with the ping method
ping = taskk_api.ping()
# define what to do on success. Should say "pong" if you have a valid key.
ping.success (data) -> alert(JSON.stringify(data))
# define what to do on error
@mscoutermarsh
mscoutermarsh / create_taskk.coffee
Last active December 11, 2015 14:58
Create tasks with taskk.js
# Create a task "Do homework" with an estimate of "1m" for list 1234
params = {title: "Do homework", estimate: "1m", list_id: 1234}
new_task = taskk_api.create_task(params)
# define what to do on success.
new_task.success (data) -> alert(JSON.stringify(data))
# define what to do on error
new_task.error (data) -> alert(JSON.stringify(data))
@mscoutermarsh
mscoutermarsh / api.rb
Created February 10, 2013 01:02
http basic authentication with Grape
class Api < Grape::API
# /private
resource :private do
http_basic do |email, password|
user = User.find_by_email(email)
user && user.valid_password?(password)
end
# this method requires authentication!
1 Dyno:
Transactions: 12715 hits
Availability: 99.99 %
Elapsed time: 59.85 secs
Data transferred: 0.13 MB
Response time: 0.44 secs
Transaction rate: 212.45 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 94.16
#!/usr/bin/env bash
# kill all connections to the postgres server
if [ -n "$1" ] ; then
where="where pg_stat_activity.datname = '$1'"
echo "killing all connections to database '$1'"
else
where="where pg_stat_activity.datname in (select datname from pg_database where datname != 'postgres')"
echo "killing all connections to database"
fi
@mscoutermarsh
mscoutermarsh / put_example.rb
Last active December 15, 2015 15:29
grape PUT method. Using update_attributes with declared.
# Edit a project
desc "Edit a project"
params do
group :project do
optional :title, type: String, desc: "Edit title."
optional :description, type: String, desc: "Edit description."
end
end
put '/:id' do
project = Project.find_by_id(params[:id])
@mscoutermarsh
mscoutermarsh / my_controller.coffee
Last active December 15, 2015 21:29
sharing data Angular JS & CoffeeScript
@MyController = ($scope, Shared) ->
$scope.attribute_one = Shared.get_attribute_one()
$scope.attribute_two = Shared.get_attribute_two()
$scope.some_function = (new_value) ->
# A function that can be called from the DOM/controller
Shared.set_attribute_one(new_value)
# When attribute_one is updated in another controller. Retrieve new value
$scope.$on('attribute_one', ->
@mscoutermarsh
mscoutermarsh / count_position.rb
Created April 7, 2013 16:44
return position of letter. input "Hello World,r" ... what's position of r? 0 index.
File.open(ARGV[0]).each_line{ |line|
input = line.split(',')
position = 0
input[0].split("").each_with_index { |char, x|
if char == input[1]
position = x
end
}
puts position
}