x_post_sanitizer 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: c1b728cf95a2693f8bb1eb632e28ce05767f2c876854db333256e561ca50b323
4
+ data.tar.gz: 6b57921524d5c4f2891779184f432b8dbd215bf88452fb0eb37fa502bbb013c6
5
+ SHA512:
6
+ metadata.gz: 02765f0f7e8e03d3185e4b8b56b7c07d5ace9830b0276b7fc2bc1b97d8cdf42124ad852d6bb5537a2a2b51f0b526570f9e17dcdb4567472c3fd9e55649ed2b90
7
+ data.tar.gz: 3ef6a6c9c5db8bab7571d7533e4bebd038b9966e67991b7e4d8bb8240126aba492c4a4435fe9520340dfdac7eb6e14073d2fc898d9c199ed7b89345d6783f16c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.yardopts ADDED
@@ -0,0 +1,6 @@
1
+ --markup markdown
2
+ --no-private
3
+ --hide-void-return
4
+ -
5
+ CHANGELOG.md
6
+ LICENSE.txt
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## [Unreleased]
2
+ [full changelog](http://github.com/sue445/x_post_sanitizer/compare/v0.1.0...main)
3
+
4
+ ## [0.1.0](https://github.com/sue445/x_post_sanitizer/releases/tag/v0.1.0) - 2025-05-04
5
+
6
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 sue445
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,68 @@
1
+ # XPostSanitizer
2
+ Sanitize X Post (formerly Twitter Tweet)
3
+
4
+ [![test](https://github.com/sue445/x_post_sanitizer/actions/workflows/test.yml/badge.svg)](https://github.com/sue445/x_post_sanitizer/actions/workflows/test.yml)
5
+
6
+ ## Installation
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```bash
10
+ bundle add x_post_sanitizer
11
+ ```
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ ```bash
16
+ gem install x_post_sanitizer
17
+ ```
18
+
19
+ ## Usage
20
+ Use `XPostSanitizer.sanitize_text`
21
+
22
+ e.g
23
+
24
+ ```ruby
25
+ require "json"
26
+ require "x_post_sanitizer"
27
+
28
+ # https://x.com/github/status/866677968608927744
29
+ tweet = JSON.parse(File.read("spec/support/fixtures/full_text_tweet1.json"))
30
+
31
+ tweet["full_text"]
32
+ #=> "Introducing GitHub Marketplace, a new place to browse and buy integrations using your GitHub account. https://t.co/mPTtAxnU5z https://t.co/Wz2mUql2lc"
33
+
34
+ XPostSanitizer.sanitize_text(tweet)
35
+ #=> "Introducing GitHub Marketplace, a new place to browse and buy integrations using your GitHub account. https://github.com/blog/2359-introducing-github-marketplace-and-more-tools-to-customize-your-workflow"
36
+ ```
37
+
38
+ ## Features
39
+ * Expand urls in `text` or `full_text` (e.g. `t.co` url -> original url)
40
+ * Remove media urls in `text` or `full_text`
41
+ * Unescape special html characters in `text` or `full_text` (e.g. `(&gt; &lt;)` -> `(> <)`)
42
+
43
+ All methods and options are followings
44
+
45
+ https://sue445.github.io/x_post_sanitizer/XPostSanitizer.html
46
+
47
+ ## vs tweet_sanitizer
48
+ This has the same features as [tweet_sanitizer](https://github.com/sue445/tweet_sanitizer) but with the following differences
49
+
50
+ * tweet_sanitizer
51
+ * Requires https://github.com/sferik/twitter-ruby
52
+ * x_post_sanitizer
53
+ * Requires only plain X API response. (`Hash` which parsed JSON string)
54
+ * No requires https://github.com/sferik/twitter-ruby
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ 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
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sue445/x_post_sanitizer.
65
+
66
+ ## License
67
+
68
+ 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,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ namespace :rbs do
9
+ desc "`rbs collection install` and `git commit`"
10
+ task :install do
11
+ sh "rbs collection install"
12
+ sh "git add rbs_collection.lock.yaml"
13
+ sh "git commit -m 'rbs collection install' || true"
14
+ end
15
+ end
16
+
17
+ desc "Check rbs"
18
+ task :rbs do
19
+ sh "rbs validate"
20
+ sh "steep check"
21
+ end
22
+
23
+ task default: %i[spec rbs]
data/Steepfile ADDED
@@ -0,0 +1,34 @@
1
+ # D = Steep::Diagnostic
2
+
3
+ target :lib do
4
+ signature "sig"
5
+ ignore_signature "sig/test"
6
+
7
+ check "lib" # Directory name
8
+ check "path/to/source.rb" # File name
9
+ check "app/models/**/*.rb" # Glob
10
+ # ignore "lib/templates/*.rb"
11
+
12
+ # library "pathname" # Standard libraries
13
+ # library "strong_json" # Gems
14
+
15
+ collection_config "rbs_collection.yaml"
16
+
17
+ # configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
18
+ # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
19
+ # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
20
+ # configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
21
+ # configure_code_diagnostics do |hash| # You can setup everything yourself
22
+ # hash[D::Ruby::NoMethod] = :information
23
+ # end
24
+ end
25
+
26
+ # target :test do
27
+ # unreferenced! # Skip type checking the `lib` code when types in `test` target is changed
28
+ # signature "sig/test" # Put RBS files for tests under `sig/test`
29
+ # check "test" # Type check Ruby scripts under `test`
30
+ #
31
+ # configure_code_diagnostics(D::Ruby.lenient) # Weak type checking for test code
32
+ #
33
+ # # library "pathname" # Standard libraries
34
+ # end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XPostSanitizer
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "x_post_sanitizer/version"
4
+
5
+ module XPostSanitizer
6
+ class Error < StandardError; end
7
+
8
+ # Sanitize X Post (formerly Twitter Tweet)
9
+ #
10
+ # @param tweet [Hash<String, Object>] Tweet object
11
+ # @param use_retweeted_tweet [Boolean] Use original retweeted tweet if exists
12
+ # @param expand_url [Boolean] Whether expand url in tweet (e.g. `t.co` url -> original url)
13
+ # @param remove_media_url [Boolean] Whether remove media url in tweet
14
+ # @param unescape [Boolean] Whether unescape in tweet (e.g. `(&gt; &lt;)` -> `(> <)`)
15
+ #
16
+ # @return [String] Sanitized text in tweet
17
+ #
18
+ # @see https://developer.x.com/en/docs/x-api/v1/data-dictionary/object-model/tweet
19
+ def self.sanitize_text(tweet, use_retweeted_tweet: true, expand_url: true, remove_media_url: true, unescape: true)
20
+ # Original RT status exists in retweeted_status
21
+ tweet = tweet["retweeted_status"] if use_retweeted_tweet && tweet["retweeted_status"]
22
+
23
+ text = tweet_full_text(tweet)
24
+ text = expand_urls_in_text(tweet, text) if expand_url
25
+ text = remove_media_urls_in_tweet(tweet, text) if remove_media_url
26
+ text = CGI.unescapeHTML(text) if unescape
27
+ text
28
+ end
29
+
30
+ # @param tweet [Hash<String, Object>] Tweet object
31
+ # @param text [String]
32
+ #
33
+ # @return [String]
34
+ #
35
+ # @see https://developer.x.com/en/docs/x-api/v1/data-dictionary/object-model/tweet
36
+ def self.expand_urls_in_text(tweet, text)
37
+ urls = tweet.dig("entities", "urls")
38
+
39
+ return text unless urls
40
+
41
+ urls.reverse.each_with_object(text.dup) do |url, expanded|
42
+ pos1 = url.dig("indices", 0)
43
+ pos2 = url.dig("indices", 1)
44
+ expanded[pos1, pos2-pos1] = url["expanded_url"] if url["expanded_url"] && pos1 && pos2
45
+ end
46
+ end
47
+
48
+ # @param tweet [Hash<String, Object>] Tweet object
49
+ #
50
+ # @return [String] `full_text` attribute if exist
51
+ #
52
+ # @see https://developer.x.com/en/docs/x-api/v1/data-dictionary/object-model/tweet
53
+ def self.tweet_full_text(tweet)
54
+ tweet["full_text"] || tweet["text"]
55
+ end
56
+
57
+ # @param tweet [Hash<String, Object>] Tweet object
58
+ # @param text [String]
59
+ #
60
+ # @return [String]
61
+ #
62
+ # @see https://developer.x.com/en/docs/x-api/v1/data-dictionary/object-model/tweet
63
+ def self.remove_media_urls_in_tweet(tweet, text)
64
+ medias = get_medias(tweet)
65
+
66
+ return text if medias.empty?
67
+
68
+ medias.each_with_object(text.dup) do |media, t|
69
+ t.gsub!(media["url"], "")
70
+ t.strip!
71
+ end
72
+ end
73
+
74
+ # @param tweet [Hash<String, Object>] Tweet object
75
+ #
76
+ # @return [Array<Hash>]
77
+ #
78
+ # @see https://developer.x.com/en/docs/x-api/v1/data-dictionary/object-model/tweet
79
+ def self.get_medias(tweet)
80
+ extended_entities_medias = tweet.dig("extended_entities", "media")
81
+ return extended_entities_medias if extended_entities_medias
82
+
83
+ tweet.dig("entities", "media") || []
84
+ end
85
+ private_class_method :get_medias
86
+ end
@@ -0,0 +1,100 @@
1
+ ---
2
+ path: ".gem_rbs_collection"
3
+ gems:
4
+ - name: ast
5
+ version: '2.4'
6
+ source:
7
+ type: git
8
+ name: ruby/gem_rbs_collection
9
+ revision: eb6867fdb70e82e3aac6b51623ed1b6760c3c072
10
+ remote: https://github.com/ruby/gem_rbs_collection.git
11
+ repo_dir: gems
12
+ - name: binding_of_caller
13
+ version: '1.0'
14
+ source:
15
+ type: git
16
+ name: ruby/gem_rbs_collection
17
+ revision: eb6867fdb70e82e3aac6b51623ed1b6760c3c072
18
+ remote: https://github.com/ruby/gem_rbs_collection.git
19
+ repo_dir: gems
20
+ - name: cgi
21
+ version: '0'
22
+ source:
23
+ type: stdlib
24
+ - name: dbm
25
+ version: '0'
26
+ source:
27
+ type: stdlib
28
+ - name: diff-lcs
29
+ version: '1.5'
30
+ source:
31
+ type: git
32
+ name: ruby/gem_rbs_collection
33
+ revision: eb6867fdb70e82e3aac6b51623ed1b6760c3c072
34
+ remote: https://github.com/ruby/gem_rbs_collection.git
35
+ repo_dir: gems
36
+ - name: fileutils
37
+ version: '0'
38
+ source:
39
+ type: stdlib
40
+ - name: io-console
41
+ version: '0'
42
+ source:
43
+ type: stdlib
44
+ - name: parser
45
+ version: '3.2'
46
+ source:
47
+ type: git
48
+ name: ruby/gem_rbs_collection
49
+ revision: eb6867fdb70e82e3aac6b51623ed1b6760c3c072
50
+ remote: https://github.com/ruby/gem_rbs_collection.git
51
+ repo_dir: gems
52
+ - name: pp
53
+ version: '0'
54
+ source:
55
+ type: stdlib
56
+ - name: prettyprint
57
+ version: '0'
58
+ source:
59
+ type: stdlib
60
+ - name: prism
61
+ version: 1.4.0
62
+ source:
63
+ type: rubygems
64
+ - name: pstore
65
+ version: '0'
66
+ source:
67
+ type: stdlib
68
+ - name: psych
69
+ version: '0'
70
+ source:
71
+ type: stdlib
72
+ - name: rake
73
+ version: '13.0'
74
+ source:
75
+ type: git
76
+ name: ruby/gem_rbs_collection
77
+ revision: eb6867fdb70e82e3aac6b51623ed1b6760c3c072
78
+ remote: https://github.com/ruby/gem_rbs_collection.git
79
+ repo_dir: gems
80
+ - name: rdoc
81
+ version: '0'
82
+ source:
83
+ type: stdlib
84
+ - name: rspec-parameterized-core
85
+ version: 2.0.0
86
+ source:
87
+ type: rubygems
88
+ - name: rspec-parameterized-table_syntax
89
+ version: 2.0.0
90
+ source:
91
+ type: rubygems
92
+ - name: stringio
93
+ version: '0'
94
+ source:
95
+ type: stdlib
96
+ - name: tempfile
97
+ version: '0'
98
+ source:
99
+ type: stdlib
100
+ gemfile_lock_path: Gemfile.lock
@@ -0,0 +1,23 @@
1
+ # Download sources
2
+ sources:
3
+ - type: git
4
+ name: ruby/gem_rbs_collection
5
+ remote: https://github.com/ruby/gem_rbs_collection.git
6
+ revision: main
7
+ repo_dir: gems
8
+
9
+ # You can specify local directories as sources also.
10
+ # - type: local
11
+ # path: path/to/your/local/repository
12
+
13
+ # A directory to install the downloaded RBSs
14
+ path: .gem_rbs_collection
15
+
16
+ gems:
17
+ - name: cgi
18
+ - name: rbs
19
+ ignore: true
20
+ - name: steep
21
+ ignore: true
22
+ - name: yard
23
+ ignore: true
@@ -0,0 +1,26 @@
1
+ module XPostSanitizer
2
+ VERSION: String
3
+
4
+ class Error < StandardError
5
+ end
6
+
7
+ type tweet = Hash[String, untyped]
8
+
9
+ def self.sanitize_text: (
10
+ tweet tweet,
11
+ ?use_retweeted_tweet: bool,
12
+ ?expand_url: bool,
13
+ ?remove_media_url: bool,
14
+ ?unescape: bool
15
+ ) -> String
16
+
17
+ def self.expand_urls_in_text: (tweet tweet, String text) -> String
18
+
19
+ def self.tweet_full_text: (tweet tweet) -> String
20
+
21
+ def self.remove_media_urls_in_tweet: (tweet tweet, String text) -> String
22
+
23
+ def self.get_medias: (tweet tweet) -> Array[Hash[String, untyped]]
24
+
25
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
26
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: x_post_sanitizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sue445
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: irb
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '13.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '13.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rbs
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rspec-parameterized
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ - !ruby/object:Gem::Dependency
83
+ name: steep
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ - !ruby/object:Gem::Dependency
97
+ name: yard
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Sanitize X Post (formerly Twitter Tweet)
111
+ email:
112
+ - sue445@sue445.net
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - ".rspec"
118
+ - ".yardopts"
119
+ - CHANGELOG.md
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - Steepfile
124
+ - lib/x_post_sanitizer.rb
125
+ - lib/x_post_sanitizer/version.rb
126
+ - rbs_collection.lock.yaml
127
+ - rbs_collection.yaml
128
+ - sig/x_post_sanitizer.rbs
129
+ homepage: https://github.com/sue445/x_post_sanitizer
130
+ licenses:
131
+ - MIT
132
+ metadata:
133
+ homepage_uri: https://github.com/sue445/x_post_sanitizer
134
+ source_code_uri: https://github.com/sue445/x_post_sanitizer
135
+ changelog_uri: https://github.com/sue445/x_post_sanitizer/blob/main/CHANGELOG.md
136
+ documentation_uri: https://sue445.github.io/x_post_sanitizer/
137
+ rubygems_mfa_required: 'true'
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: 3.1.0
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubygems_version: 3.6.7
153
+ specification_version: 4
154
+ summary: Sanitize X Post (formerly Twitter Tweet)
155
+ test_files: []