Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extract Configuration, refactor thoroughly

  • Loading branch information...
commit c688a8adc139c9afbc6022141d36b355baccfa99 1 parent ee860ac
@iain authored
View
32 lib/capistrano/blaze.rb
@@ -1,28 +1,24 @@
require 'net/http'
require 'json'
-require 'ostruct'
-require 'forwardable'
require 'capistrano/blaze/version'
-require 'capistrano/blaze/messages'
+require 'capistrano/blaze/message'
+require 'capistrano/blaze/configuration'
require 'capistrano/blaze/recipes' if defined?(Capistrano::Configuration)
module Capistrano
module Blaze
extend self
- extend Forwardable
-
- def_delegators :Messages, :start, :failure, :success, :test
def configure
yield configuration
end
def configuration
- @config ||= OpenStruct.new
+ @configuration ||= Configuration.new
end
def speak(message)
- validate_configuration
+ configuration.validate!
port = configuration.ssl ? 443 : 80
req = Net::HTTP::Post.new("/room/#{configuration.room_id}/speak.json")
@@ -44,25 +40,5 @@ def speak(message)
end
end
- def validate_configuration
- %w(account room_id token).each do |option|
- if configuration.send(option).nil?
- fail MissingConfigurationOption.new(option)
- end
- end
- end
-
- class MissingConfigurationOption < RuntimeError
-
- def initialize(option)
- @option = option
- end
-
- def to_s
- "Please specify the #{@option} option"
- end
-
- end
-
end
end
View
38 lib/capistrano/blaze/configuration.rb
@@ -0,0 +1,38 @@
+module Capistrano
+ module Blaze
+
+ class Configuration
+
+ attr_accessor :account, :room_id, :token, :ssl
+
+ def []=(option, value)
+ send "#{option}=", value
+ end
+
+ def [](option)
+ send option
+ end
+
+ def validate!
+ %w(account room_id token).each do |option|
+ if send(option).nil?
+ fail MissingOption.new(option)
+ end
+ end
+ end
+
+ class MissingOption < RuntimeError
+
+ def initialize(option)
+ @option = option
+ end
+
+ def to_s
+ "Please specify the #{@option} option"
+ end
+
+ end
+ end
+
+ end
+end
View
2  lib/capistrano/blaze/messages.rb → lib/capistrano/blaze/message.rb
@@ -1,6 +1,6 @@
module Capistrano
module Blaze
- class Messages
+ class Message
def self.start(context)
new(context).start
View
8 lib/capistrano/blaze/recipes.rb
@@ -2,23 +2,23 @@
at_exit do
if exception = $!
- Capistrano::Blaze.failure(self, exception)
+ Capistrano::Blaze::Message.failure(self, exception)
end
end
namespace :campfire do
task :start do
- Capistrano::Blaze.start(self)
+ Capistrano::Blaze::Message.start(self)
end
task :success do
- Capistrano::Blaze.success(self)
+ Capistrano::Blaze::Message.success(self)
end
desc "Sends a test message to Campfire"
task :test_config do
- Capistrano::Blaze.test(self)
+ Capistrano::Blaze::Message.test(self)
end
end
View
33 spec/blaze_spec.rb
@@ -0,0 +1,33 @@
+require 'capistrano/blaze'
+require 'webmock'
+
+describe Capistrano::Blaze do
+ include WebMock::API
+
+ around do |example|
+ $stderr = StringIO.new
+ example.run
+ $stderr = STDERR
+ end
+
+ it "can speak" do
+ token = "abc"
+ room_id = 1234
+ account = "abcd"
+
+ stub_request(:post, "http://#{token}:X@#{account}.campfirenow.com/room/#{room_id}/speak.json").
+ with(:body => "{\"message\":{\"body\":\"Ik ben een gem aan het maken\"}}",
+ :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Capistrano::Blaze'}).
+ to_return(:status => 200, :body => "", :headers => {})
+
+ subject.configure do |config|
+ config.account = account
+ config.room_id = room_id
+ config.token = token
+ config.ssl = false
+ end
+
+ subject.speak "Ik ben een gem aan het maken"
+ end
+
+end
View
86 spec/camp_spec.rb
@@ -1,86 +0,0 @@
-require 'capistrano/blaze'
-require 'webmock'
-
-describe Capistrano::Blaze do
- include WebMock::API
-
- before do
- Capistrano::Blaze::Messages.any_instance.stub(:user) { "your mom" }
- end
-
- around do |example|
- $stderr = StringIO.new
- example.run
- $stderr = STDERR
- end
-
- it "can speak" do
- token = "abc"
- room_id = 1234
- account = "abcd"
-
- stub_request(:post, "http://#{token}:X@#{account}.campfirenow.com/room/#{room_id}/speak.json").
- with(:body => "{\"message\":{\"body\":\"Ik ben een gem aan het maken\"}}",
- :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Capistrano::Blaze'}).
- to_return(:status => 200, :body => "", :headers => {})
-
- subject.configure do |config|
- config.account = account
- config.room_id = room_id
- config.token = token
- config.ssl = false
- end
-
- subject.speak "Ik ben een gem aan het maken"
- end
-
- it "validates configuration" do
- subject.configure do |config|
- config.account = nil
- end
- expect { subject.speak "something" }.to raise_error("Please specify the account option")
- end
-
- it "displays a start message" do
- subject.should_receive(:speak).with("your mom is deploying to the production stage of basecamp, via `#{command}`")
- context = stub(:stage => "production", :application => "basecamp")
- subject.start(context)
- end
-
- it "displays a failure message" do
- subject.should_receive(:speak).with(":warning: your mom failed to deploy to the production stage of basecamp, via `#{command}`: woops (RuntimeError)")
- context = stub(:stage => "production", :application => "basecamp")
- exception = RuntimeError.new("woops")
- subject.failure(context, exception)
- end
-
- it "displays success message" do
- subject.should_receive(:speak).with("your mom succesfully deployed to the production stage of basecamp, via `#{command}`")
- context = stub(:stage => "production", :application => "basecamp")
- subject.success(context)
- end
-
- it "sends a test message" do
- subject.should_receive(:speak).with(":heart: basecamp!")
- context = stub(:application => "basecamp")
- subject.test(context)
- end
-
- it "displays success message without a stage" do
- subject.should_receive(:speak).with("your mom succesfully deployed basecamp, via `#{command}`")
- context = stub(:application => "basecamp")
- subject.success(context)
- end
-
- it "displays failure message without a stage" do
- subject.should_receive(:speak).with(":warning: your mom failed to deploy basecamp, via `#{command}`: woops (RuntimeError)")
- context = stub(:application => "basecamp")
- exception = RuntimeError.new("woops")
- subject.failure(context, exception)
- end
-
- def command
- [ 'cap', *ARGV ] * ' '
- end
-
-end
View
20 spec/configuration_spec.rb
@@ -0,0 +1,20 @@
+require 'capistrano/blaze/configuration'
+
+describe Capistrano::Blaze::Configuration do
+
+ REQUIRED = %W(account room_id token)
+
+ before do
+ REQUIRED.each do |option|
+ subject[option] = "foo"
+ end
+ end
+
+ REQUIRED.each do |option|
+ it "requires #{option} to be set" do
+ subject[option] = nil
+ expect { subject.validate! }.to raise_error("Please specify the #{option} option")
+ end
+ end
+
+end
View
68 spec/message_spec.rb
@@ -0,0 +1,68 @@
+require 'capistrano/blaze/message'
+
+describe Capistrano::Blaze::Message do
+
+ subject { Capistrano::Blaze::Message }
+ let(:exception) { RuntimeError.new("woops") }
+
+ before do
+ subject.any_instance.stub(:user) { "your mom" }
+ end
+
+ it "sends a test message" do
+ should_speak(":heart: basecamp!")
+ context = stub(:application => "basecamp")
+ subject.test(context)
+ end
+
+ context "with multistage extension" do
+
+ let(:context) { stub(:stage => "production", :application => "basecamp") }
+
+ it "displays a start message" do
+ should_speak("your mom is deploying to the production stage of basecamp, via `#{command}`")
+ subject.start(context)
+ end
+
+ it "displays a failure message" do
+ should_speak(":warning: your mom failed to deploy to the production stage of basecamp, via `#{command}`: woops (RuntimeError)")
+ subject.failure(context, exception)
+ end
+
+ it "displays success message" do
+ should_speak("your mom succesfully deployed to the production stage of basecamp, via `#{command}`")
+ subject.success(context)
+ end
+
+ end
+
+ context "without multistage extension" do
+
+ let(:context) { stub(:application => "basecamp") }
+
+ it "displays a start message" do
+ should_speak("your mom is deploying basecamp, via `#{command}`")
+ subject.start(context)
+ end
+
+ it "displays success message" do
+ should_speak("your mom succesfully deployed basecamp, via `#{command}`")
+ subject.success(context)
+ end
+
+ it "displays failure message" do
+ should_speak(":warning: your mom failed to deploy basecamp, via `#{command}`: woops (RuntimeError)")
+ subject.failure(context, exception)
+ end
+
+ end
+
+ def command
+ [ 'cap', *ARGV ] * ' '
+ end
+
+ def should_speak(message)
+ Capistrano::Blaze.should_receive(:speak).with(message)
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.