faraday-hot_mock 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7155695e309162e7cc229f4954856d783aaf0eb628d6308b54cd4dacdd7eedef
4
- data.tar.gz: 3fd121884c136ef252c655f649f68bf2b235d76eea1e55cda5e70c9ccde0d5f2
3
+ metadata.gz: ca41f6d974866b895fa2ce0da58d30c61120d63c342dcb39d353346156692abe
4
+ data.tar.gz: f45729e717e06848776bca596533d8e4af59cf22eebd83ee5a0e4405ff003ec1
5
5
  SHA512:
6
- metadata.gz: 9723060205d8b5fc9c84b6545fc594a6355f56a0dce8f7a3b938efacd94933b5aefb220be446490fdecacb2632382d546bca2cea6b77410fe41fe5c36ba731ff
7
- data.tar.gz: c264d25bf46fe7ca705308bb6af50c69a2488a4adc761a12cc52964214038998b553b9cb9ff93b75b05cd885db3b36af1316da800c9917338da759d82eba8b0b
6
+ metadata.gz: af9f9770c7e9893202512044fc30502871766c4056541eb3f723fc4975948fa72d6d8567e4a751085dbbfd2f0fbbb0b32bcd4dae06d93862575763e9b1045a57
7
+ data.tar.gz: f99a15ab120d185c605ba7f9982c1e9925ca9fe8065645152236c3094fc6bcfe5db1878f6a8fcac5f1ae321cad235b97330ba58fe03e735f953882ac15a22f56
data/README.md CHANGED
@@ -7,9 +7,13 @@ This adapter attempts to make that simpler by parsing YAML files at runtime. If
7
7
  _**This adapter is meant for Faraday usage in Rails, not for Faraday that's used in other frameworks or situations.**_
8
8
 
9
9
 
10
- ## Usage
10
+ ## How It Works
11
+
12
+ When a request is made, Faraday::HotMock checks for the presence of a file named `tmp/mocking-#{Rails.env}.txt`. If that file exists, HotMock is enabled.
13
+
14
+ When HotMock is enabled, it looks for YAML files in `lib/faraday/mocks/#{Rails.env}`. Each YAML file can contain one or more mock definitions.
11
15
 
12
- Create YAML files in `lib/faraday/mocks/#{Rails.env}` - the name of the files doesn't matter, and you can nest them in subdirectories.
16
+ For the YAML files in `lib/faraday/mocks/#{Rails.env}`, the name of the files don't matter, and you can nest them in subdirectories.
13
17
 
14
18
  This means that if you have a Staging environment, or a UAT environment along with a Demo and Development environment, you can mock each separately.
15
19
 
@@ -29,7 +33,10 @@ And then execute:
29
33
  $ bundle
30
34
  ```
31
35
 
32
- Then, use this adapter in your middleware pipeline, making sure that it's last:
36
+
37
+ ## Usage
38
+
39
+ Add this adapter to your middleware pipeline, making sure that it's last:
33
40
 
34
41
  ```ruby
35
42
  @conn = Faraday.new(url: "https://dog.ceo/api/") do |faraday|
@@ -59,6 +66,27 @@ Now, create the directory `lib/faraday/mocks/` and a subdirectory for each envir
59
66
 
60
67
  Consider adding these directories to .gitignore unless you want mocks to be shared.
61
68
 
69
+ ### Convenience Methods
70
+
71
+ You can enable or disable mocking programmatically with these methods:
72
+
73
+ ```ruby
74
+ Faraday::HotMock.enable! # creates tmp/mocking-#{Rails.env}.txt
75
+ Faraday::HotMock.disable! # deletes tmp/mocking-#{Rails.env}.txt
76
+ Faraday::HotMock.toggle! # creates tmp/mocking-#{Rails.env}.txt if missing, deletes if present
77
+ ```
78
+
79
+ In addition, you can check if mocking is enabled with:
80
+
81
+ ```ruby
82
+ Faraday::HotMock.enabled? # returns true/false;
83
+ Faraday::HotMock.disabled? # returns true/false;
84
+ ```
85
+
86
+ These methods have limited use, but can be helpful in scripting scenarios.
87
+
88
+ ### Defining Mocks
89
+
62
90
  ```yaml
63
91
  # lib/faraday/mocks/development/vendor_name_mocks.yml
64
92
  - url_pattern: vendorname.com.*/endpoint
@@ -97,6 +125,7 @@ If you want to disable mocks, you can:
97
125
  - Delete the entry
98
126
  - Delete the file(s)
99
127
  - Delete the directory
128
+ - Use the [convenience methods](#convenience-methods)
100
129
 
101
130
  If you'd rather keep the file(s) around, just delete `tmp/mocking-development.txt`. That will globally disable any mocked responses.
102
131
 
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
2
  module HotMock
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -5,6 +5,31 @@ require "faraday"
5
5
 
6
6
  module Faraday
7
7
  module HotMock
8
+ module_function
9
+
10
+ def disable!
11
+ FileUtils.rm_f(Rails.root.join("tmp/mocking-#{Rails.env}.txt"))
12
+ end
13
+
14
+ def disabled?
15
+ !File.exist?(Rails.root.join("tmp/mocking-#{Rails.env}.txt"))
16
+ end
17
+
18
+ def enable!
19
+ FileUtils.touch(Rails.root.join("tmp/mocking-#{Rails.env}.txt"))
20
+ end
21
+
22
+ def enabled?
23
+ File.exist?(Rails.root.join("tmp/mocking-#{Rails.env}.txt"))
24
+ end
25
+
26
+ def toggle!
27
+ if File.exist?(Rails.root.join("tmp/mocking-#{Rails.env}.txt"))
28
+ disable!
29
+ else
30
+ enable!
31
+ end
32
+ end
8
33
  end
9
34
  end
10
35
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-hot_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Hogge