response_faker 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/README.md +59 -17
- data/lib/response_faker/version.rb +1 -1
- data/lib/response_faker.rb +42 -15
- metadata +4 -4
- /data/lib/response_faker/responses/{razorpay.rb → razorpay.json} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c357b03fe9d5e885512d715fbb896367f823464cb2e30efe434f9be95333552
|
4
|
+
data.tar.gz: 522d00c417d269f315668a05f7836fcd33179a4478d4b861ca616211f9890da2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f950795d8e4909e41a0f94b977d890bbf9e4bce6e7a9ea27402eb367ce0a93a8203ea95e86c7c54aec598137cdc540fb27841a877e81dc5625b202062852759e
|
7
|
+
data.tar.gz: 1c0b296cf8c95b2c7fcb46029c4674eec98aaad48d0f75450b1c8f821a0fedb9acb057f6aa4e25db37189155d8209b1e2a74046220c0872d3b2df9008f6de5ba
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,39 +1,81 @@
|
|
1
|
-
#
|
1
|
+
# Response Faker Gem
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/response_faker`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
The `response_faker` gem provides a way to generate fake responses for third-party calls using JSON files. It dynamically creates methods based on the JSON files in the `lib/response_faker/responses/` directory, and automatically reloads these responses when files are added or modified.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'response_faker'
|
11
|
+
```
|
10
12
|
|
11
|
-
|
13
|
+
And then execute:
|
12
14
|
|
13
|
-
|
15
|
+
```bash
|
16
|
+
bundle install
|
17
|
+
```
|
14
18
|
|
15
|
-
|
19
|
+
Or install it yourself as:
|
16
20
|
|
17
|
-
|
21
|
+
```bash
|
22
|
+
gem install response_faker
|
23
|
+
```
|
18
24
|
|
19
25
|
## Usage
|
20
26
|
|
21
|
-
|
27
|
+
### Setup
|
28
|
+
|
29
|
+
1. **Create JSON Response Files**
|
30
|
+
|
31
|
+
Place your JSON files in the `lib/response_faker/responses/` directory. Each file should contain a valid JSON object. The filename will be used to create a class with corresponding methods.
|
32
|
+
|
33
|
+
Example JSON file (`lib/response_faker/responses/example_response.json`):
|
34
|
+
```json
|
35
|
+
{
|
36
|
+
"example_method_1": "This is a fake response.",
|
37
|
+
"example_method_2": "Another fake response."
|
38
|
+
}
|
39
|
+
```
|
40
|
+
|
41
|
+
2. **Load Responses**
|
42
|
+
|
43
|
+
The gem automatically loads all JSON files from the `responses` directory when it's first initialized. The methods defined in these files will be available as class methods on the dynamically created classes.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
ResponseFaker::ExampleResponse.example_method_1 # => "This is a fake response."
|
47
|
+
```
|
48
|
+
|
49
|
+
### Auto-Reload
|
50
|
+
|
51
|
+
The gem watches the `lib/response_faker/responses/` directory for changes. If you add or modify any JSON files, the gem will automatically reload the responses and update the available methods.
|
52
|
+
|
53
|
+
## Error Handling
|
54
|
+
|
55
|
+
If a JSON file is improperly formatted, the gem will skip that file and print an error message. Ensure that all JSON files in the directory are valid to avoid issues.
|
22
56
|
|
23
57
|
## Development
|
24
58
|
|
25
|
-
|
59
|
+
To contribute to the development of `response_faker`, follow these steps:
|
26
60
|
|
27
|
-
|
61
|
+
1. Fork the repository.
|
62
|
+
2. Create a new branch (`git checkout -b my-new-feature`).
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`).
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`).
|
65
|
+
5. Create a new Pull Request.
|
66
|
+
|
67
|
+
## License
|
68
|
+
|
69
|
+
This gem is available as open-source software under the terms of the MIT License. See the [LICENSE](LICENSE) file for more information.
|
28
70
|
|
29
71
|
## Contributing
|
30
72
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
73
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/rajputlakhveer/response_faker](https://github.com/rajputlakhveer/response_faker).
|
32
74
|
|
33
|
-
##
|
75
|
+
## Contact
|
34
76
|
|
35
|
-
|
77
|
+
For any questions or support, please reach out to [your email] or create an issue on the GitHub repository.
|
36
78
|
|
37
|
-
|
79
|
+
---
|
38
80
|
|
39
|
-
|
81
|
+
Thank you for using `response_faker`!
|
data/lib/response_faker.rb
CHANGED
@@ -1,30 +1,57 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "response_faker/version"
|
4
|
+
require 'json'
|
5
|
+
require 'listen'
|
4
6
|
|
5
7
|
module ResponseFaker
|
6
8
|
class Error < StandardError; end
|
7
|
-
|
8
|
-
directory_path = "lib/response_faker/responses/"
|
9
|
-
|
10
|
-
# Iterate over each file in the directory
|
11
|
-
Dir.glob(File.join(directory_path, "*")).each do |file|
|
12
|
-
# Get the filename without extension
|
13
|
-
class_name = File.basename(file, ".*").split('_').map(&:capitalize).join
|
14
|
-
klass = Class.new do
|
15
|
-
def self.load_responses(file)
|
16
9
|
|
10
|
+
class << self
|
11
|
+
def load_responses(file)
|
12
|
+
begin
|
17
13
|
responses = JSON.parse(File.read(file))
|
18
|
-
|
19
|
-
|
14
|
+
rescue JSON::ParserError => e
|
15
|
+
raise ResponseFaker::Error, "Invalid JSON format in file: #{file}. Error: #{e.message}"
|
16
|
+
end
|
17
|
+
|
18
|
+
class_name = File.basename(file, ".*").split('_').map(&:capitalize).join
|
19
|
+
klass = const_set(class_name, Class.new)
|
20
|
+
|
21
|
+
responses.each do |key, value|
|
22
|
+
klass.define_singleton_method(key) { value }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def reload_responses
|
27
|
+
directory_path = "./lib/response_faker/responses/"
|
28
|
+
|
29
|
+
# Iterate over each file in the directory
|
30
|
+
Dir.glob(File.join(directory_path, "*")).each do |file|
|
31
|
+
begin
|
32
|
+
load_responses(file)
|
33
|
+
puts "Loaded: #{file}"
|
34
|
+
rescue ResponseFaker::Error => e
|
35
|
+
puts "Skipping file due to error: #{e.message}"
|
20
36
|
end
|
21
37
|
end
|
22
38
|
end
|
39
|
+
end
|
23
40
|
|
24
|
-
|
25
|
-
|
41
|
+
# Initial load of responses
|
42
|
+
reload_responses
|
26
43
|
|
27
|
-
|
28
|
-
|
44
|
+
# Watch for file changes in the responses directory
|
45
|
+
listener = Listen.to('./lib/response_faker/responses/') do |modified, added, _removed|
|
46
|
+
(modified + added).each do |file|
|
47
|
+
begin
|
48
|
+
load_responses(file)
|
49
|
+
puts "Reloaded: #{file}"
|
50
|
+
rescue ResponseFaker::Error => e
|
51
|
+
puts "Error reloading #{file}: #{e.message}"
|
52
|
+
end
|
53
|
+
end
|
29
54
|
end
|
55
|
+
|
56
|
+
listener.start
|
30
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: response_faker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lakhveer Singh Rajput
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Response Faker is a lightweight Ruby gem designed for developers to easily
|
14
14
|
mock third-party responses during testing or development. With Response Faker, users
|
@@ -31,7 +31,7 @@ files:
|
|
31
31
|
- README.md
|
32
32
|
- Rakefile
|
33
33
|
- lib/response_faker.rb
|
34
|
-
- lib/response_faker/responses/razorpay.
|
34
|
+
- lib/response_faker/responses/razorpay.json
|
35
35
|
- lib/response_faker/version.rb
|
36
36
|
- sig/response_faker.rbs
|
37
37
|
homepage: https://github.com/rajputlakhveer/response_faker
|
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
56
|
- !ruby/object:Gem::Version
|
57
57
|
version: '0'
|
58
58
|
requirements: []
|
59
|
-
rubygems_version: 3.5.
|
59
|
+
rubygems_version: 3.5.9
|
60
60
|
signing_key:
|
61
61
|
specification_version: 4
|
62
62
|
summary: Response Faker is a lightweight Ruby gem designed for developers to easily
|
File without changes
|