Skip to content

Instantly share code, notes, and snippets.

@jadon1979
jadon1979 / test_splat_tap.rb
Created January 16, 2013 15:56
splat n tap
def blah(x, *y, z)
return x, y, z
end
a = blah 1, 'A', 'B', 'C', 'X', 'Y', 'Z', "*"
p a
a.tap { |b| b[1].reverse! }
p a
@jadon1979
jadon1979 / logging_handler.rb
Created May 1, 2013 15:31
Stop logging Assets and other particular messages. A great example: An Ajax call checking a Queue every nSeconds and filling up the log with simple GET requests. Add to initializers
Rails.application.assets.logger = Logger.new('/dev/null')
Rails::Rack::Logger.class_eval do
alias_method :logger_call, :call
def call(env)
previous_level = Rails.logger.level
#TODO:
#Move paths to yaml file as it grows
paths = %w(/assets/ /my_queues/ /their_queues/)
@jadon1979
jadon1979 / http_requests.rb
Last active December 18, 2015 22:18
Http Requests
module SomeLib
module Utilities
module HttpRequest
#send get request to sensor
def send_get_request(url = nil)
send_data url
end
@jadon1979
jadon1979 / ssh_base.rb
Last active December 19, 2015 02:49
ssh wrapper
class SshBase
attr_accessor :host, :username, :password, :timeout
class SSHException < Exception; end;
# Callbacks storage for Channel Execute Callbacks
class ChannelExecCallBacks
attr_accessor :on_success, :on_failure, :on_data, :on_extended_data, :on_close
end
en:
api_responses:
status_codes:
code-200: "Operation successful."
code-400: "Bad Request: The request could not be understood by the server due to malformed syntax."
code-401: "Not Authorized: Either the header did not contain an acceptable Authorization or the username/password was invalid. The server response MUST include a WWW-Authenticate header field."
code-402: "Payment Required: The requested transaction requires a payment which could not be authorized."
code-403: "Forbidden: The server understood the request, but is refusing to fulfill it."
code-404: "Not Found: The server has not found anything matching the Request-URI."
@jadon1979
jadon1979 / job_states.rb
Created January 10, 2014 14:30
Job Models / Concern
require 'active_support/concern'
module Concerns
module JobStates
extend ActiveSupport::Concern
def self.extended(base)
base.class_eval do
def self.job_is_pending(id)
update_job id, 1, "Pending"
@jadon1979
jadon1979 / notifications.rb
Last active January 2, 2016 20:09
devices/common/notifiers.rb devices/common/notifier.rb
module Devices
module Common
class Reports < Devices::Common::Notifiers; end
class Warnings < Devices::Common::Notifiers; end
class Errors < Devices::Common::Notifiers; end
end
end
@jadon1979
jadon1979 / site_variables.rb
Last active January 3, 2016 11:28
YAML to OpenStruct
class SiteVariables < YamlToOpenStruct
def initialize
variables_yml = YAML::load(File.open('yaml_location'))
@struct = convert_yml(variables_yml)
end
# send method calls to generated struct
def method_missing(method, *args, &block)
@struct.send(method, *args, &block)
@jadon1979
jadon1979 / employee_example.rb
Created February 9, 2014 18:00
Based on a question on Quora: http://www.quora.com/When-naming-classes-should-class-names-end-in-Manager "Does having class names as '...Manager' indicate poor design and mean the finer grained functionality of each component has not been well thought out?"
#Using the code below as an example
class Employees
include Enumerable
attr_reader :employees
def employees
@employees ||= []
end
<?xml version="1.0"?>
<REData>
<REProperties>
<CopyrightNotice/>
<Disclaimer/>
<ResidentialProperty>
<Listing>
<StreetAddress/>
<ListingData>
<REAgent>