suls-smsr 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ module SmsR
2
+ module Actions
3
+
4
+ class Config < RunnableAction
5
+ runnable do |provider, user, password|
6
+ SmsR.debug "Store entry for #{provider}"
7
+ SmsR.config[:"#{provider}"] = user, password
8
+ SmsR.config.save!
9
+ SmsR.info "Config for #{provider} added."
10
+ end
11
+
12
+ runnable :config do |provider|
13
+ p_c = SmsR.config[:"#{provider}"]
14
+ SmsR.info "Saved config for Provider '#{provider}':"
15
+ %w{user password}.each { |e| SmsR.info " #{e}: #{p_c.send e}"}
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module SmsR
2
+ module Actions
3
+
4
+ class List < RunnableAction
5
+ runnable :load_providers do
6
+
7
+ SmsR.info "","Available providers:", ""
8
+ SmsR::Providers.providers.each do |name, prov|
9
+ if SmsR.config[name.to_sym]
10
+ has_config = ""
11
+ else
12
+ has_config = "*"
13
+ end
14
+ SmsR.info "\t#{name} #{has_config}"
15
+ end
16
+ SmsR.info "","* indicates no config saved for the provider."
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ require "rubygems"
2
+ require "rake"
3
+
4
+ module SmsR
5
+ module Actions
6
+
7
+ class Send < RunnableAction
8
+ runnable :load_providers do
9
+ SmsR.info "Available providers:"
10
+ SmsR::Providers.providers.each { |k,v| SmsR.info " #{k}"}
11
+ end
12
+
13
+ runnable :load_providers, :config, :provider do |provider_name, number, message|
14
+
15
+ SmsR.debug "using #{@provider} " +
16
+ "with config #{@config}"
17
+
18
+ @provider.call @config.user, @config.password, number, message
19
+
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ require 'yaml'
2
+
3
+ module SmsR
4
+ OperatorConfig = Struct.new(:user, :password)
5
+
6
+ class Config
7
+ attr_reader :last_saved
8
+
9
+ def self.load(file=ENV['HOME']+'/.smsr_config')
10
+ return File.open(file) { |f| YAML.load(f) } if File.exists? file
11
+ Config.new(file)
12
+ end
13
+
14
+ def save!(f_to_save=@config_file)
15
+ @last_saved = Time.now
16
+ File.open(f_to_save, "w") { |f| YAML.dump(self, f) }
17
+ end
18
+
19
+ def initialize(f=nil)
20
+ @config_file = f
21
+ @config = {}
22
+ end
23
+
24
+ def []=(key, params)
25
+ @config[key] = OperatorConfig.new(*params)
26
+ end
27
+
28
+ def [](key)
29
+ @config[key]
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ # from http://eigenclass.org/hiki/bounded+space+instance_exec
2
+ class Object
3
+ module InstanceExecHelper; end
4
+ include InstanceExecHelper
5
+ def instance_exec(*args, &block)
6
+ begin
7
+ old_critical, Thread.critical = Thread.critical, true
8
+ n = 0
9
+ n += 1 while respond_to?(mname="__instance_exec#{n}")
10
+ InstanceExecHelper.module_eval{ define_method(mname, &block) }
11
+ ensure
12
+ Thread.critical = old_critical
13
+ end
14
+ begin
15
+ ret = send(mname, *args)
16
+ ensure
17
+ InstanceExecHelper.module_eval{ remove_method(mname) } rescue nil
18
+ end
19
+ ret
20
+ end
21
+ end
22
+
23
+ # http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
24
+ class Object
25
+ # The hidden singleton lurks behind everyone
26
+ def eigenclass; class << self; self; end; end
27
+
28
+ def add_instance_method(name, blk)
29
+ eigenclass.send :define_method, name, blk
30
+ end
31
+ end
@@ -0,0 +1,54 @@
1
+ module SmsR
2
+ module Providers
3
+
4
+ DEFAULT_PROVIDERS = File.dirname(__FILE__) +
5
+ '/../providers'
6
+
7
+ extend self
8
+
9
+ def provider(provider_name, &block)
10
+ SmsR.debug "registering #{provider_name}"
11
+ providers[provider_name] = Provider.new(block)
12
+ end
13
+
14
+ def providers
15
+ @providers ||= {}
16
+ end
17
+
18
+ def reset
19
+ @providers = {}
20
+ end
21
+
22
+ def load(*files)
23
+ files.each do |file|
24
+ if File.exists? file
25
+ SmsR.debug "Loading providers from: #{file}"
26
+ data = File.read(file)
27
+ # @providers = {} if drop_existing
28
+ module_eval data, file
29
+ else
30
+ SmsR.debug "Provider file #{file} doesn't exist."
31
+ end
32
+ end
33
+ providers
34
+ end
35
+
36
+ end
37
+
38
+ module ProviderHelper
39
+
40
+ def find_form_with_field(page, fieldname)
41
+ page.forms.each do |form|
42
+ if form.fields.find{|f| f.name == fieldname}
43
+ yield form
44
+ end
45
+ end
46
+ end
47
+
48
+ def to_ISO_8859_1(msg)
49
+ require "iconv"
50
+ Iconv.conv("ISO-8859-1", "UTF-8", message)
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,14 @@
1
+ module SmsR
2
+ module Providers
3
+
4
+ class Provider
5
+
6
+ include ProviderHelper
7
+
8
+ def initialize(block)
9
+ add_instance_method :call, block
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,97 @@
1
+ require "ostruct"
2
+ require "optparse"
3
+
4
+ module SmsR
5
+
6
+ extend self
7
+
8
+ def start(args, io=STDOUT)
9
+ @io = io
10
+ args_copy = args.dup
11
+ @options = parse_options(args)
12
+
13
+ info ""
14
+ debug "args: ", args_copy << " "
15
+ debug "options: ", options
16
+
17
+ debug "SmsR #{VERSION::STRING} started .."
18
+
19
+ invoke_action(args.shift, args) if args.size > 0 if args
20
+
21
+ debug ".. exiting SmsR"
22
+ info ""
23
+ end
24
+
25
+ def debug?
26
+ !!@debug
27
+ end
28
+
29
+ # FIXME: duplication!
30
+ def debug(*messages)
31
+ @io.puts messages.map { |m| "** #{m}" } if debug?
32
+ end
33
+
34
+ # FIXME: duplication!
35
+ def info(*messages)
36
+ @io.puts messages.map { |m| " #{m}" }
37
+ end
38
+
39
+ def parse_options(args)
40
+ options = OpenStruct.new
41
+ options.debug = false
42
+
43
+ begin
44
+ OptionParser.new do |opts|
45
+ opts.banner = <<BANNER
46
+ Usage: smsr [action] [parameters] [options]
47
+
48
+ To send a sms:
49
+
50
+ $ smsr-send provider_name 079xx "this is my message"
51
+
52
+ Provider 'provider_name' must exist. To see what providers exist:
53
+
54
+ $ smsr list
55
+
56
+ BANNER
57
+
58
+ opts.on("-d", "--debug", "Show debug log statements") do |d|
59
+ options.debug = d
60
+ end
61
+
62
+ opts.on_tail("--version", "Show version") do
63
+ @io.puts VERSION::STRING
64
+ exit
65
+ end
66
+ end.parse!(args)
67
+ rescue OptionParser::InvalidOption => e
68
+ @io.puts e
69
+ exit
70
+ end
71
+
72
+ @debug = options.debug
73
+
74
+ options
75
+ end
76
+
77
+ def options
78
+ @options
79
+ end
80
+
81
+ def config
82
+ @config ||= Config.load
83
+ end
84
+
85
+ def invoke_action(action, args)
86
+ action = action.capitalize
87
+ available_actions = (Actions.constants - ['RunnableAction'])
88
+ debug "available actions: ", *(available_actions.map{|e| "\t"+e}) << " "
89
+
90
+ if available_actions.include?(action)
91
+ Actions.const_get(action).run(args)
92
+ else
93
+ debug "selected action not available: #{action}"
94
+ end
95
+ end
96
+
97
+ end
@@ -0,0 +1,10 @@
1
+ module SmsR
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ SUMMARY = "SmsR version #{STRING}"
9
+ end
10
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{smsr}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Mathias Sulser"]
9
+ s.date = %q{2008-11-04}
10
+ s.description = %q{Simple commandline utility for sending sms.}
11
+ s.email = ["suls@suls.org"]
12
+ s.executables = ["smsr", "smsr-config", "smsr-send"]
13
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
14
+ s.files = [".autotest", ".gitignore", "History.txt", "Manifest.txt", "README.txt", "Rakefile", "bin/smsr", "bin/smsr-config", "bin/smsr-send", "lib/providers/hispeed.rb", "lib/providers/o2.rb", "lib/providers/orange.rb", "lib/providers/swisscom.rb", "lib/smsr.rb", "lib/smsr/actions.rb", "lib/smsr/actions/config.rb", "lib/smsr/actions/list.rb", "lib/smsr/actions/send.rb", "lib/smsr/config.rb", "lib/smsr/extensions.rb", "lib/smsr/providers.rb", "lib/smsr/providers/provider.rb", "lib/smsr/smsr.rb", "lib/smsr/version.rb", "smsr.gemspec", "spec/providers/hispeed_spec.rb", "spec/providers/orange_spec.rb", "spec/providers/provider_helper.rb", "spec/providers/swisscom_spec.rb", "spec/smsr/actions/config_spec.rb", "spec/smsr/actions/send_spec.rb", "spec/smsr/actions_requirements_spec.rb", "spec/smsr/actions_spec.rb", "spec/smsr/config_spec.rb", "spec/smsr/providers_spec.rb", "spec/smsr/version_spec.rb", "spec/smsr_spec.rb", "spec/spec_helper.rb"]
15
+ s.has_rdoc = true
16
+ s.homepage = %q{http://sulsarts.ch/p/smsr}
17
+ s.rdoc_options = ["--main", "README.txt"]
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = %q{smsr}
20
+ s.rubygems_version = %q{1.3.1}
21
+ s.summary = %q{SmsR version 0.0.1}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<mechanize>, [">= 0.7.8"])
29
+ s.add_runtime_dependency(%q<rspec>, [">= 1.1.4"])
30
+ s.add_development_dependency(%q<hoe>, [">= 1.8.2"])
31
+ else
32
+ s.add_dependency(%q<mechanize>, [">= 0.7.8"])
33
+ s.add_dependency(%q<rspec>, [">= 1.1.4"])
34
+ s.add_dependency(%q<hoe>, [">= 1.8.2"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<mechanize>, [">= 0.7.8"])
38
+ s.add_dependency(%q<rspec>, [">= 1.1.4"])
39
+ s.add_dependency(%q<hoe>, [">= 1.8.2"])
40
+ end
41
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/provider_helper'
2
+
3
+ describe "Hispeed" do
4
+ it "should test the orange provider" do
5
+ running :hispeed, "usr", "pwd", "num", "msg" do
6
+ WWW::Mechanize.stub!(:new)
7
+ end
8
+
9
+ pending
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/provider_helper'
2
+
3
+ describe "Orange" do
4
+ it "should test the orange provider" do
5
+ running :orange, "usr", "pwd", "num", "msg" do
6
+ WWW::Mechanize.stub!(:new)
7
+ end
8
+
9
+ pending
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ def running(cmd, *args, &block)
4
+ SmsR::Providers.load File.dirname(__FILE__) +
5
+ "/../../lib/providers/#{cmd}.rb"
6
+
7
+ # rcov hack
8
+ begin
9
+ require File.dirname(__FILE__) +
10
+ "/../../lib/providers/#{cmd}.rb"
11
+ rescue NoMethodError => e
12
+ end
13
+
14
+ block.call
15
+ SmsR::Providers.providers[cmd].call args
16
+ end
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/provider_helper'
2
+
3
+ describe SmsR::Providers::Provider, "Swisscom" do
4
+ it "should test the swisscom provider" do
5
+ running :swisscom, "usr", "pwd", "num", "msg" do
6
+ WWW::Mechanize.stub!(:new)
7
+ end
8
+
9
+ pending
10
+ end
11
+
12
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe SmsR::Actions::Config do
4
+ it "should respond to the config action"
5
+ it "should list the configs entries when run with 0 argument"
6
+ it "should list the provider's config entry when run with 1 argument"
7
+ it "should save the provider's config entry when run with 3 argument"
8
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe SmsR::Actions::Send do
4
+ it "should respond to the send action"
5
+ it "should list the available providers when run with 0 argument"
6
+ it "should send the sms using the give provider when run with 3 arguments"
7
+ end
@@ -0,0 +1,143 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe SmsR::Actions::Requirements do
4
+
5
+ before(:each) do
6
+ Object.send(:remove_const, :SubTest) if defined? SubTest
7
+ end
8
+
9
+ after(:each) do
10
+ Object.send(:remove_const, :SubTest) if defined? SubTest
11
+ end
12
+
13
+
14
+ it "should run the defined requirements in sequence" do
15
+ helper = mock("RequirementsFoo")
16
+ helper.should_receive(:first).and_return(true)
17
+ helper.should_receive(:second).and_return(false)
18
+ helper.should_not_receive(:third)
19
+
20
+ helper_mod = MixinMock.new(helper)
21
+
22
+ SmsR::Actions.stub!(:requirements).and_return(helper_mod)
23
+
24
+ class SubTest < SmsR::Actions::RunnableAction
25
+ runnable :first, :second, :third do |needed|
26
+ end
27
+ end
28
+
29
+ st = SubTest.new :p
30
+ st.requirements = [:first, :second, :third]
31
+ st.check.should be(false)
32
+ end
33
+ end
34
+
35
+ describe SmsR::Actions::Requirements, "requirement #config" do
36
+
37
+ before(:all) do
38
+ Test = Class.new do
39
+ attr_accessor :provider_name, :error
40
+ include SmsR::Actions::Requirements
41
+
42
+ def initialize
43
+ @error = []
44
+ end
45
+ end
46
+ @tester = Test.new
47
+ @config = mock("Config")
48
+ end
49
+
50
+ after(:each) do
51
+ @tester = nil
52
+ @config = nil
53
+ SmsR::Config.stub!(:load).and_return(nil)
54
+ end
55
+
56
+ it "should return false if no config for provider :nonexisting_provider exists" do
57
+ @tester.provider_name = :nonexisting_provider
58
+
59
+ @config.should_receive(:[]).
60
+ with(:nonexisting_provider).
61
+ and_return(nil)
62
+
63
+ SmsR.stub!(:config).and_return(@config)
64
+
65
+ @tester.config.should be(false)
66
+ end
67
+
68
+ it "should return 'true' if config for :existing_provider_1 exists" do
69
+ @tester.provider_name = :existing_provider
70
+
71
+ @config.should_receive(:[]).
72
+ with(:existing_provider).
73
+ and_return(true)
74
+
75
+ SmsR.stub!(:config).and_return(@config)
76
+
77
+ @tester.config.should be(true)
78
+ end
79
+
80
+ it "should add an error to @error when failing" do
81
+ @tester.provider_name = :nonexisting_provider
82
+
83
+ @config.should_receive(:[]).
84
+ with(:nonexisting_provider).
85
+ and_return(nil)
86
+
87
+ SmsR.stub!(:config).and_return(@config)
88
+
89
+ @tester.config
90
+
91
+ @tester.error.size.should_not be(0)
92
+ end
93
+ end
94
+
95
+ describe SmsR::Actions::Requirements, "requirement #provider" do
96
+
97
+ include SmsR::Actions::Requirements
98
+
99
+ before(:each) do
100
+ @providers = mock("Providers")
101
+ @provider_name = :blubb
102
+ @error = []
103
+ end
104
+
105
+ it "should return false if no provider :nonexisting_provider exists" do
106
+
107
+ @providers.should_receive(:[]).
108
+ with(:nonexisting_provider).
109
+ and_return(nil)
110
+
111
+ SmsR::Providers.stub!(:providers).and_return(@providers)
112
+
113
+ @provider_name = :nonexisting_provider
114
+
115
+ provider.should be(false)
116
+
117
+ end
118
+
119
+ it "should return 'true' if provider :existing_provider exists" do
120
+ @providers.should_receive(:[]).
121
+ with(:existing_provider).
122
+ and_return(true)
123
+
124
+ SmsR::Providers.stub!(:providers).and_return(@providers)
125
+
126
+ @provider_name = :existing_provider
127
+
128
+ provider.should be(true)
129
+ end
130
+
131
+ it "should add an error to @error when failing" do
132
+ @providers.should_receive(:[]).
133
+ with(:existing_provider).
134
+ and_return(true)
135
+
136
+ SmsR::Providers.stub!(:providers).and_return(@providers)
137
+
138
+ @provider_name = :existing_provider
139
+ provider
140
+
141
+ @error.size.should >= 1
142
+ end
143
+ end