cm-sms 0.1.0

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: 6ae5829f7cdabf0ba1718966f9c60d8385652c23
4
+ data.tar.gz: ccce1ca4e19521f593939c4aeb9ddbe4ec5eadbb
5
+ SHA512:
6
+ metadata.gz: 1653ab8d2a9a400ed8264557eeb2bc5c9b7dcb09df4411920c315f467c427d8e5c8eaf169eaff7293b2b6527ee15b39d3eb32bf6beb29e049ef515b9ddaf6286
7
+ data.tar.gz: 0ec72afd8498809c8de78ecbe6e84d2f8a54948f4290db9f52cefa17e7a119e8b48a2116c336cddc6b4add37342b097e8a681ac654327f2888fceaf9b35c3b55
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .DS_Store
12
+ coverage
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cm-sms.gemspec
4
+ gemspec
5
+
6
+ gem 'simplecov', require: false, group: :test
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 itschn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Cm::Sms
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cm/sms`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cm-sms'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cm-sms
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cm-sms. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cm/sms"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cm_sms/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cm-sms"
8
+ spec.version = CmSms.version
9
+ spec.authors = ["itschn"]
10
+ spec.email = ["michael.rueffer@hitfoxgroup.com"]
11
+
12
+ spec.summary = %q{Wrapper for the CM SMS Gateway API.}
13
+ spec.description = %q{Send text messages by means of the HTTP protocol with the service of https://www.cmtelecom.com.}
14
+ spec.homepage = "https://github.com/HitFox/cm-sms"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ spec.add_runtime_dependency "phony", "~> 2.12"
28
+ spec.add_runtime_dependency "builder", "~> 3.2"
29
+ else
30
+ spec.add_dependency "phony", "~> 2.15"
31
+ spec.add_dependency "builder", "~> 3.2"
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ require 'cm_sms'
@@ -0,0 +1,20 @@
1
+ require 'cm_sms/configuration'
2
+
3
+ module CmSms
4
+ autoload :Messenger, 'cm_sms/messenger'
5
+ autoload :Message, 'cm_sms/message'
6
+ autoload :MessageDelivery, 'cm_sms/message_delivery'
7
+ autoload :DeliveryJob, 'cm_sms/delivery_job' if defined?(ActiveJob)
8
+
9
+ class << self
10
+ def configure
11
+ yield(configuration)
12
+ end
13
+
14
+ def configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ alias :config :configuration
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ module CmSms
2
+ class Configuration
3
+ class ProductTokenMissing < ArgumentError; end
4
+ class EndpointMissing < ArgumentError; end
5
+ class PathMissing < ArgumentError; end
6
+
7
+ ENDPOINT = 'https://sgw01.cm.nl'
8
+ PATH = '/gateway.ashx'
9
+
10
+ attr_accessor :from, :to, :product_token, :endpoint, :path
11
+
12
+ alias :'api_key=' :'product_token='
13
+
14
+ def endpoint
15
+ @endpoint || ENDPOINT
16
+ end
17
+
18
+ def path
19
+ @path || PATH
20
+ end
21
+
22
+ def defaults
23
+ @defaults ||= { from: from, to: to }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ module CmSms
2
+ # Returns the version of the currently loaded Action Mailer as a <tt>Gem::Version</tt>
3
+ def self.gem_version
4
+ Gem::Version.new VERSION::STRING
5
+ end
6
+
7
+ module VERSION
8
+ MAJOR = 0
9
+ MINOR = 1
10
+ TINY = 0
11
+ PRE = nil
12
+
13
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
14
+ end
15
+ end
@@ -0,0 +1,81 @@
1
+ require 'builder'
2
+ require 'phony'
3
+ require 'cm_sms/request'
4
+
5
+ module CmSms
6
+ class Message
7
+ class FromMissing < ArgumentError; end
8
+ class ToMissing < ArgumentError; end
9
+ class BodyMissing < ArgumentError; end
10
+ class ToUnplausible < ArgumentError; end
11
+
12
+ attr_accessor :from, :to, :body, :dcs, :reference
13
+
14
+ def initialize(attributes = {})
15
+ @from = attributes[:from]
16
+ @to = attributes[:to]
17
+ @dcs = attributes[:dcs]
18
+ @body = attributes[:body]
19
+ @reference = attributes[:reference]
20
+
21
+ @product_token = CmSms.config.product_token
22
+ end
23
+
24
+ def receiver_plausible?
25
+ receiver_present? && Phony.plausible?(to)
26
+ end
27
+
28
+ def receiver_present?
29
+ !to.nil? && !to.empty?
30
+ end
31
+
32
+ def sender_present?
33
+ !from.nil? && !from.empty?
34
+ end
35
+
36
+ def body_present?
37
+ !body.nil? && !body.empty?
38
+ end
39
+
40
+ def product_token_present?
41
+ !@product_token.nil? && !@product_token.empty?
42
+ end
43
+
44
+ def request
45
+ Request.new(to_xml)
46
+ end
47
+
48
+ def deliver
49
+ raise CmSms::Configuration::ProductTokenMissing.new("Please provide an valid product key.\nAfter signup at https://www.cmtelecom.de/, you will find one in your settings.") unless product_token_present?
50
+
51
+ request.perform
52
+ end
53
+
54
+ def deliver!
55
+ raise FromMissing.new('The from attribute is missing.') unless sender_present?
56
+ raise ToMissing.new('The to attribute is missing.') unless receiver_present?
57
+ raise BodyMissing.new('The body of the message is missing.') unless body_present?
58
+ raise ToUnplausible.new("THe given to attribute is not a plausible phone number.\nMaybe the country code is missing.") unless receiver_plausible?
59
+
60
+ deliver
61
+ end
62
+
63
+ def to_xml
64
+ builder = Builder::XmlMarkup.new
65
+ builder.instruct! :xml, version: '1.0'
66
+
67
+ xml = builder.MESSAGES do |m|
68
+ m.AUTHENTICATION do |authentication|
69
+ authentication.PRODUCTTOKEN @product_token
70
+ end
71
+ m.MSG do |msg|
72
+ msg.FROM from
73
+ msg.TO to
74
+ msg.DCS dcs if dcs
75
+ msg.BODY body
76
+ msg.REFERENCE reference if reference
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,38 @@
1
+ require 'delegate'
2
+
3
+ module CmSms
4
+ class MessageDelivery < Delegator
5
+ def initialize(messenger, message_method, *args) #:nodoc:
6
+ @messenger = messenger
7
+ @message_method = message_method
8
+ @args = args
9
+ end
10
+
11
+ def __getobj__ #:nodoc:
12
+ @obj ||= @messenger.send(:new).send(@message_method, *@args).message
13
+ end
14
+
15
+ alias :message :__getobj__
16
+
17
+ def __setobj__(obj) #:nodoc:
18
+ @obj = obj
19
+ end
20
+
21
+ def deliver_now!
22
+ message.deliver!
23
+ end
24
+
25
+ def deliver_now
26
+ message.deliver
27
+ end
28
+
29
+ def inspect
30
+ prefix = "#<#{self.class}:0x#{self.__id__.to_s(16)}"
31
+ parts = instance_variables.map { |var| "#{var}=#{instance_variable_get(var).inspect}" }
32
+ str = [prefix, parts, ']>'].join(' ')
33
+
34
+ str.taint if tainted?
35
+ str
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,51 @@
1
+ module CmSms
2
+ class Messenger
3
+
4
+ attr_accessor :from, :to, :body, :dcs
5
+
6
+ def initialize(attributes = {})
7
+ self.class.default_params ||= {}
8
+
9
+ @from = attributes[:from] || self.class.default_params[:from]
10
+ @to = attributes[:to] || self.class.default_params[:to]
11
+ @dcs = attributes[:dcs] || self.class.default_params[:dcs]
12
+ @body = attributes[:body]
13
+ end
14
+
15
+ def content(attributes = {})
16
+ attributes.each { |attr, value| send("#{attr}=", value) }
17
+ self
18
+ end
19
+
20
+ def message
21
+ @message ||= CmSms::Message.new(from: from, to: to, dcs: dcs, body: body)
22
+ end
23
+
24
+ def self.method_missing(method_name, *args) # :nodoc:
25
+ if new.respond_to?(method_name.to_s)
26
+ CmSms::MessageDelivery.new(self, method_name, *args)
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ def self.respond_to_missing?(method_name, *args) # :nodoc:
33
+ new.respond_to?(method_name.to_s) || super
34
+ end
35
+
36
+ def self.default_params
37
+ @@default_params ||= CmSms.config.defaults
38
+ end
39
+
40
+ def self.default_params=(params = {})
41
+ @@default_params = params
42
+ end
43
+
44
+ def self.default(hash = {})
45
+ self.default_params = CmSms.config.defaults.merge(hash).freeze
46
+ default_params
47
+ end
48
+
49
+ end
50
+ end
51
+
@@ -0,0 +1,26 @@
1
+ require 'cm_sms/response'
2
+
3
+ module CmSms
4
+ class Request
5
+ attr_accessor :body
6
+
7
+ attr_reader :response
8
+
9
+ def initialize(body)
10
+ @body = body
11
+ @endpoint = CmSms.config.endpoint
12
+ @path = CmSms.config.path
13
+ end
14
+
15
+ def perform
16
+ raise CmSms::Configuration::EndpointMissing.new("Please provide an valid api endpoint.\nIf you leave this config blank, the default will be set to https://sgw01.cm.nl.") if @endpoint.nil? || @endpoint.empty?
17
+ raise CmSms::Configuration::PathMissing.new("Please provide an valid api path.\nIf you leave this config blank, the default will be set to /gateway.ashx.") if @path.nil? || @path.empty?
18
+
19
+ uri = URI.parse(@endpoint)
20
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
21
+ @response = Response.new(http.post(@path, body, initheader = { 'Content-Type' => 'application/xml' }))
22
+ end
23
+ response
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ module CmSms
2
+ class Response
3
+ attr_reader :net_http_response, :body, :code
4
+
5
+ def initialize(net_http_response)
6
+ @net_http_response = net_http_response
7
+ @body = @net_http_response.body
8
+ @code = @net_http_response.code
9
+ end
10
+
11
+ def success?
12
+ body.empty? && code.to_i == 200
13
+ end
14
+
15
+ def failure?
16
+ !success?
17
+ end
18
+
19
+ def error
20
+ body.sub('Error: ERROR', '').strip
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'gem_version'
2
+
3
+ module CmSms
4
+ def self.version
5
+ gem_version
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cm-sms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - itschn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-16 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: phony
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.12'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: builder
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ description: Send text messages by means of the HTTP protocol with the service of
84
+ https://www.cmtelecom.com.
85
+ email:
86
+ - michael.rueffer@hitfoxgroup.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - cm-sms.gemspec
102
+ - lib/cm-sms.rb
103
+ - lib/cm_sms.rb
104
+ - lib/cm_sms/configuration.rb
105
+ - lib/cm_sms/gem_version.rb
106
+ - lib/cm_sms/message.rb
107
+ - lib/cm_sms/message_delivery.rb
108
+ - lib/cm_sms/messenger.rb
109
+ - lib/cm_sms/request.rb
110
+ - lib/cm_sms/response.rb
111
+ - lib/cm_sms/version.rb
112
+ homepage: https://github.com/HitFox/cm-sms
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.8
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Wrapper for the CM SMS Gateway API.
136
+ test_files: []