Module: ForceDream::Http
- Defined in:
- lib/force_dream/http.rb
Overview
Thin wrapper over Ruby's standard-library Net::HTTP -- no external HTTP gem needed.
Defined Under Namespace
Classes: Result
Class Method Summary collapse
- .get(url, bearer: nil) ⇒ Object
- .get_result(url, bearer: nil) ⇒ Object
-
.parse(response) ⇒ Object
Returns the parsed JSON body directly (raising on a non-2xx status), matching the simplest, throwing style already used by the Kotlin/Java SDKs' main methods -- callers that need the real status without raising (delete-agent's real 404/403/200 are all meaningful) use get_result/post_result below instead.
- .post(url, body:, bearer: nil) ⇒ Object
- .post_result(url, body:, bearer: nil) ⇒ Object
- .safe_parse(body) ⇒ Object
Class Method Details
.get(url, bearer: nil) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/force_dream/http.rb', line 14 def get(url, bearer: nil) uri = URI(url) req = Net::HTTP::Get.new(uri) req['Authorization'] = "Bearer #{bearer}" if bearer response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') { |http| http.request(req) } parse(response) end |
.get_result(url, bearer: nil) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/force_dream/http.rb', line 44 def get_result(url, bearer: nil) uri = URI(url) req = Net::HTTP::Get.new(uri) req['Authorization'] = "Bearer #{bearer}" if bearer response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') { |http| http.request(req) } Result.new(status: response.code.to_i, json: safe_parse(response.body)) end |
.parse(response) ⇒ Object
Returns the parsed JSON body directly (raising on a non-2xx status), matching the simplest, throwing style already used by the Kotlin/Java SDKs' main methods -- callers that need the real status without raising (delete-agent's real 404/403/200 are all meaningful) use get_result/post_result below instead.
36 37 38 39 40 41 42 |
# File 'lib/force_dream/http.rb', line 36 def parse(response) json = safe_parse(response.body) status = response.code.to_i raise "HTTP #{status}: #{response.body}" unless (200..299).cover?(status) json end |
.post(url, body:, bearer: nil) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/force_dream/http.rb', line 22 def post(url, body:, bearer: nil) uri = URI(url) req = Net::HTTP::Post.new(uri) req['Content-Type'] = 'application/json' req['Authorization'] = "Bearer #{bearer}" if bearer req.body = body.to_json response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') { |http| http.request(req) } parse(response) end |
.post_result(url, body:, bearer: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/force_dream/http.rb', line 52 def post_result(url, body:, bearer: nil) uri = URI(url) req = Net::HTTP::Post.new(uri) req['Content-Type'] = 'application/json' req['Authorization'] = "Bearer #{bearer}" if bearer req.body = body.to_json response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') { |http| http.request(req) } Result.new(status: response.code.to_i, json: safe_parse(response.body)) end |
.safe_parse(body) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/force_dream/http.rb', line 62 def safe_parse(body) return {} if body.nil? || body.empty? JSON.parse(body) rescue JSON::ParserError {} end |