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 +4 -4
- data/README.md +32 -3
- data/lib/faraday/hot_mock/version.rb +1 -1
- data/lib/faraday/hot_mock.rb +25 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ca41f6d974866b895fa2ce0da58d30c61120d63c342dcb39d353346156692abe
|
|
4
|
+
data.tar.gz: f45729e717e06848776bca596533d8e4af59cf22eebd83ee5a0e4405ff003ec1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
##
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
data/lib/faraday/hot_mock.rb
CHANGED
|
@@ -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
|
|