Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Code examples for Ruby Dependencies, Notifications, and Adjustments (Ruby DNA)
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
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
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
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
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
# 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
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.