zenvia-rb 0.0.4 → 0.0.5

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: 45630c99fb6c3b92ef041b075aa4604ff1510686
4
- data.tar.gz: 35f252225ca1536648befa7e3b8df79979c79fb7
3
+ metadata.gz: dd4ed3e11aa13c235c4032f290bc96d36c89fb13
4
+ data.tar.gz: be275be2d47c4c31089cc4458c8af979d4ea6e76
5
5
  SHA512:
6
- metadata.gz: 4ff7e3fe6960b1ade586035710a36e6e238fb787258e2738c3e7e1b4db3b403a920eea2f33aba39eb248153ba1ea9b9cf1efb81c5da311e97eb9cd0924ffc431
7
- data.tar.gz: 025e845ec65683c865641df226e06e3b9b4ec9cda86cfd89d1dd9a803a48d47ad93e09dd3eee69daf9b3dcd3d26bac16eee92553c8bde8225b162faf5cd5d7c0
6
+ metadata.gz: c202e68f27e82bad7795d58f7a5739c6ec7d7848d03d969412c4879b19740b4aca7024a130f6fe082eac6ade4ec4b0012305233991598c01a7b79ede4ea4c9f0
7
+ data.tar.gz: 9d97a88d09bde51510570baa0cfaad74924236ac3e245dcc49f579cb57a5e50eb3a92d6fbeafb6f4cff6d659f9466a2ec42a6c990dee0bbe5f0aaad4e1613f46
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Add this line to your application's Gemfile:
6
6
 
7
7
  ```ruby
8
- gem 'zenvia-rb', '~> 0.0.3'
8
+ gem 'zenvia-rb', '~> 0.0.5'
9
9
  ```
10
10
 
11
11
  And then execute:
@@ -19,24 +19,18 @@ HTTParty is the only dependency for this gem.
19
19
 
20
20
  ## Usage
21
21
 
22
- ### Configuration
23
22
  In your script
23
+
24
24
  ```ruby
25
25
  require 'zenvia'
26
+
26
27
  Zenvia.configure {|config|
27
28
  config.account = account_given_by_zenvia
28
29
  config.code = code_given_by_zenvia
29
- config.name = user_or_enterprise_name # optional
30
+ config.from = user_or_enterprise_name # optional
30
31
  }
31
32
 
32
- ```
33
-
34
- ### Usage
35
- ```ruby
36
- # if from is nil, the sender name will be set as config.name (as above)
37
- # if you prefer a definitely nil sender, you can set from = ''
38
- sms = Zenvia::SMS.new(from, number, message)
39
- sms.send_message
33
+ Zenvia.send_message(from = config.from, number, message)
40
34
  ```
41
35
 
42
36
  That's all ;)
@@ -1,13 +1,18 @@
1
- class Zenvia
1
+ require "zenvia/version"
2
+ require 'zenvia/config'
3
+ require 'zenvia/sms'
4
+
5
+ module Zenvia
6
+ # block of configuration
2
7
  def self.configure
3
- yield Config
8
+ yield self.config
4
9
  end
5
10
 
6
11
  def self.config
7
12
  Config
8
13
  end
9
- end
10
14
 
11
- require "zenvia/version"
12
- require 'zenvia/config'
13
- require 'zenvia/sms'
15
+ def self.send_message(from = nil, number, message)
16
+ SMS.send_message(from, number, message)
17
+ end
18
+ end
@@ -1,7 +1,7 @@
1
1
  require 'base64'
2
- class Zenvia
3
- module Config
4
- attr_accessor :account, :code
2
+ module Zenvia
3
+ class Config
4
+ attr_accessor :account, :code, :name
5
5
 
6
6
  def self.account; @account; end
7
7
 
@@ -11,9 +11,9 @@ class Zenvia
11
11
 
12
12
  def self.code; @code; end
13
13
 
14
- def self.name=(name); @name = name; end
14
+ def self.from=(from); @from = from; end
15
15
 
16
- def self.name; @name; end
16
+ def self.from; @from; end
17
17
 
18
18
  private
19
19
  def self.auth; Base64.encode64("#{@account}:#{@code}").strip; end
@@ -1,48 +1,52 @@
1
1
  require 'httparty'
2
2
  require 'json'
3
3
 
4
- class Zenvia::SMS
5
- attr_writer :from, :number, :message
4
+ module Zenvia
5
+ class SMS
6
+ attr_writer :from, :number, :message
6
7
 
7
- # initiate SMS object with
8
- # from: user or enterprise name, number: receiver number, message: text
9
- def initialize(from = nil, number, message)
10
- @from = from.nil? ? Zenvia.config.name : from
11
- @number = number
12
- exit puts 'letters and other special chars are not accepted in number parameter' unless /^\d*$/.match(@number)
13
- @message = message
14
- end
15
-
16
- def send_message
17
- response = send_sms
18
- response = JSON.parse(response.body)
19
- puts response['sendSmsResponse']['detailDescription']
20
- end
8
+ # function to send the message
9
+ # from: user or enterprise name, number: receiver number, message: text
10
+ def self.send_message(from = nil, number, message)
11
+ begin
12
+ @from = from.nil? ? Zenvia.config.from : from
13
+ @number = number
14
+ @message = message
15
+ response = self.send_sms
16
+ # todo improve returning message with auth error
17
+ response = JSON.parse(response.body)
18
+ puts response['sendSmsResponse']['detailDescription']
19
+ rescue => e
20
+ puts 'Error!'
21
+ raise e
22
+ end
23
+ end
21
24
 
22
- private
23
- def send_sms
24
- # convert number to string (if isn't yet) and insert the country code (standard: BR, 55)
25
- # if not found
26
- @number = @number.to_s unless @number.class.eql? String
27
- @number.insert(0, '55') unless /^55/.match(@number)
28
- # retrieve auth value set in Config class
29
- @auth = Zenvia.config.auth
30
- # Zenvia api's endpoint to send sms
31
- endpoint = 'https://api-rest.zenvia360.com.br/services/send-sms'
32
- HTTParty.post(endpoint,
33
- body: {
34
- sendSmsRequest: {
35
- from: @from,
36
- to: @number,
37
- msg: @message,
38
- callbackOption: 'NONE'
39
- }
40
- }.to_json,
41
- headers: {
42
- 'Content-Type' => 'application/json',
43
- 'Authorization' => "Basic #{@auth}",
44
- 'Accept' => 'application/json'
45
- }
46
- )
25
+ private
26
+ def self.send_sms
27
+ # convert number to string (if isn't yet) and insert the country code (standard: BR, 55)
28
+ # if not found
29
+ @number = @number.to_s unless @number.class.eql? String
30
+ @number.insert(0, '55') unless /^55/.match(@number)
31
+ # retrieve auth value set in Config class
32
+ @auth = Zenvia.config.auth
33
+ # Zenvia api's endpoint to send sms
34
+ endpoint = 'https://api-rest.zenvia360.com.br/services/send-sms'
35
+ HTTParty.post(endpoint,
36
+ body: {
37
+ sendSmsRequest: {
38
+ from: @from,
39
+ to: @number,
40
+ msg: @message,
41
+ callbackOption: 'NONE'
42
+ }
43
+ }.to_json,
44
+ headers: {
45
+ 'Content-Type' => 'application/json',
46
+ 'Authorization' => "Basic #{@auth}",
47
+ 'Accept' => 'application/json'
48
+ }
49
+ )
50
+ end
47
51
  end
48
52
  end
@@ -1,3 +1,3 @@
1
- class Zenvia
2
- VERSION = "0.0.4"
1
+ module Zenvia
2
+ VERSION = "0.0.5"
3
3
  end
@@ -14,8 +14,6 @@ Gem::Specification.new do |spec|
14
14
  spec.homepage = "https://github.com/jefersonhuan/zenvia-rb"
15
15
  spec.license = "MIT"
16
16
 
17
- # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
- # delete this section to allow pushing this gem to any host.
19
17
  if spec.respond_to?(:metadata)
20
18
  spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
19
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zenvia-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeferson Huan