whatsapp 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6dc5df79a0df03527f9af0b731cab5aa86d4555d82c622719abe3d5a57d30f6a
4
+ data.tar.gz: 9917d7853f64b765ee69f7fdd768c268f1cf2c1ffd2eed4fb54db93f74233a9b
5
+ SHA512:
6
+ metadata.gz: 919cf020ce02a91dca4bed6fa3070f126e0afd6c12015d19a92577e16d719e3897fe3674dfc3ea6de8a97b49480698b3f2b7df953b2216139b4ea68761117294
7
+ data.tar.gz: 53487ff2c96ffc761c48b3b31f843ce65c7f0f9e157b530b89fc8fbfdd85987ae44dbdde6aa837aa7983b1aa1da7a536cd458a7dba99bc58be4f5fd9ec401498
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in whatsapp.gemspec
6
+ gemspec
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ whatsapp (0.1.0)
5
+ typhoeus
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
14
+ diff-lcs (1.3)
15
+ ethon (0.11.0)
16
+ ffi (>= 1.3.0)
17
+ ffi (1.9.23)
18
+ hashdiff (0.3.7)
19
+ public_suffix (3.0.2)
20
+ rake (10.5.0)
21
+ rspec (3.7.0)
22
+ rspec-core (~> 3.7.0)
23
+ rspec-expectations (~> 3.7.0)
24
+ rspec-mocks (~> 3.7.0)
25
+ rspec-core (3.7.1)
26
+ rspec-support (~> 3.7.0)
27
+ rspec-expectations (3.7.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.7.0)
30
+ rspec-mocks (3.7.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.7.0)
33
+ rspec-support (3.7.1)
34
+ safe_yaml (1.0.4)
35
+ typhoeus (1.3.0)
36
+ ethon (>= 0.9.0)
37
+ webmock (3.3.0)
38
+ addressable (>= 2.3.6)
39
+ crack (>= 0.3.2)
40
+ hashdiff
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ bundler (~> 1.16)
47
+ rake (~> 10.0)
48
+ rspec (~> 3.0)
49
+ webmock
50
+ whatsapp!
51
+
52
+ BUNDLED WITH
53
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Bruno Soares
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # WhatsApp
2
+
3
+ An ruby interface to WhatsApp Enterprise API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "whatsapp"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Instantiation
16
+
17
+ Create an instance of the API client passing the base path of your endpoint:
18
+
19
+ ```ruby
20
+ whats = Whats::Api.new("https://my-whatsapp-endpoint.com")
21
+ ```
22
+
23
+ ### Check Contacts
24
+
25
+ Take a look [here](https://developers.facebook.com/docs/whatsapp/check-contacts) (WhatsApp Check Contacts doc) for more information.
26
+
27
+ ```ruby
28
+ whats.check_contacts(["+5511942424242"])
29
+
30
+ # output:
31
+ {
32
+ "meta" => {
33
+ "waent version" => "2.18.4"
34
+ },
35
+ "payload" => {
36
+ "results" => [
37
+ {
38
+ "input_number" => "+5511942424242",
39
+ "wa_exists" => true,
40
+ "wa_username" => "5511942424242"
41
+ }
42
+ ],
43
+ "total" => 1
44
+ },
45
+ "error" => false
46
+ }
47
+ ```
48
+
49
+ ### Send Message
50
+
51
+ Take a look [here](https://developers.facebook.com/docs/whatsapp/send-api) (WhatsApp Send Message doc) for more information.
52
+
53
+ *The first parameter is the WhatsApp **username**!*
54
+
55
+ ```ruby
56
+ whats.send_message("5511942424242", "Message goes here.")
57
+
58
+ # output:
59
+
60
+ {
61
+ "meta" => {
62
+ "waent version" => "2.18.4"
63
+ },
64
+ "payload" => {
65
+ "message_id" => "BAEC4D1D7549842627"
66
+ },
67
+ "error" => false
68
+ }
69
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "whatsapp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Whats
4
+ module Actions
5
+ class CheckContacts
6
+ PATH = "/api/check_contacts.php"
7
+
8
+ def initialize(client, numbers)
9
+ @client = client
10
+ @numbers = numbers
11
+ end
12
+
13
+ def call
14
+ client.request PATH, payload
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :client, :numbers
20
+
21
+ def payload
22
+ {
23
+ payload: {
24
+ blocking: "wait",
25
+ users: numbers
26
+ }
27
+ }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Whats
4
+ module Actions
5
+ class SendHsmMessage
6
+ PATH = "/api/rest_send.php"
7
+
8
+ def initialize(client, username, namespace, element_name, params)
9
+ @client = client
10
+ @username = username
11
+ @namespace = namespace
12
+ @element_name = element_name
13
+ @params = params
14
+ end
15
+
16
+ def call
17
+ client.request PATH, payload
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :client, :username, :namespace, :element_name, :params
23
+
24
+ def payload
25
+ {
26
+ payload: {
27
+ to: username,
28
+ hsm: {
29
+ namespace: namespace,
30
+ element_name: element_name,
31
+ localizable_params: params
32
+ }
33
+ }
34
+ }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Whats
4
+ module Actions
5
+ class SendMessage
6
+ PATH = "/api/rest_send.php"
7
+
8
+ def initialize(client, username, body)
9
+ @client = client
10
+ @username = username
11
+ @body = body
12
+ end
13
+
14
+ def call
15
+ client.request PATH, payload
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :client, :username, :body
21
+
22
+ def payload
23
+ {
24
+ payload: {
25
+ to: username,
26
+ body: body
27
+ }
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "whats/actions/check_contacts"
4
+ require "whats/actions/send_message"
5
+ require "whats/actions/send_hsm_message"
6
+
7
+ module Whats
8
+ class Api
9
+ def initialize(base_path)
10
+ @base_path = base_path
11
+ end
12
+
13
+ def check_contacts(numbers)
14
+ Actions::CheckContacts.new(client, numbers).call
15
+ end
16
+
17
+ def check_contact(number)
18
+ response = check_contacts([number])
19
+
20
+ result = \
21
+ response["payload"]["results"].reduce({}) do |temp, hash|
22
+ temp.merge(hash["input_number"] => hash)
23
+ end
24
+
25
+ result[number]
26
+ end
27
+
28
+ def send_message(username, body)
29
+ Actions::SendMessage.new(client, username, body).call
30
+ end
31
+
32
+ def send_hsm_message(username, namespace, element_name, params)
33
+ Actions::SendHsmMessage.new(
34
+ client,
35
+ username,
36
+ namespace,
37
+ element_name,
38
+ params
39
+ ).call
40
+ end
41
+
42
+ private
43
+
44
+ attr_reader :base_path
45
+
46
+ def client
47
+ @client ||= Whats::Client.new(base_path)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Whats
4
+ class Client
5
+ def initialize(base_path)
6
+ @base_path = base_path
7
+ end
8
+
9
+ def request(path, payload)
10
+ full_path = "#{base_path}#{path}"
11
+
12
+ response = Typhoeus.post(
13
+ full_path,
14
+ headers: { "Content-Type" => "application/json" },
15
+ body: payload.to_json
16
+ )
17
+
18
+ unless response.success?
19
+ raise Whats::Errors::RequestError.new("API request error.", response)
20
+ end
21
+
22
+ JSON.parse(response.response_body)
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :base_path
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Whats
4
+ module Errors
5
+ class RequestError < StandardError
6
+ attr_reader :response
7
+
8
+ def initialize(message, response)
9
+ @response = response
10
+
11
+ super message
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Whats
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "whats/version"
4
+ require "whats/errors/request_error"
5
+ require "whats/client"
6
+ require "whats/api"
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require "whats/version"
8
+
9
+ Gem::Specification.new do |spec|
10
+ spec.name = "whatsapp"
11
+ spec.version = Whats::VERSION
12
+ spec.authors = ["Bruno Soares", "GetNinjas"]
13
+ spec.email = ["bruno@bsoares.com", "tech@getninjas.com.br"]
14
+
15
+ spec.description = spec.summary = "An interface to WhatsApp Enterprise API."
16
+ spec.homepage = "https://github.com/getninjas/whatsapp"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "webmock"
30
+
31
+ spec.add_dependency "typhoeus"
32
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whatsapp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Soares
8
+ - GetNinjas
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2018-03-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.16'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.16'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: webmock
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: typhoeus
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: An interface to WhatsApp Enterprise API.
85
+ email:
86
+ - bruno@bsoares.com
87
+ - tech@getninjas.com.br
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/whats/actions/check_contacts.rb
102
+ - lib/whats/actions/send_hsm_message.rb
103
+ - lib/whats/actions/send_message.rb
104
+ - lib/whats/api.rb
105
+ - lib/whats/client.rb
106
+ - lib/whats/errors/request_error.rb
107
+ - lib/whats/version.rb
108
+ - lib/whatsapp.rb
109
+ - whatsapp.gemspec
110
+ homepage: https://github.com/getninjas/whatsapp
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.3
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: An interface to WhatsApp Enterprise API.
134
+ test_files: []