mainsms_api 0.0.1

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: 5cbb84cce368569930e3fd8fca82bf42177b6b44
4
+ data.tar.gz: 27e4bc23e5663d9307d5fdc7cb900eaf567b68bb
5
+ SHA512:
6
+ metadata.gz: 65ecba59c6374101caf3e08fc84c03458827344df47c5f0db439dcac23dff13d1c7e666d55ec5beae4fe4cf05bd05400803cbb468ae685dd3cebd9f729cfc063
7
+ data.tar.gz: 938409e6f58a09ec8c78b5d7a6510038324ac72ecbbb8262dfbaf7eaca92e7123dcdadcfe891f45421dac124ba3f81c57d956a9a34977c5309789d7ea564257f
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 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,83 @@
1
+ ## Использование
2
+
3
+ Добавить в Gemfile:
4
+ ```ruby
5
+ gem 'mainsms_api', :git => 'git://github.com/evserykh/mainsms_api.git'
6
+ ```
7
+
8
+ ### Настройка
9
+ При использовании в Rails приложении запустить генератор
10
+ ```ruby
11
+ bundle exec rails g mainsms_api:install
12
+ ```
13
+ и указать настройки в __config/mainsms.yml__
14
+
15
+ Если gem используется не в Rails приложении, то можно задать настройки следующим образом:
16
+
17
+ ```ruby
18
+ MainsmsApi::Configuration.setup project: 'PROJECT', api_key: 'SECRET'
19
+ ```
20
+
21
+ ### Отправка сообщения
22
+ ```ruby
23
+ message = MainsmsApi::Message.new(sender: 'SENDER', message: 'MESSAGE', recipients: ['89112223344'])
24
+ response = message.deliver
25
+ ```
26
+ Параметры:
27
+ * __sender__ - имя отправителя
28
+ * __message__ - текст сообщения
29
+ * __recipients__ - массив с номерами получателей
30
+ * __run_at__ - время отправки
31
+ * __test__ - тестовый режим ('1' для включения)
32
+
33
+ Объект _response_ является экземпляром класса [Hashie::Mash](https://github.com/intridea/hashie#mash). Для него доступны методы, возвращаемые в ответе сервера. Например, обработать статус ответа можно так:
34
+ ```ruby
35
+ if response.status == 'success'
36
+ #code
37
+ end
38
+ ```
39
+ или
40
+ ```ruby
41
+ if response['status'] == 'success'
42
+ #code
43
+ end
44
+ ```
45
+ Подробнее об ответе сервера можно прочитать [тут](http://mainsms.ru/home/mainapi#send_api)
46
+
47
+ ### Запрос статуса сообщения
48
+ ```ruby
49
+ status = MainsmsApi::Status.new(:message_ids => ['1', '2'])
50
+ response = status.check
51
+ ```
52
+ Параметры:
53
+ * __message_ids__ - идентификаторы сообщений
54
+
55
+ Подробнее [тут](http://mainsms.ru/home/mainapi#status_api)
56
+
57
+ ### Определение цены
58
+ ```ruby
59
+ price = MainsmsApi::Price.new(:message => 'MESSAGE', :recipients => ['89112223344'])
60
+ response = price.calculate
61
+ ```
62
+ Параметры:
63
+ * __message__ - текст сообщения
64
+ * __recipients__ - массив с номерами получателей
65
+
66
+ Подробнее [тут](http://mainsms.ru/home/mainapi#price_api)
67
+
68
+ ### Запрос баланса
69
+ ```ruby
70
+ balance = MainsmsApi::Balance.new
71
+ response = balance.check
72
+ ```
73
+ Подробнее [тут](http://mainsms.ru/home/mainapi#balance_api)
74
+
75
+ ### Запрос информации о номерах
76
+ ```ruby
77
+ info = MainsmsApi::Info.new(:phones => ['89112223344'])
78
+ response = info.get
79
+ ```
80
+ Параметры:
81
+ * __phones__ - массив номеров
82
+
83
+ Подробнее [тут](http://mainsms.ru/home/mainapi#info_api)
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'MainsmsApi'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,12 @@
1
+ module MainsmsApi
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ def copy_config_file
7
+ copy_file 'mainsms.yml', 'config/mainsms.yml'
8
+ copy_file 'mainsms_api.rb', 'config/initializers/mainsms_api.rb'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ mainsms:
2
+ project: PROJECT
3
+ api_key: SECRET
@@ -0,0 +1 @@
1
+ MainsmsApi::Configuration.setup YAML.load_file(Rails.root.join('config', 'mainsms.yml'))['mainsms']
@@ -0,0 +1,16 @@
1
+ require 'active_attr'
2
+ require 'active_support/core_ext/object'
3
+ require 'active_support/core_ext/string'
4
+ require 'hashie'
5
+ require 'httparty'
6
+
7
+ require 'mainsms_api/configuration'
8
+ require 'mainsms_api/common'
9
+ require 'mainsms_api/message'
10
+ require 'mainsms_api/status'
11
+ require 'mainsms_api/price'
12
+ require 'mainsms_api/balance'
13
+ require 'mainsms_api/info'
14
+
15
+ module MainsmsApi
16
+ end
@@ -0,0 +1,22 @@
1
+ module MainsmsApi
2
+ class Balance
3
+ include ActiveAttr::MassAssignment
4
+ include Common
5
+
6
+ def initialize(args = {})
7
+ super(args)
8
+ end
9
+
10
+ alias_method :check, :response
11
+
12
+ private
13
+
14
+ def params
15
+ { project: project }
16
+ end
17
+
18
+ def path
19
+ 'api/mainsms/message/balance'
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,55 @@
1
+ module MainsmsApi
2
+ module Common
3
+ def response
4
+ @response ||= Hashie::Mash.new(JSON.parse(response_body))
5
+ end
6
+
7
+ private
8
+
9
+ def project
10
+ MainsmsApi::Configuration.project
11
+ end
12
+
13
+ def api_key
14
+ MainsmsApi::Configuration.api_key
15
+ end
16
+
17
+ def used_params
18
+ params.select { |k, v| v.present? }
19
+ end
20
+
21
+ def sorted_params
22
+ used_params.values.map(&:to_s).sort
23
+ end
24
+
25
+ def sign
26
+ sha1 = Digest::SHA1.hexdigest((sorted_params + [api_key]).join(';'))
27
+
28
+ md5 = Digest::MD5.hexdigest(sha1)
29
+ end
30
+
31
+ def query
32
+ used_params.merge(sign: sign).to_query
33
+ end
34
+
35
+ def protocol
36
+ 'http'
37
+ end
38
+
39
+ def host
40
+ 'mainsms.ru'
41
+ end
42
+
43
+ def url
44
+ "#{protocol}://#{host}/#{path}?#{query}"
45
+ end
46
+
47
+ def request
48
+ @request ||= HTTParty.get(url)
49
+ end
50
+
51
+ def response_body
52
+ @response_body ||= request.body
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,19 @@
1
+ require 'hashie'
2
+
3
+ module MainsmsApi
4
+ class Configuration
5
+
6
+ def self.api_key
7
+ @settings.api_key
8
+ end
9
+
10
+ def self.project
11
+ @settings.project
12
+ end
13
+
14
+ def self.setup(options)
15
+ @settings ||= Hashie::Mash.new(options)
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,28 @@
1
+ module MainsmsApi
2
+ class Info
3
+ include ActiveAttr::MassAssignment
4
+ include Common
5
+
6
+ attr_accessor :phones
7
+
8
+ def initialize(args = {})
9
+ super(args)
10
+ end
11
+
12
+ alias_method :get, :response
13
+
14
+ private
15
+
16
+ def phones_string
17
+ phones.try(:join, ',')
18
+ end
19
+
20
+ def params
21
+ { project: project, phones: phones_string }
22
+ end
23
+
24
+ def path
25
+ 'api/mainsms/message/info'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module MainsmsApi
2
+ class Message
3
+ include ActiveAttr::MassAssignment
4
+ include Common
5
+
6
+ attr_accessor :sender, :message, :recipients, :run_at, :test
7
+
8
+ def initialize(args = {})
9
+ super(args)
10
+ end
11
+
12
+ alias_method :deliver, :response
13
+
14
+ private
15
+
16
+ def recipients_string
17
+ recipients.try(:join, ',')
18
+ end
19
+
20
+ def params
21
+ { project: project, sender: sender , message: message, recipients: recipients_string, run_at: run_at, test: test }
22
+ end
23
+
24
+ def path
25
+ 'api/mainsms/message/send'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module MainsmsApi
2
+ class Price
3
+ include ActiveAttr::MassAssignment
4
+ include Common
5
+
6
+ attr_accessor :recipients, :message
7
+
8
+ def initialize(args = {})
9
+ super(args)
10
+ end
11
+
12
+ alias_method :calculate, :response
13
+
14
+ private
15
+
16
+ def recipients_string
17
+ recipients.try(:join, ',')
18
+ end
19
+
20
+ def params
21
+ { project: project, message: message, recipients: recipients_string }
22
+ end
23
+
24
+ def path
25
+ 'api/mainsms/message/price'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module MainsmsApi
2
+ class Status
3
+ include ActiveAttr::MassAssignment
4
+ include Common
5
+
6
+ attr_accessor :message_ids
7
+
8
+ def initialize(args = {})
9
+ super(args)
10
+ end
11
+
12
+ alias_method :check, :response
13
+
14
+ private
15
+
16
+ def message_ids_string
17
+ message_ids.try(:join, ',')
18
+ end
19
+
20
+ def params
21
+ { project: project, messages_id: message_ids_string }
22
+ end
23
+
24
+ def path
25
+ 'api/mainsms/message/status'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module MainsmsApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :mainsms_api do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mainsms_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Evgeniy Serykh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_attr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hashie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: mainsms.ru API
84
+ email:
85
+ - e.v.serykh@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/generators/mainsms_api/install/install_generator.rb
91
+ - lib/generators/mainsms_api/install/templates/mainsms.yml
92
+ - lib/generators/mainsms_api/install/templates/mainsms_api.rb
93
+ - lib/mainsms_api/balance.rb
94
+ - lib/mainsms_api/common.rb
95
+ - lib/mainsms_api/configuration.rb
96
+ - lib/mainsms_api/info.rb
97
+ - lib/mainsms_api/message.rb
98
+ - lib/mainsms_api/price.rb
99
+ - lib/mainsms_api/status.rb
100
+ - lib/mainsms_api/version.rb
101
+ - lib/mainsms_api.rb
102
+ - lib/tasks/mainsms_api_tasks.rake
103
+ - MIT-LICENSE
104
+ - Rakefile
105
+ - README.md
106
+ homepage: https://github.com/evserykh/mainsms_api
107
+ licenses: []
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.0.3
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: mainsms.ru API
129
+ test_files: []