Skip to content
Browse files

Functionality

  • Loading branch information...
1 parent c8bb472 commit d3156525290d138f8a6d8839781911f85c81df6e @iain committed Jun 14, 2012
Showing with 190 additions and 7 deletions.
  1. +2 −0 .rspec
  2. +1 −0 .rvmrc
  3. +12 −0 .travis.yml
  4. +38 −2 README.md
  5. +4 −0 Rakefile
  6. +7 −3 blaze.gemspec
  7. +37 −2 lib/blaze.rb
  8. +36 −0 lib/blaze/configuration.rb
  9. +33 −0 spec/blaze/blaze_spec.rb
  10. +20 −0 spec/blaze/configuration_spec.rb
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format documentation
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm 1.9.3@blaze
View
12 .travis.yml
@@ -0,0 +1,12 @@
+language: ruby
+rvm:
+ - 1.8.7
+ - 1.9.2
+ - 1.9.3
+ - jruby-18mode
+ - jruby-19mode
+ - rbx-18mode
+ - rbx-19mode
+ - ruby-head
+ - jruby-head
+ - ree
View
40 README.md
@@ -1,6 +1,8 @@
# Blaze
-TODO: Write a gem description
+[![Build Status](https://secure.travis-ci.org/iain/blaze.png?branch=master)](http://travis-ci.org/iain/blaze)
+
+Just a tiny simple gem which lets you speak on Campfire. No runtime dependencies!
## Installation
@@ -18,7 +20,41 @@ Or install it yourself as:
## Usage
-TODO: Write usage instructions here
+Configure blaze:
+
+``` ruby
+require 'blaze'
+
+Blaze.configure do |config|
+ config.account = "your-subdomain"
+ config.room_id = 12345
+ config.token = "abcd"
+ config.ssl = true
+end
+```
+
+And speak:
+
+``` ruby
+Blaze.speak "hello there!"
+```
+
+## Coming up in v1.0
+
+A way to allow you to spam more rooms in one Ruby process.
+
+This would probably look something like:
+
+``` ruby
+client = Blaze::Client.new do |client|
+ client.account = "your-subdomain"
+ client.room_id = 12345
+ client.token = "abcd"
+ client.ssl = true
+end
+
+client.speak "Hello"
+```
## Contributing
View
4 Rakefile
@@ -1,2 +1,6 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec)
+task :default => :spec
View
10 blaze.gemspec
@@ -4,14 +4,18 @@ require File.expand_path('../lib/blaze/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["iain"]
gem.email = ["iain@iain.nl"]
- gem.description = %q{TODO: Write a gem description}
- gem.summary = %q{TODO: Write a gem summary}
- gem.homepage = ""
+ gem.description = %q{Very simple Campfire notification gem}
+ gem.summary = %q{Very simple Campfire notification gem}
+ gem.homepage = "https://github.com/iain/blaze"
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "blaze"
gem.require_paths = ["lib"]
gem.version = Blaze::VERSION
+
+ gem.add_development_dependency 'rspec'
+ gem.add_development_dependency 'webmock'
+
end
View
39 lib/blaze.rb
@@ -1,5 +1,40 @@
-require "blaze/version"
+require 'blaze/version'
+require 'blaze/configuration'
+require 'net/http'
+require 'json'
module Blaze
- # Your code goes here...
+ extend self
+
+ def configure
+ yield configuration
+ end
+
+ def configuration
+ @configuration ||= Configuration.new
+ end
+
+ def speak(message)
+ configuration.validate!
+ port = configuration.ssl ? 443 : 80
+
+ req = Net::HTTP::Post.new("/room/#{configuration.room_id}/speak.json")
+ req.basic_auth configuration.token, 'X'
+ req.body = { :message => { :body => message } }.to_json
+ req.content_type = "application/json"
+ req["User-Agent"] = "Blaze"
+
+ res = Net::HTTP.start("#{configuration.account}.campfirenow.com", port, :use_ssl => configuration.ssl) do |http|
+ http.request(req)
+ end
+
+ if res.is_a?(Net::HTTPSuccess)
+ warn "Campfire message sent!"
+ else
+ warn "Campfire communication failed!"
+ warn res.inspect
+ warn res.body.inspect
+ end
+ end
+
end
View
36 lib/blaze/configuration.rb
@@ -0,0 +1,36 @@
+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
View
33 spec/blaze/blaze_spec.rb
@@ -0,0 +1,33 @@
+require 'blaze'
+require 'webmock'
+
+describe 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'=>'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
20 spec/blaze/configuration_spec.rb
@@ -0,0 +1,20 @@
+require 'blaze/configuration'
+
+describe 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

0 comments on commit d315652

Please sign in to comment.
Something went wrong with that request. Please try again.