Skip to content

Commit

Permalink
Return true/false to WarmingDrawer.warm calls. Move configuration cod…
Browse files Browse the repository at this point in the history
…e for main module into main module. Start yard docs.
  • Loading branch information
cball committed Nov 12, 2012
1 parent fbb1caa commit a4c035e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .yardopts
@@ -0,0 +1,3 @@
lib/**/*.rb
-
LICENSE
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2012 Chris Ball and echobind, inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
27 changes: 23 additions & 4 deletions lib/warming_drawer.rb
Expand Up @@ -5,10 +5,22 @@

module WarmingDrawer

class << self
attr_accessor :configuration
end

def self.configure
self.configuration ||= Configuration.new
yield(configuration)
end

# Warms a cache. Delegates to the proper worker depending on the type.
#
# @param [Array] Arguments or Array to warm
# @return [Boolean]
# @example
# WarmingDrawer.warm('http://sweet.dev/1', 'http://sweet2.dev/2', :type => :url)
# => true
def self.warm(*args)
options = args.extract_options!
options[:type] ||= :url
Expand All @@ -18,16 +30,23 @@ def self.warm(*args)
worker.options queue: configuration.queue_name, retry: configuration.retry
worker.perform_with(args)
end

!worker.nil?
end

# Returns true if a queuing system is defined
# @return [Boolean]
def self.use_queue?
configuration.queue_system != :inline
end

def self.available_worker_for_type(type)
if worker_name = Workers.constants.detect {|c| c.downcase.match /^#{type.to_s.downcase}/}
WarmingDrawer::Workers.const_get worker_name.to_s

private

def self.available_worker_for_type(type)
if worker_name = Workers.constants.detect {|c| c.downcase.match /^#{type.to_s.downcase}/}
WarmingDrawer::Workers.const_get worker_name.to_s
end
end
end

end
11 changes: 1 addition & 10 deletions lib/warming_drawer/configuration.rb
@@ -1,6 +1,6 @@
module WarmingDrawer
class Configuration
attr_accessor :queue_name, :retry, :queue_system, :basic_auth_username, :basic_auth_password
attr_accessor :queue_name, :retry, :queue_system, :basic_auth_username, :basic_auth_password, :preheat_url

def initialize
@queue_name = 'high'
Expand All @@ -12,13 +12,4 @@ def initialize
end

end

class << self
attr_accessor :configuration
end

def self.configure
self.configuration ||= Configuration.new
yield(configuration)
end
end
16 changes: 14 additions & 2 deletions spec/warming_drawer_spec.rb
Expand Up @@ -7,15 +7,27 @@

describe 'warm' do

it 'takes any number of items to warm' do
before do
stub_request(:any, 'foo.com').to_return(:body => "good", :status => 200)
stub_request(:any, 'yep.com').to_return(:body => "good", :status => 200)
end

it 'takes any number of items to warm' do
lambda {
WarmingDrawer.warm 'http://foo.com'
WarmingDrawer.warm 'http://foo.com', 'http://yep.com'
WarmingDrawer.warm 'http://foo.com', :something => :else
}.must_be_silent
end

it 'returns true if the warming request is queued' do
warmed = WarmingDrawer.warm 'http://foo.com'
warmed.must_equal true
end

it 'returns false if the warming request fails' do
warmed = WarmingDrawer.warm 'http://foo.com', :type => :fakeworker
warmed.must_equal false
end
end

describe 'use_queue?' do
Expand Down

0 comments on commit a4c035e

Please sign in to comment.