kjappsms 0.1.0

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.
data/Manifest ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ Rakefile
3
+ lib/kjappsms.rb
4
+ test/test_kjappsms.rb
5
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,14 @@
1
+ == Kjappsms
2
+
3
+ A simple library for sending SMS from www.kjappsms.no
4
+
5
+ == Install
6
+
7
+ gem install kjappsms --source http://gems.github.com
8
+
9
+ == Usage
10
+
11
+ require 'kjappsms'
12
+ k = kjappsms.new
13
+ output = k.sendSMS("username", "password", "receiver phonenumber", "message")
14
+ puts output
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('kjappsms', '0.1.0') do |p|
6
+ p.description = "Screenscraps kjappsms.no to send SMS. To use this, you have to sign up for a account at www.kjappsms.no"
7
+ p.url = "http://github.com/fossmo/kjappsms"
8
+ p.author = "Pål Fossmo"
9
+ p.email = "pal@fossmo.net"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
15
+
data/kjappsms.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{kjappsms}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["P\303\245l Fossmo"]
9
+ s.date = %q{2010-07-23}
10
+ s.description = %q{Screenscraps kjappsms.no to send SMS. To use this, you have to sign up for a account at www.kjappsms.no}
11
+ s.email = %q{pal@fossmo.net}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/kjappsms.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "lib/kjappsms.rb", "test/test_kjappsms.rb", "Manifest", "kjappsms.gemspec"]
14
+ s.homepage = %q{http://github.com/fossmo/kjappsms}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Kjappsms", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{kjappsms}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Screenscraps kjappsms.no to send SMS. To use this, you have to sign up for a account at www.kjappsms.no}
20
+ s.test_files = ["test/test_kjappsms.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
data/lib/kjappsms.rb ADDED
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'open-uri'
3
+ require 'mechanize'
4
+ require 'nokogiri'
5
+
6
+ class Kjappsms
7
+ def sendSMS(username, password, phonenumber, message)
8
+
9
+ return "Beskjeden er for lang" unless message.length < 350
10
+ return "Beskjed mangler" unless message.length > 0
11
+ return "Passord mangler" unless password.length > 0
12
+ return "Mottaker telefonnummer mangler siffer" unless phonenumber.length == 8
13
+ return "Brukernavn mangler" unless username.length > 0
14
+
15
+ agent = Mechanize.new
16
+ agent.user_agent_alias = 'Windows IE 6'
17
+
18
+ page = fillInLoginForm(agent, username, password)
19
+
20
+ errorMessage = checkForErrorMessage(page)
21
+
22
+ if errorMessage.length > 0 then
23
+ return errorMessage
24
+ end
25
+
26
+ page = sendMessage(page, agent, phonenumber, message)
27
+
28
+ errorMessage = checkForErrorMessage(page)
29
+
30
+ if errorMessage.length > 0 then
31
+ return errorMessage
32
+ end
33
+
34
+ smsleft = Nokogiri.HTML(page.content)
35
+
36
+ return smsleft.at_css("#numSmsLeft").text
37
+ end
38
+
39
+ def sendMessage(page, agent, phonenumber, message)
40
+ page = agent.page.link_with(:href => '?w=1').click
41
+ form2 = agent.page.forms.last
42
+ form2.numb = phonenumber
43
+ form2.msg = message
44
+ page = agent.submit(form2, form2.buttons.first)
45
+ return page
46
+ end
47
+
48
+ def checkForErrorMessage(page)
49
+ loginProblem = Nokogiri.HTML(page.content)
50
+ if loginProblem.css('script').text.length > 0 then
51
+ r = /'(\w.+)'/
52
+ return r.match(loginProblem.css('script').text)
53
+ end
54
+ return ""
55
+ end
56
+
57
+ def fillInLoginForm(agent, username, password)
58
+ url = "http://www.kjappsms.no/WebLogin.aspx"
59
+ form = agent.get(url).forms.last
60
+ form.mobile = username
61
+ form.password = password
62
+ page = agent.submit(form, form.buttons.first)
63
+ return page
64
+ end
65
+
66
+ end
@@ -0,0 +1,56 @@
1
+ require 'test/unit'
2
+ require '../lib/kjappsms'
3
+
4
+ class KjappsmsTest < Test::Unit::TestCase
5
+
6
+ def test_that_error_message_is_returned
7
+ myHtml = "<html><body><form><script>alert('MyContent')</script></form></body></html>"
8
+ mock = Mock.new
9
+ mock.content = myHtml
10
+ k = Kjappsms.new
11
+ result = k.checkForErrorMessage(mock)
12
+ assert_equal "'MyContent'", result.to_s
13
+ end
14
+
15
+ def test_that_a_blank_error_message_is_returned
16
+ myHtml = "<html><body><form><script>NoContent</script></form></body></html>"
17
+ mock = Mock.new
18
+ mock.content = myHtml
19
+ k = Kjappsms.new
20
+ result = k.checkForErrorMessage(mock)
21
+ assert_equal "", result.to_s
22
+ end
23
+
24
+ def test_that_the_length_of_the_message_is_within_bounds
25
+ k = Kjappsms.new
26
+ result = k.sendSMS("00000000", "111111", "22222222", "")
27
+ assert_equal "Beskjed mangler", result
28
+
29
+ longMsg = ""
30
+ 351.times do
31
+ longMsg << "a"
32
+ end
33
+ result = k.sendSMS("00000000", "111111", "22222222", longMsg)
34
+ assert_equal "Beskjeden er for lang", result
35
+ end
36
+
37
+
38
+ def test_that_the_length_of_the_phonenumber_is_within_bounds
39
+ k = Kjappsms.new
40
+ result = k.sendSMS("00000000", "111111", "", "msg")
41
+ assert_equal "Mottaker telefonnummer mangler siffer", result
42
+ end
43
+
44
+ def test_that_the_length_of_the_username_is_within_bounds
45
+ k = Kjappsms.new
46
+ result = k.sendSMS("", "111111", "12345678", "msg")
47
+ assert_equal "Brukernavn mangler", result
48
+ end
49
+ end
50
+
51
+ class Mock
52
+ attr :content, true
53
+ end
54
+
55
+
56
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kjappsms
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - "P\xC3\xA5l Fossmo"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-23 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Screenscraps kjappsms.no to send SMS. To use this, you have to sign up for a account at www.kjappsms.no
23
+ email: pal@fossmo.net
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ - lib/kjappsms.rb
31
+ files:
32
+ - README.rdoc
33
+ - Rakefile
34
+ - lib/kjappsms.rb
35
+ - test/test_kjappsms.rb
36
+ - Manifest
37
+ - kjappsms.gemspec
38
+ has_rdoc: true
39
+ homepage: http://github.com/fossmo/kjappsms
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --line-numbers
45
+ - --inline-source
46
+ - --title
47
+ - Kjappsms
48
+ - --main
49
+ - README.rdoc
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 11
67
+ segments:
68
+ - 1
69
+ - 2
70
+ version: "1.2"
71
+ requirements: []
72
+
73
+ rubyforge_project: kjappsms
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Screenscraps kjappsms.no to send SMS. To use this, you have to sign up for a account at www.kjappsms.no
78
+ test_files:
79
+ - test/test_kjappsms.rb