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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9e8ef5503a278a3f73a267522f4e5808f06fb46c403bbdcc708a79d8e268e96
4
- data.tar.gz: b27560cf3cac60e7f10f9701e4670c46d78cf84e2175b3a5eb186832716c0c07
3
+ metadata.gz: 8c357b03fe9d5e885512d715fbb896367f823464cb2e30efe434f9be95333552
4
+ data.tar.gz: 522d00c417d269f315668a05f7836fcd33179a4478d4b861ca616211f9890da2
5
5
  SHA512:
6
- metadata.gz: c90d1c286bb4831107c9c8c3718b93fd14fb96dc126fd26a4e36803252318f579864e9a2a11053033b8ddde3072f20a40e6d86f0cf283cd882b8389cc0c44e03
7
- data.tar.gz: ab7e368b7edaf274db3b1633f76834876e12c5271410281ef90f2c1177d7a683240bf2a941c6d00b2f47a562cea3d9fe109e10efdc12e2502e015c63cde1e90c
6
+ metadata.gz: f950795d8e4909e41a0f94b977d890bbf9e4bce6e7a9ea27402eb367ce0a93a8203ea95e86c7c54aec598137cdc540fb27841a877e81dc5625b202062852759e
7
+ data.tar.gz: 1c0b296cf8c95b2c7fcb46029c4674eec98aaad48d0f75450b1c8f821a0fedb9acb057f6aa4e25db37189155d8209b1e2a74046220c0872d3b2df9008f6de5ba
data/Gemfile CHANGED
@@ -9,4 +9,5 @@ gem "rake", "~> 13.0"
9
9
 
10
10
  # gem "bundle config gem.rspec", "~> "
11
11
 
12
- gem 'json'
12
+ gem 'json'
13
+ gem 'listen'
data/README.md CHANGED
@@ -1,39 +1,81 @@
1
- # ResponseFaker
1
+ # Response Faker Gem
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
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
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'response_faker'
11
+ ```
10
12
 
11
- Install the gem and add to the application's Gemfile by executing:
13
+ And then execute:
12
14
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```bash
16
+ bundle install
17
+ ```
14
18
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
19
+ Or install it yourself as:
16
20
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```bash
22
+ gem install response_faker
23
+ ```
18
24
 
19
25
  ## Usage
20
26
 
21
- TODO: Write usage instructions here
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
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake bundle config gem.spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+ To contribute to the development of `response_faker`, follow these steps:
26
60
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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/[USERNAME]/response_faker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/response_faker/blob/master/CODE_OF_CONDUCT.md).
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
- ## License
75
+ ## Contact
34
76
 
35
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
77
+ For any questions or support, please reach out to [your email] or create an issue on the GitHub repository.
36
78
 
37
- ## Code of Conduct
79
+ ---
38
80
 
39
- Everyone interacting in the ResponseFaker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/response_faker/blob/master/CODE_OF_CONDUCT.md).
81
+ Thank you for using `response_faker`!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ResponseFaker
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -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
- responses.each do |key, value|
19
- define_singleton_method(key) { value }
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
- # Load responses into the dynamically created class
25
- klass.load_responses(file)
41
+ # Initial load of responses
42
+ reload_responses
26
43
 
27
- # Define the class within the module
28
- const_set(class_name, klass)
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.1.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-05-03 00:00:00.000000000 Z
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.rb
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.6
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