Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

makin donuts, makin requests

  • Loading branch information...
commit cb333fcc6f4612b8bbb5004da147b7ab7e29a09a 1 parent 24d9f46
@qrush qrush authored
View
8 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"
View
4 Gemfile.lock
@@ -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)
@@ -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)
View
2  lib/daikon.rb
@@ -4,6 +4,8 @@
require 'socket'
require 'daemons'
+require 'json'
+require 'net/http/persistent'
require 'redis'
__DIR__ = File.dirname(__FILE__)
View
39 lib/daikon/client.rb
@@ -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)
View
37 spec/client_spec.rb
@@ -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
View
2  spec/spec_helper.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.