locasms 0.1.7 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.rubocop.yml +6 -0
- data/.rubocop_todo.yml +30 -0
- data/.travis.yml +13 -5
- data/Gemfile +2 -0
- data/Guardfile +4 -2
- data/README.md +18 -4
- data/Rakefile +3 -1
- data/bin/console +9 -0
- data/lib/locasms.rb +11 -9
- data/lib/locasms/client.rb +49 -33
- data/lib/locasms/exception.rb +7 -7
- data/lib/locasms/helpers/date_time_helper.rb +38 -69
- data/lib/locasms/numbers.rb +14 -12
- data/lib/locasms/rest_client.rb +28 -15
- data/lib/locasms/version.rb +3 -1
- data/locasms.gemspec +28 -26
- data/spec/lib/locasms/client_spec.rb +114 -108
- data/spec/lib/locasms/helpers/date_time_helper_spec.rb +11 -35
- data/spec/lib/locasms/numbers_spec.rb +54 -68
- data/spec/lib/locasms/rest_client_spec.rb +55 -23
- data/spec/spec.opts +1 -2
- data/spec/spec_helper.rb +5 -4
- metadata +55 -80
- data/.hound.yml +0 -2
- data/.ruby-style.yml +0 -1063
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 39596e51a28908193ac0455bf97fb1c703becaedc33c569fe509e1ce450a6e70
|
4
|
+
data.tar.gz: f4a5d880167d667f952ebd75f68116b4dcfda9da03579d9a2b4bca87f735b1ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '028885bb02e34a3f5bb918e805795e24e62a1f7d9bd18acc81bafb53e824502b263128b159c75af90d06d5764b210cd0fc01968d865cfed31a89bf1041fbdbd6'
|
7
|
+
data.tar.gz: 56ca283b83035314141fadcf0b48cedbf875176822c5ca159c99c7a70857260cfe9c3a8284f0ddcd8fe9b147edd4f4a3f91542df4107c683a1f1d2e1a87f3422
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2020-10-15 23:34:33 UTC using RuboCop version 0.93.1.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 8
|
10
|
+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
|
11
|
+
# ExcludedMethods: refine
|
12
|
+
Metrics/BlockLength:
|
13
|
+
Max: 171
|
14
|
+
|
15
|
+
# Offense count: 1
|
16
|
+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
|
17
|
+
Metrics/MethodLength:
|
18
|
+
Max: 27
|
19
|
+
|
20
|
+
# Offense count: 1
|
21
|
+
# Configuration parameters: CountKeywordArgs.
|
22
|
+
Metrics/ParameterLists:
|
23
|
+
Max: 13
|
24
|
+
|
25
|
+
# Offense count: 1
|
26
|
+
# Cop supports --auto-correct.
|
27
|
+
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
28
|
+
# URISchemes: http, https
|
29
|
+
Layout/LineLength:
|
30
|
+
Max: 179
|
data/.travis.yml
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
- 2.
|
4
|
-
- 2.
|
5
|
-
- 2.
|
6
|
-
- 2.
|
3
|
+
- 2.7
|
4
|
+
- 2.6
|
5
|
+
- 2.5
|
6
|
+
- 2.4
|
7
7
|
env:
|
8
8
|
- TRAVIS=true
|
9
|
-
|
9
|
+
before_script:
|
10
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
11
|
+
- chmod +x ./cc-test-reporter
|
12
|
+
- ./cc-test-reporter before-build
|
13
|
+
script:
|
14
|
+
- bundle exec rspec
|
15
|
+
- bundle exec rubocop
|
16
|
+
after_script:
|
17
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/Gemfile
CHANGED
data/Guardfile
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# locasms
|
2
|
-
[![Gem Version](https://badge.fury.io/rb/locasms.svg)](http://badge.fury.io/rb/locasms) [![Build Status](https://travis-ci.org/mcorp/locasms.png?branch=master)](https://travis-ci.org/mcorp/locasms) [![
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/locasms.svg)](http://badge.fury.io/rb/locasms) [![Build Status](https://travis-ci.org/mcorp/locasms.png?branch=master)](https://travis-ci.org/mcorp/locasms) [![Code Climate](https://api.codeclimate.com/v1/badges/4187ea939aabd3b66bf7/maintainability)](https://codeclimate.com/github/mcorp/locasms) [![CC Coverage](https://api.codeclimate.com/v1/badges/4187ea939aabd3b66bf7/test_coverage)](https://codeclimate.com/github/mcorp/locasms/test_coverage) [![Inline docs](http://inch-ci.org/github/mcorp/locasms.svg?branch=master)](http://inch-ci.org/github/mcorp/locasms)
|
3
3
|
|
4
|
-
> :warning: After `
|
4
|
+
> :warning: After `February, 10, 2018` the base IP of the service will change as noticed on this [issue](https://github.com/mcorp/locasms/issues/21). If you don't upgrade to version `0.3.1` your app will stop delivering SMS.
|
5
5
|
|
6
|
-
Client to consume [LocaSMS
|
6
|
+
Client to consume API's from [LocaSMS][0] and its Short Code SMS version [SMS Plataforma][1].
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -26,8 +26,15 @@ Simple example:
|
|
26
26
|
```ruby
|
27
27
|
require 'locasms'
|
28
28
|
|
29
|
+
# Default:
|
29
30
|
cli = LocaSMS::Client.new 'LOGIN', 'PASSWORD'
|
30
31
|
|
32
|
+
# Short Code:
|
33
|
+
cli = LocaSMS::Client.new 'LOGIN', 'PASSWORD', type: :shortcode
|
34
|
+
|
35
|
+
# With default url callback (optional):
|
36
|
+
cli = LocaSMS::Client.new 'LOGIN', 'PASSWORD', url_callback: 'http://url.to/callback'
|
37
|
+
|
31
38
|
# delivering message to one mobile
|
32
39
|
cli.deliver 'my message', '1155559999'
|
33
40
|
|
@@ -37,6 +44,9 @@ cli.deliver 'my message', '1199998888', '5500002222'
|
|
37
44
|
cli.deliver 'my message', ['1199998888', '5500002222']
|
38
45
|
cli.deliver 'my message', %w(1199998888 5500002222)
|
39
46
|
|
47
|
+
# delivering message with url callback
|
48
|
+
cli.deliver 'my message', '1155559999', url_callback: 'http://url.to/callback'
|
49
|
+
|
40
50
|
# scheduling the deliver of a message to one mobile
|
41
51
|
cli.deliver_at 'my message', '2013-10-12 20:33:00', '1155559999'
|
42
52
|
|
@@ -46,6 +56,9 @@ cli.deliver_at 'my message', '2013-10-12 20:33:00', '1199998888', '5500002222'
|
|
46
56
|
cli.deliver_at 'my message', '2013-10-12 20:33:00', ['1199998888', '5500002222']
|
47
57
|
cli.deliver_at 'my message', '2013-10-12 20:33:00', %w(1199998888 5500002222)
|
48
58
|
|
59
|
+
# scheduling the deliver of a message with url callback
|
60
|
+
cli.deliver_at 'my message', '2013-10-12 20:33:00', '1155559999', url_callback: 'http://url.to/callback'
|
61
|
+
|
49
62
|
# geting the remaining balance
|
50
63
|
cli.balance
|
51
64
|
|
@@ -67,4 +80,5 @@ cli.campaign_release '0000'
|
|
67
80
|
4. Push to the branch (`git push origin my-new-feature`)
|
68
81
|
5. Create new Pull Request
|
69
82
|
|
70
|
-
[0]: http://locasms.com.br
|
83
|
+
[0]: http://locasms.com.br
|
84
|
+
[1]: http://smsplataforma.com.br
|
data/Rakefile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rubygems'
|
2
4
|
require 'bundler'
|
3
5
|
|
@@ -14,4 +16,4 @@ task :console do
|
|
14
16
|
sh 'bundle exec irb -rubygems -I lib -r locasms.rb'
|
15
17
|
end
|
16
18
|
|
17
|
-
task :
|
19
|
+
task default: (ENV['TRAVIS'] ? :spec : :console)
|
data/bin/console
ADDED
data/lib/locasms.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'locasms/version'
|
2
4
|
|
3
|
-
autoload :CSV,
|
4
|
-
autoload :
|
5
|
-
autoload :RestClient, 'rest_client'
|
6
|
-
autoload :Logger, 'logger'
|
5
|
+
autoload :CSV, 'csv'
|
6
|
+
autoload :MultiJson, 'multi_json'
|
7
7
|
|
8
|
+
# Module to encapsulate implementation
|
8
9
|
module LocaSMS
|
9
|
-
autoload :Client,
|
10
|
-
autoload :Exception,
|
11
|
-
autoload :InvalidLogin,
|
10
|
+
autoload :Client, 'locasms/client'
|
11
|
+
autoload :Exception, 'locasms/exception'
|
12
|
+
autoload :InvalidLogin, 'locasms/exception'
|
12
13
|
autoload :InvalidOperation, 'locasms/exception'
|
13
|
-
autoload :Numbers,
|
14
|
-
autoload :RestClient,
|
14
|
+
autoload :Numbers, 'locasms/numbers'
|
15
|
+
autoload :RestClient, 'locasms/rest_client'
|
15
16
|
|
17
|
+
# Module to encapsulate helpers
|
16
18
|
module Helpers
|
17
19
|
autoload :DateTimeHelper, 'locasms/helpers/date_time_helper'
|
18
20
|
end
|
data/lib/locasms/client.rb
CHANGED
@@ -1,27 +1,29 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module LocaSMS
|
3
4
|
# Client to interact with LocaSMS API
|
4
5
|
class Client
|
6
|
+
# Default API "domain"
|
7
|
+
DOMAIN = 'app.locasms.com.br'
|
8
|
+
|
5
9
|
# Default API address
|
6
|
-
ENDPOINT =
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
else
|
11
|
-
'http://173.44.33.18/painel/api.ashx'
|
12
|
-
end
|
10
|
+
ENDPOINT = {
|
11
|
+
default: "http://#{DOMAIN}/painel/api.ashx",
|
12
|
+
shortcode: "http://#{DOMAIN}/shortcode/api.ashx"
|
13
|
+
}.freeze
|
13
14
|
|
14
|
-
attr_reader :login, :password
|
15
|
+
attr_reader :login, :password, :type, :callback
|
15
16
|
|
16
17
|
# @param [String] login authorized user
|
17
18
|
# @param [String] password access password
|
18
19
|
# @param [Hash] opts
|
19
20
|
# @option opts :rest_client (RestClient) client to be used to handle http requests
|
20
|
-
def initialize(login, password, opts={})
|
21
|
+
def initialize(login, password, opts = {})
|
21
22
|
@login = login
|
22
23
|
@password = password
|
23
|
-
|
24
|
-
@rest
|
24
|
+
@type = opts[:type] || :default
|
25
|
+
@rest = opts[:rest_client]
|
26
|
+
@callback = opts[:url_callback]
|
25
27
|
end
|
26
28
|
|
27
29
|
# Sends a message to one or more mobiles
|
@@ -29,8 +31,13 @@ module LocaSMS
|
|
29
31
|
# @param [String,Array<String>] mobiles number of the mobiles to address the message
|
30
32
|
# @return [String] campaign id on success
|
31
33
|
# @raise [LocaSMS::Exception] if bad numbers were given
|
32
|
-
def deliver(message, *mobiles)
|
33
|
-
|
34
|
+
def deliver(message, *mobiles, **opts)
|
35
|
+
attrs = {
|
36
|
+
msg: message,
|
37
|
+
numbers: numbers(mobiles),
|
38
|
+
url_callback: callback
|
39
|
+
}.merge(opts)
|
40
|
+
rest.get(:sendsms, attrs)['data']
|
34
41
|
end
|
35
42
|
|
36
43
|
# Schedule the send of a message to one or more mobiles
|
@@ -39,9 +46,16 @@ module LocaSMS
|
|
39
46
|
# @param [String,Array<String>] mobiles number of the mobiles to address the message
|
40
47
|
# @return UNDEF
|
41
48
|
# @raise [LocaSMS::Exception] if bad numbers were given
|
42
|
-
def deliver_at(message, datetime, *mobiles)
|
49
|
+
def deliver_at(message, datetime, *mobiles, **opts)
|
43
50
|
date, time = Helpers::DateTimeHelper.split datetime
|
44
|
-
|
51
|
+
attrs = {
|
52
|
+
msg: message,
|
53
|
+
numbers: numbers(mobiles),
|
54
|
+
jobdate: date,
|
55
|
+
jobtime: time,
|
56
|
+
url_callback: callback
|
57
|
+
}.merge(opts)
|
58
|
+
rest.get(:sendsms, attrs)['data']
|
45
59
|
end
|
46
60
|
|
47
61
|
# Get de current amount of sending credits
|
@@ -52,20 +66,24 @@ module LocaSMS
|
|
52
66
|
|
53
67
|
# Gets the current status of the given campaign
|
54
68
|
# @param [String] id campaign id
|
55
|
-
# @return [Array<Hash>]
|
69
|
+
# @return [Array<Hash>]
|
70
|
+
# { campaign_id: id, delivery_id: delivery_id, enqueue_time: enqueue_time,
|
71
|
+
# delivery_time: delivery_time, status: status, carrier: carrier,
|
72
|
+
# mobile_number: mobile_number, message: message }
|
56
73
|
def campaign_status(id)
|
57
74
|
response = rest.get(:getstatus, id: id)
|
58
75
|
begin
|
59
76
|
CSV.new(response['data'] || '', col_sep: ';', quote_char: '"').map do |delivery_id, _, enqueue_time, _, delivery_time, _, status, _, _, carrier, mobile_number, _, message|
|
60
|
-
status =
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
77
|
+
status = case status
|
78
|
+
when /aguardando envio/i
|
79
|
+
waiting
|
80
|
+
when /sucesso/i
|
81
|
+
success
|
82
|
+
when /numero invalido|nao cadastrado/i
|
83
|
+
invalid
|
84
|
+
else
|
85
|
+
unknown
|
86
|
+
end
|
69
87
|
|
70
88
|
{
|
71
89
|
campaign_id: id,
|
@@ -78,8 +96,8 @@ module LocaSMS
|
|
78
96
|
message: message
|
79
97
|
}
|
80
98
|
end
|
81
|
-
rescue
|
82
|
-
raise Exception.new 'Invalid delivery response data'
|
99
|
+
rescue StandardError
|
100
|
+
raise LocaSMS::Exception.new(message: 'Invalid delivery response data')
|
83
101
|
end
|
84
102
|
end
|
85
103
|
|
@@ -97,13 +115,13 @@ module LocaSMS
|
|
97
115
|
rest.get(:releasesms, id: id)['data']
|
98
116
|
end
|
99
117
|
|
100
|
-
|
118
|
+
private
|
101
119
|
|
102
120
|
# Gets the current RestClient to handle http requests
|
103
121
|
# @return [RestClient] you can set on class creation passing it on the options
|
104
122
|
# @private
|
105
123
|
def rest
|
106
|
-
@rest ||= RestClient.new ENDPOINT, lgn: login, pwd: password
|
124
|
+
@rest ||= RestClient.new ENDPOINT[type], lgn: login, pwd: password
|
107
125
|
end
|
108
126
|
|
109
127
|
# Processes and returns all good numbers in a string
|
@@ -115,9 +133,7 @@ module LocaSMS
|
|
115
133
|
numbers = Numbers.new mobiles
|
116
134
|
return numbers.to_s unless numbers.bad?
|
117
135
|
|
118
|
-
raise Exception("Bad numbers were given: #{numbers.bad.join(',')}")
|
136
|
+
raise LocaSMS::Exception.new(message: "Bad numbers were given: #{numbers.bad.join(',')}")
|
119
137
|
end
|
120
|
-
|
121
138
|
end
|
122
|
-
|
123
139
|
end
|
data/lib/locasms/exception.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module LocaSMS
|
3
4
|
# Common base exception
|
4
|
-
class Exception <
|
5
|
+
class Exception < RuntimeError
|
5
6
|
attr_reader :raw, :action
|
6
7
|
|
7
8
|
def initialize(data = {})
|
@@ -11,7 +12,7 @@ module LocaSMS
|
|
11
12
|
super data[:message] || default_message
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
+
private
|
15
16
|
|
16
17
|
def default_message
|
17
18
|
nil
|
@@ -20,17 +21,16 @@ module LocaSMS
|
|
20
21
|
|
21
22
|
# Raised when asked for an invalid operation
|
22
23
|
# @see https://github.com/mcorp/locasms/wiki/A-API-de-envio#lista-das-a%C3%A7%C3%B5es-dispon%C3%ADveis
|
23
|
-
class InvalidOperation <
|
24
|
+
class InvalidOperation < RuntimeError
|
24
25
|
def default_message
|
25
26
|
'Invalid Operation'
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
30
|
# Raised when the given credentials are invalid
|
30
|
-
class InvalidLogin <
|
31
|
+
class InvalidLogin < RuntimeError
|
31
32
|
def default_message
|
32
33
|
'Invalid Login'
|
33
34
|
end
|
34
35
|
end
|
35
|
-
|
36
|
-
end
|
36
|
+
end
|
@@ -1,74 +1,43 @@
|
|
1
|
-
|
2
|
-
module Helpers
|
3
|
-
|
4
|
-
# Helper class to handle with time parsing
|
5
|
-
class DateTimeHelper
|
6
|
-
# Parse a value into a time
|
7
|
-
# @param [Fixnum,String,DateTime,Time,#to_time] date
|
8
|
-
# @result [Time] return a parsed time
|
9
|
-
#
|
10
|
-
# @example
|
11
|
-
#
|
12
|
-
# DateTimeHelper.parse '1977-03-14 14:12:00'
|
13
|
-
# # => 1977-03-14 14:12:00 -0300
|
14
|
-
#
|
15
|
-
# DateTimeHelper.split 227207520
|
16
|
-
# # => 1977-03-14 14:12:00 -0300
|
17
|
-
#
|
18
|
-
def self.parse(date)
|
19
|
-
date = Time.at(date) if date.is_a? Fixnum
|
20
|
-
date = Time.parse(date) if date.is_a? String
|
21
|
-
date = date.to_time if date.respond_to? :to_time
|
22
|
-
end
|
23
|
-
|
24
|
-
# Breaks a given date in date and time
|
25
|
-
# @param [Fixnum,String,DateTime,Time,#to_time] date
|
26
|
-
# @result [Array<String>] an array containing respectively DD/MM/YYYY and HH:MM
|
27
|
-
#
|
28
|
-
# @example
|
29
|
-
#
|
30
|
-
# DateTimeHelper.split Time.now
|
31
|
-
# # => ['14/03/1977', '14:12']
|
32
|
-
#
|
33
|
-
# DateTimeHelper.split 227207520
|
34
|
-
# # => ['14/03/1977', '14:12']
|
35
|
-
#
|
36
|
-
def self.split(date)
|
37
|
-
parse(date).strftime('%d/%m/%Y %H:%M').split(' ')
|
38
|
-
end
|
1
|
+
# frozen_string_literal: true
|
39
2
|
|
40
|
-
|
41
|
-
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
DateTimeHelper.
|
54
|
-
|
3
|
+
module LocaSMS
|
4
|
+
module Helpers
|
5
|
+
# Helper class to handle with time parsing
|
6
|
+
class DateTimeHelper
|
7
|
+
# Parse a value into a time
|
8
|
+
# @param [Fixnum,String,DateTime,Time,#to_time] date
|
9
|
+
# @result [Time] return a parsed time
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
#
|
13
|
+
# DateTimeHelper.parse '1977-03-14 14:12:00'
|
14
|
+
# # => 1977-03-14 14:12:00 -0300
|
15
|
+
#
|
16
|
+
# DateTimeHelper.split 227207520
|
17
|
+
# # => 1977-03-14 14:12:00 -0300
|
18
|
+
#
|
19
|
+
def self.parse(date)
|
20
|
+
date = Time.at(date) if date.is_a? Integer
|
21
|
+
date = Time.parse(date) if date.is_a? String
|
22
|
+
date = date.to_time if date.respond_to? :to_time
|
23
|
+
date
|
24
|
+
end
|
55
25
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
26
|
+
# Breaks a given date in date and time
|
27
|
+
# @param [Fixnum,String,DateTime,Time,#to_time] date
|
28
|
+
# @result [Array<String>] an array containing respectively DD/MM/YYYY and HH:MM
|
29
|
+
#
|
30
|
+
# @example
|
31
|
+
#
|
32
|
+
# DateTimeHelper.split Time.now
|
33
|
+
# # => ['14/03/1977', '14:12']
|
34
|
+
#
|
35
|
+
# DateTimeHelper.split 227207520
|
36
|
+
# # => ['14/03/1977', '14:12']
|
37
|
+
#
|
38
|
+
def self.split(date)
|
39
|
+
parse(date).strftime('%d/%m/%Y %H:%M').split(' ')
|
40
|
+
end
|
70
41
|
end
|
71
42
|
end
|
72
|
-
|
73
43
|
end
|
74
|
-
end
|