zenvia-rb 0.0.2 → 0.0.3

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
2
  SHA1:
3
- metadata.gz: b5aa70cb21355907f090155c6b338a53762c679e
4
- data.tar.gz: 142fbd5fc352c3ae22ddb5282e32364e20d57616
3
+ metadata.gz: 6a05678bd530a4dea308034e8ade55e4af28d5e8
4
+ data.tar.gz: e900b5dd9ad6b073a8aebf811db0def346ad7c5e
5
5
  SHA512:
6
- metadata.gz: 005c4bd586f8064531939d02dd9e71f2f4ca462ec83b0dc5e1437bd5978a3009af45d2ea71133db4b6ee2d9000d888033a0099d4edbf5623995ad41b0636ef7f
7
- data.tar.gz: c43d285e01a59ad293a9635c2b3d7491f108fbbeae1c0fcc58e9de6267bf3eecccbb03c5b4e13ac79f66af2fb4e9431d44fbe1ffde98616881276bf3830be039
6
+ metadata.gz: 1bf7c83dab5d9b7d7047956f73d87ed16fafaa5e3160a270645307fa5757f930a7093d74a90d16f8cb00e019d1d28ac68fb3a709530fba7b26c289e8508a02a7
7
+ data.tar.gz: 1b15cc1ad049bb88456ae119c6c8a0ad3e1ade210e7ab4fcdf3404ab7265ec1a0722234150a6014d895f1ff67fe6425682e4f2df02532918ab45ad4c1277565f
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.1'
8
+ gem 'zenvia-rb', '~> 0.0.2'
9
9
  ```
10
10
 
11
11
  And then execute:
@@ -20,25 +20,38 @@ HTTParty is the only dependency for this gem.
20
20
 
21
21
  ## Usage
22
22
 
23
+ ### Configuration
23
24
  In your script
24
25
  ```ruby
26
+
27
+ Zenvia.configure {|config|
28
+ config.account = account_given_by_zenvia
29
+ config.code = code_given_by_zenvia
30
+ config.name = user_or_enterprise_name # optional
31
+ }
32
+
33
+ ```
34
+
35
+ ### Usage
36
+ ```ruby
25
37
  require 'zenvia'
26
38
 
27
- sms = Zenvia.new(USER, CODE)
28
- sms.send_message(FROM, NUMBER, TEXT, DELAY=0)
39
+ # if from is nil, the sender name will be set as config.name (as above)
40
+ # if you prefer a definitely nil sender, you can set from = ''
41
+
42
+ sms = Zenvia::SMS.new(from, number, message)
43
+ sms.send_message
29
44
  ```
30
45
 
31
46
  That's all ;)
32
47
 
33
48
  ## Development
34
49
 
35
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
-
37
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
51
 
39
52
  ## Contributing
40
53
 
41
- Bug reports and pull requests are welcome on GitHub at https://github.com/jeferson/zenvia
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jefersonhuan/zenvia-rb
42
55
 
43
56
 
44
57
  ## License
@@ -0,0 +1,21 @@
1
+ require 'base64'
2
+ class Zenvia
3
+ module Config
4
+ attr_accessor :account, :code
5
+
6
+ def self.account; @account; end
7
+
8
+ def self.account=(account); @account = account; end
9
+
10
+ def self.code=(code); @code = code; end
11
+
12
+ def self.code; @code; end
13
+
14
+ def self.name=(name); @name = name; end
15
+
16
+ def self.name; @name; end
17
+
18
+ private
19
+ def self.auth; Base64.encode64("#{@account}:#{@code}").strip; end
20
+ end
21
+ end
data/lib/zenvia/sms.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ class Zenvia::SMS
5
+ attr_writer :from, :number, :message
6
+
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 'The number must be composed with just numbers' 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
21
+
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
+ )
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  class Zenvia
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/zenvia.rb CHANGED
@@ -1,52 +1,13 @@
1
- require "zenvia/version"
2
- require 'httparty'
3
- require 'base64'
4
- require 'json'
5
-
6
1
  class Zenvia
7
- # initialize Zenvia class with user and code, both given from Zenvia (the corp)
8
- def initialize(user, code)
9
- return puts 'User and code must no be nil' if user.nil? or code.nil?
10
- # such as done in shell (echo -n user:code | base64), the strip method removes '\n' from the generated code
11
- @auth = Base64.encode64("#{user}:#{code}").strip
2
+ def self.configure
3
+ yield Config
12
4
  end
13
5
 
14
- attr_writer :auth
15
-
16
- # send the message itself, from: user or enterprise name, number: receiver number, message: text
17
- # delay: given in seconds to determinate
18
- def send_message(from, number, message, delay = 0)
19
- return puts 'The receiver must not be nil' if number.nil?
20
- return puts 'The sender cannot be nil' if from.nil?
21
- number = number.to_s unless number.class.eql? String
22
- return puts 'The number must have just numbers!' unless /\d*/.match(number)
23
- response = send_sms(from, number, message, delay)
24
- response = JSON.parse(response.body)
25
- puts 'Response: ' + response['sendSmsResponse']['statusDescription']
26
- end
27
-
28
- private
29
- # todo add multiple sms
30
- def send_sms(from, number, message, delay)
31
- # due to timezone, I decided to use strftime in order to send the message instantly
32
- schedule = (Time.now + delay).strftime("%Y-%m-%dT%H:%M:%S")
33
- number.insert(0, '55') unless /^55/.match(number)
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
- schedule: schedule,
41
- msg: message,
42
- callbackOption: 'NONE'
43
- }
44
- }.to_json,
45
- headers: {
46
- 'Content-Type' => 'application/json',
47
- 'Authorization' => "Basic #{@auth}",
48
- 'Accept' => 'application/json'
49
- }
50
- )
6
+ def self.config
7
+ Config
51
8
  end
52
9
  end
10
+
11
+ require "zenvia/version"
12
+ require 'zenvia/config'
13
+ require 'zenvia/sms'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zenvia-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeferson Huan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-29 00:00:00.000000000 Z
11
+ date: 2015-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,6 +68,8 @@ files:
68
68
  - bin/console
69
69
  - bin/setup
70
70
  - lib/zenvia.rb
71
+ - lib/zenvia/config.rb
72
+ - lib/zenvia/sms.rb
71
73
  - lib/zenvia/version.rb
72
74
  - zenvia-rb.gemspec
73
75
  homepage: https://github.com/jefersonhuan/zenvia-rb