Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Big reorganisation; use TxtMachine::Base so the gem loads properly

  • Loading branch information...
commit 8468babe2e4cdb35e73e4e2408497088fe2e5029 1 parent 5aa422a
@jaz303 authored
View
2  Rakefile
@@ -15,7 +15,7 @@ rescue LoadError
end
task :txt_machine do
- require 'txt_machine'
+ require 'lib/txt_machine'
end
desc "Run the tests"
View
52 lib/txt_machine.rb
@@ -1,4 +1,15 @@
-class TxtMachine
+%w(
+ base
+ message
+ gateways/skeleton
+ gateways/discard
+ gateways/test
+ gateways/itagg
+).each do |file|
+ require File.dirname(__FILE__) + '/txt_machine/' + file
+end
+
+module TxtMachine
@@config = {
:gateway => nil,
@@ -21,6 +32,10 @@ def self.configure!(config)
@@config = config
end
+ def self.new(config = {})
+ Base.new(@@config.merge(config))
+ end
+
def self.start(config = {}, &block)
new(config).start(&block)
end
@@ -28,40 +43,5 @@ def self.start(config = {}, &block)
def self.send_message(*args)
start { |me| me.send_message(*args) }
end
-
- def initialize(config = {})
- @config = @@config.merge(config)
- end
-
- def start
- gateway.connect { yield self }
- end
-
- def send_message(*args)
- gateway.send(build_message(*args))
- end
-
- def gateway
- @gateway ||= gateway_class.new(@config)
- end
-
- def build_message(*params)
- if params.first.is_a?(Message)
- params.first
- else
- Message.new(*params)
- end
- end
-
-private
-
- def gateway_class
- case @config[:gateway]
- when Class
- @config[:gateway]
- else
- raise ArgumentError
- end
- end
end
View
40 lib/txt_machine/base.rb
@@ -0,0 +1,40 @@
+module TxtMachine
+ class Base
+
+ def initialize(config = {})
+ @config = config
+ end
+
+ def start
+ gateway.connect { yield self }
+ end
+
+ def send_message(*args)
+ gateway.send(build_message(*args))
+ end
+
+ def gateway
+ @gateway ||= gateway_class.new(@config)
+ end
+
+ def build_message(*params)
+ if params.first.is_a?(Message)
+ params.first
+ else
+ Message.new(*params)
+ end
+ end
+
+ private
+
+ def gateway_class
+ case @config[:gateway]
+ when Class
+ @config[:gateway]
+ else
+ raise ArgumentError
+ end
+ end
+
+ end
+end
View
2  lib/txt_machine/gateways/discard.rb
@@ -1,4 +1,4 @@
-class TxtMachine
+module TxtMachine
module Gateways
class Discard < Skeleton
def send(message)
View
2  lib/txt_machine/gateways/itagg.rb
@@ -3,7 +3,7 @@
require 'net/https'
require 'cgi'
-class TxtMachine
+module TxtMachine
module Gateways
class Itagg < Skeleton
View
2  lib/txt_machine/gateways/skeleton.rb
@@ -1,4 +1,4 @@
-class TxtMachine
+module TxtMachine
module Gateways
class Skeleton
View
2  lib/txt_machine/gateways/test.rb
@@ -1,4 +1,4 @@
-class TxtMachine
+module TxtMachine
@@deliveries = []
View
40 lib/txt_machine/message.rb
@@ -1,28 +1,30 @@
-class TxtMachine::Message
- attr_accessor :originator
- attr_accessor :body
- attr_reader :to
+module TxtMachine
+ class Message
+ attr_accessor :originator
+ attr_accessor :body
+ attr_reader :to
- def initialize(*args)
+ def initialize(*args)
- options = args.last.is_a?(Hash) ? args.pop : {}
+ options = args.last.is_a?(Hash) ? args.pop : {}
- @originator = options[:originator]
- @body = options[:body] || ''
- @to = [options[:to]].flatten.compact
+ @originator = options[:originator]
+ @body = options[:body] || ''
+ @to = [options[:to]].flatten.compact
- unless args.empty?
- @body = args.first.to_s
- end
+ unless args.empty?
+ @body = args.first.to_s
+ end
- end
+ end
- def <<(recipient)
- @to << recipient
- self
- end
+ def <<(recipient)
+ @to << recipient
+ self
+ end
- def recipients
- [to].flatten
+ def recipients
+ [to].flatten
+ end
end
end
View
3  test/txt_machine_test.rb
@@ -36,7 +36,7 @@ def test_build_message_handles_string_and_options
def test_start_should_yield_self
TxtMachine.start do |tm|
- assert tm.is_a?(TxtMachine)
+ assert tm.is_a?(TxtMachine::Base)
end
end
@@ -48,5 +48,4 @@ def test_send_message_should_invoke_send_on_gateway
end
end
-
end
View
10 txt_machine.rb
@@ -1,10 +0,0 @@
-%w(
- txt_machine
- txt_machine/message
- txt_machine/gateways/skeleton
- txt_machine/gateways/discard
- txt_machine/gateways/test
- txt_machine/gateways/itagg
-).each do |file|
- require File.dirname(__FILE__) + '/lib/' + file
-end
Please sign in to comment.
Something went wrong with that request. Please try again.