-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from a-chacon/add_ip_model
feat: add ipgeo object related to visit with data of ip
- Loading branch information
Showing
18 changed files
with
326 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module RailsUrlShortener | ||
class ApplicationJob < ActiveJob::Base | ||
# Automatically retry jobs that encountered a deadlock | ||
# retry_on ActiveRecord::Deadlocked | ||
|
||
# Most jobs are safe to ignore if the underlying records are no longer available | ||
# discard_on ActiveJob::DeserializationError | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module RailsUrlShortener | ||
class IpCrawlerJob < ApplicationJob | ||
queue_as :default | ||
|
||
require 'http' | ||
require 'json' | ||
|
||
## | ||
# this function get the ip related data | ||
# | ||
# create or update an existing record information for an ip | ||
|
||
def perform(visit) | ||
if Ipgeo.exists?(ip: visit.ip) && Ipgeo.find_by(ip: visit.ip).updated_at <= Time.now - 3.months | ||
# Then update | ||
ip = HTTP.get("http://ip-api.com/json/#{visit.ip}?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,mobile,proxy,hosting,query") | ||
if ip.code == 200 | ||
ipgeo = Ipgeo.find_by(ip: visit.ip) | ||
ipgeo.update(JSON.parse(ip.body).transform_keys { |key| key.to_s.underscore }.slice(*Ipgeo.column_names)) | ||
end | ||
elsif !Ipgeo.exists?(ip: visit.ip) | ||
# Then create a new record | ||
ip = HTTP.get("http://ip-api.com/json/#{visit.ip}?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,mobile,proxy,hosting,query") | ||
if ip.code == 200 | ||
ipgeo = Ipgeo.new(JSON.parse(ip.body).transform_keys { |key| key.to_s.underscore }.slice(*Ipgeo.column_names)) | ||
ipgeo.ip = JSON.parse(ip.body)['query'] | ||
ipgeo.save | ||
visit.ipgeo = ipgeo | ||
end | ||
end | ||
rescue Exception => e | ||
print('Error' + e.to_s) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module RailsUrlShortener | ||
class Ipgeo < ApplicationRecord | ||
has_many :visits | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
db/migrate/20220418184647_create_rails_url_shortener_ipgeos.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class CreateRailsUrlShortenerIpgeos < ActiveRecord::Migration[7.0] | ||
def up | ||
create_table :rails_url_shortener_ipgeos do |t| | ||
t.string :ip | ||
t.string :country | ||
t.string :country_code | ||
t.string :region | ||
t.string :region_name | ||
t.string :city | ||
t.string :lat | ||
t.string :lon | ||
t.string :timezone | ||
t.string :isp | ||
t.string :org | ||
t.string :as | ||
t.boolean :mobile | ||
t.boolean :proxy | ||
t.boolean :hosting | ||
t.timestamps | ||
end | ||
add_column :rails_url_shortener_visits, :ipgeo_id, :integer | ||
add_index :rails_url_shortener_visits, :ipgeo_id | ||
end | ||
def down | ||
remove_column :rails_url_shortener_visits, :ipgeo_id | ||
drop_table :rails_url_shortener_ipgeos | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module RailsUrlShortener | ||
VERSION = "0.1.4" | ||
VERSION = "0.2.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
# This model initially had no columns defined. If you add columns to the | ||
# model remove the "{}" from the fixture names and add the columns immediately | ||
# below each fixture, per the syntax in the comments below | ||
# | ||
one: | ||
ip: 66.90.76.179 | ||
country_code: cl | ||
country: chile | ||
region: | ||
region_name: Metropolitana | ||
city: santiago |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.