Skip to content

Instantly share code, notes, and snippets.

View Fivell's full-sized avatar
🏠
Working from home

Igor Fedoronchuk Fivell

🏠
Working from home
View GitHub Profile
@Fivell
Fivell / type_caster.rb
Created January 12, 2017 22:12 — forked from masqita/type_caster.rb
TypeCaster module
class Boolean
def self.from_json(value)
if value.is_a?(TrueClass) || value.is_a?(FalseClass)
return value
elsif value.is_a?(String)
return value == ("true" || "1")
elsif value.is_a?(Integer)
return value == 1
else
nil
@Fivell
Fivell / form_block.rb
Created December 28, 2016 09:38
form_block
form_block = proc do |has_many_form|
form_block_contents = "".html_safe
index = parent_child_index options[:parent] if options[:parent]
form_block_contents << (block.call(has_many_form, index) || "".html_safe)
form_block_actions = has_many_actions(has_many_form, builder_options, "".html_safe)
form_block_contents << form_block_actions
template.concat(form_block_actions)
form_block_contents
end
require 'rubygems'
gem 'nokogiri'
require 'nokogiri'
def validate(document_path, schema_path, root_element)
schema = Nokogiri::XML::Schema(File.read(schema_path))
document = Nokogiri::XML(File.read(document_path))
schema.validate(document.xpath("//#{root_element}").to_s)
end
@Fivell
Fivell / ordered_options.rb
Last active December 15, 2016 09:29
alternative to open struct
# h = ActiveSupport::OrderedOptions.new
# h.boy = 'John'
# h.girl = 'Mary'
# h.boy # => 'John'
# h.girl # => 'Mary'
class OrderedOptions < Hash
alias_method :_get, :[] # preserve the original #[] method
protected :_get # make it protected
def []=(key, value)
@Fivell
Fivell / faraday_typh_default.rb
Created December 13, 2016 19:21 — forked from ezkl/faraday_typh_default.rb
Parallel Requests w/ Faraday + Typhoeus
require "faraday"
require 'typhoeus'
conn = Faraday.new(:url => 'http://httpstat.us') do |builder|
builder.request :url_encoded
builder.response :logger
builder.adapter :typhoeus
end
conn.in_parallel do
@Fivell
Fivell / gemfile_lock2geminabox.rb
Created December 13, 2016 11:08 — forked from flavio/gemfile_lock2geminabox.rb
Parse Gemfile.lock, download all gems from rubygems and then upload them to a local instance of geminabox
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
require 'fileutils'
require 'net/http'
require 'net/https'
require 'uri'
TMP_DIR = "/tmp/gems"
{"data"=>[{"id"=>"526", "type"=>"termination_cdrs", "links"=>{"self"=>"http://www.example.com/api/rest/public/termination_cdrs/526"}, "attributes"=>{"time_start"=>"2016-11-02T11:44:00.485Z", "time_connect"=>"2016-11-02T11:44:00.485Z", "time_end"=>"2016-11-02T11:49:00.489Z", "duration"=>nil, "aleg_code"=>nil, "aleg_reason"=>nil, "src_name_in"=>nil, "src_number_in"=>nil, "dst_number_in"=>nil, "orig_call_id"=>nil, "billing_duration"=>nil, "billing_rate"=>nil, "billing_connection_fee"=>nil, "billing_initial_interval"=>nil, "billing_next_interval"=>nil, "billing_price"=>nil}}, {"id"=>"527", "type"=>"termination_cdrs", "links"=>{"self"=>"http://www.example.com/api/rest/public/termination_cdrs/527"}, "attributes"=>{"time_start"=>"2016-11-02T11:44:00.529Z", "time_connect"=>"2016-11-02T11:44:00.529Z", "time_end"=>"2016-11-02T11:49:00.532Z", "duration"=>nil, "aleg_code"=>nil, "aleg_reason"=>nil, "src_name_in"=>nil, "src_number_in"=>nil, "dst_number_in"=>nil, "orig_call_id"=>nil, "billing_duration"=>nil, "billing_rate"=
class String
def words
scan(/\w[\w\'\-]*/)
end
end
@Fivell
Fivell / rack-notags.rb
Created July 7, 2016 10:04 — forked from shinzui/rack-notags.rb
Rack middleware to clean post and get params
# # # #
# Rack::NoTags removes < and > from all incoming requests
# http://rbjl.net/12-rack-notags
#
# (c) 2009 - 2010 Jan Lelis
# This software is licensed under the MIT license.
# # # #
module Rack
#template for select option, I use it to show regions and draw country flags for each city
cityOptionTpl = (obj)->
_.tpl('CitySelectOption')(obj)
#helper for rendering option
renderCityOption = (entry)->
tplRes = cityOptionTpl({it:
title : entry.title
region : entry.region
country_name : entry.country_name