smsway_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 09176d1235ea2f380396332d79a8df243b8ea598
4
+ data.tar.gz: 571f4da870f66092fb25d42f4ef2c05635ebeb5a
5
+ SHA512:
6
+ metadata.gz: 2aa89c3222baaa899d4432c4c120afc63dfba66322f7edd77294278d3cae214f7bb039d7ee63789cdf90264682e1ec54cee426d255f7ce1e49cf1126cb61af88
7
+ data.tar.gz: 76ff8095fa0df5eafb4f8db85816b5b1087224d90156176506475a5d9f1b184a6d4942afbb7f30fef683665bf3e4799efc2ba0211aed109f513d7311d226eba0
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in smsway_api.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use debugger
14
+ # gem 'debugger'
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ smsway_api (0.1.0)
5
+ builder (~> 3.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ builder (3.2.2)
11
+ diff-lcs (1.2.5)
12
+ rake (10.2.2)
13
+ rspec (2.14.1)
14
+ rspec-core (~> 2.14.0)
15
+ rspec-expectations (~> 2.14.0)
16
+ rspec-mocks (~> 2.14.0)
17
+ rspec-core (2.14.8)
18
+ rspec-expectations (2.14.5)
19
+ diff-lcs (>= 1.1.3, < 2.0)
20
+ rspec-mocks (2.14.6)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ rake
27
+ rspec (~> 2.14)
28
+ smsway_api!
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ ## smsway_api [![Build Status](https://secure.travis-ci.org/kelion/smsway_api.png)](http://travis-ci.org/kelion/smsway_api) [![Gem Version](https://badge.fury.io/rb/smsway_api.png)](http://badge.fury.io/rb/smswy_api) [![Dependency Status](https://gemnasium.com/kelion/smsway_api.png)](https://gemnasium.com/kelion/smsway_api) [![Code Climate](https://codeclimate.com/github/kelion/smsway_api.png)](https://codeclimate.com/github/kelion/smsway_api)
2
+
3
+ Ruby wrapper for smsway.ru xml api
4
+
5
+ ## Installation
6
+
7
+ ``` ruby
8
+ # Gemfile
9
+ gem 'smsway_api'
10
+ ```
11
+
12
+ or just
13
+
14
+ ``` sh
15
+ $ gem install smsway_api
16
+ ```
@@ -0,0 +1,2 @@
1
+ Description:
2
+ Create a smsway_api config file in config/initializers/smsway_api.rb.
@@ -0,0 +1,9 @@
1
+ # A rails generator `vkontakte:install`. It creates a config file in `config/initializers/vkontakte_api.rb`.
2
+ class SmswayApi::InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ # Creates the config file.
6
+ def create_initializer
7
+ copy_file 'initializer.rb', 'config/initializers/smsway_api.rb'
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ SmswayApi.configure do |config|
2
+ # Authorization parameters
3
+ # config.login = '123'
4
+ # config.password = 'AbCdE654'
5
+
6
+ # set this to have persistent sender through your application
7
+ # config.default_sender = 'SMSWAY'
8
+
9
+ # Logging parameters:
10
+ # log everything through the rails logger
11
+ config.logger = Rails.logger
12
+
13
+ # log requests' URLs
14
+ # config.log_requests = true
15
+
16
+ # log response JSON after errors
17
+ # config.log_errors = true
18
+
19
+ # log response JSON after successful responses
20
+ # config.log_responses = false
21
+ end
data/lib/smsway_api.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'builder'
2
+
3
+ require 'smsway_api/configuration.rb'
4
+ require 'smsway_api/api.rb'
5
+ require 'smsway_api/message.rb'
6
+ require 'smsway_api/method.rb'
7
+ require 'smsway_api/message/sms.rb'
8
+ require 'smsway_api/message/vcard.rb'
9
+ require 'smsway_api/message/wap_push.rb'
10
+ require 'smsway_api/method/send_message.rb'
11
+ require 'smsway_api/method/balance.rb'
12
+
13
+ module SmswayApi
14
+ extend SmswayApi::Configuration
15
+ end
@@ -0,0 +1,36 @@
1
+ module SmswayApi
2
+ module API
3
+ URL_PREFIX = 'http://my.smsway.ru/'
4
+ DEFAULT_HEADERS = {"Content-Type" => "text/xml; charset=utf-8"}
5
+
6
+ class << self
7
+ def call(method, options = {})
8
+ xml = build(method, options)
9
+ request({url: URL, headers: HEADERS, request: xml})
10
+ end
11
+
12
+ def request options
13
+ headers = options.delete(:headers) || {}
14
+
15
+ uri = URI.parse(URL_PREFIX+options.delete(:path))
16
+ http = Net::HTTP.new(uri.host, uri.port)
17
+
18
+ req = Net::HTTP::Post.new(uri.path, DEFAULT_HEADERS.merge(headers))
19
+ req.body = options.delete(:request)
20
+ http.request(req).body
21
+ end
22
+
23
+ def method_missing name, *args
24
+ api_method = "SmswayApi::Method::#{name.to_s.camelize}".constantize.new(*args)
25
+ api_method.build_xml
26
+ response = request({
27
+ path: api_method.uri,
28
+ request: api_method.build_xml
29
+ })
30
+ api_method.parse(response)
31
+ rescue NameError
32
+ super
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ module SmswayApi
2
+ class Client
3
+ URL = 'http://my.smsway.ru/xml/'
4
+
5
+ def send(request)
6
+ uri = URI.parse(URL)
7
+ http = Net::HTTP.new(uri.host, uri.port)
8
+ req = Net::HTTP::Post.new(uri.path, headers)
9
+ req.body = request
10
+ response = http.request(req)
11
+ response.body
12
+ end
13
+
14
+ def send_message(message, number, sender)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,60 @@
1
+ require 'logger'
2
+
3
+ module SmswayApi
4
+ # General configuration module.
5
+ #
6
+ # @note `SmswayApi::Configuration` extends `Smsway` so these methods should be called from the latter.
7
+ module Configuration
8
+ # Available options.
9
+ OPTION_NAMES = [
10
+ :login,
11
+ :password,
12
+ :default_sender,
13
+ :logger,
14
+ :log_requests,
15
+ :log_errors,
16
+ :log_responses
17
+ ]
18
+
19
+ attr_accessor *OPTION_NAMES
20
+
21
+ alias_method :log_requests?, :log_requests
22
+ alias_method :log_errors?, :log_errors
23
+ alias_method :log_responses?, :log_responses
24
+
25
+ # Default max retries count.
26
+ DEFAULT_MAX_RETRIES = 2
27
+
28
+ # Logger default options.
29
+ DEFAULT_LOGGER_OPTIONS = {
30
+ requests: true,
31
+ errors: true,
32
+ responses: false
33
+ }
34
+
35
+ # A global configuration set via the block.
36
+ # @example
37
+ # SmswayApi.configure do |config|
38
+ # config.adapter = :net_http
39
+ # config.logger = Rails.logger
40
+ # end
41
+ def configure
42
+ yield self if block_given?
43
+ self
44
+ end
45
+
46
+ # Reset all configuration options to defaults.
47
+ def reset
48
+ @max_retries = DEFAULT_MAX_RETRIES
49
+ @logger = ::Logger.new(STDOUT)
50
+ @log_requests = DEFAULT_LOGGER_OPTIONS[:requests]
51
+ @log_errors = DEFAULT_LOGGER_OPTIONS[:errors]
52
+ @log_responses = DEFAULT_LOGGER_OPTIONS[:responses]
53
+ end
54
+
55
+ # When this module is extended, set all configuration options to their default values.
56
+ def self.extended(base)
57
+ base.reset
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,54 @@
1
+ module SmswayApi
2
+ class Message
3
+ attr_accessor :sender
4
+
5
+ attr_reader :recepients
6
+ attr_reader :type
7
+
8
+ TYPES = [:flashsms, :sms, :vcard, :wappush]
9
+
10
+ def initialize(type = :sms)
11
+ self.type = type
12
+ @sender = SmswayApi.default_sender
13
+ @recepients = []
14
+ end
15
+
16
+ def type=(type)
17
+ @type = type if TYPES.include?(type)
18
+ end
19
+
20
+ def build(xml, start_index = 0)
21
+ xml.message(type: @type) do
22
+ xml.sender @sender
23
+ yield(xml)
24
+ @recepients.each.with_index do |r, index|
25
+ xml.abonent(r.merge(number_sms: start_index + index))
26
+ end
27
+ end
28
+ end
29
+
30
+ # add recepient
31
+ # recepient (Hash or String) options for recipient
32
+ # allowed options are:
33
+ # phone: phone number
34
+ # client_id: sms id in your system
35
+ # time_send: Time of sms sending
36
+ # validity_period: Date of
37
+ # if String is given than it means only phone
38
+ def add_recipient(recepient = {})
39
+ if recepient.is_a?(String)
40
+ # convert string value to correct hash
41
+ recepient = {phone: recepient}
42
+ end
43
+ recepient[:phone] = format_phone(recepient[:phone])
44
+ @recepients.push(recepient)
45
+ # time-send format: YYYY-MM-DD HH:MM
46
+ # validity_period format: YYYY-MM-DD HH:MM
47
+ end
48
+
49
+ private
50
+ def format_phone phone
51
+ phone.gsub(/\A(\+7|8)/, '7').gsub(/[^\d]/, '')
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,16 @@
1
+ module SmswayApi
2
+ class Message::Sms < Message
3
+ attr_accessor :message
4
+
5
+ def initialize(message= nil)
6
+ super(:sms)
7
+ @message = message
8
+ end
9
+
10
+ def build(xml, start_index = 0)
11
+ super do |xml|
12
+ xml.text @message
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,36 @@
1
+ module SmswayApi
2
+ class Message::Vcard < Message
3
+ # allowed options:
4
+ # url
5
+ # name
6
+ # phone
7
+ # cell
8
+ # work
9
+ # fax
10
+ # email
11
+ # position
12
+ # organization
13
+ # address
14
+ # post_office_box
15
+ # street
16
+ # city
17
+ # region
18
+ # postal_code
19
+ # country
20
+ # additional
21
+ def initialize(vcard = {})
22
+ super(:vcard)
23
+ @vcard = vcard
24
+ end
25
+
26
+ def build(xml, start_index = 0)
27
+ super do |xml|
28
+ [:url, :name, :email, :position, :organization, :additional, :phone, :address].each do |key|
29
+ if @vcard.has_key?(key)
30
+ xml.tag!(key, @vcard[key])
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ module SmswayApi
2
+ class Message::WapPush < Message
3
+ attr_accessor :url
4
+
5
+ def initialize(url = nil)
6
+ super(:wappush)
7
+ @url = url
8
+ end
9
+
10
+ def build(xml, start_index = 0)
11
+ super do |xml|
12
+ xml.url @url
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ module SmswayApi
2
+ class Method
3
+
4
+ def build_xml
5
+ xml = Builder::XmlMarkup.new
6
+ xml.instruct!
7
+ xml.request do
8
+ yield(xml) if block_given?
9
+ xml.security do
10
+ xml.login(value: SmswayApi.login)
11
+ xml.password(value: SmswayApi.password)
12
+ end
13
+ end
14
+ xml.target!
15
+ end
16
+
17
+ def parse_response response
18
+ raise 'not implemented'
19
+ end
20
+
21
+ def uri
22
+ ''
23
+ end
24
+
25
+ def http_verb
26
+ :post
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ module SmswayApi
2
+ class Method::Balance < SmswayApi::Method
3
+ def uri
4
+ 'xml/balance.php'
5
+ end
6
+
7
+ def parse response
8
+ Hash.from_xml(response)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ module SmswayApi
2
+ class Method::SendMessage < SmswayApi::Method
3
+ def initialize *args
4
+ if args.first.is_a?(SmswayApi::Message)
5
+ @messages = args
6
+ elsif args.length == 2 && args.first.is_a?(String)
7
+ @messages = []
8
+ sms = SmswayApi::Message::Sms.new(args.first)
9
+ sms.add_recipient(args.last)
10
+ @messages << sms
11
+ else
12
+ @messages = Array.wrap(args.first)
13
+ end
14
+ end
15
+
16
+ def build_xml
17
+ super do |xml|
18
+ abonents = 1
19
+ @messages.each do |m|
20
+ m.build(xml, abonents)
21
+ abonents += m.recepients.size
22
+ end
23
+ end
24
+ end
25
+
26
+ def uri
27
+ 'xml/index.php'
28
+ end
29
+
30
+ def parse response
31
+ Hash.from_xml(response)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module SmswayApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "smsway_api/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "smsway_api"
9
+ s.version = SmswayApi::VERSION
10
+ s.authors = ["Alexandr Subbotin"]
11
+ s.email = ["kelionweb@gmail.com"]
12
+ s.homepage = "https://github.com/KELiON/smsway_api"
13
+ s.summary = "Ruby wrapper for smsway.ru xml API"
14
+ s.description = "Simple ruby wrapper for smsway.ru XML API. Provides simples methods for sending sms, flash sms, vcard and wappushes, check balance."
15
+
16
+ s.license = 'MIT'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
21
+ s.require_paths = ['lib']
22
+
23
+ s.add_runtime_dependency 'builder', '~> 3.2'
24
+
25
+ s.add_development_dependency 'rake'
26
+ s.add_development_dependency 'rspec', '~> 2.14'
27
+
28
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ class Configurable
4
+ extend SmswayApi::Configuration
5
+ end
6
+
7
+ describe SmswayApi::Configuration do
8
+ describe "#configure" do
9
+ it "yields self" do
10
+ Configurable.should_receive(:some_method)
11
+ Configurable.configure do |config|
12
+ config.some_method
13
+ end
14
+ end
15
+
16
+ it "returns self" do
17
+ Configurable.configure.should == Configurable
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe SmswayApi::Message::Sms do
4
+ let(:sms) { SmswayApi::Message::Sms.new }
5
+
6
+ describe ".initialize" do
7
+ it "should have sms type" do
8
+ expect(sms.type).to eq(:sms)
9
+ end
10
+
11
+ it "should have message" do
12
+ expect(SmswayApi::Message::Sms.new('test').message).to eq('test')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ require './lib/smsway_api.rb'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smsway_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexandr Subbotin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: builder
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ description: Simple ruby wrapper for smsway.ru XML API. Provides simples methods for
56
+ sending sms, flash sms, vcard and wappushes, check balance.
57
+ email:
58
+ - kelionweb@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - MIT-LICENSE
66
+ - README.md
67
+ - lib/generators/smsway_api/install/USAGE
68
+ - lib/generators/smsway_api/install/install_generator.rb
69
+ - lib/generators/smsway_api/install/templates/initializer.rb
70
+ - lib/smsway_api.rb
71
+ - lib/smsway_api/api.rb
72
+ - lib/smsway_api/client.rb
73
+ - lib/smsway_api/configuration.rb
74
+ - lib/smsway_api/message.rb
75
+ - lib/smsway_api/message/sms.rb
76
+ - lib/smsway_api/message/vcard.rb
77
+ - lib/smsway_api/message/wap_push.rb
78
+ - lib/smsway_api/method.rb
79
+ - lib/smsway_api/method/balance.rb
80
+ - lib/smsway_api/method/send_message.rb
81
+ - lib/smsway_api/version.rb
82
+ - smsway_api.gemspec
83
+ - spec/smsway_api/configuration_spec.rb
84
+ - spec/smsway_api/message/sms_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: https://github.com/KELiON/smsway_api
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Ruby wrapper for smsway.ru xml API
110
+ test_files:
111
+ - spec/smsway_api/configuration_spec.rb
112
+ - spec/smsway_api/message/sms_spec.rb
113
+ - spec/spec_helper.rb
114
+ has_rdoc: