Class: SFML::Network::Http
- Inherits:
-
Object
- Object
- SFML::Network::Http
- Defined in:
- lib/sfml/network/http.rb
Overview
CSFML’s tiny HTTP/1.x client. Useful when an SFML 3 game wants to talk to a leaderboard / asset server without pulling in ‘Net::HTTP`. For anything more involved (TLS, redirects, streaming, retries, JSON), Ruby’s stdlib ‘Net::HTTP` is the better tool — this wrapper exists for parity with CSFML, not because we recommend it.
http = SFML::Network::Http.new("http://localhost", port: 8080)
resp = http.send_request(method: :get, uri: "/")
resp.status #=> 200
resp.body #=> "Hello world\n"
‘send_request` accepts:
method: :get / :post / :head / :put / :delete (default :get)
uri: path string (default "/")
fields: Hash of header name → value
body: String body (POST/PUT)
http_version: [major, minor] (default [1, 0])
timeout: SFML::Time or seconds (default 0 = no timeout)
Defined Under Namespace
Classes: Response
Constant Summary collapse
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#initialize(host, port: 0) ⇒ Http
constructor
A new instance of Http.
- #send_request(method: :get, uri: "/", fields: nil, body: nil, http_version: DEFAULT_VERSION, timeout: DEFAULT_TIMEOUT) ⇒ Object
Constructor Details
#initialize(host, port: 0) ⇒ Http
Returns a new instance of Http.
26 27 28 29 30 31 32 |
# File 'lib/sfml/network/http.rb', line 26 def initialize(host, port: 0) ptr = C::Network.sfHttp_create raise Error, "sfHttp_create returned NULL" if ptr.null? @handle = FFI::AutoPointer.new(ptr, C::Network.method(:sfHttp_destroy)) C::Network.sfHttp_setHost(@handle, host.to_s, Integer(port)) end |
Instance Attribute Details
#handle ⇒ Object (readonly)
:nodoc:
61 62 63 |
# File 'lib/sfml/network/http.rb', line 61 def handle @handle end |
Instance Method Details
#send_request(method: :get, uri: "/", fields: nil, body: nil, http_version: DEFAULT_VERSION, timeout: DEFAULT_TIMEOUT) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sfml/network/http.rb', line 34 def send_request(method: :get, uri: "/", fields: nil, body: nil, http_version: DEFAULT_VERSION, timeout: DEFAULT_TIMEOUT) request_ptr = C::Network.sfHttpRequest_create raise Error, "sfHttpRequest_create returned NULL" if request_ptr.null? begin method_idx = C::Network::HTTP_METHODS.index(method) || raise(ArgumentError, "Unknown HTTP method: #{method.inspect} " \ "(expected one of #{C::Network::HTTP_METHODS.inspect})") C::Network.sfHttpRequest_setMethod(request_ptr, method_idx) C::Network.sfHttpRequest_setUri(request_ptr, uri.to_s) C::Network.sfHttpRequest_setHttpVersion(request_ptr, Integer(http_version[0]), Integer(http_version[1])) C::Network.sfHttpRequest_setBody(request_ptr, body.to_s) if body fields&.each_pair do |name, value| C::Network.sfHttpRequest_setField(request_ptr, name.to_s, value.to_s) end t = timeout.is_a?(Time) ? timeout : Time.seconds(timeout.to_f) response_ptr = C::Network.sfHttp_sendRequest(@handle, request_ptr, t.to_native) raise Error, "sfHttp_sendRequest returned NULL" if response_ptr.null? Response.send(:_take_ownership, response_ptr) ensure C::Network.sfHttpRequest_destroy(request_ptr) end end |