str_masker 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6e4d859aa7040645a8c484b5ac506641393b3dc556ee37c8a68d7d1afb75b48e
4
+ data.tar.gz: 5f2ea19605cde435670c90d155430b7b4162a38c8fd08771fa0f0a473020ad24
5
+ SHA512:
6
+ metadata.gz: 8a4f798f31b2198a4333cecabc54aa59752eb6d21fff87322a2079496369237e2627b3a76c5ab88279567c1512e54e06ab8b34bbbca0075a969c6fa0dfcccc2a
7
+ data.tar.gz: 6cce3c03db32628e41a6e4daf1af3e12bd93ac3932a9305f53362f70ee6d0712534317e7581ed2a5730fa99ce8958733b0208c1b99822dd30db7ae17aebd7e66
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ inherit_gem:
2
+ rubocop-espago: rubocop.yml
3
+
4
+ Bundler/GemComment:
5
+ Enabled: false
6
+
7
+ Layout/LeadingCommentSpace:
8
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2025-07-18
2
+
3
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Michał Zaporski
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # StrMasker
2
+
3
+ A simple and flexible Ruby gem for masking portions of strings with customizable characters.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'str_masker'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install str_masker
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ StrMasker provides a simple interface for masking strings with asterisks or any custom character.
28
+
29
+ ### Basic Usage
30
+
31
+ ```ruby
32
+ require 'str_masker'
33
+
34
+ # Mask entire string (default behavior)
35
+ StrMasker.mask_string("password")
36
+ #=> "********"
37
+
38
+ # Mask middle characters, keeping first and last
39
+ StrMasker.mask_string("password", from: 1, to: -2)
40
+ #=> "p******d"
41
+
42
+ # Mask from beginning, keeping last character
43
+ StrMasker.mask_string("secret", from: 0, to: -2)
44
+ #=> "*****t"
45
+
46
+ # Mask from second character to end
47
+ StrMasker.mask_string("confidential", from: 1, to: -1)
48
+ #=> "c***********"
49
+ ```
50
+
51
+ ### Custom Masking Character
52
+
53
+ ```ruby
54
+ # Use a different character for masking
55
+ StrMasker.mask_string("password", from: 1, to: -2, char: '#')
56
+ #=> "p######d"
57
+
58
+ StrMasker.mask_string("email@example.com", from: 2, to: -5, char: 'X')
59
+ #=> "emXXXXXXXXX.com"
60
+ ```
61
+
62
+ ### Edge Cases
63
+
64
+ ```ruby
65
+ # Returns nil for nil input
66
+ StrMasker.mask_string(nil)
67
+ #=> nil
68
+
69
+ # Returns original string if no masking is needed
70
+ StrMasker.mask_string("ab", from: 1, to: -2)
71
+ #=> "ab"
72
+
73
+ # Empty string
74
+ StrMasker.mask_string("")
75
+ #=> ""
76
+ ```
77
+
78
+ ## API Reference
79
+
80
+ ### `mask_string(val, from: 0, to: -1, char: '*')`
81
+
82
+ Masks a portion of a string with the specified character.
83
+
84
+ #### Parameters
85
+
86
+ - `val` (String, nil): The string to mask. Returns `nil` if input is `nil`.
87
+ - `from` (Integer, optional): Starting position for masking (inclusive). Must be non-negative. Default: `0`.
88
+ - `to` (Integer, optional): Ending position for masking (must be negative, counts from end). Default: `-1`.
89
+ - `char` (String, optional): The character to use for masking. Must be a single character. Default: `'*'`.
90
+
91
+ #### Returns
92
+
93
+ - `String` or `nil`: The masked string, or `nil` if input was `nil`.
94
+
95
+ #### Raises
96
+
97
+ - `ArgumentError`: If `from` is negative, `to` is non-negative, or `char` is not a single character.
98
+
99
+
100
+
101
+ ## Development
102
+
103
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
104
+
105
+ 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).
106
+
107
+ ## Contributing
108
+
109
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MichalZaporski/str_masker.
110
+
111
+ ## License
112
+
113
+ The gem is available as open source under the terms of the [MIT License](LICENSE).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'minitest/test_task'
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StrMasker
4
+ VERSION = '0.1.0'
5
+ end
data/lib/str_masker.rb ADDED
@@ -0,0 +1,46 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require_relative 'str_masker/version'
5
+
6
+ # A StrMasker gem main module.
7
+ module StrMasker
8
+ class << self
9
+ # Mask a portion of a string with asterisks (*) or your selected char between the specified positions.
10
+ #
11
+ # @param val: The string to mask
12
+ # @param from: Starting position for masking (inclusive, default: 0)
13
+ # @param to: Ending position for masking (must be negative, default: -1)
14
+ # @param char: The character to use for masking (default: '*')
15
+ # @return The masked string or nil if input is nil
16
+ #
17
+ # Example:
18
+ #
19
+ # Mask middle characters
20
+ # mask_string("password", from: 1, to: -2) #=> "p******d"
21
+ #
22
+ # Mask from start
23
+ # mask_string("secret", from: 0, to: -2) #=> "*****t"
24
+ #
25
+ # Default usage:
26
+ # mask_string("secret") #=> "******"
27
+ #
28
+ #: (String?, ?from: Integer, ?to: Integer, ?char: String) -> String?
29
+ def mask_string(val, from: 0, to: -1, char: '*')
30
+ raise ArgumentError, '"from" must be a non-negative int' unless from.is_a?(Integer) && from >= 0
31
+ raise ArgumentError, '"to" must be a negative int' unless to.is_a?(Integer) && to < 0
32
+ raise ArgumentError, '"char" must be a single character string' unless char.is_a?(String) && char.length == 1
33
+
34
+ return unless val
35
+
36
+ len = val.length
37
+ masked_chars_len = len - from + to + 1
38
+ return val if masked_chars_len <= 0
39
+
40
+ prefix = from.zero? ? '' : val[..(from - 1)]
41
+ suffix = to == -1 ? '' : val[(to + 1)..]
42
+ filling = char * masked_chars_len
43
+ "#{prefix}#{filling}#{suffix}"
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: str_masker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michał Zaporski
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-07-18 00:00:00.000000000 Z
11
+ dependencies: []
12
+ email:
13
+ - focus16k@gmail.com
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - ".rubocop.yml"
19
+ - CHANGELOG.md
20
+ - LICENSE
21
+ - README.md
22
+ - Rakefile
23
+ - lib/str_masker.rb
24
+ - lib/str_masker/version.rb
25
+ homepage: https://github.com/MichalZaporski/str_masker
26
+ licenses: []
27
+ metadata:
28
+ homepage_uri: https://github.com/MichalZaporski/str_masker
29
+ source_code_uri: https://github.com/MichalZaporski/str_masker
30
+ rubygems_mfa_required: 'true'
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 3.0.0
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.6.2
46
+ specification_version: 4
47
+ summary: A simple gem for masking sensitive strings.
48
+ test_files: []