Skip to content

feat: add referer to visits model #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ GEM
tzinfo (~> 2.0, >= 2.0.5)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.2)
ast (2.4.3)
base64 (0.2.0)
benchmark (0.4.0)
bigdecimal (3.1.9)
browser (6.2.0)
builder (3.3.0)
byebug (11.1.3)
byebug (12.0.0)
concurrent-ruby (1.3.5)
connection_pool (2.5.0)
connection_pool (2.5.3)
crack (1.0.0)
bigdecimal
rexml
Expand All @@ -100,7 +100,7 @@ GEM
erubi (1.13.1)
faker (3.5.1)
i18n (>= 1.8.11, < 2)
ffi (1.17.1-x86_64-linux-gnu)
ffi (1.17.2-x86_64-linux-gnu)
ffi-compiler (1.3.2)
ffi (>= 1.15.5)
rake
Expand All @@ -119,17 +119,17 @@ GEM
i18n (1.14.7)
concurrent-ruby (~> 1.0)
io-console (0.8.0)
irb (1.15.1)
irb (1.15.2)
pp (>= 0.6.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.10.2)
json (2.11.3)
language_server-protocol (3.17.0.4)
lint_roller (1.1.0)
llhttp-ffi (0.5.1)
ffi-compiler (~> 1.0)
rake (~> 13.0)
logger (1.6.6)
logger (1.7.0)
loofah (2.24.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -142,7 +142,7 @@ GEM
mini_mime (1.1.5)
minitest (5.25.5)
minitest-cc (1.0.0)
net-imap (0.5.6)
net-imap (0.5.8)
date
net-protocol
net-pop (0.1.2)
Expand All @@ -152,22 +152,23 @@ GEM
net-smtp (0.5.1)
net-protocol
nio4r (2.7.4)
nokogiri (1.18.4-x86_64-linux-gnu)
nokogiri (1.18.8-x86_64-linux-gnu)
racc (~> 1.4)
parallel (1.26.3)
parser (3.3.7.1)
parallel (1.27.0)
parser (3.3.8.0)
ast (~> 2.4.1)
racc
pp (0.6.2)
prettyprint
prettyprint (0.2.0)
psych (5.2.3)
prism (1.4.0)
psych (5.2.4)
date
stringio
public_suffix (6.0.1)
public_suffix (6.0.2)
racc (1.8.1)
rack (3.1.12)
rack-session (2.1.0)
rack (3.1.14)
rack-session (2.1.1)
base64 (>= 0.1.0)
rack (>= 3.0.0)
rack-test (2.2.0)
Expand Down Expand Up @@ -205,36 +206,38 @@ GEM
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.2.1)
rdoc (6.12.0)
rdoc (6.13.1)
psych (>= 4.0.0)
regexp_parser (2.10.0)
reline (0.6.0)
reline (0.6.1)
io-console (~> 0.5)
rexml (3.4.1)
rubocop (1.74.0)
rubocop (1.75.5)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.1.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.38.0, < 2.0)
rubocop-ast (>= 1.44.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.39.0)
parser (>= 3.3.1.0)
rubocop-ast (1.44.1)
parser (>= 3.3.7.2)
prism (~> 1.4)
ruby-progressbar (1.13.0)
securerandom (0.4.1)
sprockets (4.2.1)
sprockets (4.2.2)
concurrent-ruby (~> 1.0)
logger
rack (>= 2.2.4, < 4)
sprockets-rails (3.5.2)
actionpack (>= 6.1)
activesupport (>= 6.1)
sprockets (>= 3.0.0)
sqlite3 (2.6.0-x86_64-linux-gnu)
stringio (3.1.5)
stringio (3.1.7)
thor (1.3.2)
timeout (0.4.3)
tzinfo (2.0.6)
Expand Down
38 changes: 18 additions & 20 deletions app/models/rails_url_shortener/visit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,27 @@ class Visit < ApplicationRecord
# Parse a request information and save
#
# Return boolean

# rubocop:disable Metrics/AbcSize
def self.parse_and_save(url, request)
# browser detection
browser(request)
if !RailsUrlShortener.save_bots_visits && @browser.bot?
false
else
# save
visit = Visit.create(
url: url,
ip: request.ip,
browser: @browser.name,
browser_version: @browser.full_version,
platform: @browser.platform.name,
platform_version: @browser.platform.version,
bot: @browser.bot?,
user_agent: request.headers['User-Agent']
)
# We enqueue a job for get more data later
IpCrawlerJob.perform_later(visit)
visit
end
return false if !RailsUrlShortener.save_bots_visits && @browser.bot?

visit = Visit.create(
url: url,
ip: request.ip,
browser: @browser.name,
browser_version: @browser.full_version,
platform: @browser.platform.name,
platform_version: @browser.platform.version,
bot: @browser.bot?,
user_agent: request.headers['User-Agent'],
referer: request.headers['Referer']
)

IpCrawlerJob.perform_later(visit)
visit
end
# rubocop:enable Metrics/AbcSize

def self.browser(request)
@browser = Browser.new(request.headers['User-Agent'])
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20250508120951_add_referer_to_vistis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRefererToVistis < ActiveRecord::Migration[7.0]
def change
add_column :rails_url_shortener_visits, :referer, :string, default: ""
end
end
4 changes: 2 additions & 2 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2022_04_18_184647) do
ActiveRecord::Schema[7.0].define(version: 2025_05_08_120951) do
create_table "rails_url_shortener_ipgeos", force: :cascade do |t|
t.string "ip"
t.string "country"
Expand Down Expand Up @@ -56,6 +56,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "ipgeo_id"
t.string "referer", default: ""
t.index ["ipgeo_id"], name: "index_rails_url_shortener_visits_on_ipgeo_id"
t.index ["url_id"], name: "index_rails_url_shortener_visits_on_url_id"
end
Expand All @@ -66,5 +67,4 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
7 changes: 5 additions & 2 deletions test/models/rails_url_shortener/visit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class VisitTest < ActiveSupport::TestCase
url: rails_url_shortener_urls(:one),
ip: '192.168.8.1',
user_agent: 'Mozilla/5.0 (X11; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0',
meta: 'Mozilla/5.0 (X11; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0'
meta: 'Mozilla/5.0 (X11; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0',
referer: 'https://example.com'
)
assert visit.save
assert_equal visit.url, rails_url_shortener_urls(:one)
Expand All @@ -43,7 +44,8 @@ class VisitTest < ActiveSupport::TestCase
test 'parse and save' do
# generate a fake request
request = ActionDispatch::TestRequest.create(env = Rack::MockRequest.env_for('/', 'HTTP_HOST' => 'test.host'.b,
'REMOTE_ADDR' => '1.0.0.0'.b, 'HTTP_USER_AGENT' => 'Rails Testing'.b))
'REMOTE_ADDR' => '1.0.0.0'.b, 'HTTP_USER_AGENT' => 'Rails Testing'.b,
'HTTP_REFERER' => 'https://example.com'.b))
request.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Safari/605.1.15'
# implement the method
visit = nil
Expand All @@ -58,6 +60,7 @@ class VisitTest < ActiveSupport::TestCase
assert visit.browser_version, Browser.new(request.user_agent).full_version
assert visit.platform, Browser.new(request.user_agent).platform.name
assert visit.platform_version, Browser.new(request.user_agent).platform.version
assert visit.referer, request.headers['Referer']
end

test "don't save bots" do
Expand Down