keysms 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.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in keysms.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ keysms (0.0.1)
5
+ json
6
+ patron
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.1.3)
12
+ json (1.6.1)
13
+ patron (0.4.16)
14
+ rspec (2.7.0)
15
+ rspec-core (~> 2.7.0)
16
+ rspec-expectations (~> 2.7.0)
17
+ rspec-mocks (~> 2.7.0)
18
+ rspec-core (2.7.1)
19
+ rspec-expectations (2.7.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.7.0)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ keysms!
28
+ rspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/keysms.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "keysms/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "keysms"
7
+ s.version = Keysms::VERSION
8
+ s.authors = ["Ricco Førgaard"]
9
+ s.email = ["ricco@fiskeben.dk"]
10
+ s.homepage = "http://fiskeben.dk/keysms"
11
+ s.summary = %q{Send text messages to mobile phones using the KeySMS gateway.}
12
+ s.description = %q{This gem wraps the API for the SMS gateway KeySMS run by the Norwegian company Keyteq. For more info, see http://keysms.no}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "rspec"
20
+ s.add_dependency "json"
21
+ s.add_dependency "patron"
22
+ end
@@ -0,0 +1,3 @@
1
+ module Keysms
2
+ VERSION = "0.0.1"
3
+ end
data/lib/keysms.rb ADDED
@@ -0,0 +1,154 @@
1
+ # encoding: utf-8
2
+
3
+ module Keysms
4
+
5
+ require 'digest/md5'
6
+ require 'json'
7
+ require 'patron'
8
+
9
+ class KeyteqService
10
+ attr_accessor :result
11
+
12
+ def initialize(url = "http://app.keysms.no", options = {})
13
+ @options, @payload, @values = {}, {}, {}
14
+ @options = @options.merge(options)
15
+ @options[:url] = url
16
+ end
17
+
18
+ def authenticate(username, key)
19
+ @options[:auth] = {}
20
+ @options[:auth][:username] = username
21
+ @options[:auth][:key] = key
22
+ self
23
+ end
24
+
25
+ private
26
+
27
+ def prepare_session
28
+ @session = Patron::Session.new
29
+ @session.base_url = @options.delete(:url)
30
+ end
31
+
32
+ def prepare_request
33
+ @values[:username] = @options[:auth][:username]
34
+ @values[:signature] = sign
35
+ @values[:payload] = @payload.to_json
36
+ end
37
+
38
+ def sign
39
+ Digest::MD5.hexdigest(@payload.to_json + @options[:auth][:key])
40
+ end
41
+
42
+ def call
43
+ data = @values.collect do | key, value |
44
+ "#{key}=#{value}"
45
+ end
46
+
47
+ response = @session.post(@options[:path], data.join("&"))
48
+ handle_response(response.body)
49
+ @response
50
+ end
51
+
52
+ def handle_response(response_text)
53
+ @result = JSON.parse(response_text)
54
+ p @result
55
+ begin
56
+ if (@result["ok"] == false)
57
+ error_code = find_error_code(@result)
58
+ if (error_code == "not_authed")
59
+ raise NotAuthenticatedError.new(@result)
60
+ elsif (error_code == "message_no_valid_receivers")
61
+ raise NoValidReceiversError.new(@result)
62
+ elsif (error_code == "message_internal_error")
63
+ raise InternalError.new(@result)
64
+ else
65
+ raise SMSError.new(@result)
66
+ end
67
+ end
68
+ rescue NoMethodError => e
69
+ raise SMSError.new(@result)
70
+ end
71
+ end
72
+
73
+ def find_error_code(structure)
74
+ structure.each do | key, value |
75
+ if (key == "code")
76
+ return value
77
+ elsif (key == "error")
78
+ if (value.is_a? String)
79
+ return value
80
+ elsif (value.is_a? Hash)
81
+ return find_error_code(value)
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ end
88
+
89
+ class SMS < KeyteqService
90
+ def send(message, receivers, options = {})
91
+ @options = @options.merge(options)
92
+ @options[:path] = "/messages"
93
+
94
+ if (receivers.is_a? String)
95
+ receivers = [receivers]
96
+ end
97
+
98
+ @payload[:message] = message
99
+ @payload[:receivers] = receivers
100
+
101
+ prepare_request
102
+ prepare_session
103
+ call
104
+ end
105
+ end
106
+
107
+ class Info < KeyteqService
108
+ def info
109
+ @options[:path] = "/auth/current.json"
110
+ @payload[:user] = true
111
+ @payload[:account] = true
112
+
113
+ prepare_request
114
+ prepare_session
115
+ call
116
+ end
117
+ end
118
+
119
+ class SMSError < StandardError
120
+ attr_accessor :error
121
+ def initialize(error)
122
+ @error = error["error"]
123
+ super
124
+ end
125
+ end
126
+
127
+ class NotAuthenticatedError < SMSError
128
+ attr_accessor :messages
129
+
130
+ def initialize(error)
131
+ @messages = error["messages"]
132
+ super(error)
133
+ end
134
+
135
+ def to_s
136
+ @messages.join(", ")
137
+ end
138
+ end
139
+
140
+ class NoValidReceiversError < SMSError
141
+ attr_accessor :failed_receivers
142
+
143
+ def initialize(error)
144
+ @failed_receivers = error["error"]["receivers"]["data"]["failed"]
145
+ super(error)
146
+ end
147
+
148
+ def to_s
149
+ @failed_receivers.join(", ")
150
+ end
151
+ end
152
+
153
+ class InternalError < SMSError; end
154
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keysms
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - "Ricco F\xC3\xB8rgaard"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-11-12 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: patron
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id003
48
+ description: This gem wraps the API for the SMS gateway KeySMS run by the Norwegian company Keyteq. For more info, see http://keysms.no
49
+ email:
50
+ - ricco@fiskeben.dk
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - Gemfile.lock
61
+ - Rakefile
62
+ - keysms.gemspec
63
+ - lib/keysms.rb
64
+ - lib/keysms/version.rb
65
+ homepage: http://fiskeben.dk/keysms
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.10
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Send text messages to mobile phones using the KeySMS gateway.
92
+ test_files: []
93
+