File tree Expand file tree Collapse file tree 4 files changed +18
-3
lines changed
Expand file tree Collapse file tree 4 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ ruby '~> 3.3.1'
77
88gem 'rails' , '~> 8.0.1'
99
10+ gem 'addressable' # More standards-compliant URI parser
1011gem 'bcrypt' # Use Active Model has_secure_password
1112gem 'bootsnap' , require : false # Reduces boot times through caching; required in config/boot.rb
1213gem 'feedjira' # Parse RSS feeds
Original file line number Diff line number Diff line change @@ -372,6 +372,7 @@ PLATFORMS
372372 ruby
373373
374374DEPENDENCIES
375+ addressable
375376 annotaterb
376377 bcrypt
377378 bootsnap
Original file line number Diff line number Diff line change 2727#
2828# fk_rails_... (subscription_id => subscriptions.id)
2929#
30+ require 'addressable/uri'
31+
3032class Entry < ApplicationRecord
3133 FEEDJIRA_KEYS_MAP = {
3234 author : :author ,
@@ -72,8 +74,7 @@ def read?
7274 end
7375
7476 def normalize_url ( input )
75- # Some urls might contain spaces, so we replace these
76- uri = URI ( input . gsub ( ' ' , '%20' ) )
77+ uri = Addressable ::URI . heuristic_parse ( input ) . normalize
7778 # Some entries might contain absolute/relative path to the page they were on
7879 uri = URI ( url ) . merge ( uri ) if url . present?
7980 uri . to_s
Original file line number Diff line number Diff line change @@ -71,10 +71,22 @@ class EntryTest < ActiveSupport::TestCase
7171 end
7272
7373 # Methods
74- test 'should be able to normalize urls found in post' do
74+ test 'should be able to normalize urls found in post when containing spaces ' do
7575 entry = build ( :entry , url : 'https://example.com/posts/first.html' )
7676
7777 assert_equal 'https://example.com/image%201.jpg' , entry . normalize_url ( 'https://example.com/image 1.jpg' )
78+ end
79+
80+ test 'should be able to normalize urls found in post when containing unicode' do
81+ entry = build ( :entry , url : 'https://example.com/posts/first.html' )
82+
83+ assert_equal 'https://example.com/image%F0%9F%96%A41.jpg' , entry . normalize_url ( 'https://example.com/image🖤1.jpg' )
84+ assert_equal 'https://example.com/image%E2%80%941.jpg' , entry . normalize_url ( 'https://example.com/image—1.jpg' )
85+ end
86+
87+ test 'should be able to normalize urls found in post when missing host' do
88+ entry = build ( :entry , url : 'https://example.com/posts/first.html' )
89+
7890 assert_equal 'https://example.com/image.jpg' , entry . normalize_url ( '/image.jpg' )
7991 end
8092end
You can’t perform that action at this time.
0 commit comments