Module: Faraday::HotMock
- Extended by:
- HotMock
- Included in:
- HotMock
- Defined in:
- lib/faraday/hot_mock.rb,
lib/faraday/hot_mock/adapter.rb,
lib/faraday/hot_mock/railtie.rb,
lib/faraday/hot_mock/version.rb
Defined Under Namespace
Constant Summary collapse
- HEADERS =
{ recorded: "x-hot-mock-recorded-at", mocked: "x-hot-mocked" }
- FILE_NAME =
"hot_mocks.yml"- VERSION =
"0.6.4"
Instance Attribute Summary collapse
-
#scenario ⇒ Object
Returns the value of attribute scenario.
-
#vcr ⇒ Object
Returns the value of attribute vcr.
Instance Method Summary collapse
- #all_hot_mock_files ⇒ Object
- #delete(method:, url:) ⇒ Object
- #disable! ⇒ Object
- #disabled? ⇒ Boolean
- #enable! ⇒ Object
- #enabled? ⇒ Boolean
- #hot_mock_dir ⇒ Object
- #hot_mock_file ⇒ Object
- #hot_mocking_file ⇒ Object
- #mock!(method:, url:, status:, headers: {}, body: nil) ⇒ Object
- #mocked?(method:, url:) ⇒ Boolean
- #mocks ⇒ Object
- #record(method:, headers: {}, url:, body: {}, into_scenario: nil) ⇒ Object
- #record!(method:, url:) ⇒ Object
- #recording? ⇒ Boolean
- #scenario_dir ⇒ Object
- #scenarios ⇒ Object
- #settings_file ⇒ Object
- #toggle! ⇒ Object
Instance Attribute Details
#scenario ⇒ Object
Returns the value of attribute scenario.
10 11 12 |
# File 'lib/faraday/hot_mock.rb', line 10 def scenario @scenario end |
#vcr ⇒ Object
Returns the value of attribute vcr.
10 11 12 |
# File 'lib/faraday/hot_mock.rb', line 10 def vcr @vcr end |
Instance Method Details
#all_hot_mock_files ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/faraday/hot_mock.rb', line 161 def all_hot_mock_files if scenario.present? Dir.glob(File.join(hot_mock_dir, "**", "*.{yml,yaml}")) else Dir.glob(File.join(hot_mock_dir, "**", "*.{yml,yaml}")).reject { |path| path.include?("/scenarios/") } end end |
#delete(method:, url:) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/faraday/hot_mock.rb', line 42 def delete(method:, url:) return unless File.exist?(hot_mock_file) mocks = YAML.load_file(hot_mock_file) || [] mocks.reject! { |entry| entry["url_pattern"] == url && entry["method"].to_s.upcase == method.to_s.upcase } File.write(hot_mock_file, mocks.to_yaml) end |
#disable! ⇒ Object
18 19 20 |
# File 'lib/faraday/hot_mock.rb', line 18 def disable! FileUtils.rm_f(hot_mocking_file) end |
#disabled? ⇒ Boolean
22 23 24 |
# File 'lib/faraday/hot_mock.rb', line 22 def disabled? !File.exist?(hot_mocking_file) end |
#enable! ⇒ Object
26 27 28 |
# File 'lib/faraday/hot_mock.rb', line 26 def enable! FileUtils.touch(hot_mocking_file) end |
#enabled? ⇒ Boolean
30 31 32 |
# File 'lib/faraday/hot_mock.rb', line 30 def enabled? File.exist?(hot_mocking_file) end |
#hot_mock_dir ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/faraday/hot_mock.rb', line 128 def hot_mock_dir if self.scenario Rails.root.join "lib/faraday/mocks/#{Rails.env}/scenarios/#{self.scenario}" else Rails.root.join "lib/faraday/mocks/#{Rails.env}" end end |
#hot_mock_file ⇒ Object
144 145 146 |
# File 'lib/faraday/hot_mock.rb', line 144 def hot_mock_file Rails.root.join(hot_mock_dir, FILE_NAME) end |
#hot_mocking_file ⇒ Object
140 141 142 |
# File 'lib/faraday/hot_mock.rb', line 140 def hot_mocking_file Rails.root.join "tmp/mocking-#{Rails.env}.txt" end |
#mock!(method:, url:, status:, headers: {}, body: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/faraday/hot_mock.rb', line 52 def mock!(method:, url:, status:, headers: {}, body: nil) FileUtils.mkdir_p(hot_mock_dir) FileUtils.touch(hot_mock_file) mocks = YAML.load_file(hot_mock_file) || [] mocks.reject! { |entry| entry["url_pattern"] == url && entry["method"].to_s.upcase == method.to_s.upcase } mocks << { "method" => method.to_s.upcase, "url_pattern" => url, "status" => status, "headers" => headers, "body" => body } File.write(hot_mock_file, mocks.to_yaml) end |
#mocked?(method:, url:) ⇒ Boolean
71 72 73 |
# File 'lib/faraday/hot_mock.rb', line 71 def mocked?(method:, url:) mocks.find { |entry| entry["method"].to_s.upcase == method.to_s.upcase && Regexp.new(entry["url_pattern"]).match?(url.to_s) } || false end |
#mocks ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/faraday/hot_mock.rb', line 148 def mocks return [] unless Dir.exist?(hot_mock_dir) mocks = [] all_hot_mock_files.each do |file| file_mocks = YAML.load_file(file) mocks.concat(file_mocks) if file_mocks.is_a?(Array) end mocks end |
#record(method:, headers: {}, url:, body: {}, into_scenario: nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/faraday/hot_mock.rb', line 75 def record(method:, headers: {}, url:, body: {}, into_scenario: nil) self.scenario = into_scenario if into_scenario.present? return false if mocked?(method:, url:) faraday = Faraday.new( headers:, ) response = faraday.send(method.downcase.to_sym, url, body) FileUtils.mkdir_p(hot_mock_dir) FileUtils.touch(hot_mock_file) hot_mocks = YAML.load_file(hot_mock_file) || [] hot_mocks << { "method" => method.to_s.upcase, "url_pattern" => url.to_s, "status" => response.status, "headers" => response.headers.to_h.merge(HEADERS[:recorded] => Time.now.utc.iso8601, HEADERS[:mocked] => "true"), "body" => response.body } File.write(hot_mock_file, hot_mocks.to_yaml) rescue Faraday::Error => e puts "Error recording mock for #{method.upcase} #{url}: #{e.}" end |
#record!(method:, url:) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/faraday/hot_mock.rb', line 104 def record!(method:, url:) faraday = Faraday.new response = faraday.send(method.downcase.to_sym, url) FileUtils.touch(hot_mock_file) hot_mocks = YAML.load_file(hot_mock_file) || [] hot_mocks.reject! { |entry| entry["url_pattern"] == url && entry["method"].to_s.upcase == method.to_s.upcase } hot_mocks << { "method" => method.to_s.upcase, "url_pattern" => url.to_s, "status" => response.status, "headers" => response.headers.to_h.merge(HEADERS[:recorded] => Time.now.utc.iso8601, HEADERS[:mocked] => "true"), "body" => response.body } File.write(hot_mock_file, hot_mocks.to_yaml) rescue Faraday::Error => e puts "Error recording mock for #{method.upcase} #{url}: #{e.}" end |
#recording? ⇒ Boolean
169 170 171 |
# File 'lib/faraday/hot_mock.rb', line 169 def recording? YAML.load_file(settings_file)["recording"] rescue false end |
#scenario_dir ⇒ Object
136 137 138 |
# File 'lib/faraday/hot_mock.rb', line 136 def scenario_dir Rails.root.join "lib/faraday/mocks/#{Rails.env}/scenarios" end |
#scenarios ⇒ Object
177 178 179 |
# File 'lib/faraday/hot_mock.rb', line 177 def scenarios Dir.glob(File.join(scenario_dir, "*")).map { _1.split("/").last } end |
#settings_file ⇒ Object
193 194 195 |
# File 'lib/faraday/hot_mock.rb', line 193 def settings_file Rails.root.join("tmp/hot_mock_settings.yml") end |
#toggle! ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/faraday/hot_mock.rb', line 34 def toggle! if File.exist?(hot_mocking_file) disable! else enable! end end |