locasms 0.1.1 → 0.1.2

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
  SHA1:
3
- metadata.gz: 12853fa646f333fceb3ced3a9fa7c357bb51495d
4
- data.tar.gz: 65f14f3bd5ec19d1a7a2b6da9d1cac23c72296d0
3
+ metadata.gz: b416a1dbc0a2828144adc3cad4f46f648febe3eb
4
+ data.tar.gz: 91c5669394594303a92d79ada598e96e2075198b
5
5
  SHA512:
6
- metadata.gz: 117a31d340940d5c206576cf81f1571e1b6025811624a6b6ac1b1c074b5b0b8fd29e398c04e0890778132f82b381e50a110b32cb028ba823a7a0f233d10f527b
7
- data.tar.gz: 44be44b781e6969014462c42cfe2e4e584eab62cd82ad04316b2afd8cbf0f654cb52221a7836ae920da59b879d4c790b08dfbdb712aa4fadc4dc0087c971e196
6
+ metadata.gz: c5d7e98110b2a257ec038716bbef0923fb49a9afc40edc84cbbe6a5ccd0bfebeaf5e9574219cf66ce329a36a7ad2083e786bf513d27c1212f31d031d7cc87327
7
+ data.tar.gz: 6e83d47e413f43a918f507e73b8130d8f0998c4b5caf3cdd43d86b1f67fe5964b68aba4baa9a75f6d0965199cd030487235ee167b43382a6d9605502a67732be
data/.travis.yml CHANGED
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9
4
- - 2.0
4
+ - 2.0
5
+ env:
6
+ - TRAVIS=true
data/README.md CHANGED
@@ -34,10 +34,10 @@ cli.deliver 'my message', '1199998888', '5500002222'
34
34
  cli.deliver 'my message', ['1199998888', '5500002222']
35
35
  cli.deliver 'my message', %w(1199998888 5500002222)
36
36
 
37
- # scheluling the deliver of a message to one mobile
37
+ # scheduling the deliver of a message to one mobile
38
38
  cli.deliver_at 'my message', '2013-10-12 20:33:00', '1155559999'
39
39
 
40
- # scheluling the deliver of a message to multiple mobiles at once
40
+ # scheduling the deliver of a message to multiple mobiles at once
41
41
  cli.deliver_at 'my message', '2013-10-12 20:33:00', '1199998888,5500002222'
42
42
  cli.deliver_at 'my message', '2013-10-12 20:33:00', '1199998888', '5500002222'
43
43
  cli.deliver_at 'my message', '2013-10-12 20:33:00', ['1199998888', '5500002222']
@@ -64,4 +64,4 @@ cli.campaign_release '0000'
64
64
  4. Push to the branch (`git push origin my-new-feature`)
65
65
  5. Create new Pull Request
66
66
 
67
- [0]: http://locasms.com.br/#page_2/
67
+ [0]: http://locasms.com.br/#page_2/
data/Rakefile CHANGED
@@ -14,4 +14,4 @@ task :console do
14
14
  sh 'bundle exec irb -rubygems -I lib -r locasms.rb'
15
15
  end
16
16
 
17
- task :default => :console
17
+ task :default => (ENV['TRAVIS'] ? :spec : :console)
@@ -22,8 +22,9 @@ module LocaSMS
22
22
  # @param [String] message message to be sent
23
23
  # @param [String,Array<String>] mobiles number of the mobiles to address the message
24
24
  # @return [String] campaign id on success
25
+ # @raise [LocaSMS::Exception] if bad numbers were given
25
26
  def deliver(message, *mobiles)
26
- rest.get :sendsms, msg: message, numbers: mobiles.join(',')
27
+ rest.get :sendsms, msg: message, numbers: numbers(mobiles)
27
28
  end
28
29
 
29
30
  # Schedule the send of a message to one or more mobiles
@@ -31,13 +32,10 @@ module LocaSMS
31
32
  # @param [Time,DateTime,Fixnum,String] datetime
32
33
  # @param [String,Array<String>] mobiles number of the mobiles to address the message
33
34
  # @return UNDEF
35
+ # @raise [LocaSMS::Exception] if bad numbers were given
34
36
  def deliver_at(message, datetime, *mobiles)
35
- datetime = Time.at(datetime) if datetime.is_a? Fixnum
36
- datetime = Time.parse(datetime) if datetime.is_a? String
37
- datetime = datetime.to_time if datetime.respond_to? :to_time
38
-
39
- date, time = datetime.strftime('%d/%m/%Y|%H:%M').split('|')
40
- rest.get :sendsms, numbers: mobiles.join(','), jobdate: date, jobtime: time
37
+ date, time = Helpers::DateTimeHelper.split datetime
38
+ rest.get :sendsms, msg: message, numbers: numbers(mobiles), jobdate: date, jobtime: time
41
39
  end
42
40
 
43
41
  # Get de current amount of sending credits
@@ -75,6 +73,19 @@ module LocaSMS
75
73
  def rest
76
74
  @rest ||= RestClient.new ENDPOINT, lgn: login, pwd: password
77
75
  end
76
+
77
+ # Processes and returns all good numbers in a string
78
+ # @param [Array<String>]
79
+ # @return [String]
80
+ # @raise [LocaSMS::Exception] if bad numbers were given
81
+ # @private
82
+ def numbers(*mobiles)
83
+ numbers = Numbers.new mobiles
84
+ return numbers.to_s unless numbers.bad?
85
+
86
+ raise Exception("Bad numbers were given: #{numbers.bad.join(',')}")
87
+ end
88
+
78
89
  end
79
90
 
80
91
  end
@@ -0,0 +1,6 @@
1
+ module LocaSMS
2
+
3
+ class Exception < ::Exception
4
+ end
5
+
6
+ end
@@ -0,0 +1,74 @@
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
39
+
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
55
+
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
70
+ end
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,80 @@
1
+ module LocaSMS
2
+
3
+ # Class that sanitizes and validates a list of mobile's numbers
4
+ class Numbers
5
+ attr_reader :good, :bad
6
+
7
+ # Creates a new instance of the class, separating good and bad numbers
8
+ # @param [Array<String>] numbers given mobile numbers
9
+ # @see #normalize
10
+ # @see #evaluate
11
+ def initialize(*numbers)
12
+ evaluated = evaluate(numbers)
13
+ @good, @bad = evaluated[:good], evaluated[:bad]
14
+ end
15
+
16
+ # Checks if there are bad numbers
17
+ # @return [TrueClass, FalseClass] true if there are bad numbers
18
+ # @see #valid_number?
19
+ def bad?
20
+ not bad.empty?
21
+ end
22
+
23
+ # Clears all non digits from a mobile's number and converts into a normalized array
24
+ # @param [Array<String>] numbers list of mobile numbers to be clean
25
+ # @return [Array<String>] a normalized list of mobile numbers
26
+ # @example
27
+ # numbers = Numbers.new
28
+ # numbers.normalize '+55 (41) 8888-9999'
29
+ # # => ['554188889999']
30
+ # numbers.normalize '8888-9999', '7777-0000'
31
+ # # => ['88889999','77770000']
32
+ # numbers.normalize '8888-9999,7777-0000'
33
+ # # => ['88889999','77770000']
34
+ # numbers.normalize '8888-9999', ['AA', '6666-9999', '7777-0000'], '3333-4444,555-9999'
35
+ # # => ['88889999','AA','66669999','77770000','33334444','5559999']
36
+ def normalize(*numbers)
37
+ numbers = numbers.join(',')
38
+ .split(',')
39
+ .map{|number| number.gsub(/[^0-9a-zA-Z]/, '') }
40
+ .delete_if{|number| number.empty? }
41
+ end
42
+
43
+ # Validates if a mobile's number has only digits
44
+ # @param [String] number given number to be validated
45
+ # @return [TrueClass, FalseClass] true if the number is valid
46
+ def valid_number?(number)
47
+ return false if number.nil? or number =~ /[^0-9a-zA-Z]/
48
+ [10, 11].include? number.size
49
+ end
50
+
51
+ # Evaluates a list of numbers and separat them in two groups. The good ones
52
+ # and the bad ones.
53
+ # @param [Array<String>] numbers given numbers to evaluate
54
+ # @return [Hash] containing two values. :good with the good numbers and :bad
55
+ # for the bad ones
56
+ # @example With only good numbers
57
+ # Numbers.new.evaluate('4199998888','11777770000')
58
+ # #=> {good: ['4199998888','11777770000'], bad: []}
59
+ # @example With only bad numbers
60
+ # Numbers.new.evaluate('5551212')
61
+ # #=> {good: [], bad: ['5551212']}
62
+ # @example With good and bad numbers mixed
63
+ # Numbers.new.evaluate('4199998888','11777770000','5551212')
64
+ # #=> {good: ['4199998888','11777770000'], bad: ['5551212']}
65
+ def evaluate(*numbers)
66
+ normalize(numbers).reduce({good: [], bad: []}) do |hash, number|
67
+ bucket = valid_number?(number) ? :good : :bad
68
+ hash[bucket] << number
69
+ hash
70
+ end
71
+ end
72
+
73
+ # Return all good numbers in a string comma joined
74
+ # @return [String] all good numbers in a string comma joined
75
+ def to_s
76
+ (good || []).join(',')
77
+ end
78
+ end
79
+
80
+ end
@@ -1,3 +1,3 @@
1
1
  module LocaSMS
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/locasms.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "locasms/version"
1
+ require 'locasms/version'
2
2
 
3
3
  autoload :JSON, 'json'
4
4
  autoload :RestClient, 'rest_client'
@@ -6,5 +6,11 @@ autoload :Logger, 'logger'
6
6
 
7
7
  module LocaSMS
8
8
  autoload :Client, 'locasms/client'
9
+ autoload :Exception, 'locasms/exception'
10
+ autoload :Numbers, 'locasms/numbers'
9
11
  autoload :RestClient, 'locasms/rest_client'
12
+
13
+ module Helpers
14
+ autoload :DateTimeHelper, 'locasms/helpers/date_time_helper'
15
+ end
10
16
  end
@@ -10,19 +10,66 @@ describe LocaSMS::Client do
10
10
  end
11
11
 
12
12
  describe '#deliver' do
13
- before(:each) do
13
+ it 'Should send SMS' do
14
+ subject.should_receive(:numbers)
15
+ .once
16
+ .with([:a, :b, :c])
17
+ .and_return('XXX')
18
+
14
19
  rest_client.should_receive(:get)
15
20
  .once
16
- .with(:sendsms, msg: 'given message', numbers:'1188882222,5577770000')
21
+ .with(:sendsms, msg: 'given message', numbers:'XXX')
22
+
23
+ subject.deliver 'given message', :a, :b, :c
17
24
  end
18
25
 
19
- it{ subject.deliver 'given message', '1188882222,5577770000' }
20
- it{ subject.deliver 'given message', %w(1188882222 5577770000) }
21
- it{ subject.deliver 'given message', '1188882222', '5577770000' }
22
- it{ subject.deliver 'given message', ['1188882222', '5577770000'] }
26
+ it 'Should not send SMS' do
27
+ subject.should_receive(:numbers)
28
+ .once
29
+ .with([:a, :b, :c])
30
+ .and_raise(LocaSMS::Exception)
31
+
32
+ rest_client.should_receive(:get).never
33
+
34
+ lambda{ subject.deliver('given message', :a, :b, :c) }.should raise_error(LocaSMS::Exception)
35
+ end
23
36
  end
24
37
 
25
- it '#deliver_at'
38
+ describe '#deliver_at' do
39
+ it 'Should send SMS' do
40
+ subject.should_receive(:numbers)
41
+ .once
42
+ .with([:a, :b, :c])
43
+ .and_return('XXX')
44
+
45
+ LocaSMS::Helpers::DateTimeHelper.should_receive(:split)
46
+ .once
47
+ .with(:datetime)
48
+ .and_return(%w[date time])
49
+
50
+ rest_client.should_receive(:get)
51
+ .once
52
+ .with(:sendsms, msg: 'given message', numbers:'XXX', jobdate: 'date', jobtime: 'time')
53
+
54
+ subject.deliver_at 'given message', :datetime, :a, :b, :c
55
+ end
56
+
57
+ it 'Should not send SMS' do
58
+ subject.should_receive(:numbers)
59
+ .once
60
+ .with([:a, :b, :c])
61
+ .and_raise(LocaSMS::Exception)
62
+
63
+ LocaSMS::Helpers::DateTimeHelper.should_receive(:split)
64
+ .once
65
+ .with(:datetime)
66
+ .and_return(%w[date time])
67
+
68
+ rest_client.should_receive(:get).never
69
+
70
+ lambda{ subject.deliver_at('given message', :datetime, :a, :b, :c) }.should raise_error(LocaSMS::Exception)
71
+ end
72
+ end
26
73
 
27
74
  describe '#balance' do
28
75
  it 'Should check param assignment' do
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe LocaSMS::Helpers::DateTimeHelper do
4
+ subject { LocaSMS::Helpers::DateTimeHelper }
5
+
6
+ describe '#parse' do
7
+ it 'Should call the class method' do
8
+ subject.should_receive(:parse)
9
+ .once
10
+ .with(:value)
11
+
12
+ subject.new.parse(:value)
13
+ end
14
+ end
15
+
16
+ describe '#split' do
17
+ it 'Should call the class method' do
18
+ subject.should_receive(:split)
19
+ .once
20
+ .with(:value)
21
+
22
+ subject.new.split(:value)
23
+ end
24
+ end
25
+
26
+ describe '.parse' do
27
+ let(:expected) { Time.parse '1977-03-14 14:12:00' }
28
+
29
+ def try_for(value)
30
+ subject.parse(value) == expected
31
+ end
32
+
33
+ it { try_for DateTime.parse('1977-03-14 14:12:00') }
34
+ it { try_for Time.parse('1977-03-14 14:12:00') }
35
+ it { try_for '1977-03-14 14:12:00' }
36
+ it { try_for 227207520 }
37
+ end
38
+
39
+ describe '.split' do
40
+ it 'Should break a date into date and time' do
41
+ subject.should_receive(:parse)
42
+ .once
43
+ .with(:datetime)
44
+ .and_return(Time.parse('1977-03-14 14:12:00'))
45
+
46
+ subject.split(:datetime).should == %w(14/03/1977 14:12)
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe LocaSMS::Numbers do
4
+ subject { LocaSMS::Numbers.new }
5
+
6
+ describe '.initialize' do
7
+ subject do
8
+ LocaSMS::Numbers.any_instance
9
+ .should_receive(:evaluate)
10
+ .once
11
+ .with([:numbers])
12
+ .and_return({ good: [1,3], bad: [2,4] })
13
+ LocaSMS::Numbers.new :numbers
14
+ end
15
+
16
+ it{ subject.good.should == [1,3] }
17
+ it{ subject.bad.should == [2,4] }
18
+ end
19
+
20
+ describe '#normalize' do
21
+ it{ subject.normalize('+55 (11) 8888-9999').should == %w(551188889999) }
22
+ it{ subject.normalize('55', ['11', '22']).should == %w(55 11 22) }
23
+ it{ subject.normalize(['55', 'ZZ', '22']).should == %w(55 ZZ 22) }
24
+ it{ subject.normalize('55,44,33', ['ZZ', '22,11']).should == %w(55 44 33 ZZ 22 11) }
25
+ it{ subject.normalize(55, [11, 22]).should == %w(55 11 22) }
26
+ it{ subject.normalize('Z').should == ['Z'] }
27
+ it{ subject.normalize(nil).should == [] }
28
+ end
29
+
30
+ describe '#valid_number?' do
31
+ it{ subject.valid_number?('+55 (11) 8888-9999').should be_false }
32
+ it{ subject.valid_number?('88889999').should be_false }
33
+ it{ subject.valid_number?('988889999').should be_false }
34
+ it{ subject.valid_number?('ABC').should be_false }
35
+ it{ subject.valid_number?('').should be_false }
36
+ it{ subject.valid_number?(nil).should be_false }
37
+
38
+ it{ subject.valid_number?('1188889999').should be_true }
39
+ it{ subject.valid_number?('11988889999').should be_true }
40
+ end
41
+
42
+ describe '#evaluate' do
43
+ it 'Should separate numbers in good and bad' do
44
+ subject.should_receive(:normalize)
45
+ .once
46
+ .with([:numbers])
47
+ .and_return([:good, :bad])
48
+ subject.should_receive(:valid_number?)
49
+ .once
50
+ .with(:good)
51
+ .and_return(true)
52
+ subject.should_receive(:valid_number?)
53
+ .once
54
+ .with(:bad)
55
+ .and_return(false)
56
+ subject.evaluate(:numbers).should == { good: [:good], bad: [:bad] }
57
+ end
58
+ end
59
+
60
+ describe '#bad?' do
61
+ it{ subject.should_receive(:bad).once.and_return([ ]); subject.bad?.should be_false }
62
+ it{ subject.should_receive(:bad).once.and_return([1]); subject.bad?.should be_true }
63
+ end
64
+
65
+ describe '#to_s' do
66
+ it 'Should return and empty string' do
67
+ subject.to_s.should == ''
68
+ end
69
+
70
+ it 'Should return all good numbers in a string comma separated' do
71
+ subject.should_receive(:good)
72
+ .once
73
+ .and_return([1,2,3,4])
74
+ subject.to_s.should == '1,2,3,4'
75
+ end
76
+ end
77
+
78
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locasms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adilson Carvalho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-12 00:00:00.000000000 Z
11
+ date: 2013-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -181,10 +181,15 @@ files:
181
181
  - Rakefile
182
182
  - lib/locasms.rb
183
183
  - lib/locasms/client.rb
184
+ - lib/locasms/exception.rb
185
+ - lib/locasms/helpers/date_time_helper.rb
186
+ - lib/locasms/numbers.rb
184
187
  - lib/locasms/rest_client.rb
185
188
  - lib/locasms/version.rb
186
189
  - locasms.gemspec
187
190
  - spec/lib/locasms/client_spec.rb
191
+ - spec/lib/locasms/helpers/date_time_helper_spec.rb
192
+ - spec/lib/locasms/numbers_spec.rb
188
193
  - spec/lib/locasms/rest_client_spec.rb
189
194
  - spec/spec.opts
190
195
  - spec/spec_helper.rb
@@ -214,6 +219,8 @@ specification_version: 4
214
219
  summary: Cliente para o serviço de disparo de SMS da LocaSMS
215
220
  test_files:
216
221
  - spec/lib/locasms/client_spec.rb
222
+ - spec/lib/locasms/helpers/date_time_helper_spec.rb
223
+ - spec/lib/locasms/numbers_spec.rb
217
224
  - spec/lib/locasms/rest_client_spec.rb
218
225
  - spec/spec.opts
219
226
  - spec/spec_helper.rb