Skip to content
This repository has been archived by the owner on Feb 15, 2018. It is now read-only.

Commit

Permalink
makin donuts, makin requests
Browse files Browse the repository at this point in the history
  • Loading branch information
qrush committed Nov 20, 2010
1 parent 24d9f46 commit cb333fc
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
8 changes: 5 additions & 3 deletions Gemfile
@@ -1,8 +1,10 @@
source "http://rubygems.org"

gem "daemons", "~> 1.0.0"
gem "redis", "~> 2.1.1"
gem "system_timer", "= 1.0"
gem "daemons", "~> 1.0.0"
gem "json_pure", "~> 1.4.6"
gem "redis", "~> 2.1.1"
gem "system_timer", "= 1.0"
gem "net-http-persistent", "~> 1.4.1"

gem "rspec"
gem "cucumber"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -23,8 +23,10 @@ GEM
git (>= 1.2.5)
rake
json (1.4.6)
json_pure (1.4.6)
mocha (0.9.8)
rake
net-http-persistent (1.4.1)
rake (0.8.7)
redis (2.1.1)
rspec (2.1.0)
Expand All @@ -49,6 +51,8 @@ DEPENDENCIES
cucumber
daemons (~> 1.0.0)
jeweler
json_pure (~> 1.4.6)
net-http-persistent (~> 1.4.1)
redis (~> 2.1.1)
rspec
system_timer (= 1.0)
Expand Down
2 changes: 2 additions & 0 deletions lib/daikon.rb
Expand Up @@ -4,6 +4,8 @@
require 'socket'

require 'daemons'
require 'json'
require 'net/http/persistent'
require 'redis'

__DIR__ = File.dirname(__FILE__)
Expand Down
39 changes: 33 additions & 6 deletions lib/daikon/client.rb
Expand Up @@ -2,26 +2,53 @@ module Daikon
class Client
include NamespaceTools

attr_accessor :redis, :logger, :config
attr_accessor :redis, :logger, :config, :http

def setup(config, logger)
def setup(config, logger = nil)
self.config = config
self.logger = logger
self.redis = Redis.new(:port => config.redis_port)
self.http = Net::HTTP::Persistent.new
http.headers['Authorization'] = "deadbeef"

logger.info "Started Daikon v#{VERSION}"
log "Started Daikon v#{VERSION}"
end

def log(message)
logger.info message if logger
end

def http_request(method, url)
log "#{method.to_s.upcase} #{url}"

request_uri = URI.parse("http://radishapp.com/#{url}")
request_method = Net::HTTP.const_get method.to_s.capitalize
request = request_method.new request_uri.path

yield request if block_given?

http.request request_uri, request
end

def fetch_commands
logger.info "fetch commands and run them"
raw_commands = http_request(:get, "api/v1/commands.json")
commands = JSON.parse(raw_commands.body)

commands.each do |id, command|
result = evaluate_redis(command)

http_request(:put, "api/v1/commands/#{id}.json") do |request|
request.body = {"response" => result}.to_json
end
end
end

def send_info
logger.info "sending INFO"
log "sending INFO"
end

def rotate_monitor
logger.info "wrap up and truncate monitor log"
log "wrap up and truncate monitor log"
end

def evaluate_redis(command)
Expand Down
37 changes: 37 additions & 0 deletions spec/client_spec.rb
Expand Up @@ -36,3 +36,40 @@
end
end
end

describe Daikon::Client, "fetching commands" do
subject { Daikon::Client.new }
let(:config) { Daikon::Configuration.new([]) }
let(:api_key) { "deadbeef" }
let(:api_url) { "radishapp.com/api/v1/commands.json" }
let(:body) { {"42" => "INCR foo", "43" => "DECR foo"}.to_json }

before do
subject.stubs(:evaluate_redis => "9999")
stub_request(:get, api_url).to_return(:body => body)
stub_request(:put, %r{radishapp\.com/api/v1/commands/\d+\.json})

subject.setup(config)
subject.fetch_commands
end

it "sends a request for commands" do
WebMock.should have_requested(:get, api_url).
with(:headers => {'Authorization' => api_key})
end

it "processes each command" do
subject.should have_received(:evaluate_redis).with("INCR foo")
subject.should have_received(:evaluate_redis).with("DECR foo")
end

it "shoots the results back to radish" do
results = {"response" => "9999"}.to_json

WebMock.should have_requested(:put, "radishapp.com/api/v1/commands/42.json").
with(:body => results, :headers => {'Authorization' => api_key})

WebMock.should have_requested(:put, "radishapp.com/api/v1/commands/43.json").
with(:body => results, :headers => {'Authorization' => api_key})
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -8,7 +8,9 @@
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

require 'bourne'

require 'webmock/rspec'
WebMock.disable_net_connect!

RSpec.configure do |config|
config.mock_with :mocha
Expand Down

0 comments on commit cb333fc

Please sign in to comment.