Class: Tanker::Http::HttpRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/tanker/core/http.rb

Constant Summary collapse

@@mutex =

rubocop:disable Style/ClassVars I have no idea why you don’t like class vars

Mutex.new
@@current_request_id =

rubocop:disable Style/ClassVars

0
@@running_requests =

Hash(id => request)

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(crequest:) ⇒ HttpRequest

Returns a new instance of HttpRequest.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tanker/core/http.rb', line 40

def initialize(crequest:)
  @@mutex.synchronize do
    @@current_request_id += 1 # rubocop:disable Style/ClassVars
    @id = @@current_request_id
    @@running_requests[@id] = self
  end

  @method = self.class.method_str_to_symbol crequest[:method]
  @url = crequest[:url]
  @body = crequest[:body].read_string_length(crequest[:body_size])

  count = crequest[:num_headers]
  headers_base_addr = crequest[:headers]
  @headers = count.times.map do |i|
    header_ptr = headers_base_addr + (i * CTanker::CHttpRequestHeader.size)
    c_header = CTanker::CHttpRequestHeader.new header_ptr
    HttpHeader.new(c_header[:name], c_header[:value])
  end

  # Keep the crequest because we need its address to answer to Tanker
  @crequest = crequest
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



26
27
28
# File 'lib/tanker/core/http.rb', line 26

def body
  @body
end

#crequestObject (readonly)

Returns the value of attribute crequest.



27
28
29
# File 'lib/tanker/core/http.rb', line 27

def crequest
  @crequest
end

#headersObject (readonly)

Returns the value of attribute headers.



25
26
27
# File 'lib/tanker/core/http.rb', line 25

def headers
  @headers
end

#idObject (readonly)

rubocop:disable Style/ClassVars



22
23
24
# File 'lib/tanker/core/http.rb', line 22

def id
  @id
end

#methodObject (readonly)

Returns the value of attribute method.



23
24
25
# File 'lib/tanker/core/http.rb', line 23

def method
  @method
end

#urlObject (readonly)

Returns the value of attribute url.



24
25
26
# File 'lib/tanker/core/http.rb', line 24

def url
  @url
end

Class Method Details

.cancel(id) ⇒ Object

Since Ruby’s HTTP libraries are not asynchronous, they do not support cancelation either. When a request is canceled, we let it run until the end, and then we discard its result.



65
66
67
68
69
# File 'lib/tanker/core/http.rb', line 65

def self.cancel(id)
  @@mutex.synchronize do
    @@running_requests.delete id
  end
end

.method_str_to_symbol(method) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/tanker/core/http.rb', line 29

def self.method_str_to_symbol(method)
  case method
  when 'GET' then :get
  when 'POST' then :post
  when 'PATCH' then :patch
  when 'PUT' then :put
  when 'DELETE' then :delete
  else raise "unknown HTTP method #{method}"
  end
end

Instance Method Details

#complete_if_not_canceled(&block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/tanker/core/http.rb', line 71

def complete_if_not_canceled(&block)
  @@mutex.synchronize do
    unless @@running_requests.delete @id
      # Request has been canceled, don't call Tanker back
      return
    end

    block.call
  end
end