rspec-http-fixtures 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fa13c8900b8be14e148603c8928c5fe8730540117d2bce56a46107d5576fdfa3
4
+ data.tar.gz: 1a6d27dccc36a5583f5483fc0e28c05e729cb7c5aa88af8ca6fc96156e430621
5
+ SHA512:
6
+ metadata.gz: 458225bf37a63aca184bd850a34e1efa997242b0a9c2b5ff4e9419f407087e21b2bcc1f3563e20c811319eff3ef038a9ed07a8e8d166269ab9e8b3dcc5ce0b9f
7
+ data.tar.gz: bc2d35410727954e93a181f54ae8c5c9911e3025c9fd8ba9b5d9eba3874c67dc21abbd6a07ea700e112ea627e86d9cdc3d89f0f827bd7f5985e763bf075b018f
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rspec-http-fixtures.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rspec-http-fixtures (0.0.1)
5
+ byebug
6
+ rake
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ byebug (11.1.3)
12
+ rake (13.1.0)
13
+
14
+ PLATFORMS
15
+ arm64-darwin-21
16
+
17
+ DEPENDENCIES
18
+ rspec-http-fixtures!
19
+
20
+ BUNDLED WITH
21
+ 2.4.20
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 James Hu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # rspec-http-fixtures
2
+
3
+ Provides RSpec helper methods to build HTTP fixtures.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "rspec-http-fixtures"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```shell
16
+ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```shell
22
+ gem install rspec-http-fixtures
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Within `rails_helper.rb`
28
+
29
+ ```ruby
30
+ require "rspec-http-fixtures"
31
+
32
+ RSpec.configure do |config|
33
+ # Specify where fixtures will be located
34
+ config.http_fixtures_path = Rails.root.join("spec/fixtures")
35
+
36
+
37
+ # Include helpers that are available within fixtures
38
+ config.http_fixtures_include FixtureHelpers
39
+ end
40
+ ```
41
+
42
+ Work with fixtures using the following helpers
43
+
44
+ ```ruby
45
+ read_http_fixture("stripe/subscription.json.erb",
46
+ id: "so_123",
47
+ )
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ 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).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/axsuul/rspec-http-fixtures.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,92 @@
1
+ module RSpecHTTPFixtures
2
+ class FixtureNamespace
3
+ def initialize(context:, path:, params: {})
4
+ @context = context
5
+ @path = path
6
+
7
+ params.each do |key, value|
8
+ set(key, value)
9
+ end
10
+ end
11
+
12
+ def set(key, value)
13
+ instance_variable_set("@#{key}", value)
14
+ end
15
+
16
+ def build_fixture_path(path)
17
+ # Can convert relative path to absolute path
18
+ "#{File.expand_path(path, File.dirname(@path))}.erb"
19
+ end
20
+
21
+ # To be used with arrays and objects for json fixtures and returns the actual data or null if nil
22
+ def build_json_data(value, default = nil)
23
+ value ||= default
24
+
25
+ build_json_null(value) || (value.is_a?(String) ? value : value.to_json)
26
+ end
27
+
28
+ # Returns string value enclosed in quotes or null if nil for json fixtures. Can also be forced to be null by
29
+ # passing in the string: "null"
30
+ def build_json_string(value, default = nil)
31
+ value ||= default
32
+
33
+ # Ensure any double quotes are escaped since they are part of string and won't mess up the entire JSON
34
+ build_json_null(value) || "\"#{value.to_s.gsub('"', '\"')}\""
35
+ end
36
+
37
+ # Returns integer value or null if nil for json fixtures. Can also be forced to be null by passing in the
38
+ # string: "null"
39
+ def build_json_number(value, default = nil)
40
+ value ||= default
41
+
42
+ build_json_null(value) || value
43
+ end
44
+
45
+ # Returns boolean value or null if nil for json fixtures. Can also be forced to be null by passing in the
46
+ # string: "null". Can provide default if value is nil
47
+ def build_json_boolean(value, default = nil)
48
+ return "null" if value == "null"
49
+ return value unless value.nil?
50
+
51
+ default.is_a?(TrueClass) || default.is_a?(FalseClass) ? default : "null"
52
+ end
53
+
54
+ def build_json_iso8601(value, default = nil)
55
+ value ||= default
56
+
57
+ build_json_null(value) || build_json_string(value.is_a?(String) ? value : value.iso8601)
58
+ end
59
+
60
+ def build_json_epoch(value)
61
+ build_json_number(value&.to_i)
62
+ end
63
+
64
+ def build_json_null(value)
65
+ value.nil? || value == "null" ? "null" : nil
66
+ end
67
+
68
+ def build_json_collection_fixture(fixture, params_collection = [])
69
+ @context.build_json_collection_fixture(build_fixture_path(fixture), params_collection)
70
+ end
71
+
72
+ def build_xml_collection_fixture(fixture, params_collection = [])
73
+ params_collection.map { |p| @context.read_fixture(build_fixture_path(fixture), p) }.join("")
74
+ end
75
+
76
+ # rubocop:disable Style/MethodMissingSuper
77
+ # rubocop:disable Style/MissingRespondToMissing
78
+ def method_missing(name, *args)
79
+ instance_variable = "@#{name}"
80
+
81
+ # First try to see if it's a param we're trying to access which is stored in an instance variable otherwise
82
+ # try to see if there's a method currently on class (from includes)
83
+ if instance_variable_defined?(instance_variable)
84
+ instance_variable_get(instance_variable)
85
+ elsif respond_to?(name)
86
+ send(name, *args)
87
+ end
88
+ end
89
+ # rubocop:enable Style/MethodMissingSuper
90
+ # rubocop:enable Style/MissingRespondToMissing
91
+ end
92
+ end
@@ -0,0 +1,55 @@
1
+ require_relative "fixture-namespace"
2
+
3
+ module RSpecHTTPFixtures
4
+ module Helpers
5
+ def build_http_fixture_path(path)
6
+ RSpec.configuration.http_fixtures_path.join(path)
7
+ end
8
+
9
+ def open_http_fixture(path)
10
+ File.open(build_http_fixture_path(path))
11
+ end
12
+
13
+ def read_http_fixture(read_http_fixture_path, read_fixture_params = {}, *read_fixture_args)
14
+ # Copy the params so the original doesn't get mutated
15
+ read_fixture_params = read_fixture_params.deep_dup
16
+
17
+ # If path is a symbol then we can assume it's a method so let's call it Otherwise, we assume it's a relative
18
+ # path to our fixture. We use long variables since these variables are also of the same scope that is being
19
+ # passed into the ERB template so we don't want to collide with anything in the namespace
20
+ read_fixture_body =
21
+ if read_http_fixture_path.is_a?(Symbol)
22
+ send(read_http_fixture_path, read_fixture_params, *read_fixture_args)
23
+ else
24
+ # Chomp to get rid of newline at end
25
+ open_http_fixture(read_http_fixture_path).read.chomp
26
+ end
27
+
28
+ # Return raw body unless it is an ERB. If it's ERB, then render it with params
29
+ return read_fixture_body unless File.extname(read_http_fixture_path.to_s) == ".erb"
30
+
31
+ unless read_fixture_params.is_a?(Hash)
32
+ raise ArgumentError, "params must be a Hash, instead it's #{read_fixture_params.inspect}"
33
+ end
34
+
35
+ # Use ERB to inject variables into body
36
+ erb = ERB.new(read_fixture_body)
37
+
38
+ # We use a custom namespace class so that we can include helper methods
39
+ # into the namespace to make them available for template to access
40
+ namespace = FixtureNamespace.new(
41
+ context: self,
42
+ path: build_http_fixture_path(read_http_fixture_path),
43
+ params: read_fixture_params,
44
+ )
45
+
46
+ erb.result(namespace.instance_eval { binding })
47
+ end
48
+
49
+ def build_json_collection_fixture(path, collection_params)
50
+ return unless collection_params
51
+
52
+ collection_params.map { |p| JSON.parse(read_http_fixture(path, p)) }.to_json
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec/core"
4
+ require "rspec-http-fixtures/helpers"
5
+
6
+ RSpec.configure do |config|
7
+ config.add_setting(:http_fixtures_path, default: nil)
8
+
9
+ config.include(RSpecHTTPFixtures::Helpers)
10
+ end
11
+
12
+ RSpec::Core::Configuration.class_eval do
13
+ def http_fixtures_include(mod)
14
+ RSpecHTTPFixtures::FixtureNamespace.class_eval do
15
+ include(mod)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rspec-http-fixtures"
5
+ spec.version = "0.0.1"
6
+ spec.authors = ["James Hu"]
7
+
8
+ spec.summary = "Capistrano plugin for deploying and managing Nomad jobs"
9
+ spec.description = "Capistrano plugin for deploying and managing Nomad jobs"
10
+ spec.homepage = "https://github.com/axsuul/rspec-http-fixtures"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = ">= 2.6.0"
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/axsuul/rspec-http-fixtures"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
+ end
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ # Uncomment to register a new dependency of your gem
30
+ spec.add_dependency "byebug"
31
+ spec.add_dependency "rake"
32
+
33
+ # For more information and examples about making a new gem, check out our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-http-fixtures
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Hu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Capistrano plugin for deploying and managing Nomad jobs
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - Gemfile
48
+ - Gemfile.lock
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/rspec-http-fixtures.rb
53
+ - lib/rspec-http-fixtures/fixture-namespace.rb
54
+ - lib/rspec-http-fixtures/helpers.rb
55
+ - rspec-http-fixtures.gemspec
56
+ homepage: https://github.com/axsuul/rspec-http-fixtures
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ allowed_push_host: https://rubygems.org
61
+ homepage_uri: https://github.com/axsuul/rspec-http-fixtures
62
+ source_code_uri: https://github.com/axsuul/rspec-http-fixtures
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.6.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.1.6
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Capistrano plugin for deploying and managing Nomad jobs
82
+ test_files: []