Module: Bakong::Khqr::Helpers::Http
- Defined in:
- lib/bakong/khqr/helpers/http.rb
Overview
Net::HTTP wrapper for the Bakong Open API. Pure stdlib — no Faraday, HTTParty, or gem dependency. Errors normalize into Bakong::Khqr::Error so callers can catch a single exception type.
Constant Summary collapse
- DEFAULT_TIMEOUT_SECONDS =
45
Class Method Summary collapse
Class Method Details
.parse_json(body) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/bakong/khqr/helpers/http.rb', line 38 def parse_json(body) return {} if body.nil? || body.empty? JSON.parse(body, symbolize_names: true) rescue JSON::ParserError {} end |
.post_json(url, payload, timeout: DEFAULT_TIMEOUT_SECONDS) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bakong/khqr/helpers/http.rb', line 21 def post_json(url, payload, timeout: DEFAULT_TIMEOUT_SECONDS) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == "https") http.open_timeout = timeout http.read_timeout = timeout request = Net::HTTP::Post.new(uri.request_uri) request["Content-Type"] = "application/json" request.body = JSON.generate(payload) response = http.request(request) parse_json(response.body) rescue Net::OpenTimeout, Net::ReadTimeout, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH raise Error.from(ERROR_CODES[:CONNECTION_TIMEOUT]) end |