Class: Rager::Http::Adapters::Mock

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

Constant Summary collapse

Cache =
T.type_alias { T::Hash[String, T::Hash[String, T.untyped]] }

Instance Method Summary collapse

Constructor Details

#initialize(test_file_path, fallback_adapter = nil, chunk_delimiter = nil) ⇒ Mock

Returns a new instance of Mock.



25
26
27
28
29
# File 'lib/rager/http/adapters/mock.rb', line 25

def initialize(test_file_path, fallback_adapter = nil, chunk_delimiter = nil)
  @test_file_path = T.let(test_file_path, String)
  @fallback_adapter = T.let(fallback_adapter || Rager::Http::Adapters::NetHttp.new, Rager::Http::Adapters::Abstract)
  @cache = T.let(load_cache, Cache)
end

Instance Method Details

#binary_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/rager/http/adapters/mock.rb', line 128

def binary_content?(content)
  content.include?("\0")
end

#build_response_from_cache(entry) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rager/http/adapters/mock.rb', line 87

def build_response_from_cache(entry)
  body = entry["body"]
  is_stream = entry["is_stream"] || false
  is_binary = entry["is_binary"] || false

  body = if is_stream
    chunks = T.cast(body, T::Array[String])
    Enumerator.new do |yielder|
      chunks.each { |chunk| yielder << (is_binary ? Base64.strict_decode64(chunk) : chunk) }
    end
  else
    is_binary ? Base64.strict_decode64(T.cast(body, String)) : body
  end

  Rager::Http::Response.new(
    status: T.cast(entry["status"], Integer),
    headers: T.cast(entry["headers"], T::Hash[String, String]),
    body: body
  )
end

#create_file_if_not_existsObject



109
110
111
112
# File 'lib/rager/http/adapters/mock.rb', line 109

def create_file_if_not_exists
  FileUtils.mkdir_p(File.dirname(@test_file_path))
  File.write(@test_file_path, "{}") unless File.exist?(@test_file_path)
end

#fetch_and_cache_response(request, key) ⇒ Object



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
# File 'lib/rager/http/adapters/mock.rb', line 39

def fetch_and_cache_response(request, key)
  response = @fallback_adapter.make_request(request)

  serialized_response = {
    "status" => response.status,
    "headers" => response.headers
  }

  if request.streaming && response.body.is_a?(Enumerator)
    raw_chunks = T.let([], T::Array[String])
    T.cast(response.body, T::Enumerator[String]).each { |chunk| raw_chunks << chunk }

    has_binary = raw_chunks.any? { |chunk| binary_content?(chunk) }
    chunks = raw_chunks.map { |chunk| binary_content?(chunk) ? Base64.strict_encode64(chunk) : chunk }

    serialized_response["body"] = chunks
    serialized_response["is_stream"] = true
    serialized_response["is_binary"] = has_binary

    response_body = Enumerator.new do |yielder|
      raw_chunks.each { |chunk| yielder << chunk }
    end
  else
    body = T.cast(response.body, T.nilable(String)) || ""

    if binary_content?(body)
      serialized_response["body"] = Base64.strict_encode64(body)
      serialized_response["is_binary"] = true
    else
      serialized_response["body"] = body
      serialized_response["is_binary"] = false
    end

    serialized_response["is_stream"] = false
    response_body = body
  end

  @cache[key] = serialized_response
  save_cache

  Rager::Http::Response.new(
    status: response.status,
    headers: response.headers,
    body: response_body
  )
end

#load_cacheObject



115
116
117
118
# File 'lib/rager/http/adapters/mock.rb', line 115

def load_cache
  create_file_if_not_exists
  JSON.parse(File.read(@test_file_path))
end

#make_request(request) ⇒ Object



32
33
34
35
36
# File 'lib/rager/http/adapters/mock.rb', line 32

def make_request(request)
  key = request.serialize.to_json
  cached_entry = @cache[key]
  cached_entry ? build_response_from_cache(cached_entry) : fetch_and_cache_response(request, key)
end

#save_cacheObject



121
122
123
124
125
# File 'lib/rager/http/adapters/mock.rb', line 121

def save_cache
  current_file_content = File.exist?(@test_file_path) ? JSON.parse(File.read(@test_file_path)) : {}
  output = current_file_content.merge(@cache)
  File.write(@test_file_path, JSON.generate(output))
end