Class: FriendlyCaptcha::HttpCall
- Inherits:
-
Object
- Object
- FriendlyCaptcha::HttpCall
- Defined in:
- lib/friendly_captcha/http_call.rb
Constant Summary collapse
- Errors =
[ ::SocketError, ::SystemCallError, ::Timeout::Error, ::EOFError, ::IOError, ::Net::OpenTimeout, ::Net::HTTPBadResponse, ::Net::HTTPHeaderSyntaxError, ::Net::ProtocolError, ::Net::ReadTimeout, ::Net::WriteTimeout, ::OpenSSL::SSL::SSLError, ::URI::InvalidURIError ]
Instance Method Summary collapse
Instance Method Details
#call(url:, body:, **options) ⇒ Object
25 26 27 28 29 |
# File 'lib/friendly_captcha/http_call.rb', line 25 def call(url:, body:, **) Try[*Errors] { perform(url, body, **) }.to_result.bind do |response| Success(response) end end |
#perform(url, body) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/friendly_captcha/http_call.rb', line 31 def perform(url, body) uri = URI(url) = { use_ssl: uri.scheme.eql?('https'), open_timeout: 500, read_timeout: 500 } ::Net::HTTP.start(uri.host, uri.port, ) do |http| request = ::Net::HTTP::Post.new(uri, { 'Content-Type' => 'application/json' }) request.body = body.to_json response = http.request(request) [Integer(response.code, 10), response.body] end end |