locasms 0.1.2 → 0.1.4

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: b416a1dbc0a2828144adc3cad4f46f648febe3eb
4
- data.tar.gz: 91c5669394594303a92d79ada598e96e2075198b
3
+ metadata.gz: f3206ae592ae0a88e7f3622634cfc9eb3bfa1f61
4
+ data.tar.gz: c183953bd011a3b8515fee13e4d35e59219d3f43
5
5
  SHA512:
6
- metadata.gz: c5d7e98110b2a257ec038716bbef0923fb49a9afc40edc84cbbe6a5ccd0bfebeaf5e9574219cf66ce329a36a7ad2083e786bf513d27c1212f31d031d7cc87327
7
- data.tar.gz: 6e83d47e413f43a918f507e73b8130d8f0998c4b5caf3cdd43d86b1f67fe5964b68aba4baa9a75f6d0965199cd030487235ee167b43382a6d9605502a67732be
6
+ metadata.gz: 3aa9723d090e9549008084b59217db867dd5584ead9e2b6b54f137c4f0b17af73d010236bbe6ea480828c42b1e818280e410fdeaace6e0fe4be821f04004cd36
7
+ data.tar.gz: 4d46ad710feacdf3eae061b1c3a69cdf48754ea50c4c48150b66b75a67cf1ded264988d1f100a81d9a180b0b14b61653cae3752c5cfc27d866ca3b74db1503af
data/README.md CHANGED
@@ -23,7 +23,7 @@ Simple example:
23
23
  ```ruby
24
24
  require 'locasms'
25
25
 
26
- cli = LocaSMS::Client.new login: 'LOGIN', password: 'PASSWORD'
26
+ cli = LocaSMS::Client.new 'LOGIN', 'PASSWORD'
27
27
 
28
28
  # delivering message to one mobile
29
29
  cli.deliver 'my message', '1155559999'
@@ -24,7 +24,7 @@ module LocaSMS
24
24
  # @return [String] campaign id on success
25
25
  # @raise [LocaSMS::Exception] if bad numbers were given
26
26
  def deliver(message, *mobiles)
27
- rest.get :sendsms, msg: message, numbers: numbers(mobiles)
27
+ rest.get(:sendsms, msg: message, numbers: numbers(mobiles))['data']
28
28
  end
29
29
 
30
30
  # Schedule the send of a message to one or more mobiles
@@ -35,34 +35,60 @@ module LocaSMS
35
35
  # @raise [LocaSMS::Exception] if bad numbers were given
36
36
  def deliver_at(message, datetime, *mobiles)
37
37
  date, time = Helpers::DateTimeHelper.split datetime
38
- rest.get :sendsms, msg: message, numbers: numbers(mobiles), jobdate: date, jobtime: time
38
+ rest.get(:sendsms, msg: message, numbers: numbers(mobiles), jobdate: date, jobtime: time)['data']
39
39
  end
40
40
 
41
41
  # Get de current amount of sending credits
42
42
  # @return [Fixnum] returns the balance on success
43
43
  def balance
44
- rest.get :getbalance
44
+ rest.get(:getbalance)['data']
45
45
  end
46
46
 
47
47
  # Gets the current status of the given campaign
48
48
  # @param [String] id campaign id
49
- # @return UNDEF
49
+ # @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 }
50
50
  def campaign_status(id)
51
- rest.get :getstatus, id: id
51
+ response = rest.get(:getstatus, id: id)
52
+ begin
53
+ CSV.new(response['data'] || '', col_sep: ';', quote_char: '"').map do |delivery_id, _, enqueue_time, _, delivery_time, _, status, _, _, carrier, mobile_number, _, message|
54
+ status = if status =~ /aguardando envio/i
55
+ :waiting
56
+ elsif status =~ /sucesso/i
57
+ :success
58
+ elsif status =~ /numero invalido|nao cadastrado/i
59
+ :invalid
60
+ else
61
+ :unknown
62
+ end
63
+
64
+ {
65
+ campaign_id: id,
66
+ delivery_id: delivery_id,
67
+ enqueue_time: enqueue_time,
68
+ delivery_time: delivery_time,
69
+ status: status,
70
+ carrier: carrier,
71
+ mobile_number: mobile_number,
72
+ message: message
73
+ }
74
+ end
75
+ rescue
76
+ raise Exception.new 'Invalid delivery response data'
77
+ end
52
78
  end
53
79
 
54
80
  # Holds the given campaign to fire
55
81
  # @param [String] id campaign id
56
82
  # @return [TrueClass,FalseClass] returns true on success
57
83
  def campaign_hold(id)
58
- rest.get :holdsms, id: id
84
+ rest.get(:holdsms, id: id)['data']
59
85
  end
60
86
 
61
87
  # Restart firing the given campaign
62
88
  # @param [String] id campaign id
63
89
  # @return [TrueClass,FalseClass] returns true on success
64
90
  def campaign_release(id)
65
- rest.get :releasesms, id: id
91
+ rest.get(:releasesms, id: id)['data']
66
92
  end
67
93
 
68
94
  private
@@ -1,6 +1,36 @@
1
1
  module LocaSMS
2
2
 
3
+ # Common base exception
3
4
  class Exception < ::Exception
5
+ attr_reader :raw, :action
6
+
7
+ def initialize(data = {})
8
+ @raw = data[:raw]
9
+ @action = data[:data]
10
+
11
+ super data[:message] || default_message
12
+ end
13
+
14
+ private
15
+
16
+ def default_message
17
+ nil
18
+ end
19
+ end
20
+
21
+ # Raised when asked for an invalid operation
22
+ # @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
+ def default_message
25
+ 'Invalid Operation'
26
+ end
27
+ end
28
+
29
+ # Raised when the given credentials are invalid
30
+ class InvalidLogin < Exception
31
+ def default_message
32
+ 'Invalid Login'
33
+ end
4
34
  end
5
35
 
6
36
  end
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+
1
3
  module LocaSMS
2
4
 
3
5
  # Class that handle http calls to LocaSMS api
@@ -16,25 +18,29 @@ module LocaSMS
16
18
  # Performs a GET call for an action
17
19
  # @param [String, Symbol] action the given action to perform
18
20
  # @param [Hash] params given parameters to send
19
- # @return [String]
21
+ # @return [Hash] json parsed response
20
22
  #
21
23
  # @example Calling with no extra parameters
22
24
  #
23
25
  # client = LocaSMS::RestClient('http://localhost:3000', lgn: 'LOGIN', pwd: 'PASSWORD')
24
26
  # # => GET http://localhost:3000?lgn=LOGIN&pws=PASSWORD&action=getballance
25
27
  # client.get :getballance
28
+ # # => {"status"=>1,"data"=>341,"msg"=>nil}
26
29
  #
27
30
  # @example Calling with extra parameters
28
31
  #
29
32
  # client = LocaSMS::RestClient('http://localhost:3000', lgn: 'LOGIN', pwd: 'PASSWORD')
30
33
  # # => GET http://localhost:3000?lgn=LOGIN&pws=PASSWORD&action=holdsms&id=345678
31
34
  # client.get :holdsms, id: 345678
35
+ # # => {"status"=>1,"data"=>nil,"msg"=>"SUCESSO"}
32
36
  #
33
37
  # @see https://github.com/mcorp/locasms/wiki/A-API-de-envio#lista-das-a%C3%A7%C3%B5es-dispon%C3%ADveis List of avaiable actions
38
+ # @raise [LocaSMS::InvalidOperation] when asked for an invalid operation
39
+ # @raise [LocaSMS::InvalidLogin] when the given credentials are invalid
34
40
  def get(action, params={})
35
41
  params = params_for action, params
36
42
  response = ::RestClient.get base_url, params: params
37
- JSON.parse(response) rescue response
43
+ parse_response(action, response)
38
44
  end
39
45
 
40
46
  # Composes the parameters hash
@@ -52,6 +58,23 @@ module LocaSMS
52
58
  def params_for(action, params={})
53
59
  {action: action}.merge(base_params).merge(params)
54
60
  end
61
+
62
+ # Parses a result trying to get it in json
63
+ # @param [String, Symbol] action the given action to perform
64
+ # @param [String] response body
65
+ # @return [Hash] json parsed response
66
+ # @raise [LocaSMS::InvalidOperation] when asked for an invalid operation
67
+ # @raise [LocaSMS::InvalidLogin] when the given credentials are invalid
68
+ def parse_response(action, response)
69
+ raise InvalidOperation.new(action: action) if response =~ /^0:OPERACAO INVALIDA$/i
70
+
71
+ j = JSON.parse(response) rescue { 'status' => 1, 'data' => response, 'msg' => nil }
72
+
73
+ return j if j['status'] == 1 or action == :getstatus
74
+
75
+ raise InvalidLogin.new(action: action) if j['msg'] =~ /^falha ao realizar login$/i
76
+ raise Exception.new(message: j['msg'], raw: response, action: action)
77
+ end
55
78
  end
56
79
 
57
80
  end
@@ -1,3 +1,3 @@
1
1
  module LocaSMS
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/locasms.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'locasms/version'
2
2
 
3
+ autoload :CSV, 'csv'
3
4
  autoload :JSON, 'json'
4
5
  autoload :RestClient, 'rest_client'
5
6
  autoload :Logger, 'logger'
@@ -19,6 +19,7 @@ describe LocaSMS::Client do
19
19
  rest_client.should_receive(:get)
20
20
  .once
21
21
  .with(:sendsms, msg: 'given message', numbers:'XXX')
22
+ .and_return({})
22
23
 
23
24
  subject.deliver 'given message', :a, :b, :c
24
25
  end
@@ -50,6 +51,7 @@ describe LocaSMS::Client do
50
51
  rest_client.should_receive(:get)
51
52
  .once
52
53
  .with(:sendsms, msg: 'given message', numbers:'XXX', jobdate: 'date', jobtime: 'time')
54
+ .and_return({})
53
55
 
54
56
  subject.deliver_at 'given message', :datetime, :a, :b, :c
55
57
  end
@@ -76,6 +78,7 @@ describe LocaSMS::Client do
76
78
  rest_client.should_receive(:get)
77
79
  .once
78
80
  .with(:getbalance)
81
+ .and_return({})
79
82
 
80
83
  subject.balance
81
84
  end
@@ -92,6 +95,7 @@ describe LocaSMS::Client do
92
95
  rest_client.should_receive(:get)
93
96
  .once
94
97
  .with(rest_method, id: '12345')
98
+ .and_return({})
95
99
 
96
100
  subject.send method, '12345'
97
101
  end
@@ -99,6 +103,8 @@ describe LocaSMS::Client do
99
103
  it{ check_for :campaign_status }
100
104
  it{ check_for :campaign_hold }
101
105
  it{ check_for :campaign_release }
106
+
107
+ it 'Should have tests to cover campaign_status csv result'
102
108
  end
103
109
 
104
110
  end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe LocaSMS::RestClient do
4
-
5
4
  describe '.initialize' do
6
5
  context 'When giving proper initialization parameters' do
7
6
  subject { LocaSMS::RestClient.new :url, :params }
@@ -21,4 +20,28 @@ describe LocaSMS::RestClient do
21
20
  it{ subject.params_for(:action, p1: 10).should == {action: :action, b1: 'X', p1: 10} }
22
21
  end
23
22
 
23
+ describe '#parse_response' do
24
+ subject { LocaSMS::RestClient.new :url, :params }
25
+
26
+ it 'Should raise exception on invalid operation' do
27
+ lambda{ subject.parse_response(:action, '0:OPERACAO INVALIDA') }.should raise_error(LocaSMS::InvalidOperation)
28
+ end
29
+
30
+ it 'Should raise exception on a failed response' do
31
+ lambda{ subject.parse_response(:action, '{"status":0,"data":null,"msg":"FALHA EPICA"}') }.should raise_error(LocaSMS::Exception, 'FALHA EPICA')
32
+ end
33
+
34
+ it 'Should raise exception on a failed login attempt' do
35
+ lambda{ subject.parse_response(:action, '{"status":0,"data":null,"msg":"FALHA AO REALIZAR LOGIN"}') }.should raise_error(LocaSMS::InvalidLogin)
36
+ end
37
+
38
+ it 'Should return the non-json value as a json' do
39
+ subject.parse_response(:action, 'non-json return').should == {'status' => 1, 'data' => 'non-json return', "msg" => nil}
40
+ end
41
+
42
+ it 'Should return a parsed json return' do
43
+ subject.parse_response(:action, '{"status":1,"data":28,"msg":null}').should == {'status' => 1, 'data' => 28, "msg" => nil}
44
+ end
45
+ end
46
+
24
47
  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.2
4
+ version: 0.1.4
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-14 00:00:00.000000000 Z
11
+ date: 2013-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler