Class: Net::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/web/ext/net_http.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.socket_type_with_webObject Also known as: socket_type



44
45
46
# File 'lib/web/ext/net_http.rb', line 44

def socket_type_with_web
  Web::StubSocket
end

Instance Method Details

#request_with_web(request, body = nil, &block) ⇒ Object Also known as: request



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
# File 'lib/web/ext/net_http.rb', line 53

def request_with_web(request, body = nil, &block)
  connect
  # get a faker
  headers = {}
  request.each do |key, value|
    headers[key] = value
  end
  web = Web::Faker.new request.method.downcase.to_sym, request_uri_as_string(request), body, headers
  if web.desired? && web_response = web.response_for
    # turn the web response into a Net::HTTPResponse
    response = Net::HTTPResponse.send(:response_class, web_response.code.to_s).new('1.0', web_response.code.to_s, HTTP_STATUS_CODES[web_response.code])
    web_response.headers.each do |name, value|
      if value.respond_to?(:each)
        value.each { |val| response.add_field(name, val) }
      else
        response[name] = value
      end
    end
    response.extend Web::ReadableHTTPResponse
    response.instance_variable_set(:@body, web_response.body)
    response.instance_variable_set(:@read, true)
    yield response if block_given?
    # respond cached
    response.instance_variable_set(:@cached, true)
    response
  else
    # get the  response and store it if its desirable
    if web.desired?
      response = request_without_web(request, body)
      headers = {}
      response.each do |key, value|
        headers[key] = value
      end
      response.extend Web::ReadableHTTPResponse
      web.record response.code.to_i, response.body, headers
      yield response if block_given?
      response
    else
      response = request_without_web(request, body, &block)
      response.extend Web::HTTPResponse
      response
    end
  end
end