rails_url_shortener 0.2.10 → 0.4.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: d2eae8996ec1ecb9a6edf015f801174ba561d47a67f5898f516d8c37d4c10118
4
- data.tar.gz: 4070d28713ebdf08bf96068f4a10c1ee2369c77568a8fb0a11ea8b206e1f0521
3
+ metadata.gz: bada11c316875a094b6bb41ca076db3adc1b52bf277206cea1a910b4505974b3
4
+ data.tar.gz: 3ceae3dc8be6b256b83b196b6602ae65f07c6e17d885bf7eb0308127b4a18356
5
5
  SHA512:
6
- metadata.gz: b1d74a3df31a9b3a1eaa9eba0d2387ca1b2d572f40147ae12321172a093e998af28e63bdf8b8a6ab10416843a0c7e3a2574abe37ba5603b5086d0b8e5214eb8c
7
- data.tar.gz: ccb01b9e625ee0f2a3a38992a978cade1e05b0fc8e0144e12e93e1a3c95ff59c7683d2cd20d66c3238e5a88d90432377b880c9bbf8ee6f31b69a8ebad0be791d
6
+ metadata.gz: a9081dbc5d20f346df7e9f712a509f11e6e358a13e6309ced32a8f88ea6b3ebbde0e3ef0a702b1c1e39bfd11c441122b863d73f4dba7780e920e3ebf771f6bf5
7
+ data.tar.gz: 354c754d94b1cd677523d2f4bfde370722311b584fd041ee8a2272930d2df3c09a8e18f64501d12ed8aca4a833b2b302767fb20e1b85c402af06f64dc80bb215
data/README.md CHANGED
@@ -1,25 +1,59 @@
1
+
2
+ [![Ruby on Rails CI](https://github.com/a-chacon/rails-url-shortener/actions/workflows/rubyonrails.yml/badge.svg)](https://github.com/a-chacon/rails-url-shortener/actions/workflows/rubyonrails.yml)
3
+ ![Gem Version](https://img.shields.io/gem/v/rails_url_shortener)
4
+
5
+ ![GitHub License](https://img.shields.io/github/license/a-chacon/rails_url_shortener)
6
+
1
7
  # RailsUrlShortener
2
8
 
3
- RailsUrlShortener is a small rails engine that provide your app with short URLs functionalities. Like a Bitly on your app. By default, RailsUrlShortener save the visits to your link for future interesting things that you may want to do.
9
+ RailsUrlShortener is a small Rails engine that provides your app with short URL functionality and IP logging capabilities - like having your own Bitly service. By default, RailsUrlShortener saves all visits to your links for future analysis or other interesting uses.
10
+
11
+ Why give your data to a third-party app when you can manage it yourself?
12
+
13
+ You can see a **demo project** of what you can do with this engine [HERE](https://paso.fly.dev/).
14
+
15
+ ## Key Features
16
+
17
+ Here are some of the things you can do with RailsUrlShortener:
18
+
19
+ * Generate unique keys for links
20
+ * Provide a controller method that finds, saves request information, and performs a 301 redirect to the original URL
21
+ * Associate short links with models in your app
22
+ * Save browser, system, and IP data from each request
23
+ * Create temporary short links using the expires_at option
24
+ * Get IP data from a third-party service
25
+
26
+ ## Installation
27
+
28
+ Follow these steps to install and configure RailsUrlShortener:
29
+
30
+ 1. Add this line to your application's Gemfile:
31
+
32
+ ```ruby
33
+ gem "rails_url_shortener"
34
+ ```
4
35
 
5
- Why give your data to a third party app if you can do it by yourself?
36
+ 2. Install the gem by running:
6
37
 
7
- You can see a **demo project** of what you can do with this engine [HERE](https://s.a-chacon.com/).
38
+ ```bash
39
+ bundle install
40
+ ```
8
41
 
9
- ## Key features
42
+ 3. Install and run the migrations:
10
43
 
11
- A few of the things you can do with RailsUrlShortener:
44
+ ```bash
45
+ bin/rails rails_url_shortener:install:migrations db:migrate
46
+ ```
47
+
48
+ 4. Generate the initializer for configuration:
12
49
 
13
- * Generate unique keys for links.
14
- * Provide a method controller that find, save request information and does a 301 redirect to the original url.
15
- * The short links can be associated with a model in your app.
16
- * Save interesting things like browser, system and ip data of the 'un-shortened' request.
17
- * Temporal short links using the expires_at option.
18
- * Get IP data from third part service.
50
+ ```bash
51
+ rails generate rails_url_shortener
52
+ ```
19
53
 
20
54
  ## Usage
21
55
 
22
- ### 1. Mount the engine
56
+ 1. Mount the engine
23
57
 
24
58
  Mount the engine on your app adding the next code on your config/routes.rb:
25
59
 
@@ -29,96 +63,75 @@ Mount the engine on your app adding the next code on your config/routes.rb:
29
63
  mount RailsUrlShortener::Engine, at: "/"
30
64
 
31
65
  ```
32
- ### 2. Generate the short link
66
+
67
+ 2. Generate the short link
33
68
 
34
69
  And generate the short links like you want:
35
70
 
36
- - Using the helper method, this return the ready short link.
71
+ * Using the helper method, this return the ready short link.
37
72
 
38
73
  ```ruby
39
74
  short_url("https://www.github.com/a-chacon/rails-url-shortener")
40
75
  ```
41
76
 
42
- - Or model method, this return the object built. So you can save this on a variable, extract the key and build the short link by your own:
77
+ * Or model method, this return the object built. So you can save this on a variable, extract the key and build the short link by your own:
43
78
 
44
79
  ```ruby
45
80
  RailsUrlShortener::Url.generate("https://www.github.com/a-chacon/rails-url-shortener")
46
81
  ```
47
- ### 3. Share the short link
82
+
83
+ 3. Share the short link
48
84
 
49
85
  **Then share the short link to your users or wherever you want.**
50
86
 
51
87
  ## Deeper
52
88
 
53
89
  Full params for the short_url helper:
90
+
54
91
  ```ruby
55
92
  short_url(url, owner: nil, key: nil, expires_at: nil, category: nil, url_options: {})
56
93
  ```
94
+
57
95
  Where:
58
- * **url**: Long url for short.
59
- * **owner**: Is a model of your app. You can relate an url whatever you want in your app.
60
- * **key**: Is a custom key that you want to set up.
61
- * **expires_at**: Is a datetime for expiration, after this the redirection doesn't work.
62
- * **category**: Tag that you want for that link.
63
- * **url_options**: Options for the url_for generator. Ex: subdomain or protocol.
64
96
 
97
+ * **url**: The long URL to be shortened
98
+ * **owner**: A model from your app to associate with the URL
99
+ * **key**: A custom key for the short URL (optional)
100
+ * **expires_at**: Expiration datetime (after which the redirect won't work)
101
+ * **category**: A tag for categorizing the link
102
+ * **url_options**: Options for the URL generator (e.g., subdomain or protocol)
103
+
104
+ The `generate` model method accepts the same parameters except for `url_options`:
65
105
 
66
- And the same for the generate model method except for url_options:
67
106
  ```ruby
68
107
  RailsUrlShortener::Url.generate(url, owner: nil, key: nil, expires_at: nil, category: nil)
69
108
  ```
70
109
 
71
- ### Data saved
110
+ ### Data Collection
72
111
 
73
- By default, this engine save all request made on your short url, you can use that data for some analytics or simple IP logger. So for get the data in a controller or do wherever you want, you can use the Visit model related to an Url:
112
+ By default, the engine saves all requests made to your short URLs. You can use this data for analytics or IP logging. To access the data:
74
113
 
75
- ```ruby
76
- RailsUrlShortener::Url.find_by_key("key").visits # all visits
114
+ 1. Get visits for a specific URL:
77
115
 
78
- ```
79
- Or using the model class:
80
116
  ```ruby
81
- RailsUrlShortener::Visit.all # all in database
117
+ RailsUrlShortener::Url.find_by_key("key").visits
82
118
  ```
83
119
 
84
- And a Visit is related to a Ipgeo model that contain information about the ip, so you can view this using the active record relation:
85
- ```ruby
86
- RailsUrlShortener::Visit.first.ipgeo # Ipgeo object that contain information of the ip
87
- ```
88
-
89
- ### Ip data
90
-
91
- When a Visit record is created, a job is enqueue for get Ip data from [this](https://ip-api.com/) service and create the Ipgeo record. It is integrated to the free endpoint, so if you think that you have more than 45 different IPS querying in a minute to your app, we need to think in a new solution.
92
-
93
- ## Installation
94
-
95
- Add this line to your application's Gemfile:
120
+ 2. Get all visits:
96
121
 
97
122
  ```ruby
98
- gem "rails_url_shortener"
123
+ RailsUrlShortener::Visit.all
99
124
  ```
100
125
 
101
- Or install it yourself as:
102
- ```bash
103
- gem install rails_url_shortener
104
- ```
105
-
106
- Then execute:
107
- ```bash
108
- bundle
109
- ```
126
+ Each Visit is associated with an Ipgeo model that contains information about the IP address:
110
127
 
111
- And finally install & run the migrations on your project and migrate:
112
- ```bash
113
- bin/rails rails_url_shortener:install:migrations db:migrate
128
+ ```ruby
129
+ RailsUrlShortener::Visit.first.ipgeo
114
130
  ```
115
131
 
116
- For the configurations generate the initializer whith this:
132
+ ### IP Data Collection
117
133
 
118
- ```bash
119
- rails generate RailsUrlShortener:initializer
120
- ```
121
- **Here is important to configure the host at least if your are not running your app in localhost**
134
+ When a Visit record is created, a background job is enqueued to fetch IP data from the [ip-api.com](https://ip-api.com/) service and create an Ipgeo record. This uses the free endpoint, which has a limit of 45 different IPs per minute. If you expect higher traffic, you'll need to implement an alternative solution.
122
135
 
123
136
  ## Contributing
124
137
 
@@ -134,4 +147,7 @@ Don't forget to give the project a star! Thanks again!
134
147
  5. Open a Pull Request
135
148
 
136
149
  ## License
150
+
137
151
  The gem is available as open source under the terms of the [GPL-3.0 License](https://www.github.com/a-chacon/rails-url-shortener/blob/main/LICENSE).
152
+
153
+ by: [a-chacon](https://a-chacon.com)
data/Rakefile CHANGED
@@ -17,10 +17,4 @@ Rake::TestTask.new(:test) do |t|
17
17
  t.test_files = FileList['test/**/*test.rb']
18
18
  end
19
19
 
20
- require 'rubocop/rake_task'
21
-
22
- RuboCop::RakeTask.new do |task|
23
- task.requires << 'rubocop-minitest'
24
- end
25
-
26
- task default: %i[test rubocop]
20
+ task default: %i[test]
@@ -33,29 +33,27 @@ module RailsUrlShortener
33
33
  # Parse a request information and save
34
34
  #
35
35
  # Return boolean
36
-
36
+ # rubocop:disable Metrics/AbcSize
37
37
  def self.parse_and_save(url, request)
38
- # browser detection
39
38
  browser(request)
40
- if !RailsUrlShortener.save_bots_visits && @browser.bot?
41
- false
42
- else
43
- # save
44
- visit = Visit.create(
45
- url: url,
46
- ip: request.ip,
47
- browser: @browser.name,
48
- browser_version: @browser.full_version,
49
- platform: @browser.platform.name,
50
- platform_version: @browser.platform.version,
51
- bot: @browser.bot?,
52
- user_agent: request.headers['User-Agent']
53
- )
54
- # We enqueue a job for get more data later
55
- IpCrawlerJob.perform_later(visit)
56
- visit
57
- end
39
+ return false if !RailsUrlShortener.save_bots_visits && @browser.bot?
40
+
41
+ visit = Visit.create(
42
+ url: url,
43
+ ip: request.ip,
44
+ browser: @browser.name,
45
+ browser_version: @browser.full_version,
46
+ platform: @browser.platform.name,
47
+ platform_version: @browser.platform.version,
48
+ bot: @browser.bot?,
49
+ user_agent: request.headers['User-Agent'],
50
+ referer: request.headers['Referer']
51
+ )
52
+
53
+ IpCrawlerJob.perform_later(visit)
54
+ visit
58
55
  end
56
+ # rubocop:enable Metrics/AbcSize
59
57
 
60
58
  def self.browser(request)
61
59
  @browser = Browser.new(request.headers['User-Agent'])
@@ -0,0 +1,5 @@
1
+ class AddRefererToVistis < ActiveRecord::Migration[7.0]
2
+ def change
3
+ add_column :rails_url_shortener_visits, :referer, :string, default: ""
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsUrlShortener
4
- VERSION = '0.2.10'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require 'rails_url_shortener/version'
4
4
  require 'rails_url_shortener/engine'
5
5
  require 'rails_url_shortener/model'
6
+ require_relative '../app/helpers/rails_url_shortener/urls_helper'
6
7
 
7
8
  module RailsUrlShortener
8
9
  ##
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_url_shortener
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - a-chacon
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-07-31 00:00:00.000000000 Z
10
+ date: 2025-05-08 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: browser
@@ -24,20 +23,6 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: 5.3.0
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 1.15.0
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: 1.15.0
41
26
  - !ruby/object:Gem::Dependency
42
27
  name: http
43
28
  requirement: !ruby/object:Gem::Requirement
@@ -52,10 +37,10 @@ dependencies:
52
37
  - - ">="
53
38
  - !ruby/object:Gem::Version
54
39
  version: 5.1.0
55
- description: RailsUrlShortener is a lightweight Rails engine that enables easy creation
56
- and management of short URLs within your project. Similar to bitly.com, it condenses
57
- long links into short, user-friendly addresses. Enhance your app's functionality
58
- with this simple yet powerful URL shortening solution.
40
+ description: "RailsUrlShortener is a lightweight Rails engine that enables easy creation
41
+ and management of short URLs within your project. \nSimilar to bitly.com, it condenses
42
+ long links into short, user-friendly addresses. \nEnhance your app's functionality
43
+ with this simple yet powerful URL shortening solution.\n"
59
44
  email:
60
45
  - andres.ch@protonmail.com
61
46
  executables: []
@@ -77,23 +62,22 @@ files:
77
62
  - db/migrate/20220407202526_create_rails_url_shortener_urls.rb
78
63
  - db/migrate/20220407202539_create_rails_url_shortener_visits.rb
79
64
  - db/migrate/20220418184647_create_rails_url_shortener_ipgeos.rb
65
+ - db/migrate/20250508120951_add_referer_to_vistis.rb
80
66
  - lib/generators/rails_url_shortener/rails_url_shortener_generator.rb
81
67
  - lib/generators/rails_url_shortener/templates/initializer.rb
82
68
  - lib/rails_url_shortener.rb
83
69
  - lib/rails_url_shortener/engine.rb
84
70
  - lib/rails_url_shortener/model.rb
85
71
  - lib/rails_url_shortener/version.rb
86
- - lib/tasks/auto_annotate_models.rake
87
72
  homepage: https://www.github.com/a-chacon/rails-url-shortener
88
73
  licenses:
89
74
  - GPL-3.0
90
75
  metadata:
91
76
  bug_tracker_uri: https://www.github.com/a-chacon/rails-url-shortener/issues
92
- changelog_uri: https://www.github.com/a-chacon/rails-url-shortener/releases/tag/v0.2.10
77
+ changelog_uri: https://www.github.com/a-chacon/rails-url-shortener/releases/tag/v0.4.0
93
78
  documentation_uri: https://github.com/a-chacon/rails-url-shortener/blob/main/README.md
94
- source_code_uri: https://github.com/a-chacon/rails-url-shortener/tree/v0.2.10
79
+ source_code_uri: https://github.com/a-chacon/rails-url-shortener/tree/v0.4.0
95
80
  rubygems_mfa_required: 'true'
96
- post_install_message:
97
81
  rdoc_options: []
98
82
  require_paths:
99
83
  - lib
@@ -101,15 +85,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
85
  requirements:
102
86
  - - ">="
103
87
  - !ruby/object:Gem::Version
104
- version: 2.7.0
88
+ version: '3.1'
105
89
  required_rubygems_version: !ruby/object:Gem::Requirement
106
90
  requirements:
107
91
  - - ">="
108
92
  - !ruby/object:Gem::Version
109
93
  version: 1.8.11
110
94
  requirements: []
111
- rubygems_version: 3.4.16
112
- signing_key:
95
+ rubygems_version: 3.6.2
113
96
  specification_version: 4
114
97
  summary: Rails url shortener engine.
115
98
  test_files: []
@@ -1,59 +0,0 @@
1
- # NOTE: only doing this in development as some production environments (Heroku)
2
- # NOTE: are sensitive to local FS writes, and besides -- it's just not proper
3
- # NOTE: to have a dev-mode tool do its thing in production.
4
- if Rails.env.development?
5
- require 'annotate'
6
- task :set_annotation_options do
7
- # You can override any of these by setting an environment variable of the
8
- # same name.
9
- Annotate.set_defaults(
10
- 'active_admin' => 'false',
11
- 'additional_file_patterns' => [],
12
- 'routes' => 'false',
13
- 'models' => 'true',
14
- 'position_in_routes' => 'before',
15
- 'position_in_class' => 'before',
16
- 'position_in_test' => 'before',
17
- 'position_in_fixture' => 'before',
18
- 'position_in_factory' => 'before',
19
- 'position_in_serializer' => 'before',
20
- 'show_foreign_keys' => 'true',
21
- 'show_complete_foreign_keys' => 'false',
22
- 'show_indexes' => 'true',
23
- 'simple_indexes' => 'false',
24
- 'model_dir' => 'app/models',
25
- 'root_dir' => '',
26
- 'include_version' => 'false',
27
- 'require' => '',
28
- 'exclude_tests' => 'false',
29
- 'exclude_fixtures' => 'false',
30
- 'exclude_factories' => 'false',
31
- 'exclude_serializers' => 'false',
32
- 'exclude_scaffolds' => 'true',
33
- 'exclude_controllers' => 'true',
34
- 'exclude_helpers' => 'true',
35
- 'exclude_sti_subclasses' => 'false',
36
- 'ignore_model_sub_dir' => 'false',
37
- 'ignore_columns' => nil,
38
- 'ignore_routes' => nil,
39
- 'ignore_unknown_models' => 'false',
40
- 'hide_limit_column_types' => 'integer,bigint,boolean',
41
- 'hide_default_column_types' => 'json,jsonb,hstore',
42
- 'skip_on_db_migrate' => 'false',
43
- 'format_bare' => 'true',
44
- 'format_rdoc' => 'false',
45
- 'format_yard' => 'false',
46
- 'format_markdown' => 'false',
47
- 'sort' => 'false',
48
- 'force' => 'false',
49
- 'frozen' => 'false',
50
- 'classified_sort' => 'true',
51
- 'trace' => 'false',
52
- 'wrapper_open' => nil,
53
- 'wrapper_close' => nil,
54
- 'with_comment' => 'true'
55
- )
56
- end
57
-
58
- Annotate.load_tasks
59
- end