Class: Bormashino::Fetch

Inherits:
Object
  • Object
show all
Defined in:
lib/bormashino/fetch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, resolved_to:, init: {}, options: {}) ⇒ Fetch

Returns a new instance of Fetch.



9
10
11
12
13
14
# File 'lib/bormashino/fetch.rb', line 9

def initialize(resource:, resolved_to:, init: {}, options: {})
  @resource = resource
  @init = init
  @resolved_to = resolved_to
  @options = options
end

Instance Attribute Details

#initObject

Returns the value of attribute init.



7
8
9
# File 'lib/bormashino/fetch.rb', line 7

def init
  @init
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/bormashino/fetch.rb', line 7

def options
  @options
end

#resolved_toObject

Returns the value of attribute resolved_to.



7
8
9
# File 'lib/bormashino/fetch.rb', line 7

def resolved_to
  @resolved_to
end

#resourceObject

Returns the value of attribute resource.



7
8
9
# File 'lib/bormashino/fetch.rb', line 7

def resource
  @resource
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bormashino/fetch.rb', line 16

def run
  raise 'No mounted apps' unless Bormashino::Server.mounted?

  # rubocop:disable Style::DocumentDynamicEvalDefinition
  JS.eval <<-ENDOFEVAL
    fetch(
      #{@resource.to_json},
      #{@init.to_json}
    ).then((r) => {
      r.text().then((t) => {
        window.bormashino.request(
          'post',
          #{@resolved_to.to_json},
          'status=' + r.status +
          '&payload=' + encodeURIComponent(t) +
          '&options=' + #{CGI.escape(@options.to_json).to_json}
        )
      })
    })
  ENDOFEVAL
  # rubocop:enable Style::DocumentDynamicEvalDefinition
end