Class: Mock::Twilio::Client
- Inherits:
-
Object
- Object
- Mock::Twilio::Client
- Defined in:
- lib/mock/twilio/client.rb
Overview
mock_client = Mock::Twilio::Client.new client = Twilio::REST::Client.new(nil, nil, nil, nil, mock_client) client.messages.create(to: “+593978613041”, body: “RB This is the ship that made the Kesssssel Run in fourteen parsecs?”, from: “+13212855389”)
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#last_request ⇒ Object
readonly
Returns the value of attribute last_request.
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
-
#proxy_addr ⇒ Object
readonly
Returns the value of attribute proxy_addr.
-
#proxy_port ⇒ Object
readonly
Returns the value of attribute proxy_port.
-
#proxy_prot ⇒ Object
readonly
Returns the value of attribute proxy_prot.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #_request(request) ⇒ Object
-
#initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, timeout: nil) ⇒ Client
constructor
A new instance of Client.
- #request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Object
Constructor Details
#initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, timeout: nil) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 |
# File 'lib/mock/twilio/client.rb', line 12 def initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, timeout: nil) @proxy_prot = proxy_prot || 'http' @proxy_addr = proxy_addr || 'twilio_mock_server' @proxy_port = proxy_port || '4010' @timeout = timeout @adapter = Faraday.default_adapter end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
9 10 11 |
# File 'lib/mock/twilio/client.rb', line 9 def adapter @adapter end |
#last_request ⇒ Object (readonly)
Returns the value of attribute last_request.
10 11 12 |
# File 'lib/mock/twilio/client.rb', line 10 def last_request @last_request end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
10 11 12 |
# File 'lib/mock/twilio/client.rb', line 10 def last_response @last_response end |
#proxy_addr ⇒ Object (readonly)
Returns the value of attribute proxy_addr.
10 11 12 |
# File 'lib/mock/twilio/client.rb', line 10 def proxy_addr @proxy_addr end |
#proxy_port ⇒ Object (readonly)
Returns the value of attribute proxy_port.
10 11 12 |
# File 'lib/mock/twilio/client.rb', line 10 def proxy_port @proxy_port end |
#proxy_prot ⇒ Object (readonly)
Returns the value of attribute proxy_prot.
10 11 12 |
# File 'lib/mock/twilio/client.rb', line 10 def proxy_prot @proxy_prot end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/mock/twilio/client.rb', line 10 def timeout @timeout end |
Instance Method Details
#_request(request) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mock/twilio/client.rb', line 20 def _request(request) @connection = Faraday.new(url: request.host + ":" + request.port.to_s, ssl: { verify: true }) do |f| f..params_encoder = Faraday::FlatParamsEncoder f.request :url_encoded f.adapter @adapter f.headers = request.headers f.request(:authorization, :basic, request.auth[0], request.auth[1]) if @proxy_addr f.proxy = "#{@proxy_prot}://#{@proxy_addr}:#{@proxy_port}" end f.use Mock::Twilio::Middleware::Proxy f..open_timeout = request.timeout || @timeout f..timeout = request.timeout || @timeout end @last_request = request @last_response = nil response = @connection.send(request.method.downcase.to_sym, request.url, request.method == "GET" ? request.params : request.data) if response.body && !response.body.empty? object = response.body elsif response.status == 400 object = { message: "Bad request", code: 400 }.to_json end twilio_response = Mock::Twilio::Response.new(response.status, object, request, headers: response.headers) @last_response = twilio_response twilio_response end |
#request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) ⇒ Object
55 56 57 58 |
# File 'lib/mock/twilio/client.rb', line 55 def request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) request = ::Twilio::Request.new(host, port, method, url, params, data, headers, auth, timeout) _request(request) end |