action_sms 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,8 +1,9 @@
1
1
  # action_sms
2
2
 
3
- action_sms is a lightweight wrapper based on [activesms](http://github.com/nofxx/activesms). action_sms allows you to use existing SMS Gateway Adapters and switch between them effortlessly without modifing your application code. Unlike [activesms](http://github.com/nofxx/activesms), action_sms does not implement any SMS Gateway Adapters. For this see [action_sms_gateways](http://github.com/dwilkie/action_sms_gateways)
3
+ action_sms allows you to use existing or custom SMS Gateway Adapters and switch between them effortlessly without modifying your application or test code. By keeping SMS Gateway configuration out of your code you can be more adaptable to change.
4
4
 
5
5
  ## Usage
6
+
6
7
  Establish a connection to your SMS Gateway Adapter:
7
8
 
8
9
  ActionSms::Base.establish_connection(
@@ -13,33 +14,31 @@ Establish a connection to your SMS Gateway Adapter:
13
14
  )
14
15
  The only required option is `:adapter`. This specifies the SMS Gateway adapter you want to use. The remaining options are specific to the adapter.
15
16
 
16
- Then subclass `ActionSms::Base` to define a notifier
17
+ Now from your application or test code you can call methods on the adapter by calling:
17
18
 
18
- class SMSNotifier < ActionSms::Base
19
- end
19
+ ActionSms::Base.connection
20
20
 
21
- Now you can call:
21
+ ## Adapters
22
22
 
23
- SMSNotifier.deliver(sms)
23
+ See [action_sms_gateways](http://github.com/dwilkie/action_sms_gateways) for existing adapters and information on how to create your own.
24
24
 
25
- ### Rails
26
- For convenience there are a couple of generators that can be used if you are using action_sms within a Rails app.
25
+ ## Rails
27
26
 
28
- rails g initializer
29
- Generates an initializer under config/initializers with the `establish_connection` code described above.
27
+ For convenience there is a generator that can be used if you are using action_sms within a Rails app
30
28
 
31
- rails g notifier
32
- Generates a notifier under app/notifiers with the `SMSNotifier` code described above.
29
+ rails g action_sms:initializer
30
+
31
+ Generates an initializer under config/initializers with the `establish_connection` code described above.
33
32
 
34
33
  ## Installation
35
34
 
36
35
  gem install action_sms
36
+
37
37
  ### Rails
38
+
38
39
  Place the following in your Gemfile:
39
40
 
40
41
  gem action_sms
41
- ## SMS Gateway Adapters
42
- See [action_sms_gateways](http://github.com/dwilkie/action_sms_gateways)
43
42
 
44
43
  Copyright (c) 2010 David Wilkie, released under the MIT license
45
44
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/action_sms.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{action_sms}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David Wilkie"]
12
- s.date = %q{2010-09-15}
12
+ s.date = %q{2010-10-14}
13
13
  s.email = %q{dwilkie@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.markdown"
@@ -25,14 +25,10 @@ Gem::Specification.new do |s|
25
25
  "lib/action_sms/base.rb",
26
26
  "lib/action_sms/connection_adapters.rb",
27
27
  "lib/action_sms/connection_adapters/abstract_adapter.rb",
28
- "lib/action_sms/connections.rb",
29
28
  "lib/action_sms/exceptions.rb",
30
29
  "lib/generators/action_sms/initializer/USAGE",
31
30
  "lib/generators/action_sms/initializer/initializer_generator.rb",
32
31
  "lib/generators/action_sms/initializer/templates/action_sms.rb",
33
- "lib/generators/action_sms/notifier/USAGE",
34
- "lib/generators/action_sms/notifier/notifier_generator.rb",
35
- "lib/generators/action_sms/notifier/templates/sms_notifier.rb",
36
32
  "todo"
37
33
  ]
38
34
  s.homepage = %q{http://github.com/dwilkie/action_sms}
@@ -1,10 +1,55 @@
1
1
  module ActionSms #:nodoc#
2
+
2
3
  class Base
3
- @@logger = nil
4
- cattr_accessor :logger
4
+ @@connection = nil
5
+
6
+ class << self
7
+ # Returns true if a connection that's accessible to this class has already
8
+ # been opened.
9
+ def connected?
10
+ return !@@connection.nil?
11
+ end
12
+
13
+ # Returns the connection currently associated with the class. This can
14
+ # also be used to "borrow" the connection to do work that is specific to
15
+ # a particular SMS gateway.
16
+ def connection
17
+ raise ConnectionNotEstablished unless @@connection
18
+ return @@connection
19
+ end
20
+
21
+ # Set the gateway connection for the class.
22
+ def connection=(spec) #:nodoc:
23
+ raise ConnectionNotEstablished unless spec
24
+ @@connection = spec
25
+ end
5
26
 
6
- def self.deliver(sms)
7
- self.connection.deliver(sms)
27
+ # Establishes the connection to the SMS gateway. Accepts a hash as input
28
+ # where the :adapter key must be specified with the name of a gateway
29
+ # adapter (in lower-case)
30
+ #
31
+ # ActionSms::Base.establish_connection(
32
+ # :adapter => "clickatell",
33
+ # :username => "myusername",
34
+ # :password => "mypassword"
35
+ # :api_id => "myapiid"
36
+ # )
37
+ #
38
+ # Also accepts keys as strings (for parsing from YAML, for example).
39
+ #
40
+ # The exceptions AdapterNotSpecified, AdapterNotFound, and ArgumentError
41
+ # may be returned.
42
+ def establish_connection(config)
43
+ unless config.key?(:adapter)
44
+ raise AdapterNotSpecified, "#{config} adapter is not configured"
45
+ end
46
+ adapter_method = "#{config[:adapter]}_connection"
47
+ unless respond_to?(adapter_method)
48
+ raise AdapterNotFound,
49
+ "configuration specifies nonexistent #{config[:adapter]} adapter"
50
+ end
51
+ self.connection = self.send(adapter_method, config)
52
+ end
8
53
  end
9
54
  end
10
55
  end
@@ -6,35 +6,19 @@ module ActionSms #:nodoc:
6
6
  # class. You can use this interface directly by borrowing the gateway
7
7
  # connection from the Base with Base.connection.
8
8
  class AbstractAdapter
9
-
10
- @logger = nil
11
- attr_accessor :logger
12
-
13
- def initialize(logger = nil) #:nodoc:
14
- @logger = logger
15
- end
16
-
17
- def deliver(sms)
18
- end
19
-
20
- def parse(sms)
21
- nil
22
- end
23
-
24
9
  protected
25
- # Helper method to send an HTTP request to +url+ with paramaters
10
+ # Helper method to send an HTTP POST request to +url+ with paramaters
26
11
  # specified by the +params+ hash.
27
- def send_http_request(url, params)
12
+ def send_http_request(url, data)
28
13
  uri = URI.parse(url)
29
14
  req = Net::HTTP::Post.new(uri.path)
30
- req.set_form_data(params)
15
+ data.is_a?(Hash) ? req.set_form_data(data) : req.body = data
31
16
 
32
17
  http = Net::HTTP.new(uri.host, uri.port)
33
18
  http.use_ssl = true if uri.scheme == 'https'
34
19
  resp = http.start do
35
20
  http.request(req)
36
21
  end
37
- @logger.info "Response: #{resp.body}" unless @logger.nil?
38
22
  resp.body
39
23
  end
40
24
  end
data/lib/action_sms.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'action_sms/connections'
2
1
  require 'action_sms/base'
3
2
  require 'action_sms/exceptions'
4
3
  require 'action_sms/connection_adapters'
@@ -1,4 +1,3 @@
1
- require 'rails/generators'
2
1
  module ActionSms
3
2
  class InitializerGenerator < Rails::Generators::Base
4
3
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - David Wilkie
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-15 00:00:00 +07:00
17
+ date: 2010-10-14 00:00:00 +07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -37,14 +37,10 @@ files:
37
37
  - lib/action_sms/base.rb
38
38
  - lib/action_sms/connection_adapters.rb
39
39
  - lib/action_sms/connection_adapters/abstract_adapter.rb
40
- - lib/action_sms/connections.rb
41
40
  - lib/action_sms/exceptions.rb
42
41
  - lib/generators/action_sms/initializer/USAGE
43
42
  - lib/generators/action_sms/initializer/initializer_generator.rb
44
43
  - lib/generators/action_sms/initializer/templates/action_sms.rb
45
- - lib/generators/action_sms/notifier/USAGE
46
- - lib/generators/action_sms/notifier/notifier_generator.rb
47
- - lib/generators/action_sms/notifier/templates/sms_notifier.rb
48
44
  - todo
49
45
  has_rdoc: true
50
46
  homepage: http://github.com/dwilkie/action_sms
@@ -1,64 +0,0 @@
1
- module ActionSms #:nodoc#
2
-
3
- class Base
4
- @@connection = nil
5
-
6
- class << self
7
- # Returns true if a connection that's accessible to this class has already
8
- # been opened.
9
- def connected?
10
- return !@@connection.nil?
11
- end
12
-
13
- # Returns the connection currently associated with the class. This can
14
- # also be used to "borrow" the connection to do work that is specific to
15
- # a particular SMS gateway.
16
- def connection
17
- raise ConnectionNotEstablished unless @@connection
18
- return @@connection
19
- end
20
-
21
- # Set the gateway connection for the class.
22
- def connection=(spec) #:nodoc:
23
- raise ConnectionNotEstablished unless spec
24
- @@connection = spec
25
- end
26
-
27
- # Establishes the connection to the SMS gateway. Accepts a hash as input
28
- # where the :adapter key must be specified with the name of a gateway
29
- # adapter (in lower-case)
30
- #
31
- # ActionSms::Base.establish_connection(
32
- # :adapter => "clickatell",
33
- # :username => "myusername",
34
- # :password => "mypassword"
35
- # :api_id => "myapiid"
36
- # )
37
- #
38
- # Also accepts keys as strings (for parsing from YAML, for example).
39
- #
40
- # The exceptions AdapterNotSpecified, AdapterNotFound, and ArgumentError
41
- # may be returned.
42
- def establish_connection(config)
43
- config = config.symbolize_keys
44
- unless config.key?(:adapter)
45
- raise AdapterNotSpecified, "#{config} adapter is not configured"
46
- end
47
- adapter_method = "#{config[:adapter]}_connection"
48
- unless respond_to?(adapter_method)
49
- raise AdapterNotFound,
50
- "configuration specifies nonexistent #{config[:adapter]} adapter"
51
- end
52
- self.connection = self.send(adapter_method, config)
53
- end
54
- end
55
-
56
- # Returns the connection currently associated with the class. This can
57
- # also be used to "borrow" the connection to do work that is specific to
58
- # a particular SMS gateway.
59
- def connection
60
- self.class.connection
61
- end
62
- end
63
- end
64
-
@@ -1,6 +0,0 @@
1
- Description:
2
- Creates an sms notifier under app/notifiers
3
-
4
- Example:
5
- rails g action_sms:notifier
6
-
@@ -1,14 +0,0 @@
1
- require 'rails/generators'
2
- module ActionSms
3
- class NotifierGenerator < Rails::Generators::Base
4
-
5
- def self.source_root
6
- @source_root ||= File.join(File.dirname(__FILE__), 'templates')
7
- end
8
-
9
- def copy_notifier_file
10
- copy_file "sms_notifier.rb", "app/notifiers/sms_notifier.rb"
11
- end
12
- end
13
- end
14
-
@@ -1,3 +0,0 @@
1
- class SMSNotifier < ActionSms::Base
2
- end
3
-