rails_url_shortener 0.1.4 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '00481a62dbd5a849d91d094652186a58f7c8faa238b25c00f3ee05ac30f322b9'
4
- data.tar.gz: '06594fa9241eaf4fd745da60d8f6719993baef6768b9d0926a664d6b6867c617'
3
+ metadata.gz: d3bb06e219a4cd2bdfd83ef3d6b3f892bd1c4194cad99019aecd7c3e206ce4a5
4
+ data.tar.gz: 815b320b295c2c3d38b695b3412a656f7349fe0a454ddb63d9e127e33db7daef
5
5
  SHA512:
6
- metadata.gz: de9cfc42dde1ad9426a49bb3b87b17befb2c45a509f0890f2d71d817b7cabe06368ddbd07ed19d3ebc0ed2a049c238ad597f61c9e57488f206126f22ffee07aa
7
- data.tar.gz: e01451221e61b78bd9e9b9685d2b43f489817130682db36f5b0f7b1f3bbc09ca095a018066f4a7d28b82912ff6f5edfdec4420c2f968ea77970ac5f4e2da2b6b
6
+ metadata.gz: 452449b98fdec6c11fe2f544239552fd70d7c701caf7db69e8b9a581b62921f964c3dc21c163daf3b6be65b58ceaed8948d2e8a42b1b877ff1d0033689773814
7
+ data.tar.gz: 1b20f5faa06077c88256df869aae288e863e729ee84060e752d19186116ff96b78c7ebf35482171f48a240261b6cfb163ba57eb78e91368b44d21b44bcfe8d9d
@@ -0,0 +1,9 @@
1
+ module RailsUrlShortener
2
+ class ApplicationJob < ActiveJob::Base
3
+ # Automatically retry jobs that encountered a deadlock
4
+ # retry_on ActiveRecord::Deadlocked
5
+
6
+ # Most jobs are safe to ignore if the underlying records are no longer available
7
+ # discard_on ActiveJob::DeserializationError
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ module RailsUrlShortener
2
+ class IpCrawlerJob < ApplicationJob
3
+ queue_as :default
4
+
5
+ require 'http'
6
+ require 'json'
7
+
8
+ ##
9
+ # this function get the ip related data
10
+ #
11
+ # create or update an existing record information for an ip
12
+
13
+ def perform(visit)
14
+ if Ipgeo.exists?(ip: visit.ip) && Ipgeo.find_by(ip: visit.ip).updated_at <= Time.now - 3.months
15
+ # Then update
16
+ 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")
17
+ if ip.code == 200
18
+ ipgeo = Ipgeo.find_by(ip: visit.ip)
19
+ ipgeo.update(JSON.parse(ip.body).transform_keys { |key| key.to_s.underscore }.slice(*Ipgeo.column_names))
20
+ end
21
+ elsif !Ipgeo.exists?(ip: visit.ip)
22
+ # Then create a new record
23
+ 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")
24
+ if ip.code == 200
25
+ ipgeo = Ipgeo.new(JSON.parse(ip.body).transform_keys { |key| key.to_s.underscore }.slice(*Ipgeo.column_names))
26
+ ipgeo.ip = JSON.parse(ip.body)['query']
27
+ ipgeo.save
28
+ visit.ipgeo = ipgeo
29
+ end
30
+ end
31
+ rescue Exception => e
32
+ print('Error' + e.to_s)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module RailsUrlShortener
2
+ class Ipgeo < ApplicationRecord
3
+ has_many :visits
4
+ end
5
+ end
@@ -2,6 +2,7 @@ module RailsUrlShortener
2
2
  require 'json'
3
3
  class Visit < ApplicationRecord
4
4
  belongs_to :url
5
+ belongs_to :ipgeo, optional: true
5
6
 
6
7
  ##
7
8
  # Parse a request information and save
@@ -15,7 +16,7 @@ module RailsUrlShortener
15
16
  false
16
17
  else
17
18
  # save
18
- Visit.create(
19
+ visit = Visit.create(
19
20
  url: url,
20
21
  ip: request.ip,
21
22
  browser: browser.name,
@@ -25,6 +26,9 @@ module RailsUrlShortener
25
26
  bot: browser.bot?,
26
27
  user_agent: request.headers['User-Agent']
27
28
  )
29
+ # We enqueue a job for get more data later
30
+ IpCrawlerJob.perform_later(visit)
31
+ visit
28
32
  end
29
33
  end
30
34
  end
@@ -0,0 +1,28 @@
1
+ class CreateRailsUrlShortenerIpgeos < ActiveRecord::Migration[7.0]
2
+ def up
3
+ create_table :rails_url_shortener_ipgeos do |t|
4
+ t.string :ip
5
+ t.string :country
6
+ t.string :country_code
7
+ t.string :region
8
+ t.string :region_name
9
+ t.string :city
10
+ t.string :lat
11
+ t.string :lon
12
+ t.string :timezone
13
+ t.string :isp
14
+ t.string :org
15
+ t.string :as
16
+ t.boolean :mobile
17
+ t.boolean :proxy
18
+ t.boolean :hosting
19
+ t.timestamps
20
+ end
21
+ add_column :rails_url_shortener_visits, :ipgeo_id, :integer
22
+ add_index :rails_url_shortener_visits, :ipgeo_id
23
+ end
24
+ def down
25
+ remove_column :rails_url_shortener_visits, :ipgeo_id
26
+ drop_table :rails_url_shortener_ipgeos
27
+ end
28
+ end
@@ -2,5 +2,6 @@ module RailsUrlShortener
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace RailsUrlShortener
4
4
  require 'browser'
5
+ require 'http'
5
6
  end
6
7
  end
@@ -1,3 +1,3 @@
1
1
  module RailsUrlShortener
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_url_shortener
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - a-chacon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-17 00:00:00.000000000 Z
11
+ date: 2022-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.15.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: http
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 5.0.4
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 5.0.4
55
69
  description: RailsUrlShortener is a simple engine that provide to your rail's app
56
70
  the functionalities for short URLs. Like bitly.com, but working on your project
57
71
  only.
@@ -66,12 +80,16 @@ files:
66
80
  - Rakefile
67
81
  - app/controllers/rails_url_shortener/urls_controller.rb
68
82
  - app/helpers/rails_url_shortener/urls_helper.rb
83
+ - app/jobs/rails_url_shortener/application_job.rb
84
+ - app/jobs/rails_url_shortener/ip_crawler_job.rb
69
85
  - app/models/rails_url_shortener/application_record.rb
86
+ - app/models/rails_url_shortener/ipgeo.rb
70
87
  - app/models/rails_url_shortener/url.rb
71
88
  - app/models/rails_url_shortener/visit.rb
72
89
  - config/routes.rb
73
90
  - db/migrate/20220407202526_create_rails_url_shortener_urls.rb
74
91
  - db/migrate/20220407202539_create_rails_url_shortener_visits.rb
92
+ - db/migrate/20220418184647_create_rails_url_shortener_ipgeos.rb
75
93
  - lib/generators/rails_url_shortener/rails_url_shortener_generator.rb
76
94
  - lib/generators/rails_url_shortener/templates/initializer.rb
77
95
  - lib/rails_url_shortener.rb
@@ -83,9 +101,9 @@ licenses:
83
101
  - GPL-3.0
84
102
  metadata:
85
103
  bug_tracker_uri: https://www.github.com/a-chacon/rails-url-shortener/issues
86
- changelog_uri: https://www.github.com/a-chacon/rails-url-shortener/releases/tag/v0.1.4
104
+ changelog_uri: https://www.github.com/a-chacon/rails-url-shortener/releases/tag/v0.2.0
87
105
  documentation_uri: https://github.com/a-chacon/rails-url-shortener/blob/main/README.md
88
- source_code_uri: https://github.com/a-chacon/rails-url-shortener/tree/v0.1.4
106
+ source_code_uri: https://github.com/a-chacon/rails-url-shortener/tree/v0.2.0
89
107
  rubygems_mfa_required: 'true'
90
108
  post_install_message:
91
109
  rdoc_options: []