Permalink
Browse files

try loading config from yml file before resorting to options, event n…

…ame as option
  • Loading branch information...
carhartl committed Mar 1, 2011
1 parent 65806bc commit 07ebc1f90285fa9f691e638404733360313224e5
Showing with 101 additions and 30 deletions.
  1. +27 −6 README.rdoc
  2. +26 −4 lib/guard/pusher.rb
  3. +1 −1 lib/guard/pusher/templates/Guardfile
  4. +47 −19 spec/guard/pusher_spec.rb
View
@@ -27,17 +27,22 @@ Add guard definition to your Guardfile by running this command:
Get a free Pusher developer account: http://pusherapp.com (also available as Heroku add-on).
-Add API credentials to your guard definition:
+Create a YAML file containing your API credentials for the development environment and save it in the config directory:
- guard 'pusher', :app_id => '...', :key => '...', :secret => '...' do
- ...
- end
+ # config/pusher.yml
+ development:
+ app_id: ...
+ key: ...
+ secret: ...
-Include the following script (example for reloading a page):
+You can also pass these credentials as option in your guard definition, see Options below.
+
+Include the following JavaScript:
<script src="http://js.pusherapp.com/1.8/pusher.min.js"></script>
<script>
- new Pusher(<key>).subscribe('guard-pusher').bind('reload', function() {
+ new Pusher(<key>).subscribe('guard-pusher').bind('guard', function() {
+ // do something - for instance simply reload the page:
location.reload();
});
</script>
@@ -46,6 +51,22 @@ Include the following script (example for reloading a page):
Please read {Guard usage doc}[https://github.com/guard/guard#readme]
+== Options
+
+Pass Pusher API credentials as option:
+
+ guard 'pusher', :app_id => ..., :key => '...', :secret => '...' do
+ ...
+ end
+
+Set the name of the event you're binding to:
+
+ guard 'pusher', :event => 'ping' do
+ ...
+ end
+
+ new Pusher(...).subscribe('guard-pusher').bind('ping', function() { ... });
+
== Development
- Source hosted at {GitHub}[https://github.com/guard/guard-pusher]
View
@@ -7,13 +7,35 @@ class Pusher < Guard
def initialize(watchers = [], options = {})
super
- [ :app_id, :key, :secret ].each { |attr|
- ::Pusher.send("#{attr}=", options[attr]) unless ::Pusher.send("#{attr}")
- }
+
+ @options = options
+
+ config = if File.file?('config/pusher.yml')
+ YAML.load_file('config/pusher.yml')['development']
+ else
+ options
+ end
+
+ if config_pusher_with(config)
+ UI.info('Pusher is ready.', :reset => true)
+ else
+ UI.info("D'oh! Pusher not properly configured. Make sure to add app_id, key and secret.", :reset => true)
+ end
end
def run_on_change(paths)
- ::Pusher['guard-pusher'].trigger('reload', {})
+ ::Pusher['guard-pusher'].trigger(@options[:event] || 'guard', {})
+ end
+
+
+ private
+
+ def config_pusher_with(credentials)
+ %w{app_id key secret}.each do |config|
+ return false unless (value = credentials[config] || credentials[config.to_sym])
+ ::Pusher.send("#{config}=", value)
+ end
+ true
end
end
@@ -1,4 +1,4 @@
-guard 'pusher', :app_id => 'your app_id', :key => 'your key', :secret => 'your secret' do
+guard 'pusher' do
# Rails example
watch(%r{public/.+\.(css|js)})
end
View
@@ -2,32 +2,60 @@
describe Guard::Pusher do
- describe "options" do
- context "Pusher not configured" do
+ describe "configuration" do
+ context "provided through YAML file" do
+ before(:each) do
+ File.should_receive(:file?).
+ with('config/pusher.yml').
+ and_return(true)
+
+ YAML.should_receive(:load_file).
+ with('config/pusher.yml').
+ and_return({ "development" => { "app_id" => 42, "key" => "fake_key", "secret" => "fake_secret" }})
+ end
+
it "requires 'app_id', 'key' and 'secret'" do
- Pusher.should_receive(:app_id=).with('1234')
- Pusher.should_receive(:key=).with('key')
- Pusher.should_receive(:secret=).with('secret')
+ Pusher.should_receive(:app_id=).with(42)
+ Pusher.should_receive(:key=).with('fake_key')
+ Pusher.should_receive(:secret=).with('fake_secret')
+ Guard::UI.should_receive(:info).with(/.*Pusher is ready.*/, :reset => true)
+ Guard::Pusher.new([])
+ end
+ end
+
+ context "provided through options" do
+ before(:each) do
+ File.should_receive(:file?).
+ with('config/pusher.yml').
+ and_return(false)
+ end
+
+ it "requires 'app_id', 'key' and 'secret'" do
+ Pusher.should_receive(:app_id=).with(42)
+ Pusher.should_receive(:key=).with('fake_key')
+ Pusher.should_receive(:secret=).with('fake_secret')
+ Guard::UI.should_receive(:info).with(/.*Pusher is ready.*/, :reset => true)
Guard::Pusher.new([], {
- :app_id => '1234',
- :key => 'key',
- :secret => 'secret'
+ :app_id => 42,
+ :key => 'fake_key',
+ :secret => 'fake_secret'
})
end
end
- context "Pusher already configured" do
- it "does not require 'app_id', 'key' and 'secret'" do
- Pusher.app_id = "1234"
- Pusher.key = "key"
- Pusher.secret = "secret"
+ context "missing the necessery keys" do
+ before(:each) do
+ File.should_receive(:file?).
+ with('config/pusher.yml').
+ and_return(false)
+ end
+
+ it "does not attempt to configure Pusher and issues a warning" do
Pusher.should_not_receive(:app_id=)
- Pusher.should_not_receive(:key=)
- Pusher.should_not_receive(:secret=)
+ Guard::UI.should_receive(:info).with(/.*Pusher not properly configured.*/, :reset => true)
Guard::Pusher.new([], {
- :app_id => '1234',
- :key => 'key',
- :secret => 'secret'
+ :key => 'fake_key',
+ :secret => 'fake_secret'
})
end
end
@@ -37,7 +65,7 @@
it "sends Pusher message" do
channel = mock(Pusher::Channel)
Pusher.should_receive(:[]).with('guard-pusher').and_return(channel)
- channel.should_receive(:trigger).with('reload', {})
+ channel.should_receive(:trigger).with('guard', {})
subject.run_on_change(['foo'])
end
end

0 comments on commit 07ebc1f

Please sign in to comment.