mock-twilio 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3ab342d529e8d1886517cdffe112de027d4faa800e999f1761d70cacf91da958
4
+ data.tar.gz: 42f3e99d12fe4b0e88160d52437a32c8cf20786abaebb32a4ad084579a223d4f
5
+ SHA512:
6
+ metadata.gz: a74c263e59ca0f944ef427387e60c6ec1d9a30e0e02d0583b1993171f286f58e3b6178cf9eed13fa0db68c88e2071f4512e6d18cbace5d04120c51c7bbb24759
7
+ data.tar.gz: b1782b5d6da621259f3b3d0633623b1c9cfed90993ff613f73485bf169f22317da1396f73ca1f7102fa7456261a8d2752272ac6f44f10c2452cc9f669b2660cb
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2024-05-29
2
+
3
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in mock-twilio.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Guillermo Quezada
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.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ ## Mock::Twilio::Client
2
+
3
+ This is a SchoolStatus implementation to mock twilio client to perform requests to [twilio-oai](https://github.com/twilio/twilio-oai)
4
+
5
+
6
+ # Installation
7
+
8
+ To install using [Bundler][bundler] grab the latest stable version:
9
+
10
+ ```ruby
11
+ gem 'mock-twilio', '~> 0.1.0'
12
+ ```
13
+
14
+ To manually install `twilio-ruby` via [Rubygems][rubygems] simply gem install:
15
+
16
+ ```bash
17
+ gem install mock-twilio
18
+ ```
19
+
20
+ ## Requirements
21
+ - [Twilio OAI](https://github.com/twilio/twilio-oai) and [More Info](https://www.twilio.com/en-us/blog/introducing-twilios-openapi-specification-ga)
22
+
23
+ OR
24
+
25
+ - `docker compose up twilio_mock_server` [SS Twilio Mock Server](https://github.com/schoolstatus/twilio_mock_server)
26
+
27
+ ## Defaults Prism
28
+
29
+ - `proxy_address = twilio_mock_server`
30
+ - `proxy_port = 4010`
31
+ - `proxy_protocol = http`
32
+
33
+ ## How to use
34
+
35
+ ```ruby
36
+ export TWILIO_ACCOUNT_SID=ACFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
37
+ export TWILIO_API_KEY=SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
38
+ export TWILIO_API_SECRET=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
39
+
40
+ mock_client = Mock::Twilio::Client.new
41
+ client = Twilio::REST::Client.new(nil, nil, nil, nil, mock_client)
42
+ client.messages.create(to: "+593978613041", body: "RB This is the ship that made the Kesssssel Run in fourteen parsecs?", from: "+13212855389")
43
+ ```
44
+
45
+ ## Swagger - OpenApi Issues
46
+
47
+ - Array and Boolean validations due open api formats, ie params as `status_callback_event`, `early_media`. Validations removed for them on json files.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Twilio
5
+ # mock_client = Mock::Twilio::Client.new
6
+ # client = Twilio::REST::Client.new(nil, nil, nil, nil, mock_client)
7
+ # client.messages.create(to: "+593978613041", body: "RB This is the ship that made the Kesssssel Run in fourteen parsecs?", from: "+13212855389")
8
+ class Client
9
+ attr_accessor :adapter
10
+ attr_reader :timeout, :last_response, :last_request
11
+
12
+ def initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, timeout: nil)
13
+ @proxy_prot = proxy_prot || 'http'
14
+ @proxy_addr = proxy_addr || 'twilio_mock_server'
15
+ @proxy_port = proxy_port || '4010'
16
+ @timeout = timeout
17
+ @adapter = Faraday.default_adapter
18
+ end
19
+
20
+ def _request(request)
21
+ @connection = Faraday.new(url: request.host + ":" + request.port.to_s, ssl: { verify: true }) do |f|
22
+ f.options.params_encoder = Faraday::FlatParamsEncoder
23
+ f.request :url_encoded
24
+ f.adapter @adapter
25
+ f.headers = request.headers
26
+ f.request(:authorization, :basic, request.auth[0], request.auth[1])
27
+ if @proxy_addr
28
+ f.proxy = "#{@proxy_prot}://#{@proxy_addr}:#{@proxy_port}"
29
+ end
30
+
31
+ f.use Mock::Twilio::Middleware::Proxy
32
+
33
+ f.options.open_timeout = request.timeout || @timeout
34
+ f.options.timeout = request.timeout || @timeout
35
+ end
36
+
37
+ @last_request = request
38
+ @last_response = nil
39
+ response = @connection.send(request.method.downcase.to_sym,
40
+ request.url,
41
+ request.method == "GET" ? request.params : request.data)
42
+
43
+ if response.body && !response.body.empty?
44
+ object = response.body
45
+ elsif response.status == 400
46
+ object = { message: "Bad request", code: 400 }.to_json
47
+ end
48
+
49
+ twilio_response = Mock::Twilio::Response.new(response.status, object, request, headers: response.headers)
50
+ @last_response = twilio_response
51
+
52
+ twilio_response
53
+ end
54
+
55
+ def request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil)
56
+ request = ::Twilio::Request.new(host, port, method, url, params, data, headers, auth, timeout)
57
+ _request(request)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Twilio
5
+ class Decorator
6
+ ENDPOINTS = {
7
+ api_2010: Mock::Twilio::Schemas::Api2010,
8
+ messaging_v1: Mock::Twilio::Schemas::MessagingV1,
9
+ }
10
+
11
+ class << self
12
+ def call(body, request)
13
+ body = JSON.parse(body)
14
+ schema = url_from(body, request)
15
+ # return body decorataor if needed
16
+ return ENDPOINTS[schema].decorate(body) if schema
17
+
18
+ body
19
+ end
20
+
21
+ def url_from(body, request)
22
+ url = request.url.split(request.host).last
23
+
24
+ case url
25
+ when %r{\/2010-04-01/Accounts/[A-Za-z0-9]+/}
26
+ :api_2010
27
+ when %r{\/v1/Services/[A-Za-z0-9]+/}
28
+ :messaging_v1
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ module Mock
6
+ module Twilio
7
+ module Middleware
8
+ class Proxy < Faraday::Middleware
9
+ def initialize(app)
10
+ super(app)
11
+ end
12
+
13
+ def call(env)
14
+ env.url.host = env.request.proxy.host
15
+ env.url.port = env.request.proxy.port
16
+ env.url.scheme = env.request.proxy.scheme
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ Faraday::Request.register_middleware(proxy: Mock::Twilio::Middleware::Proxy)
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Twilio
5
+ class Response
6
+ attr_accessor :status_code, :body, :headers
7
+
8
+ def initialize(status_code, body, request, headers: nil)
9
+ @status_code = status_code
10
+ body = '{}' if !body || body.empty?
11
+ @body = Mock::Twilio::Decorator.call(body, request)
12
+ @headers = !headers ? {} : headers.to_hash
13
+ end
14
+
15
+ def to_s
16
+ "[#{status_code}] #{body}"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Twilio
5
+ module Schemas
6
+ class Api2010
7
+ class << self
8
+ PAGES_KEYS = [
9
+ "end",
10
+ "first_page_uri",
11
+ "next_page_uri",
12
+ "last_page_uri",
13
+ "page",
14
+ "page_size",
15
+ "previous_page_uri",
16
+ "total",
17
+ "num_pages",
18
+ "start",
19
+ "uri"
20
+ ].freeze
21
+
22
+ def decorate(body)
23
+ body["date_updated"] = Time.current.rfc2822 if body["date_updated"]
24
+ body["date_sent"] = Time.current.rfc2822 if body["date_sent"]
25
+ body["date_created"] = Time.current.rfc2822 if body["date_created"]
26
+ body["start_time"] = Time.current.rfc2822 if body["start_time"]
27
+ body["end_time"] = Time.current.rfc2822 if body["end_time"]
28
+
29
+ pagination(body) if body["available_phone_numbers"]
30
+ body
31
+ end
32
+
33
+ def pagination(body)
34
+ # Params returned in mock_server but not on real twilio request for the moment.
35
+ # Not needed for us now.
36
+ PAGES_KEYS.each do |key|
37
+ body.delete(key) if body[key]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Twilio
5
+ module Schemas
6
+ class MessagingV1
7
+ class << self
8
+ def decorate(body)
9
+ # Params for twilio pagination, needed for twilio-ruby serializers and absolute paths
10
+ body["meta"]["key"] = "phone_numbers" if body["meta"]
11
+ body["meta"]["page_size"] = 20 if body["meta"]
12
+ body["meta"]["first_page_url"] = "https://messaging.twilio.com/v1/Services/MGFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF/PhoneNumbers?PageSize=20&Page=0" if body["meta"]
13
+ body["meta"]["previous_page_url"] = "https://messaging.twilio.com/v1/Services/MGFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF/PhoneNumbers?PageSize=20&Page=0" if body["meta"]
14
+ body["meta"]["next_page_url"] = "https://messaging.twilio.com/v1/Services/MGFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF/PhoneNumbers?PageSize=20&Page=1" if body["meta"]
15
+ body["meta"]["url"] = "https://messaging.twilio.com/v1/Services/MGFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF/PhoneNumbers?PageSize=20&Page=0" if body["meta"]
16
+ body
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Twilio
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "twilio/middleware/proxy"
4
+ require_relative "twilio/schemas/api_2010"
5
+ require_relative "twilio/schemas/messaging_v1"
6
+ require_relative "twilio/client"
7
+ require_relative "twilio/decorator"
8
+ require_relative "twilio/response"
9
+ require_relative "twilio/version"
10
+
11
+ module Mock
12
+ module Twilio
13
+ class Error < StandardError; end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ module Mock
2
+ module Twilio
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mock-twilio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - SchoolStatus Platform Team
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ description: This repository contains Mock::Twilio::Client for Twilio's API.
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - CHANGELOG.md
34
+ - Gemfile
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - lib/mock/twilio.rb
39
+ - lib/mock/twilio/client.rb
40
+ - lib/mock/twilio/decorator.rb
41
+ - lib/mock/twilio/middleware/proxy.rb
42
+ - lib/mock/twilio/response.rb
43
+ - lib/mock/twilio/schemas/api_2010.rb
44
+ - lib/mock/twilio/schemas/messaging_v1.rb
45
+ - lib/mock/twilio/version.rb
46
+ - sig/mock/twilio.rbs
47
+ homepage: https://github.com/schoolstatus/mock-twilio
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://github.com/schoolstatus/mock-twilio
52
+ source_code_uri: https://github.com/schoolstatus/mock-twilio.
53
+ changelog_uri: https://github.com/schoolstatus/mock-twilio/CHANGELOG.md
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.6.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.4.19
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: This repository contains Mock::Twilio::Client for Twilio's API.
73
+ test_files: []