mock-bandwidth 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b94456bdd96b95f64866379b3c9a06326b225d2e2f3e6bbce188e526348c506
4
- data.tar.gz: 8dcff047c29e53d2b2b79e93ac3fb7a930443549144e883f5b259aa3bf2ac04f
3
+ metadata.gz: 2b742dbbaa7cd5d4e98837e9e22fcc9d77d79d9ae229d5eec50a95e2c7dab60e
4
+ data.tar.gz: 8069867b44a7efdaa02443c36faf107217d7ec248c7602a464bf848a3038d5ea
5
5
  SHA512:
6
- metadata.gz: 01e64367e88e041c8c213fb41b3ab73cd13fe8d5a5d41dcfb3a890ec236258355eee0726109d72c13d6d3da6b49d8ef634e201ed7ed3bfb559e4040ed6941eaa
7
- data.tar.gz: f2846ce3140cbe36fdff50b602e025c558153a1e63326b09bdfcda5ff4f903ba5a4f028706d101612018f6c7996e99a56212704e110edf42e18c133c4d60af32
6
+ metadata.gz: 9a42e5f65b56c7b78b29f2d45ba22bf2e6057739dddb0f7ba10b3f2be962896d1107cb53f61d58dff9ed80a9c25bf74d6c4bae1d7520d35a8634b1f2204a1ab5
7
+ data.tar.gz: cb3960362fb2f7d31760720a15354a488f985a4a5a8a1d305541388550cee7246e928eccb660e6c87b567e173338824c8d9ddefc3dfa2f4b43ea52af72d0a8e5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## [1.1.0] - 2025-01-29
2
+ - Add webhooks messages status updates support
3
+
1
4
  ## [1.0.0] - 2025-01-25
2
5
 
3
6
  - Add create message support
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Mock::Bandwidth
2
+ [![Gem Version](https://badge.fury.io/rb/mock-bandwidth.svg)](https://badge.fury.io/rb/mock-bandwidth)
3
+ ![mock-bandwidth](https://github.com/schoolstatus/mock-bandwidth/actions/workflows/ruby.yml/badge.svg)
2
4
 
3
- This is a SchoolStatus implementation to mock Bandwidth to perform requests to [bandwidth-oai](https://docs.stoplight.io/docs/prism/83dbbd75532cf-http-mocking)
5
+ This is a SchoolStatus implementation to mock Bandwidth-sdk to perform requests to [bandwidth-oai](https://docs.stoplight.io/docs/prism/83dbbd75532cf-http-mocking)
4
6
 
5
7
 
6
8
  # Installation
@@ -13,6 +15,7 @@ gem install mock-bandwidth
13
15
 
14
16
  ## Requirements
15
17
  - [Bandwidth-oai](https://docs.stoplight.io/docs/prism/83dbbd75532cf-http-mocking)
18
+ - [Bandwidth-sdk](https://github.com/Bandwidth/ruby-sdk)
16
19
 
17
20
  ## Defaults Prism
18
21
 
@@ -26,6 +29,12 @@ gem install mock-bandwidth
26
29
  | ------------- | ------------- |
27
30
  | :white_check_mark: | `Bandwidth::MessagesApi.new.create_message(account_id, body)` |
28
31
 
32
+ ## Trigger resources updates
33
+
34
+ | Mock::Bandwidth::Webhooks | Support |
35
+ | ------------- | ------------- |
36
+ | `Webhooks::Messages` | :white_check_mark: |
37
+
29
38
 
30
39
  ## How to use
31
40
  Initializer sample
@@ -39,6 +48,10 @@ Bandwidth.configure do |config| # Configure Basic Auth
39
48
  connection.use Mock::Bandwidth::Middleware::Proxy
40
49
  end
41
50
  end
51
+
52
+ Mock::Bandwidth.configure do |config|
53
+ config.webhook_message_status_url = "http://my-server.com/webhooks/bandwidth/messages_updates"
54
+ end
42
55
  ```
43
56
 
44
57
  Example
@@ -15,15 +15,24 @@ module Mock
15
15
  body["text"] = data["text"] if body["text"]
16
16
  body["applicationId"] = data["applicationId"] if body["applicationId"]
17
17
 
18
- body["id"] = message_id if body["id"]
18
+ body["id"] = message_id(body) if body["id"]
19
19
 
20
20
  body.to_json
21
21
  end
22
22
 
23
- def message_id
23
+ def message_id(body)
24
24
  timestamp = (Time.now.to_f * 1000).to_i.to_s[0..12]
25
25
  random_part = SecureRandom.alphanumeric(15).downcase
26
- "#{timestamp}#{random_part}"
26
+ id = "#{timestamp}#{random_part}"
27
+ scheduler = Rufus::Scheduler.new
28
+ scheduler.in '2s' do
29
+ begin
30
+ Mock::Bandwidth::Webhooks::Messages.trigger(id, body)
31
+ rescue => e
32
+ puts e
33
+ end
34
+ end
35
+ id
27
36
  end
28
37
  end
29
38
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Bandwidth
5
+ module Util
6
+ class Configuration
7
+ attr_accessor :webhook_message_status_url
8
+
9
+ def webhook_message_status_url=(value)
10
+ @webhook_message_status_url = value
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Bandwidth
5
+ class ErrorHandler
6
+ def initialize(response)
7
+ @response = response
8
+ end
9
+
10
+ def raise
11
+ @response.body
12
+ end
13
+ end
14
+ end
15
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mock
4
4
  module Bandwidth
5
- VERSION = "1.0.0"
5
+ VERSION = "1.1.0"
6
6
  end
7
7
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Bandwidth
5
+ class WebhookClient
6
+ attr_reader :url, :params, :headers
7
+
8
+ def initialize(url:, params: nil, headers: {})
9
+ @url = url
10
+ @params = params
11
+ @headers = headers
12
+ end
13
+
14
+ def post
15
+ arguments = { body: params, headers: headers }
16
+
17
+ ::HTTParty.post(url, **arguments)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Bandwidth
5
+ module Webhooks
6
+ class RestError < StandardError
7
+ end
8
+ class Base
9
+ DELAY = [0.5, 0.8]
10
+
11
+ def self.webhook_client
12
+ Mock::Bandwidth::WebhookClient
13
+ end
14
+
15
+ def self.headers
16
+ { "Content-Type" => "application/json" }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Bandwidth
5
+ module Webhooks
6
+ class Messages < Base
7
+ def self.trigger(id, body)
8
+ # Wait simulation from twilio
9
+ sleep DELAY.sample
10
+
11
+ url = Mock::Bandwidth.webhook_message_status_url
12
+
13
+ params = {
14
+ _json: [
15
+ {
16
+ time: Time.current.rfc2822,
17
+ type: "message-delivered",
18
+ to: body["to"],
19
+ description: "Message delivered to carrier.",
20
+ message: {
21
+ id: id,
22
+ owner: body["from"],
23
+ applicationId: "12341234",
24
+ time: Time.current.rfc2822,
25
+ segmentCount: 1,
26
+ direction: "out",
27
+ to: [
28
+ body["to"]
29
+ ],
30
+ from: body["from"],
31
+ text: body["text"],
32
+ tag: "",
33
+ },
34
+ }
35
+ ],
36
+ }.to_json
37
+
38
+ response = webhook_client.new(url: url, params: params, headers: headers).post
39
+
40
+ case response.code.to_i
41
+ when 200..204
42
+ response
43
+ when 400..600
44
+ raise Webhooks::RestError, Mock::Twilio::ErrorHandler.new(response).raise
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,12 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "rufus-scheduler"
4
+ require "active_support"
5
+ require "active_support/core_ext/time"
6
+ require "httparty"
3
7
  require_relative "bandwidth/version"
4
8
  require_relative "bandwidth/middleware/proxy"
5
9
  require_relative "bandwidth/response"
6
10
  require_relative "bandwidth/decorator"
11
+ require_relative "bandwidth/webhook_client"
12
+ require_relative "bandwidth/webhooks/base"
13
+ require_relative "bandwidth/webhooks/messages"
14
+ require_relative "bandwidth/util/error_handler"
15
+ require_relative "bandwidth/util/configuration"
7
16
 
8
17
  module Mock
9
18
  module Bandwidth
10
- class Error < StandardError; end
19
+ extend SingleForwardable
20
+
21
+ def_delegators :configuration, :webhook_message_status_url
22
+
23
+ def self.configure(&block)
24
+ yield configuration
25
+ end
26
+
27
+ def self.configuration
28
+ @configuration ||= Util::Configuration.new
29
+ end
30
+
31
+ private_class_method :configuration
11
32
  end
12
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock-bandwidth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Guillermo Quezada
7
+ - SchoolStatus Platform Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-27 00:00:00.000000000 Z
11
+ date: 2025-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -44,6 +44,48 @@ dependencies:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: '14.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rufus-scheduler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.9.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 3.9.1
61
+ - !ruby/object:Gem::Dependency
62
+ name: activesupport
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 6.0.0
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 6.0.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: httparty
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.22.0
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.22.0
47
89
  description: This repository contains Mock::Bandwidth for Bandwidth's API.
48
90
  email:
49
91
  - meemocol@gmail.com
@@ -61,7 +103,12 @@ files:
61
103
  - lib/mock/bandwidth/middleware/proxy.rb
62
104
  - lib/mock/bandwidth/response.rb
63
105
  - lib/mock/bandwidth/schemas/messaging_v2.rb
106
+ - lib/mock/bandwidth/util/configuration.rb
107
+ - lib/mock/bandwidth/util/error_handler.rb
64
108
  - lib/mock/bandwidth/version.rb
109
+ - lib/mock/bandwidth/webhook_client.rb
110
+ - lib/mock/bandwidth/webhooks/base.rb
111
+ - lib/mock/bandwidth/webhooks/messages.rb
65
112
  - sig/mock/bandwidth.rbs
66
113
  homepage: https://github.com/schoolstatus/mock-bandwidth
67
114
  licenses:
@@ -85,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
132
  - !ruby/object:Gem::Version
86
133
  version: '0'
87
134
  requirements: []
88
- rubygems_version: 3.1.4
135
+ rubygems_version: 3.1.6
89
136
  signing_key:
90
137
  specification_version: 4
91
138
  summary: This repository contains Mock::Bandwidth and Webhooks for Bandwidth's API.