smscru 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fac96bea6cf766f3752d88786250e69ed4a88cbc
4
+ data.tar.gz: 9210a76cece445a1859f1c4be7d3d4471028793d
5
+ SHA512:
6
+ metadata.gz: 2b697a4695957fa18ad272a1ee82ab4ce9dc8a2d11920fd000658f1225edf95482608897697e4457bb646e78435a9c7623a22c4d9f6a00daf41ae2f014a8b431
7
+ data.tar.gz: 72d7b1bb3e325630a676666d3da466a7840385890cbff0ae4afdf27ec3c4a82c2b30cacf5058bba7ea825b7d70015df51731f3e790a6964dbc77a5233afd48a7
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smscru.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sergey Nartimov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,101 @@
1
+ # Smscru
2
+
3
+ A Ruby interface to the [smsc.ru](http://smsc.ru/) SMS gateway service.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'smscru'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install smscru
18
+
19
+ ## Usage
20
+
21
+ Global configuration:
22
+
23
+ ```ruby
24
+ Smscru.configure do |config|
25
+ config.login = 'smscuser' # Your login
26
+ config.password = 'passwordmd5' # MD5 of your password
27
+ config.secret = 'smscsecret' # Secret for receiving messages
28
+ end
29
+ ```
30
+
31
+ Or:
32
+
33
+ ```ruby
34
+ Smscru.configure(login: 'smscuser', password: 'passwordmd5', secret: 'smscsecret')
35
+ ```
36
+
37
+ ### Sending messages
38
+
39
+ ```ruby
40
+ client = Smscru::Client.new
41
+ client.send_message('Hello world!', '+1234567890')
42
+ client.send_message('Hello world!', '+1234567890', id: 456)
43
+ client.send_message('Hello world!', '+1234567890', sender: 'TestSender')
44
+ client.send_message('Hello world!', ['+1234567890', '+1234567891'])
45
+ ```
46
+
47
+ It's also possible to specify client configuration options:
48
+
49
+ ```ruby
50
+ Smscru::Client.new(login: 'smscuser', password: 'passwordmd5')
51
+ ```
52
+
53
+ ### Receiving messages and status updates
54
+
55
+ URL in settings:
56
+
57
+ ```
58
+ http://example.com/smscru?charset=utf-8&sha1=smscsecret
59
+ ```
60
+
61
+ Use your custom secret instead of `smscsecret`.
62
+
63
+ Routes:
64
+
65
+ ```ruby
66
+ post 'smscru' => 'smscru#callback'
67
+ ```
68
+
69
+ Controller:
70
+
71
+ ```ruby
72
+ require 'smscru'
73
+
74
+ class SmscruController < ActionController::Base
75
+ def callback
76
+ callback = Smscru::Callback.new
77
+
78
+ callback.on_message do |params|
79
+ p params # incoming message
80
+ end
81
+
82
+ callback.on_status do |params|
83
+ p params # outgoing message status update
84
+ end
85
+
86
+ callback.run(params)
87
+
88
+ render nothing: true
89
+ end
90
+ end
91
+ ```
92
+
93
+ You can use [proxylocal](http://proxylocal.com/) or [ngrok](https://ngrok.com/) to test your callback handler on local machine.
94
+
95
+ ## Contributing
96
+
97
+ 1. Fork it
98
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
99
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
100
+ 4. Push to the branch (`git push origin my-new-feature`)
101
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.pattern = 'test/*_test.rb'
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,18 @@
1
+ require 'smscru/version'
2
+ require 'smscru/configuration'
3
+ require 'smscru/client'
4
+ require 'smscru/callback'
5
+
6
+ module Smscru
7
+ class << self
8
+ attr_reader :config
9
+
10
+ def configure(options={}, &block)
11
+ @config = Configuration.new
12
+ @config.update(options)
13
+ yield @config if block_given?
14
+ end
15
+ end
16
+
17
+ configure
18
+ end
@@ -0,0 +1,42 @@
1
+ require 'digest/sha1'
2
+
3
+ module Smscru
4
+ class Callback
5
+ attr_reader :config
6
+
7
+ def initialize(options)
8
+ @config = Smscru.config.dup
9
+ @config.update(options)
10
+ end
11
+
12
+ def run(params)
13
+ return if params['sha1'] != signature(params)
14
+
15
+ if params['mes']
16
+ @on_message.call(params)
17
+ else
18
+ @on_status.call(params)
19
+ end
20
+ end
21
+
22
+ def on_message(&block)
23
+ @on_message = block
24
+ end
25
+
26
+ def on_status(&block)
27
+ @on_status = block
28
+ end
29
+
30
+ def signature(params)
31
+ keys = if params['mes']
32
+ %w(phone mes to)
33
+ else
34
+ %w(id phone status)
35
+ end
36
+
37
+ values = params.values_at(*keys) << config.secret
38
+
39
+ Digest::SHA1.hexdigest(values.join(':'))
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ module Smscru
2
+ class Client
3
+ attr_reader :config
4
+
5
+ def initialize(options)
6
+ @config = Smscru.config.dup
7
+ @config.update(options)
8
+ end
9
+
10
+ def send_message(text, phones, options={})
11
+ params = {
12
+ mes: text,
13
+ phones: Array(phones),
14
+ login: config.login,
15
+ psw: config.password,
16
+ charset: 'utf-8',
17
+ fmt: 3
18
+ }
19
+
20
+ uri = URI.parse('http://smsc.ru/sys/send.php')
21
+ response = Net::HTTP.post_form(uri, params.merge(options))
22
+
23
+ JSON.parse(response.body)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ module Smscru
2
+ class Configuration
3
+ attr_accessor :login, :password, :secret
4
+
5
+ def update(options={})
6
+ options.each_pair do |name, value|
7
+ setter = "#{name}="
8
+ if respond_to?(setter)
9
+ public_send(setter, value)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Smscru
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'smscru/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'smscru'
8
+ spec.version = Smscru::VERSION
9
+ spec.authors = ['Sergey Nartimov']
10
+ spec.email = ['just.lest@gmail.com']
11
+ spec.description = %q{Ruby interface to the smsc.ru SMS gateway service.}
12
+ spec.summary = %q{Ruby interface to the smsc.ru SMS gateway service.}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'minitest', '~> 5.0.8'
24
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'test_helper'
2
+
3
+ class CallbackTest < Minitest::Test
4
+ attr_reader :callback, :message_params
5
+
6
+ def setup
7
+ @callback = Smscru::Callback.new(secret: 'zxc')
8
+ @message_params = {
9
+ 'phone' => '123',
10
+ 'mes' => 'text',
11
+ 'to' => '234',
12
+ 'sha1' => 'b6f768ce7614d7858cc4a6116f8ffd0dc236a3cd'
13
+ }
14
+ end
15
+
16
+ def test_valid_signature
17
+ verified = false
18
+ callback.on_message { verified = true }
19
+
20
+ callback.run(message_params)
21
+
22
+ assert verified
23
+ end
24
+
25
+ def test_invalid_signature
26
+ message_params['sha1'] = 'xxx'
27
+
28
+ verified = false
29
+ callback.on_message { verified = true }
30
+
31
+ callback.run(message_params)
32
+
33
+ refute verified
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'test_helper'
2
+
3
+ class ClientTest < Minitest::Test
4
+ def test_dont_override_global_configuration
5
+ Smscru.configure(login: 'qwe')
6
+ client = Smscru::Client.new(login: 'asd')
7
+
8
+ assert_equal 'qwe', Smscru.config.login
9
+ assert_equal 'asd', client.config.login
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require 'minitest/autorun'
2
+
3
+ $VERBOSE = true
4
+
5
+ require 'smscru'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smscru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Nartimov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 5.0.8
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 5.0.8
55
+ description: Ruby interface to the smsc.ru SMS gateway service.
56
+ email:
57
+ - just.lest@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/smscru.rb
69
+ - lib/smscru/callback.rb
70
+ - lib/smscru/client.rb
71
+ - lib/smscru/configuration.rb
72
+ - lib/smscru/version.rb
73
+ - smscru.gemspec
74
+ - test/callback_test.rb
75
+ - test/client_test.rb
76
+ - test/test_helper.rb
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.14
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Ruby interface to the smsc.ru SMS gateway service.
101
+ test_files:
102
+ - test/callback_test.rb
103
+ - test/client_test.rb
104
+ - test/test_helper.rb