promotexter 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d4c0b227950260b42734726e893e36a4e022f06d835e00587700cae21fd3829
4
- data.tar.gz: e8537cc531c6cc6729efd9e21b663961bcc11b700be030b0510a40345f37768f
3
+ metadata.gz: a3d3343973a602a8045b300e1cc00c1a3b617a4a60c6cbc38724451535836a62
4
+ data.tar.gz: 6b6bb40e95f313a268597959048f921df1c3d14a4254957b85aa7597512b105d
5
5
  SHA512:
6
- metadata.gz: 240d8c193c701508b96e3e4046d599f6e9f688057848eeb0844ffb094a56ea7e2aec898a5dcf7c194ed7a4166c63eb4b7abb98a8a822f9c39a0e56ab77b416cc
7
- data.tar.gz: 85065e96c9c97641a4eafbc39b7adada742b0954bb9c7e7b1dacc75885594ff191108afd2e9e5576ad09e4f9dc6f03c3b22fbfd7dc20d818eadf1ac31093d59c
6
+ metadata.gz: 9e77f347e8418af5cc13a78da359cd9880460185659972ea2fca12aca85e3d526ee33870f3dbbc5d6421c95b5819d1d4b1a4db523374905d753860f69a1ef182
7
+ data.tar.gz: fe7e4063ebd44d02e7784070ab32c0d0dcd70a8d66ffb2160e122f2cf5952248404ab31d8ab2be10b8037ee689490395845b48bb3eb46a129b32c50c4385c42d
@@ -1,4 +1,4 @@
1
- PROMOTEXTER_API_KEY = api-key-here
2
- PROMOTEXTER_API_SECRET = api-secret-here
3
- PROMOTEXTER_FROM = api-sender-id
4
- RECEIVING_NUMBER = 639000000000
1
+ PROMOTEXTER_API_KEY = api-key-here
2
+ PROMOTEXTER_API_SECRET = api-secret-here
3
+ PROMOTEXTER_SENDER_ID = api-sender-id
4
+ RECEIVING_NUMBER = 639000000000
data/.gitignore CHANGED
@@ -10,6 +10,7 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  .env
13
+ .byebug_history
13
14
 
14
15
  # ignore files
15
16
  Gemfile.lock
@@ -1,168 +1,56 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2018-07-27 21:28:57 +0800 using RuboCop version 0.58.2.
3
+ # on 2018-10-04 01:42:38 +0800 using RuboCop version 0.58.2.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 1
10
- # Cop supports --auto-correct.
11
- # Configuration parameters: Include, TreatCommentsAsGroupSeparators.
12
- # Include: **/*.gemspec
13
- Gemspec/OrderedDependencies:
14
- Exclude:
15
- # - 'promotexter.gemspec'
16
-
17
- # Offense count: 1
9
+ # Offense count: 7
18
10
  # Cop supports --auto-correct.
19
- Layout/EmptyLines:
20
- Exclude:
21
- # - 'spec/promotexter_spec.rb'
11
+ # Configuration parameters: .
12
+ # SupportedStyles: space, no_space
13
+ Layout/SpaceAroundEqualsInParameterDefault:
14
+ EnforcedStyle: no_space
22
15
 
23
16
  # Offense count: 1
24
- # Cop supports --auto-correct.
25
- # Configuration parameters: EnforcedStyle.
26
- # SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines
27
- Layout/EmptyLinesAroundModuleBody:
28
- Exclude:
29
- # - 'lib/promotexter/client.rb'
30
-
31
- # Offense count: 4
32
- # Cop supports --auto-correct.
33
- # Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
34
- Layout/ExtraSpacing:
17
+ Lint/DuplicateMethods:
35
18
  Exclude:
36
- # - 'lib/promotexter/client.rb'
19
+ - 'lib/promotexter.rb'
37
20
 
38
21
  # Offense count: 1
39
- # Cop supports --auto-correct.
40
- # Configuration parameters: AllowForAlignment.
41
- Layout/SpaceAroundOperators:
42
- Exclude:
43
- # - 'lib/promotexter/client.rb'
44
-
45
- # Offense count: 3
46
- # Cop supports --auto-correct.
47
- # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
48
- # SupportedStyles: space, no_space
49
- # SupportedStylesForEmptyBraces: space, no_space
50
- Layout/SpaceInsideBlockBraces:
51
- Exclude:
52
- # - 'Gemfile'
53
- # - 'spec/promotexter_spec.rb'
54
-
55
- # Offense count: 20
56
- # Cop supports --auto-correct.
57
- # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
58
- # SupportedStyles: space, no_space, compact
59
- # SupportedStylesForEmptyBraces: space, no_space
60
- Layout/SpaceInsideHashLiteralBraces:
61
- Exclude:
62
- # - 'spec/promotexter_spec.rb'
63
-
64
- # Offense count: 3
65
- # Cop supports --auto-correct.
66
- # Configuration parameters: EnforcedStyle.
67
- # SupportedStyles: final_newline, final_blank_line
68
- Layout/TrailingBlankLines:
69
- Exclude:
70
- # - 'Gemfile'
71
- # - 'lib/promotexter.rb'
72
- # - 'lib/promotexter/client.rb'
73
-
74
- # Offense count: 6
75
- # Cop supports --auto-correct.
76
- # Configuration parameters: AllowInHeredoc.
77
- Layout/TrailingWhitespace:
78
- Exclude:
79
- # - 'lib/promotexter/client.rb'
80
- # - 'promotexter.gemspec'
81
- # - 'spec/promotexter_spec.rb'
82
-
83
- # Offense count: 2
84
- Lint/LiteralAsCondition:
85
- Exclude:
86
- # - 'lib/promotexter/client.rb'
87
-
88
- # Offense count: 5
89
22
  Lint/UselessAssignment:
90
23
  Exclude:
91
- # - 'spec/promotexter_spec.rb'
24
+ - 'lib/promotexter/base.rb'
92
25
 
93
- # Offense count: 1
26
+ # Offense count: 4
94
27
  Metrics/AbcSize:
95
- Max: 21
28
+ Max: 49
96
29
 
97
- # Offense count: 2
30
+ # Offense count: 7
98
31
  # Configuration parameters: CountComments, ExcludedMethods.
99
32
  # ExcludedMethods: refine
100
33
  Metrics/BlockLength:
101
- Max: 89
34
+ Max: 135
102
35
 
103
36
  # Offense count: 1
37
+ Metrics/CyclomaticComplexity:
38
+ Max: 9
39
+
40
+ # Offense count: 3
104
41
  # Configuration parameters: CountComments.
105
42
  Metrics/MethodLength:
106
- Max: 18
107
-
108
- # Offense count: 10
109
- # Cop supports --auto-correct.
110
- # Configuration parameters: EnforcedStyle.
111
- # SupportedStyles: braces, no_braces, context_dependent
112
- Style/BracesAroundHashParameters:
113
- Exclude:
114
- # - 'spec/promotexter_spec.rb'
115
-
116
- # Offense count: 2
117
- Style/Documentation:
118
- Exclude:
119
- - 'spec/**/*'
120
- - 'test/**/*'
121
- - 'lib/promotexter/client.rb'
43
+ Max: 13
122
44
 
123
45
  # Offense count: 1
124
- # Cop supports --auto-correct.
125
- Style/EmptyCaseCondition:
126
- Exclude:
127
- # - 'lib/promotexter/client.rb'
46
+ Metrics/PerceivedComplexity:
47
+ Max: 9
128
48
 
129
- # Offense count: 2
49
+ # Offense count: 19
130
50
  # Cop supports --auto-correct.
131
- Style/ExpandPathArguments:
132
- Exclude:
133
- # - 'promotexter.gemspec'
134
-
135
- # Offense count: 1
136
- # Cop supports --auto-correct.
137
- # Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
138
- # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
139
- Style/HashSyntax:
140
- Exclude:
141
- # - 'Rakefile'
142
-
143
- # Offense count: 3
144
- # Cop supports --auto-correct.
145
- Style/MutableConstant:
146
- Exclude:
147
- - 'lib/promotexter/client.rb'
148
- - 'lib/promotexter/version.rb'
149
-
150
- # Offense count: 5
151
- # Cop supports --auto-correct.
152
- # Configuration parameters: EnforcedStyle.
153
- # SupportedStyles: short, verbose
154
- Style/PreferredHashMethods:
155
- Exclude:
156
- # - 'lib/promotexter/client.rb'
157
- # - 'spec/promotexter_spec.rb'
158
-
159
- # Offense count: 1
160
- # Cop supports --auto-correct.
161
- # Configuration parameters: EnforcedStyle.
162
- # SupportedStyles: compact, exploded
163
- Style/RaiseArgs:
164
- Exclude:
165
- # - 'lib/promotexter/client.rb'
51
+ # Configuration parameters: Strict.
52
+ Style/NumericLiterals:
53
+ MinDigits: 7
166
54
 
167
55
  # Offense count: 1
168
56
  # Cop supports --auto-correct.
@@ -171,22 +59,8 @@ Style/RaiseArgs:
171
59
  Style/SpecialGlobalVars:
172
60
  EnforcedStyle: use_perl_names
173
61
 
174
- # Offense count: 52
175
- # Cop supports --auto-correct.
176
- # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
177
- # SupportedStyles: single_quotes, double_quotes
178
- Style/StringLiterals:
179
- Exclude:
180
- # - 'Gemfile'
181
- # - 'Rakefile'
182
- # - 'lib/promotexter.rb'
183
- # - 'lib/promotexter/version.rb'
184
- # - 'promotexter.gemspec'
185
- # - 'spec/promotexter_spec.rb'
186
- # - 'spec/spec_helper.rb'
187
-
188
- # Offense count: 41
62
+ # Offense count: 84
189
63
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
190
64
  # URISchemes: http, https
191
65
  Metrics/LineLength:
192
- Max: 261
66
+ Max: 160
data/Gemfile CHANGED
@@ -7,8 +7,11 @@ gemspec
7
7
 
8
8
  group :test do
9
9
  gem 'byebug'
10
+ gem 'dotenv', '~> 2.5', '>= 2.5.0'
10
11
  gem 'generator_spec'
12
+ gem 'rspec', '~> 3.0'
11
13
  gem 'rubocop'
12
14
  gem 'simplecov'
13
15
  gem 'simplecov-console'
16
+ gem 'webmock', '~> 3.4', '>= 3.4.2'
14
17
  end
data/README.md CHANGED
@@ -17,6 +17,12 @@ A Ruby gem for consuming the [Promotexter API](http://promotexter.com/index.php/
17
17
 
18
18
  gem promotexter
19
19
 
20
+ Then generate initializer:
21
+
22
+ $ rails g promotexter:install initializer
23
+
24
+ then edit the file in `config/initializer/promotexter.rb` and supply the necessary details.
25
+
20
26
  ## Usage
21
27
 
22
28
  Construct a Promotexter::Client object and use the #send_message method to
@@ -25,16 +31,59 @@ send a message. For example:
25
31
  ```ruby
26
32
  require 'promotexter'
27
33
 
28
- client = Promotexter::Client.new(api_key:'key', api_secret:'secret', from:'xxxxxx')
34
+ # send sms
35
+ client = Promotexter::Client.new
36
+ client.send_message(message: 'This is a test', to: '639171234567')
37
+
38
+ # or simply
39
+ Promotexter::Client.send_message(message: 'This is a test', to: '639171234567')
40
+
41
+ # or you can also pass the configurations as arguments
42
+ client = Promotexter::Client.new(api_key:'key', api_secret:'secret', sender_id:'xxxxxx')
29
43
  client.send_message(message:'This is a test', to:'639171234567')
30
44
  ```
45
+ If you want to use the delivery reports, head over to your initializer and uncomment the dlr_callback & dlr_reports lines
46
+
47
+ ### Reports
48
+ ```ruby
49
+ require 'promotexter'
50
+
51
+ # get account balance
52
+ Promotexter::Report.balance
53
+
54
+ # get sms transactions
55
+ reports = Promotexter::Report.new
56
+ reports.sms_transactions
57
+ ```
58
+ Available filters:
59
+
60
+ Params | Required | Description
61
+ --- | --- | ---
62
+ after | No | Filter transactions with creation date greater than this value.
63
+ before | No | Filter transactions with creation date less than this value.
64
+ transaction_id | No | Specific transactionId lookup
65
+ reference_id | No | Specific referenceId lookup
66
+ category_id | No | Specific categoryId lookup
67
+ request_id | No | Specific requestId lookup
68
+ from | No | Filter transactions with creation date greater than or equal to this value.
69
+ to | No | Filter transactions with creation date less than or equal to this value.
70
+
71
+ example:
72
+
73
+ ```ruby
74
+ reports = Promotexter::Report.new
75
+ reports.sms_transactions(from: '2018-10-03T14:44:23.186Z', to: '2018-11-03T14:44:23.186Z')
76
+
77
+ # or simply:
78
+ Promotexter::Report.sms_transactions(from: '2018-10-03T14:44:23.186Z', to: '2018-11-03T14:44:23.186Z')
79
+ ```
31
80
 
32
81
  ## Production environment variables
33
82
 
34
83
  Best practice for storing credentials for external services in production is
35
84
  to use environment variables, as described by [12factor.net/config](http://12factor.net/config).
36
85
  Promotexter::Client defaults to extracting the client_id, secret_key and shortcode it needs from the
37
- PROMOTEXTER_API_KEY, PROMOTEXTER_API_SECRET and PROMOTEXTER_FROM environment variables if the
86
+ PROMOTEXTER_API_KEY, PROMOTEXTER_API_SECRET and PROMOTEXTER_SENDER_ID environment variables if the
38
87
  options were not specified explicitly.
39
88
 
40
89
  ## Contributing
@@ -54,6 +103,9 @@ The above copyright notice and this permission notice shall be included in all c
54
103
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55
104
 
56
105
  ## Acknowledgement
57
- Originally created by Nujian Den Mark Meralpis, Updated by Scrambled Eggs Software Inc.
106
+ Originally created by Nujian Den Mark Meralpis. Updated by Scrambled Eggs Software Inc.
107
+
108
+ Thanks to all [awesome contributors](https://github.com/denmarkmeralpis/promotexter/graphs/contributors). Cheers!
109
+
58
110
 
59
111
  2016-2018
@@ -0,0 +1,18 @@
1
+ require 'rails/generators'
2
+
3
+ # name
4
+ module Promotexter
5
+ # Generator
6
+ module Generators
7
+ # Class generatpr
8
+ class InstallGenerator < Rails::Generators::NamedBase
9
+ source_root File.expand_path('templates', __dir__)
10
+ desc 'Creates promotexter.rb initializer for your application'
11
+
12
+ def copy_initializer
13
+ template 'promotexter_initializer.rb', 'config/initializers/promotexter.rb'
14
+ puts 'Done install! Configure the file in config/initializers/promotexter.rb'
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ Promotexter.configure do |config|
2
+ # Get it from https://portal.promotexter.com/
3
+ config.api_key = ENV['PROMOTEXTER_API_KEY']
4
+ config.api_secret = ENV['PROMOTEXTER_API_SECRET']
5
+
6
+ # Get it from promotexter portal under to Sender IDs tab
7
+ config.sender_id = ENV['PROMOTEXTER_SENDER_ID']
8
+
9
+ # If set to true, only transactionId will be returned in promotexter response.
10
+ # config.async = true
11
+
12
+ # -------------------------------------------------------
13
+ # Uncomment the lines below to use Delivery Report module
14
+ # See full documentation at https://promotexter.docs.apiary.io/
15
+ # -------------------------------------------------------
16
+
17
+ # Must be set this to true if you want to receive DLR notifications to the indicated dlr_callback. This overrides account settings.
18
+ # config.dlr_report = true
19
+
20
+ # Your own API endpoint where we will be sending DLR notifications. This overrides account settings.
21
+ # config.dlr_callback = 'https://example.com'
22
+ end
@@ -1,2 +1,27 @@
1
1
  require 'promotexter/version'
2
+ require 'promotexter/errors/base'
3
+ require 'promotexter/configuration'
4
+ require 'promotexter/base'
2
5
  require 'promotexter/client'
6
+ require 'promotexter/report'
7
+ require 'promotexter/response_parser'
8
+ require 'generators/promotexter/install_generator'
9
+
10
+ # Promotexter configuration block
11
+ module Promotexter
12
+ class << self
13
+ attr_accessor :configuration
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def self.reset
21
+ @configuration = Configuration.new
22
+ end
23
+
24
+ def self.configure
25
+ yield(configuration)
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module Promotexter
2
+ # base class
3
+ class Base
4
+ API_HOST = 'https://rest-portal.promotexter.com'.freeze
5
+
6
+ def api_key
7
+ @api_key ||= Promotexter.configuration.api_key
8
+ end
9
+
10
+ def api_secret
11
+ @api_secret ||= Promotexter.configuration.api_secret
12
+ end
13
+
14
+ def sender_id
15
+ @sender_id ||= Promotexter.configuration.sender_id
16
+ end
17
+
18
+ def uri_params
19
+ @uri_params ||= {}
20
+ end
21
+
22
+ private
23
+
24
+ def reset_uri_params
25
+ uri_params = {}
26
+ end
27
+ end
28
+ end
@@ -1,56 +1,57 @@
1
1
  require 'net/http'
2
2
  require 'json'
3
3
 
4
+ # namespace
4
5
  module Promotexter
5
- class Error < StandardError
6
- def initialize(message)
7
- super(message)
6
+ # sending of sms
7
+ class Client < Base
8
+ attr_accessor :to, :text, :reference_id, :category_id
9
+
10
+ def initialize(options={})
11
+ @api_key = options[:api_key]
12
+ @api_secret = options[:api_secret]
13
+ @sender_id = options[:sender_id]
14
+ @reference_id = options[:reference_id]
15
+ @category_id = options[:category_id]
8
16
  end
9
- end
10
17
 
11
- class BadRequestError < Error; end
18
+ def send_message(options={})
19
+ @to = options.fetch(:to)
20
+ @text = options.fetch(:text)
12
21
 
13
- class AuthenticationError < Error; end
22
+ endpoint = API_HOST + '/api/sms'
23
+ uri = URI(endpoint)
24
+ request = Net::HTTP.post_form(uri, request_params)
25
+ Promotexter::ResponseParser.new(request.body).parse
26
+ end
14
27
 
15
- class Client
16
- attr_accessor :api_secret, :api_key, :from, :http, :to, :text
17
- SMSAPI_PATH = '/sms/send'
18
- MESSAGE_PARAMS = {}
28
+ def self.send_message(options={})
29
+ new(options).send_message(options)
30
+ end
19
31
 
20
- def initialize(options = {})
21
- @api_key = options[:api_key]
22
- @api_secret = options[:api_secret]
23
- @from = options[:from]
32
+ private
24
33
 
25
- @host = 'https://rest-portal.promotexter.com'
26
- @http = Net::HTTP.new(@host, Net::HTTP.https_default_port)
34
+ def dlr_report_params
35
+ '1' if Promotexter.configuration.dlr_report
36
+ end
27
37
 
28
- MESSAGE_PARAMS['apiKey'] = @api_key
29
- MESSAGE_PARAMS['apiSecret'] = @api_secret
30
- MESSAGE_PARAMS['from'] = @from
38
+ def async_params
39
+ '1' if Promotexter.configuration.async
31
40
  end
32
41
 
33
- def send_message(params = {})
34
- uri = URI(@host + SMSAPI_PATH)
35
- MESSAGE_PARAMS['to'] = params[:to]
36
- MESSAGE_PARAMS['text'] = params[:text]
37
-
38
- res = Net::HTTP.post_form(uri, MESSAGE_PARAMS)
39
- puts res.inspect
40
- response = JSON.parse(res.body, symbolize_names: true)
41
-
42
- if response.key?(:statusCode)
43
- case response[:statusCode]
44
- when 400
45
- raise BadRequestError.new(message: response[:message])
46
- when 401
47
- raise AuthenticationError.new(message: response[:message])
48
- else
49
- raise Error, response[:message]
50
- end
51
- else
52
- response
53
- end
42
+ def request_params
43
+ reset_uri_params
44
+ uri_params[:apiKey] = api_key
45
+ uri_params[:apiSecret] = api_secret
46
+ uri_params[:from] = sender_id
47
+ uri_params[:to] = to
48
+ uri_params[:text] = text
49
+ uri_params[:dlrCallback] = Promotexter.configuration.dlr_callback unless Promotexter.configuration.dlr_report.blank?
50
+ uri_params[:dlrReport] = dlr_report_params unless Promotexter.configuration.dlr_report.blank?
51
+ uri_params[:async] = async_params unless Promotexter.configuration.async.blank?
52
+ uri_params[:referenceId] = reference_id unless reference_id.blank?
53
+ uri_params[:categoryId] = category_id unless category_id.blank?
54
+ uri_params
54
55
  end
55
56
  end
56
57
  end
@@ -0,0 +1,32 @@
1
+ # namespacing
2
+ module Promotexter
3
+ # class cofiguration
4
+ class Configuration
5
+ attr_writer :api_key, :api_secret, :sender_id, :dlr_callback, :dlr_report, :async
6
+ attr_reader :dlr_report, :async
7
+
8
+ def initialize
9
+ @api_key, @api_secret, @sender_id, @dlr_callback, @dlr_report, @async = nil
10
+ end
11
+
12
+ def api_key
13
+ raise Errors::Configuration, 'Promotexter api_key is missing! See documentation for configuration settings.' unless @api_key
14
+ @api_key
15
+ end
16
+
17
+ def api_secret
18
+ raise Errors::Configuration, 'Promotexter api_secret is missing! See documentation for configuration settings.' unless @api_secret
19
+ @api_secret
20
+ end
21
+
22
+ def sender_id
23
+ raise Errors::Configuration, 'Promotexter sender_id is missing! See documentation for configuration settings.' unless @sender_id
24
+ @sender_id
25
+ end
26
+
27
+ def dlr_callback
28
+ raise Errors::Configuration, 'Promotexter dlr_callback should have a valid value since dlr_report is set to true' if @dlr_report && @dlr_callback.blank?
29
+ @dlr_callback
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,8 @@
1
+ module Promotexter
2
+ module Errors
3
+ class Configuration < StandardError; end
4
+ class Authentication < StandardError; end
5
+ class BadRequest < StandardError; end
6
+ class NotAccepted < StandardError; end
7
+ end
8
+ end
@@ -0,0 +1,65 @@
1
+ # Get transaction lists
2
+ module Promotexter
3
+ # report class
4
+ class Report < Base
5
+ attr_accessor :after, :before, :transaction_id, :reference_id, :category_id, :request_id, :from, :to
6
+
7
+ def initialize(options={})
8
+ @api_key = options[:api_key]
9
+ @api_secret = options[:api_secret]
10
+ end
11
+
12
+ def sms_transactions(filters={})
13
+ @after = filters[:after]
14
+ @before = filters[:before]
15
+ @transaction_id = filters[:transaction_id]
16
+ @reference_id = filters[:reference_id]
17
+ @category_id = filters[:category_id]
18
+ @request_id = filters[:request_id]
19
+ @from = filters[:from]
20
+ @to = filters[:to]
21
+
22
+ endpoint = API_HOST + '/api/sms'
23
+ uri = URI(endpoint)
24
+ uri.query = request_params.to_query
25
+
26
+ request = Net::HTTP.get_response(uri)
27
+ Promotexter::ResponseParser.new(request.body).parse
28
+ end
29
+
30
+ def self.sms_transactions(options={})
31
+ new(options).sms_transactions(options)
32
+ end
33
+
34
+ def balance
35
+ endpoint = API_HOST + '/api/account/balance'
36
+ uri = URI(endpoint)
37
+ uri.query = request_params.to_query
38
+
39
+ request = Net::HTTP.get_response(uri)
40
+ Promotexter::ResponseParser.new(request.body).parse
41
+ end
42
+
43
+ def self.balance(options={})
44
+ new(options).balance
45
+ end
46
+
47
+ private
48
+
49
+ def request_params
50
+ reset_uri_params
51
+ uri_params[:apiKey] = api_key
52
+ uri_params[:apiSecret] = api_secret
53
+ uri_params[:filter] = {}
54
+ uri_params[:filter][:dateCreatedGt] = after unless after.blank?
55
+ uri_params[:filter][:dateCreatedLt] = before unless before.blank?
56
+ uri_params[:filter][:transactionId] = transaction_id unless transaction_id.blank?
57
+ uri_params[:filter][:referenceId] = reference_id unless reference_id.blank?
58
+ uri_params[:filter][:categoryId] = category_id unless category_id.blank?
59
+ uri_params[:filter][:requestId] = request_id unless request_id.blank?
60
+ uri_params[:filter][:from] = from unless from.blank?
61
+ uri_params[:filter][:to] = to unless to.blank?
62
+ uri_params
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,37 @@
1
+ module Promotexter
2
+ # parser class
3
+ class ResponseParser
4
+ attr_accessor :response
5
+
6
+ def initialize(response)
7
+ @response = JSON.parse(response, symbolize_names: true)
8
+ end
9
+
10
+ def parse
11
+ if response.key?(:code)
12
+ error_code_parser
13
+ else
14
+ response
15
+ end
16
+ end
17
+
18
+ def self.parse(response)
19
+ new(response).parse
20
+ end
21
+
22
+ private
23
+
24
+ def error_code_parser
25
+ case response[:code].to_i
26
+ when 200100, 200301, 200102, 200201, 200202, 200203, 200204, 200205, 200206, 40002, 40003, 40004, 40005, 40006, 40007, 50001
27
+ raise Errors::BadRequest, response[:message]
28
+ when 200101, 500101
29
+ raise Errors::NotAccepted, response[:message]
30
+ when 40001
31
+ raise Errors::Authentication, response[:message]
32
+ else
33
+ raise StandardError, response[:message]
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module Promotexter
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'.freeze
3
3
  end
@@ -32,8 +32,5 @@ Gem::Specification.new do |s|
32
32
  s.require_paths = ['lib']
33
33
 
34
34
  s.add_development_dependency 'bundler', '~> 1.16'
35
- s.add_development_dependency 'dotenv', '~> 2.5.0'
36
35
  s.add_development_dependency 'rake', '~> 10.0'
37
- s.add_development_dependency 'rspec', '~> 3.0'
38
- s.add_development_dependency 'webmock', '~> 3.4.2'
39
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: promotexter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nujian Den Mark Meralpis
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2018-07-27 00:00:00.000000000 Z
15
+ date: 2018-10-03 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: bundler
@@ -28,20 +28,6 @@ dependencies:
28
28
  - - "~>"
29
29
  - !ruby/object:Gem::Version
30
30
  version: '1.16'
31
- - !ruby/object:Gem::Dependency
32
- name: dotenv
33
- requirement: !ruby/object:Gem::Requirement
34
- requirements:
35
- - - "~>"
36
- - !ruby/object:Gem::Version
37
- version: 2.5.0
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- requirements:
42
- - - "~>"
43
- - !ruby/object:Gem::Version
44
- version: 2.5.0
45
31
  - !ruby/object:Gem::Dependency
46
32
  name: rake
47
33
  requirement: !ruby/object:Gem::Requirement
@@ -56,34 +42,6 @@ dependencies:
56
42
  - - "~>"
57
43
  - !ruby/object:Gem::Version
58
44
  version: '10.0'
59
- - !ruby/object:Gem::Dependency
60
- name: rspec
61
- requirement: !ruby/object:Gem::Requirement
62
- requirements:
63
- - - "~>"
64
- - !ruby/object:Gem::Version
65
- version: '3.0'
66
- type: :development
67
- prerelease: false
68
- version_requirements: !ruby/object:Gem::Requirement
69
- requirements:
70
- - - "~>"
71
- - !ruby/object:Gem::Version
72
- version: '3.0'
73
- - !ruby/object:Gem::Dependency
74
- name: webmock
75
- requirement: !ruby/object:Gem::Requirement
76
- requirements:
77
- - - "~>"
78
- - !ruby/object:Gem::Version
79
- version: 3.4.2
80
- type: :development
81
- prerelease: false
82
- version_requirements: !ruby/object:Gem::Requirement
83
- requirements:
84
- - - "~>"
85
- - !ruby/object:Gem::Version
86
- version: 3.4.2
87
45
  description: The gem provides a simple way of sending SMS via Promotexter API
88
46
  email:
89
47
  - denmarkmeralpis@gmail.com
@@ -107,8 +65,15 @@ files:
107
65
  - LICENSE.txt
108
66
  - README.md
109
67
  - Rakefile
68
+ - lib/generators/promotexter/install_generator.rb
69
+ - lib/generators/promotexter/templates/promotexter_initializer.rb
110
70
  - lib/promotexter.rb
71
+ - lib/promotexter/base.rb
111
72
  - lib/promotexter/client.rb
73
+ - lib/promotexter/configuration.rb
74
+ - lib/promotexter/errors/base.rb
75
+ - lib/promotexter/report.rb
76
+ - lib/promotexter/response_parser.rb
112
77
  - lib/promotexter/version.rb
113
78
  - promotexter.gemspec
114
79
  homepage: https://github.com/denmarkmeralpis/promotexter