whatsapp 0.1.4 → 0.1.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
  SHA256:
3
- metadata.gz: 536440b28d75243958b82ea41651b44c34dee77dfbdeaa57ee0b6ad55b683836
4
- data.tar.gz: 04c56ea459763511156f581ec97b3aec10dea9d482144e2fc8560b362ad4b8c3
3
+ metadata.gz: 458e2786e1e1e5e98deadfdf790e536be4510dd1819d95dfe0e3e50ccc9c6714
4
+ data.tar.gz: 32861807fbb15d15b1ee9c88e64e8635d562542572cfb93eece0ffc6db798481
5
5
  SHA512:
6
- metadata.gz: c2d12f0270c4756b365455bbb4f418776304a6854a34ea10c02216630490b38487fed95668e35781d72d6a7119f46ee781620e7334dae71db693016ffc50b481
7
- data.tar.gz: 5a7efdd9178548d72470e2d0a3efe96543238c0b11b6aeed0720518f5ed6f02e915b518386d1c99cb763be0b748b23ed158001dd851b635ccd15da9add5a2c5d
6
+ metadata.gz: c28cec96255e41a21b72837d80664feb75a60cf1dfb640bfa304d0a582d740778d9f7bba80a66eaaca6ee974c47f62366a1f61ed49360c106dfc3ab622e8a14e
7
+ data.tar.gz: be9eb1faa74246b5e83fb824168d1d0a32871a527cbe76def638ee8a176ce096cd0925740d8df44497ce883c38ca00012fa641909d1d6d91e9d5e4d187bbe4fc
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+
5
+ module Whats
6
+ module Actions
7
+ class Login
8
+ PATH = "/v1/users/login"
9
+
10
+ def initialize
11
+ @user = Whats.configuration.user
12
+ @password = Whats.configuration.password
13
+ end
14
+
15
+ def token
16
+ return @token if valid?
17
+
18
+ full_path = "#{Whats.configuration.base_path}#{PATH}"
19
+
20
+ response = Typhoeus.post(
21
+ full_path,
22
+ headers: { "Authorization" => "Basic #{encoded_auth}" },
23
+ body: {}
24
+ )
25
+ update_atributes response
26
+
27
+ @token
28
+ end
29
+
30
+ private
31
+
32
+ def update_atributes(response)
33
+ if response.failure?
34
+ raise Whats::Errors::RequestError.new("API request error.", response)
35
+ end
36
+
37
+ response = JSON.parse response.body
38
+
39
+ @token = response["users"].first["token"]
40
+ @expires_after = response["users"].first["expires_after"]
41
+ end
42
+
43
+ def encoded_auth
44
+ Base64.encode64("#{@user}:#{@password}").chomp
45
+ end
46
+
47
+ def valid?
48
+ return false if @expires_after.nil?
49
+
50
+ @expires_after > Time.now
51
+ end
52
+ end
53
+ end
54
+ end
@@ -23,16 +23,18 @@ module Whats
23
23
 
24
24
  def payload
25
25
  {
26
- type: "hsm",
27
- recipient_type: "individual",
28
- to: wa_id,
29
26
  hsm: {
30
- namespace: namespace,
31
- element_name: element_name,
32
- fallback_lg: "pt",
33
- fallback_lc: "BR",
34
- localizable_params: params
35
- }
27
+ element_name: element_name,
28
+ language: {
29
+ code: :pt_BR,
30
+ policy: :deterministic
31
+ },
32
+ localizable_params: params,
33
+ namespace: namespace
34
+ },
35
+ recipient_type: :individual,
36
+ to: wa_id,
37
+ type: :hsm
36
38
  }
37
39
  end
38
40
  end
@@ -6,8 +6,8 @@ require "whats/actions/send_hsm_message"
6
6
 
7
7
  module Whats
8
8
  class Api
9
- def initialize(base_path)
10
- @base_path = base_path
9
+ def initialize
10
+ @base_path = Whats.configuration.base_path
11
11
  end
12
12
 
13
13
  def check_contacts(numbers)
@@ -17,6 +17,10 @@ module Whats
17
17
  def check_contact(number)
18
18
  response = check_contacts([number])
19
19
 
20
+ if response["errors"]
21
+ raise Whats::Errors::RequestError.new("WhatsApp error.", response)
22
+ end
23
+
20
24
  result = \
21
25
  response["contacts"].reduce({}) do |temp, hash|
22
26
  temp.merge(hash["input"] => hash)
@@ -44,7 +48,7 @@ module Whats
44
48
  attr_reader :base_path
45
49
 
46
50
  def client
47
- @client ||= Whats::Client.new(base_path)
51
+ @client ||= Whats::Client.new
48
52
  end
49
53
  end
50
54
  end
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "whats/actions/login"
4
+
3
5
  module Whats
4
6
  class Client
5
- def initialize(base_path)
6
- @base_path = base_path
7
+ def initialize(login = Whats::Actions::Login.new)
8
+ @base_path = Whats.configuration.base_path
9
+ @login = login
7
10
  end
8
11
 
9
12
  def request(path, payload)
@@ -11,7 +14,10 @@ module Whats
11
14
 
12
15
  response = Typhoeus.post(
13
16
  full_path,
14
- headers: { "Content-Type" => "application/json" },
17
+ headers: {
18
+ "Authorization" => "Bearer #{login.token}",
19
+ "Content-Type" => "application/json"
20
+ },
15
21
  body: payload.to_json
16
22
  )
17
23
 
@@ -22,6 +28,6 @@ module Whats
22
28
 
23
29
  private
24
30
 
25
- attr_reader :base_path
31
+ attr_reader :base_path, :login
26
32
  end
27
33
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Whats
4
+ class << self
5
+ attr_accessor :configuration
6
+ end
7
+
8
+ def self.configure
9
+ self.configuration ||= Configuration.new
10
+ yield(configuration)
11
+ end
12
+
13
+ class Configuration
14
+ attr_accessor :base_path, :user, :password
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Whats
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
@@ -5,6 +5,7 @@ require "json"
5
5
  require "typhoeus"
6
6
 
7
7
  # Source
8
+ require "whats/configuration"
8
9
  require "whats/version"
9
10
  require "whats/errors/request_error"
10
11
  require "whats/client"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whatsapp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Soares
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-05-14 00:00:00.000000000 Z
12
+ date: 2018-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus
@@ -188,10 +188,12 @@ extensions: []
188
188
  extra_rdoc_files: []
189
189
  files:
190
190
  - lib/whats/actions/check_contacts.rb
191
+ - lib/whats/actions/login.rb
191
192
  - lib/whats/actions/send_hsm_message.rb
192
193
  - lib/whats/actions/send_message.rb
193
194
  - lib/whats/api.rb
194
195
  - lib/whats/client.rb
196
+ - lib/whats/configuration.rb
195
197
  - lib/whats/errors/request_error.rb
196
198
  - lib/whats/version.rb
197
199
  - lib/whatsapp.rb
@@ -215,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
217
  version: '0'
216
218
  requirements: []
217
219
  rubyforge_project:
218
- rubygems_version: 2.7.6
220
+ rubygems_version: 2.7.7
219
221
  signing_key:
220
222
  specification_version: 4
221
223
  summary: WhatsApp Enterprise API interface.