public
Last active

Code examples for Ruby Dependencies, Notifications, and Adjustments (Ruby DNA)

  • Download Gist
dependency_injection_container.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
require "dim"
AppContainer = Dim::Container.new
 
AppContainer.register(:env) { ENV['ADWORK_ENV'] || "development" }
AppContainer.register(:production?) { |c| c.env == 'production' }
AppContainer.register(:development?) { |c| c.env == 'development' }
AppContainer.register(:test?) { |c| c.env == 'test' }
AppContainer.register(:root) { File.expand_path(File.dirname(__FILE__)+"/../..") }
 
AppContainer.register(:logger) do |c|
if c.test?
Logger.new("#{c.root}/log/#{c.env}.log")
elsif c.production?
Sidekiq.logger.tap { |l| l.level = ENV["DEBUG"].present? ? Logger::DEBUG : Logger::INFO }
else
Logger.new(STDOUT)
end
end
 
AppContainer.register(:mechanize) do |c|
Mechanize.new do |agent|
agent.log = c.logger
agent.user_agent_alias = "Mac Safari"
agent.keep_alive = false
end
end
 
AppContainer.register(:salesforce_client) do |c|
require "databasedotcom"
Databasedotcom::Client.new(client_id: c.salesforce_consumer_key, client_secret: c.salesforce_consumer_secret)
end
http_request.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
require "typhoeus"
 
class HttpRequest
attr_reader :typhoeus_request
 
def initialize(url,options = {})
@typhoeus_request = Typhoeus::Request.new(url,options)
@success_callbacks = []
@failure_callbacks = []
 
@typhoeus_request.on_complete do |response|
if response.success?
success_callbacks.each { |sc| sc.call(response.body,"Success") }
elsif response.timed_out?
failure_callbacks.each { |sc| sc.call("Request timed out for #{url}") }
elsif response.code == 0
failure_callbacks.each { |sc| sc.call("No HTTP response for #{url}: #{response.curl_error_message}") }
else
failure_callbacks.each { |sc| sc.call("HTTP request failed for #{url} (#{response.code})",response.body) }
end
end
end
 
def on_success(&block)
success_callbacks << block
end
 
def on_failure(&block)
failure_callbacks << block
end
 
private
 
# I picked this technique up from Gregory; I think it leads to fewer bugs
attr_reader :success_callbacks, :failure_callbacks
end
http_request_service.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
require "typhoeus"
require_relative "http_request"
 
class HttpRequestService
attr_writer :hydra
 
def request(url,options = {})
HttpRequest.new(url,options).tap do |http_request|
hydra.queue(http_request.typhoeus_request)
end
end
 
def run
hydra.run
end
 
private
 
def hydra
@hydra ||= Typhoeus::Hydra.new
end
end
master_class_with_adjustments.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
class DataFetcher
# these are all Adjustments
attr_writer :authorization_agent, :shaz_fetcher, :bot_fetcher
 
attr_writer :logger
 
def fetch(credentials)
authorization_agent.login(credentials) do |err_msg|
logger.warn "Could not login due to #{err_msg}"
return
end
 
shaz_fetcher.fetch(authorization_agent.token) do |err_msg|
logger.warn "Could not fetch shaz due to #{err_msg}"
end
 
bot_fetcher.fetch(shaz_fetcher.data) do |err_msg|
logger.warn "Could not fetch bot due to #{err_msg}"
return
end
# do something with results of bot_fetcher
end
 
private
 
def authorization_agent
@authorization_agent ||= AuthorizationAgent.new
end
 
def shaz_fetcher
@shaz_fetcher ||= ShazFetcher.new
end
 
def bot_fetcher
@bot_fetcher ||= BotFetcher.new
end
 
def logger
@logger ||= Logger.new(STDOUT)
end
end
notification_logging_example.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class DataFetcher
attr_writer :logger # makes it easy to customize our object with a new notification
 
def fetch
# do important things
logger.info("I did something important")
end
 
private
 
def logger
@logger ||= Logger.new(STDOUT)
end
end
notification_on_success_example.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12
# how to use HttpRequest#on_success to register a Notification
stats_request.on_success do |body|
xml = Nokogiri::XML(body)
 
xml.xpath("//Audience").each do |aud|
key = aud.at("id").text
if dsh = datapoint_hashes.find { |dsh| dsh[:key] == key }
dsh[:values][:abbreviation] = aud.at("abbreviation").text
end
end
@success = true
end
passing_notification_blocks.rb
Ruby
1 2 3 4 5 6
data_fetcher.fetch do |err_msg|
puts "We couldn't complete our task because of #{err_msg}"
return
end
 
# do rest of our work, confident that data_fetcher succeeded
strategy_pattern_adjustment.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class DataFetcher
attr_writer :admin_checker
 
def fetch(query_params)
yield "You are not authorized to access that data" unless admin_checker.valid?(query_params)
# proceed to fetch data
end
 
private
 
def admin_checker
@admin_checker ||= AdminChecker.new
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.