Class: WebFetch::Promise

Inherits:
Object
  • Object
show all
Defined in:
lib/web_fetch/promise.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ Promise

Returns a new instance of Promise.



7
8
9
10
11
# File 'lib/web_fetch/promise.rb', line 7

def initialize(client, options = {})
  @client = client
  @uid = options.fetch(:uid)
  @request = Request.from_hash(options.fetch(:request))
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/web_fetch/promise.rb', line 5

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/web_fetch/promise.rb', line 5

def response
  @response
end

#uidObject (readonly)

Returns the value of attribute uid.



5
6
7
# File 'lib/web_fetch/promise.rb', line 5

def uid
  @uid
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/web_fetch/promise.rb', line 24

def complete?
  return false if @response.nil?

  @response.complete?
end

#customObject



20
21
22
# File 'lib/web_fetch/promise.rb', line 20

def custom
  request&.custom
end

#errorObject



40
41
42
43
44
# File 'lib/web_fetch/promise.rb', line 40

def error
  return nil unless complete?

  @response.error
end

#fetch(options = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/web_fetch/promise.rb', line 13

def fetch(options = {})
  return @response if complete?

  wait = options.fetch(:wait, true)
  @response = @client.fetch(@uid, wait: wait)
end

#pending?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/web_fetch/promise.rb', line 30

def pending?
  return false if @response.nil?

  @response.pending?
end

#success?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/web_fetch/promise.rb', line 36

def success?
  complete? && @response.success?
end