Class: Rager::Http::Adapters::NetHttp

Inherits:
Abstract
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rager/http/adapters/net_http.rb

Instance Method Summary collapse

Constructor Details

#initializeNetHttp



15
16
# File 'lib/rager/http/adapters/net_http.rb', line 15

def initialize
end

Instance Method Details

#body_enum(response) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/rager/http/adapters/net_http.rb', line 104

def body_enum(response)
  chunks = T.let([], T::Array[String])

  response.read_body do |chunk|
    chunks << chunk
  end

  chunks.to_enum
end

#build_http_request(request, uri) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rager/http/adapters/net_http.rb', line 120

def build_http_request(request, uri)
  http_request = case request.verb
  when Rager::Http::Verb::Get then Net::HTTP::Get.new(uri)
  when Rager::Http::Verb::Post then Net::HTTP::Post.new(uri)
  when Rager::Http::Verb::Put then Net::HTTP::Put.new(uri)
  when Rager::Http::Verb::Patch then Net::HTTP::Patch.new(uri)
  when Rager::Http::Verb::Delete then Net::HTTP::Delete.new(uri)
  when Rager::Http::Verb::Head then Net::HTTP::Head.new(uri)
  when Rager::Http::Verb::Options then Net::HTTP::Options.new(uri)
  end

  request.headers.each do |key, value|
    http_request[key] = value
  end

  if request.body && http_request.request_body_permitted?
    http_request.body = request.body
  end

  http_request
end

#make_request(request) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rager/http/adapters/net_http.rb', line 23

def make_request(request)
  uri = URI(request.url)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"

  if request.timeout
    http.open_timeout = request.timeout
    http.read_timeout = request.timeout
  end

  http_request = build_http_request(request, uri)

  http.request(http_request) do |response|
    body = if request.streaming
      body_enum(response)
    else
      response.body
    end

    return Rager::Http::Response.new(
      status: response.code.to_i,
      headers: response.to_hash,
      body: body
    )
  end
rescue SocketError => e
  raise Rager::Errors::HttpError.new(
    self,
    request.url,
    0,
    body: nil,
    details: "DNS resolution failed: #{e.message}"
  )
rescue Errno::ECONNREFUSED => e
  raise Rager::Errors::HttpError.new(
    self,
    request.url,
    0,
    body: nil,
    details: "Connection refused: #{e.message}"
  )
rescue Errno::EBUSY => e
  raise Rager::Errors::HttpError.new(
    self,
    request.url,
    0,
    body: nil,
    details: "Device or resource busy: #{e.message}"
  )
rescue Net::OpenTimeout => e
  raise Rager::Errors::HttpError.new(
    self,
    request.url,
    0,
    body: nil,
    details: "Connection timed out: #{e.message}"
  )
rescue Net::ReadTimeout => e
  raise Rager::Errors::HttpError.new(
    self,
    request.url,
    0,
    body: nil,
    details: "Read timed out: #{e.message}"
  )
rescue EOFError => e
  raise Rager::Errors::HttpError.new(
    self,
    request.url,
    0,
    body: nil,
    details: "Connection closed unexpectedly: #{e.message}"
  )
end