Class: Krikri::AsyncUriGetter::Request

Inherits:
Struct
  • Object
show all
Defined in:
lib/krikri/async_uri_getter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



76
77
78
79
# File 'lib/krikri/async_uri_getter.rb', line 76

def initialize(*)
  super
  @request_thread = start_request
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



75
76
77
# File 'lib/krikri/async_uri_getter.rb', line 75

def headers
  @headers
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



75
76
77
# File 'lib/krikri/async_uri_getter.rb', line 75

def opts
  @opts
end

#uriObject

Returns the value of attribute uri

Returns:

  • (Object)

    the current value of uri



75
76
77
# File 'lib/krikri/async_uri_getter.rb', line 75

def uri
  @uri
end

Instance Method Details

#joinObject

Wait for the request thread to complete



83
84
85
86
87
88
89
90
# File 'lib/krikri/async_uri_getter.rb', line 83

def join
  @request_thread.join
rescue => e
  # If the join throws an exception, the thread is dead anyway.  The
  # subsequent call to `with_response` will propagate the exception to the
  # calling thread.
  raise e unless inline_exceptions?
end

#with_response {|Faraday::Response| ... } ⇒ Object

Yields:

  • (Faraday::Response)

    the response returned for the request



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/krikri/async_uri_getter.rb', line 94

def with_response
  begin
    response = @request_thread.value
  rescue => e
    if inline_exceptions?
      # Deliver an error response to the caller to allow uniform access
      msg = e.message + "\n\n" + e.backtrace.join("\n")
      response = Faraday::Response.new(status: 500,
                                       body: msg,
                                       response_headers: {
                                         'X-Exception' => e,
                                         'X-Exception-Message' => e.message,
                                         'X-Internal-Response' => 'true'
                                       })
    else
      raise e
    end
  end

  yield response
end