locasms 0.1.7 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9fcc47dc9d81331e507553c2bec302c2fd41ea5e
4
- data.tar.gz: 875d43f697d0a428cd93e1ec8b9d06cc0cdbdb0f
2
+ SHA256:
3
+ metadata.gz: 39596e51a28908193ac0455bf97fb1c703becaedc33c569fe509e1ce450a6e70
4
+ data.tar.gz: f4a5d880167d667f952ebd75f68116b4dcfda9da03579d9a2b4bca87f735b1ac
5
5
  SHA512:
6
- metadata.gz: d7ac7758b75ec458c061b5bad63612a7c2ae131bd363c86f14c04e51ee436788b3674db4cd2402b85baf6efa884a18ca469d383a7ad7eef4da611f453a471549
7
- data.tar.gz: e371973b0a48b2708ec2e9bae5e03964b1b7e155e18494e8e5c9d1b676ef498c725bbad2cd0ecb7d38341ac15818675df635f6ff4e13fe252955be8397dd0e7f
6
+ metadata.gz: '028885bb02e34a3f5bb918e805795e24e62a1f7d9bd18acc81bafb53e824502b263128b159c75af90d06d5764b210cd0fc01968d865cfed31a89bf1041fbdbd6'
7
+ data.tar.gz: 56ca283b83035314141fadcf0b48cedbf875176822c5ca159c99c7a70857260cfe9c3a8284f0ddcd8fe9b147edd4f4a3f91542df4107c683a1f1d2e1a87f3422
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *.swp
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
@@ -0,0 +1,6 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require: rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
@@ -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
@@ -1,9 +1,17 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0
4
- - 2.1
5
- - 2.2
6
- - 2.3
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in locasms.gemspec
data/Guardfile CHANGED
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  guard 'rspec' do
2
4
  watch(%r{^spec/.+_spec\.rb})
3
5
  watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
4
- watch('spec/spec_helper.rb') { "spec" }
5
- end
6
+ watch('spec/spec_helper.rb') { 'spec' }
7
+ end
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) [![Dependency Status](https://gemnasium.com/mcorp/locasms.png)](https://gemnasium.com/mcorp/locasms) [![Code Climate](https://codeclimate.com/github/mcorp/locasms.png)](https://codeclimate.com/github/mcorp/locasms) [![Inline docs](http://inch-ci.org/github/mcorp/locasms.svg?branch=master)](http://inch-ci.org/github/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 `March 29, 2016` the base IP of the service will change as noticed on this [issue](https://github.com/mcorp/locasms/issues/6). If you don't upgrade to version `0.1.7` your app will stop delivering SMS
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 api's][0].
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/#page_2/
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 :default => (ENV['TRAVIS'] ? :spec : :console)
19
+ task default: (ENV['TRAVIS'] ? :spec : :console)
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'bundler/setup'
6
+ require 'locasms'
7
+
8
+ require 'irb'
9
+ IRB.start
@@ -1,18 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'locasms/version'
2
4
 
3
- autoload :CSV, 'csv'
4
- autoload :JSON, 'json'
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, 'locasms/client'
10
- autoload :Exception, 'locasms/exception'
11
- autoload :InvalidLogin, 'locasms/exception'
10
+ autoload :Client, 'locasms/client'
11
+ autoload :Exception, 'locasms/exception'
12
+ autoload :InvalidLogin, 'locasms/exception'
12
13
  autoload :InvalidOperation, 'locasms/exception'
13
- autoload :Numbers, 'locasms/numbers'
14
- autoload :RestClient, 'locasms/rest_client'
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
@@ -1,27 +1,29 @@
1
- module LocaSMS
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 = if Time.now >= Time.parse('2016-03-29T00:00:00-0300')
7
- 'http://54.173.24.177/painel/api.ashx'
8
- elsif Time.now >= Time.parse('2015-06-01T00:00:00-0300')
9
- 'http://209.133.196.250/painel/api.ashx'
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 = opts[:rest_client]
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
- rest.get(:sendsms, msg: message, numbers: numbers(mobiles))['data']
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
- rest.get(:sendsms, msg: message, numbers: numbers(mobiles), jobdate: date, jobtime: time)['data']
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>] {campaign_id: id, delivery_id: delivery_id, enqueue_time: enqueue_time, delivery_time: delivery_time, status: status, carrier: carrier, mobile_number: mobile_number, message: message }
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 = if status =~ /aguardando envio/i
61
- :waiting
62
- elsif status =~ /sucesso/i
63
- :success
64
- elsif status =~ /numero invalido|nao cadastrado/i
65
- :invalid
66
- else
67
- :unknown
68
- end
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
- private
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
@@ -1,7 +1,8 @@
1
- module LocaSMS
1
+ # frozen_string_literal: true
2
2
 
3
+ module LocaSMS
3
4
  # Common base exception
4
- class Exception < ::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
- private
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 < Exception
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 < Exception
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
- module LocaSMS
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
- # Parse a value into a time
41
- # @param [Fixnum,String,DateTime,Time,#to_time] date
42
- # @result [Time] return a parsed time
43
- #
44
- # @example
45
- #
46
- # DateTimeHelper.parse '1977-03-14 14:12:00'
47
- # # => 1977-03-14 14:12:00 -0300
48
- #
49
- # DateTimeHelper.split 227207520
50
- # # => 1977-03-14 14:12:00 -0300
51
- #
52
- def parse(date)
53
- DateTimeHelper.parse date
54
- end
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
- # Breaks a given date in date and time
57
- # @param [Fixnum,String,DateTime,Time,#to_time] date
58
- # @result [Array<String>] an array containing respectively DD/MM/YYYY and HH:MM
59
- #
60
- # @example
61
- #
62
- # DateTimeHelper.split Time.now
63
- # # => ['14/03/1977', '14:12']
64
- #
65
- # DateTimeHelper.split 227207520
66
- # # => ['14/03/1977', '14:12']
67
- #
68
- def split(date)
69
- DateTimeHelper.split date
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