twilio_mock 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
+ SHA1:
3
+ metadata.gz: 00a6a986657d167465ce4b98b08c5ba79ec1cc1d
4
+ data.tar.gz: 73923634aca40b7300487b27a731567c4897a318
5
+ SHA512:
6
+ metadata.gz: dbd3e06f261efb1aaa88ab2e70f80317a2de6a889980f86d7fdfbbdb68985bc29e3761771be4f163c82779c476628672747bb7e535525848048cc76dd1f15a6a
7
+ data.tar.gz: 7a16f1baf1dcf024c2af1d7e50c12784fa491a90a01273ff8f21dbd229c66da45d2c22b883f42d8eca92371f30b848c8b3395a1b69d81ddc5d939e7cebc6148a
data/lib/rest.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'twilio-ruby'
2
+ require 'twilio_mocker'
3
+
4
+ module Twilio
5
+ module REST
6
+ class Messages < ListResource
7
+ def create(attrs)
8
+ TwilioMocker.new.stub_create_message(attrs)
9
+ super(attrs)
10
+ end
11
+ end
12
+
13
+ class IncomingPhoneNumbers < ListResource
14
+ def create(attrs)
15
+ TwilioMocker.new.stub_buy_number(attrs)
16
+ super(attrs)
17
+ end
18
+ end
19
+
20
+ class AvailablePhoneNumbers < ListResource
21
+ def get(sid)
22
+ TwilioMocker.new.stub_available_numbers
23
+ super(sid)
24
+ end
25
+ end
26
+
27
+ module Lookups
28
+ class PhoneNumbers < NextGenListResource
29
+ def get(number, query={})
30
+ TwilioLookupMocker.new.stub_lookup(number)
31
+ super(number, query)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ require_relative './twilio_mocker'
2
+
3
+ class TwilioLookupMocker < TwilioMocker
4
+ API_VERSION = 'v1'.freeze
5
+ HOST = 'lookups.twilio.com'.freeze
6
+
7
+ def stub_lookup(number, country_code = nil)
8
+ stub_request(:get, "#{base_twilio_url}/PhoneNumbers/#{number}")
9
+ .with(headers: headers)
10
+ .to_return(status: 200, body: response(number, country_code), headers: {})
11
+ end
12
+
13
+ private
14
+
15
+ def response(number, country_code)
16
+ {
17
+ country_code: country_code.presence || 'US',
18
+ phone_number: number,
19
+ url: "#{base_twilio_url}/PhoneNumber/number"
20
+ }.to_json
21
+ end
22
+
23
+ def base_twilio_url
24
+ "https://#{HOST}/#{API_VERSION}"
25
+ end
26
+ end
@@ -0,0 +1,64 @@
1
+ require 'twilio-ruby'
2
+ require 'webmock'
3
+ require 'twilio_number_generator'
4
+
5
+ class TwilioMocker
6
+ include Twilio::REST::Utils
7
+
8
+ API_VERSION = '2010-04-01'.freeze
9
+ HOST = 'api.twilio.com'.freeze
10
+
11
+ def initialize
12
+ @username = Twilio.account_sid
13
+ @token = Twilio.auth_token
14
+ end
15
+
16
+ def stub_create_message(attrs)
17
+ prepare_stub(attrs, 'Messages.json')
18
+ end
19
+
20
+ def stub_available_numbers
21
+ response_numbers = {
22
+ sid: @username,
23
+ available_phone_numbers: [{ 'PhoneNumber' => TwilioNumberGenerator.instance.generate }]
24
+ }.to_json
25
+ stub_request(:get, "#{base_twilio_url}/AvailablePhoneNumbers/US/Local.json")
26
+ .with(headers: headers, basic_auth: basic_auth)
27
+ .to_return(status: 200, body: response_numbers, headers: {})
28
+ end
29
+
30
+ def stub_buy_number(attrs)
31
+ prepare_stub(attrs, 'IncomingPhoneNumbers.json')
32
+ end
33
+
34
+ private
35
+
36
+ def response
37
+ {
38
+ sid: @username
39
+ }.to_json
40
+ end
41
+
42
+ def headers
43
+ Twilio::REST::Client::HTTP_HEADERS
44
+ end
45
+
46
+ def basic_auth
47
+ [@username, @token]
48
+ end
49
+
50
+ def stub_request(method, url)
51
+ WebMock.stub_request(method, url)
52
+ end
53
+
54
+ def base_twilio_url
55
+ "https://#{HOST}/#{API_VERSION}/Accounts/#{@username}"
56
+ end
57
+
58
+ def prepare_stub(attrs, path)
59
+ body = twilify(attrs).map { |k, val| [k, val.to_s] }.to_h
60
+ stub_request(:post, "#{base_twilio_url}/#{path}")
61
+ .with(body: body, headers: headers, basic_auth: basic_auth)
62
+ .to_return(status: 200, body: response, headers: {})
63
+ end
64
+ end
@@ -0,0 +1,21 @@
1
+ class TwilioNumberGenerator
2
+ include Singleton
3
+
4
+ BASE_TEST_NUMBER = '+1500555'.freeze
5
+
6
+ attr_reader :available_numbers
7
+
8
+ def initialize
9
+ @available_numbers = []
10
+ end
11
+
12
+ def generate
13
+ number = loop do
14
+ number = "#{BASE_TEST_NUMBER}#{rand(9999).to_s.rjust(4, '0')}"
15
+ unless @available_numbers.include?(number)
16
+ @available_numbers << number
17
+ break number
18
+ end
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twilio_mock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maicol Bentancor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: twilio-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.13'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 4.13.0
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '4.13'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 4.13.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: webmock
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.1
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '3.0'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.0.1
67
+ - !ruby/object:Gem::Dependency
68
+ name: rspec
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '3.6'
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 3.6.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.6'
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 3.6.0
87
+ description: ock for the twilio gem using webmock
88
+ email:
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - lib/rest.rb
94
+ - lib/twilio_lookup_mocker.rb
95
+ - lib/twilio_mocker.rb
96
+ - lib/twilio_number_generator.rb
97
+ homepage:
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.10
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Mock for the twilio gem
121
+ test_files: []