mini_mock 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mini_mock.rb +58 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9e96d52e166cf0503f9a5e09800f9aa87accf0ae89d649e3e7554afb4a619a46
4
+ data.tar.gz: b320f3f91aca310659abb3391c8bac039b9b435797cbdd36e9626c3a66fd0a0b
5
+ SHA512:
6
+ metadata.gz: f3d2097d3ab1bc147126578f6495e4b300036e5c7ad1405cf346e2457a0c4c2f20c1d241b357edc231b1431fd7f3580d0732a9147e32bde8710292bf40a6b6d5
7
+ data.tar.gz: 758fa946b59033b18e7d42dc97c0dba27b574a4d725944d0ce65fa853092c26696739396a70e1ee78fb47707431fb8d954693f6f66ac2212690186411f536f6a
data/lib/mini_mock.rb ADDED
@@ -0,0 +1,58 @@
1
+ class MiniMock
2
+ RECORDINGS_PATH = 'mini_mock'
3
+ DEFAULT_FILE = "mocks"
4
+
5
+ class << self
6
+
7
+ def record file_name=DEFAULT_FILE
8
+ FileUtils.mkdir_p(RECORDINGS_PATH)
9
+ @@file_name = file_name
10
+ Typhoeus::Config.block_connection = false
11
+ add_callback
12
+ true
13
+ end
14
+
15
+ def replay file_name=DEFAULT_FILE
16
+ Typhoeus::Config.block_connection = true
17
+ remove_callback
18
+ load "#{RECORDINGS_PATH}/#{file_name}.rb"
19
+ true
20
+ end
21
+
22
+ def off
23
+ Typhoeus::Config.block_connection = false
24
+ remove_callback
25
+ true
26
+ end
27
+
28
+ private
29
+ def add_callback
30
+ unless Typhoeus.on_complete.include?(AFTER_REQUEST_CALLBACK)
31
+ Typhoeus.on_complete << AFTER_REQUEST_CALLBACK
32
+ end
33
+ end
34
+
35
+ def remove_callback
36
+ Typhoeus.on_complete.delete_if {|v| v == AFTER_REQUEST_CALLBACK }
37
+ end
38
+
39
+ end
40
+
41
+ AFTER_REQUEST_CALLBACK = Proc.new do |response|
42
+ code = <<~RUBY
43
+ response = Typhoeus::Response.new(
44
+ code: #{response.code},
45
+ status_message: "#{response.status_message}",
46
+ body: #{response.body.inspect},
47
+ headers: #{response.headers},
48
+ effective_url: #{response.effective_url.inspect},
49
+ options: #{response.options.tap{|a| a[:debug_info] = ""}.inspect}
50
+ )
51
+ Typhoeus.stub(#{response.request.base_url.inspect}, #{response.request.original_options})
52
+ .and_return(response)
53
+
54
+ RUBY
55
+ File.write "#{RECORDINGS_PATH}/#{@@file_name}.rb", code, mode: 'a+'
56
+ end
57
+
58
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_mock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Felipe Mesquita
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: felipemesquita@hey.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/mini_mock.rb
20
+ homepage: https://github.com/felipedmesquita/mini_mock
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.3.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Record and replay Typhoeus requests
43
+ test_files: []