Class: Cloudflare::DurableObjectRequest
- Inherits:
-
Object
- Object
- Cloudflare::DurableObjectRequest
- Defined in:
- lib/cloudflare_workers/durable_object.rb
Overview
The incoming ‘request` argument passed to DO handlers. `body_text` is pre-awaited by the JS dispatcher because Ruby runs synchronously under Opal (same pattern as `Rack::Handler::CloudflareWorkers.call`).
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#js_request ⇒ Object
readonly
Returns the value of attribute js_request.
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#headers ⇒ Object
Shallow header Hash with lowercased keys.
-
#initialize(js_request, body_text = '') ⇒ DurableObjectRequest
constructor
A new instance of DurableObjectRequest.
- #json ⇒ Object
- #method ⇒ Object
- #path ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(js_request, body_text = '') ⇒ DurableObjectRequest
Returns a new instance of DurableObjectRequest.
523 524 525 526 |
# File 'lib/cloudflare_workers/durable_object.rb', line 523 def initialize(js_request, body_text = '') @js_request = js_request @body = body_text.to_s end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
521 522 523 |
# File 'lib/cloudflare_workers/durable_object.rb', line 521 def body @body end |
#js_request ⇒ Object (readonly)
Returns the value of attribute js_request.
521 522 523 |
# File 'lib/cloudflare_workers/durable_object.rb', line 521 def js_request @js_request end |
Instance Method Details
#[](name) ⇒ Object
561 562 563 |
# File 'lib/cloudflare_workers/durable_object.rb', line 561 def [](name) headers[name.to_s.downcase] end |
#headers ⇒ Object
Shallow header Hash with lowercased keys.
553 554 555 556 557 558 559 |
# File 'lib/cloudflare_workers/durable_object.rb', line 553 def headers return @headers if @headers h = {} js = @js_request `(#{js} && #{js}.headers && typeof #{js}.headers.forEach === 'function') && #{js}.headers.forEach(function(v, k) { #{h}.$store(String(k).toLowerCase(), String(v)); })` @headers = h end |
#json ⇒ Object
565 566 567 568 569 |
# File 'lib/cloudflare_workers/durable_object.rb', line 565 def json JSON.parse(body) rescue JSON::ParserError {} end |
#method ⇒ Object
528 529 530 531 |
# File 'lib/cloudflare_workers/durable_object.rb', line 528 def method js = @js_request `(#{js} ? String(#{js}.method || 'GET') : 'GET')` end |
#path ⇒ Object
538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'lib/cloudflare_workers/durable_object.rb', line 538 def path u = url return '' if u.nil? || u.empty? begin # Extract pathname via URL() so relative paths aren't mangled. u_str = u `new URL(#{u_str}).pathname` rescue StandardError # Fallback regex — strip scheme+host, keep everything up to ?/#. m = u.match(%r{\Ahttps?://[^/]+([^?#]*)}) m ? m[1] : u end end |
#url ⇒ Object
533 534 535 536 |
# File 'lib/cloudflare_workers/durable_object.rb', line 533 def url js = @js_request `(#{js} ? String(#{js}.url || '') : '')` end |