Skip to content

Instantly share code, notes, and snippets.

View lorenzosinisi's full-sized avatar
🦄
Elixir

Lorenzo Sinisi lorenzosinisi

🦄
Elixir
View GitHub Profile
@lorenzosinisi
lorenzosinisi / herversine_ruby.rb
Last active August 29, 2015 14:16
Heversine Rails to check if there are people around me
def distance loc1, loc2
rad_per_deg = Math::PI/180 # PI / 180
rkm = 6371 # Earth radius in kilometers
rm = rkm * 1000 # Radius in meters
dlat_rad = (loc2[0]-loc1[0]) * rad_per_deg # Delta, converted to rad
dlon_rad = (loc2[1]-loc1[1]) * rad_per_deg
lat1_rad, lon1_rad = loc1.map {|i| i * rad_per_deg }
lat2_rad, lon2_rad = loc2.map {|i| i * rad_per_deg }
/var/lib/docker/aufs/mnt/tmp-*
@lorenzosinisi
lorenzosinisi / cap.rb
Last active August 29, 2015 14:22
Use workers:clockwork:start to start clockwork or delayed_job on your server using capistrano
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'myapp'
set :repo_url, ''
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
cap production workers:clockwork:stop; cap production workers:delayed_job:stop; cap production deploy;cap production workers:clockwork:start; cap production workers:delayed_job:start;
@lorenzosinisi
lorenzosinisi / post.rb
Last active August 29, 2015 14:22
Show posts nearby with Rails Model
def self.close_by(lat1, lon1)
posts_around = []
Post.where("latitude IS NOT NULL AND longitude IS NOT NULL").order('id DESC').each do |post|
distance = Post.new.distance(lat1.to_f, lon1.to_f, post.latitude.to_f, post.longitude.to_f)
radius = Setting.find(1).usersight
if (distance <= radius.to_f)
post_array = {}
if post.user.nil?
username = ""
else
@lorenzosinisi
lorenzosinisi / validatefb.rb
Last active August 29, 2015 14:22
Validate Google Login / Facebook Login
def self.is_valid_google?(google_id, user_token)
user = user_token.to_s
uri = URI('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + user)
string = JSON.parse(Net::HTTP.get(uri))
error = string["error"] if string["error"].present?
if error
return false
elsif string["user_id"].present?
if string["user_id"] == google_id
true
@lorenzosinisi
lorenzosinisi / is_valid_api?(api_key).rb
Last active August 29, 2015 14:22
Check if an API is valid and present into the db and update the number of request made by the client
def self.valid_api?(api_key)
if api_key and Apikey.where(:api_key => api_key).present?
api = Apikey.where(:api_key => api_key).first
if !api.requests.nil?
api.requests = api.requests + 1
else
api.requests = 1
end
api.save
true
@lorenzosinisi
lorenzosinisi / agent_rspec.rb
Created June 2, 2015 07:55
Test model Agent that makes http requests with rspec
require 'spec_helper'
describe Agent do
describe "1. calling #request" do
it "serves the action POST if the request is in POST" do
variable = Agent.request("http://google.com", "params", "post")
variable.should be_truthy
end
it "serves the action GET if the request is in GET" do
@lorenzosinisi
lorenzosinisi / post.rb
Created June 2, 2015 07:59
Limitating resource that a user can create
def jobs_count_within_user
limit = LIMIT
if self.user.jobs.count > limit
errors.add(:base, "Every user can create max #{limit} jobs")
false
else
true
end
end
@lorenzosinisi
lorenzosinisi / model.rb
Created June 2, 2015 08:01
Useful scopes for MicroServices in Ruby on Rails
scope :to_do, lambda { where(:processed => false) }
scope :processed, lambda { where(:processed => true) }
scope :to_do_now, lambda { where("due_date > ? AND due_date < ?", DateTime.now, DateTime.now + 3.minutes) }
scope :not_processed, lambda { where(:processed => false) }
scope :circular, lambda { where("minutes IS NOT NULL") }