Skip to content

Instantly share code, notes, and snippets.

View mamantoha's full-sized avatar
🇺🇦
#StandWithUkraine

Anton Maminov mamantoha

🇺🇦
#StandWithUkraine
  • Ternopil, Ukraine
  • 13:44 (UTC +03:00)
View GitHub Profile
@mamantoha
mamantoha / sinatra_chat.rb
Created April 29, 2012 15:37
Simple Chat with Sinatra
# -*- encoding: utf-8 -*-
require 'sinatra'
require 'slim'
set :server, 'thin'
connections = []
get '/' do
halt slim(:login) unless params[:user]
# -*- encoding: utf-8 -*-
require 'date'
class BirthdayCountdown
# Cache @month, @day and @year on initialization
def initialize(month, day, zone = '+00:00')
@birthday_month = month
@birthday_day = day
@zone = zone
I, [2012-05-17T14:06:40.816605 #2480] INFO -- : Net::HTTP::Get: /
D, [2012-05-17T14:06:40.817531 #2480] DEBUG -- : request-header: accept => */*
D, [2012-05-17T14:06:40.818051 #2480] DEBUG -- : request-header: user-agent => Mechanize/2.5.1 Ruby/1.9.3p125 (http://github.com/tenderlove/mechanize/)
D, [2012-05-17T14:06:40.818556 #2480] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2012-05-17T14:06:40.819057 #2480] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2012-05-17T14:06:40.819613 #2480] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2012-05-17T14:06:40.820090 #2480] DEBUG -- : request-header: host => google.com
I, [2012-05-17T14:06:41.150941 #2480] INFO -- : status: Net::HTTPMovedPermanently 1.1 301 Moved Permanently
D, [2012-05-17T14:06:41.151576 #2480] DEBUG -- : response-header: location => http://www.google.com/
D, [2012-05-17T14:06:41.152028 #2480] DEBUG -- : response-header: content-type => text/html; charset=UTF-8
@mamantoha
mamantoha / app.rb
Created May 22, 2012 20:12
CarrierWave and DataMapper
# encoding: utf-8
require 'sinatra'
require 'slim'
require 'data_mapper'
require 'carrierwave'
require 'carrierwave/datamapper'
class ImageUploader < CarrierWave::Uploader::Base
@mamantoha
mamantoha / kyivstar.rb
Last active October 6, 2015 04:27
Current balance my.kyivstar.net
# encoding: utf-8
require 'pp'
require 'mechanize'
require 'nokogiri'
user = '103900'
password = '***'
agent = Mechanize.new do |a|
@mamantoha
mamantoha / 1_params.rb
Created July 3, 2012 13:23
Vkontakte Ruby Net::HTTP
require 'net/http'
email = 'user@example.com'
pass = 'secret'
client_id = '1234567'
scope = 'friends,audio'
redirect_uri = 'http://oauth.vk.com/blank.html'
display = 'wap'
response_type = 'code'
@mamantoha
mamantoha / style.rb
Created August 15, 2012 09:30
sinatra-flash and bootstrap alert
module Sinatra
module Flash
module Style
def styled_flash(key=:flash)
return "" if flash(key).empty?
id = (key == :flash ? "flash" : "flash_#{key}")
close = '<a class="close" data-dismiss="alert" href="#">×</a>'
messages = flash(key).collect {|message| " <div class='alert alert-#{message[0]}'>#{close}\n #{message[1]}</div>\n"}
"<div id='#{id}'>\n" + messages.join + "</div>"
end
@mamantoha
mamantoha / common_prefix.rb
Created October 16, 2012 11:04
Finding the longest common prefix of an array of paths in Ruby
# Return the longest path prefix (taken character-by-character) that is a prefix of all paths in array.
# If array is empty, return the empty string ('').
# Note that this may return invalid paths because it works a character at a time.
#
def common_prefix(m)
# Given a array of pathnames, returns the longest common leading component
return '' if m.empty?
s1, s2 = m.min, m.max
s1.each_char.with_index do |c, i|
return s1[0...i] if c != s2[i]
@mamantoha
mamantoha / ask_for_credentials.rb
Created October 30, 2012 13:37
Ask for a email and password in Linux terminal
# encoding: utf-8
class AskForCredentials
attr_reader :email, :password
def initialize
ask_for_credentials
end
private
@mamantoha
mamantoha / C
Created November 22, 2012 07:55
Ternary operator associativity
$ cat ternary.c
#include <stdio.h>
int main() {
printf("%s\n", 0 ? "a" : 0 ? "b" : "c");
printf("%s\n", 0 ? "a" : 1 ? "b" : "c");
printf("%s\n", 1 ? "a" : 0 ? "b" : "c");
printf("%s\n", 1 ? "a" : 1 ? "b" : "c");
}