Skip to content

Instantly share code, notes, and snippets.

#spec/requests/the_service/ping_spec.rb
require 'spec_helper'
describe 'ping request' do
include_context 'the_service_black_box'
it 'returns response back' do
params[:type] = 'ping'
#spec/support/shared_contexts/the_service_black_box.rb
shared_context 'the_service_black_box' do
let(:params) do
{
type: 'save',
data: 1
}
end
#/spec/support/the_service.rb
class TheServiceControl
#....
def stop
return if @pid.nil?
print "Stopping TheService (PID: #{@pid}). "
Process.kill("KILL", -Process.getpgid(@pid))
res = Process.wait
@pid = nil
#/spec/support/the_service_config.yml
server:
addr: 127.0.0.1:8082
db:
dsn: dbname=project_test sslmode=disable user=postgres password=secret
redis:
url: redis://127.0.0.1:6379/1
rails:
#/spec/support/the_service.rb
class TheServiceControl
#....
def start
return unless @pid.nil?
puts "TheService starting. "
env = config['rails']['env']
cmd = "go run #{config['rails']['main_go']} --config.file=#{config_path}"
puts cmd #useful for debug when need run project manually
#/spec/support/the_service.rb
#ensure that after all specs TheService will be stopped
RSpec.configure do |config|
config.after :suite do
TheServiceControl.stop
end
end
class TheServiceControl
@charger
charger / luhn.rb
Created August 27, 2015 17:00
Luhn algorythm
#https://en.wikipedia.org/wiki/Luhn_algorithm
# v1
def self.check_digit_for(number)
digits = number.to_s.reverse.split(//)
sum = 0
digits.each_with_index do |digit, index|
digit = digit.to_i
digit *= 2 if index.even?
digit -= 9 if digit > 9
@charger
charger / turbo_sms.rb
Last active January 27, 2017 13:04
Using Turbo SMS service with ruby
#для работы этого кода нужно установить гем 'savon'
#для этого выполните: gem install savon
#или пропишите в Gemfile: gem 'savon'
class TurboSMS
LOGIN = '...' # не забудьте активировать SOAP шлюз в личном кабинете и создать логин/пароль
PASSWORD = '...'
SENDER = 'MySite' #не забудьте добавить эту подпись в личном кабинете
def get_balance
@charger
charger / sftp_uploader.rb
Last active August 29, 2015 14:00
SftpUploader
class SftpUploader
def upload!(local_filename, remote_filename)
raise "#{local_filename} not readable" unless File.exist?(local_filename)
connect do |sftp|
dirs = File.dirname(remote_filename).split('/')
dir_to_create = credentials.path
dirs.each do |dir|
dir_to_create = File.join(dir_to_create, dir)
sftp.mkdir(dir_to_create)
end
@charger
charger / main.php
Last active December 29, 2015 13:58
Для Yii-framework (1.1) приложений
<?php
//protected/config/main.php
return array(
//..
'components'=>array(
//...
"clientScript"=>array(
"class"=>"myClientScript",
),
//..