rails_url_shortener 0.1.0 → 0.1.3
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 +4 -4
- data/README.md +53 -18
- data/app/helpers/rails_url_shortener/urls_helper.rb +17 -9
- data/lib/generators/rails_url_shortener/templates/initializer.rb +6 -5
- data/lib/rails_url_shortener/version.rb +1 -1
- data/lib/rails_url_shortener.rb +4 -0
- metadata +27 -11
- data/MIT-LICENSE +0 -20
- data/app/helpers/rails_url_shortener/application_helper.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ffbe916c8f609fdc0f2d3eeeaa418fef1ac4ee79ef06168d49fe5e203d0cb13a
|
4
|
+
data.tar.gz: c9c73896eb476662e60e2999bc88a1a664c70b2d963100fbd4dc782ccd52249e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f084c9720acb322664f424ba1d6d8e3f30b102caa5091eae1a66a2eae62242460dda2b86b0eeef46ade2321101cfd0f4fd6b9fea1818ba10653e96ce876374e1
|
7
|
+
data.tar.gz: 0141c770be77c10aa15354685e21ef51539547d690cdb2cd3e66f2deda6472245506e9704c7b1b6149dda8f5d417433293bcc8c7b6e1dd9ed9e22cbbe692e66d
|
data/README.md
CHANGED
@@ -1,37 +1,76 @@
|
|
1
1
|
# RailsUrlShortener
|
2
2
|
|
3
|
-
|
4
|
-
It could be used like a url shortener or a ip logger, it is your choice.
|
5
|
-
The app generate a short url for you and then (if you want) receive the requests and redirect to the original url.
|
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.
|
6
4
|
|
7
5
|
Why give your data to a third party app if you can do it by yourself?
|
8
6
|
|
7
|
+
You can see a **demo project** of what you can do with this engine [HERE]("https://paso-app.herokuapp.com/").
|
8
|
+
|
9
|
+
## Key features
|
10
|
+
|
11
|
+
A few of the things you can do with RailsUrlShortener:
|
12
|
+
|
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
|
+
|
9
19
|
## Usage
|
10
20
|
|
11
|
-
|
21
|
+
### 1. Mount the engine
|
22
|
+
|
23
|
+
Mount the engine on your app adding the next code on your config/routes.rb:
|
24
|
+
|
25
|
+
**If you want to mount this on the root of your app, this should be on the bottom of your routes file.**
|
12
26
|
|
13
27
|
```ruby
|
14
28
|
mount RailsUrlShortener::Engine, at: "/"
|
15
29
|
|
16
30
|
```
|
31
|
+
### 2. Generate the short link
|
32
|
+
|
33
|
+
And generate the short links like you want:
|
17
34
|
|
18
|
-
|
35
|
+
- Using the helper method, this return the ready short link.
|
19
36
|
|
20
37
|
```ruby
|
21
|
-
short_url("https://www.github.com/a-chacon/
|
38
|
+
short_url("https://www.github.com/a-chacon/rails-url-shortener")
|
22
39
|
```
|
23
40
|
|
24
|
-
|
41
|
+
- 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:
|
25
42
|
|
26
43
|
```ruby
|
27
|
-
RailsUrlShortener::Url.generate("https://www.github.com/a-chacon/
|
44
|
+
RailsUrlShortener::Url.generate("https://www.github.com/a-chacon/rails-url-shortener")
|
28
45
|
```
|
46
|
+
### 3. Share the short link
|
29
47
|
|
30
|
-
**Then share the short link.**
|
48
|
+
**Then share the short link to your users or wherever you want.**
|
31
49
|
|
32
|
-
|
50
|
+
## Deeper
|
51
|
+
|
52
|
+
Full params for the short_url helper:
|
53
|
+
```ruby
|
54
|
+
short_url(url, owner: nil, key: nil, expires_at: nil, category: nil, url_options: {})
|
55
|
+
```
|
56
|
+
Where:
|
57
|
+
* **url**: Long url for short.
|
58
|
+
* **owner**: Is a model of your app. You can relate an url whatever you want in your app.
|
59
|
+
* **key**: Is a custom key that you want to set up.
|
60
|
+
* **expires_at**: Is a datetime for expiration, after this the redirection doesn't work.
|
61
|
+
* **category**: Tag that you want for that link.
|
62
|
+
* **url_options**: Options for the url_for generator. Ex: subdomain or protocol.
|
63
|
+
|
64
|
+
|
65
|
+
And the same for the generate model method except for url_options:
|
66
|
+
```ruby
|
67
|
+
RailsUrlShortener::Url.generate(url, owner: nil, key: nil, expires_at: nil, category: nil)
|
68
|
+
```
|
69
|
+
|
70
|
+
### Data saved
|
71
|
+
|
72
|
+
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:
|
33
73
|
|
34
|
-
By default this engine save all request made on your short url, you can use that data for some analitics 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 a Url:
|
35
74
|
```ruby
|
36
75
|
RailsUrlShortener::Url.find_by_key("key").visits # all visits
|
37
76
|
|
@@ -41,11 +80,6 @@ Or using the model class:
|
|
41
80
|
RailsUrlShortener::Visit.all # all in database
|
42
81
|
```
|
43
82
|
|
44
|
-
Also the Url model has a polymorphic relation with an owner that is optional. So you can relate an url whatever you want in your app adding the next relation in a model:
|
45
|
-
```ruby
|
46
|
-
has_many :urls, as: :owner
|
47
|
-
```
|
48
|
-
|
49
83
|
## Installation
|
50
84
|
|
51
85
|
Add this line to your application's Gemfile:
|
@@ -69,11 +103,12 @@ And finally install the migrations on your project and migrate:
|
|
69
103
|
bin/rails rails_url_shortener:install:migrations db:migrate
|
70
104
|
```
|
71
105
|
|
72
|
-
|
106
|
+
For the configurations generate the initializer whith this:
|
73
107
|
|
74
108
|
```bash
|
75
109
|
rails generate RailsUrlShortener:initializer
|
76
110
|
```
|
111
|
+
**Here is important to configure the host at least if your are not running your app in localhost**
|
77
112
|
|
78
113
|
## Contributing
|
79
114
|
|
@@ -89,4 +124,4 @@ Don't forget to give the project a star! Thanks again!
|
|
89
124
|
5. Open a Pull Request
|
90
125
|
|
91
126
|
## License
|
92
|
-
The gem is available as open source under the terms of the [
|
127
|
+
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).
|
@@ -1,6 +1,13 @@
|
|
1
1
|
module RailsUrlShortener
|
2
2
|
module UrlsHelper
|
3
|
+
##
|
4
|
+
# helper for generate short urls
|
5
|
+
#
|
6
|
+
# this method return a short url or the original url if something is bad
|
7
|
+
# Usage:
|
8
|
+
# short_url("https://tools.ietf.org/search/rfc2616#section-5.3")
|
3
9
|
def short_url(url, owner: nil, key: nil, expires_at: nil, category: nil, url_options: {})
|
10
|
+
# generate
|
4
11
|
url_object = Url.generate(
|
5
12
|
url,
|
6
13
|
owner: owner,
|
@@ -10,17 +17,18 @@ module RailsUrlShortener
|
|
10
17
|
)
|
11
18
|
|
12
19
|
if url_object.errors.empty?
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
+
# options for url_for
|
21
|
+
options = {
|
22
|
+
controller: "rails_url_shortener/urls",
|
23
|
+
action: "show",
|
24
|
+
key: url_object.key,
|
25
|
+
host: RailsUrlShortener.host
|
26
|
+
}.merge(url_options)
|
20
27
|
|
21
|
-
#
|
22
|
-
|
28
|
+
# use helper of this engine
|
29
|
+
RailsUrlShortener::Engine.routes.url_for(options)
|
23
30
|
else
|
31
|
+
# if not saved, return original url
|
24
32
|
url
|
25
33
|
end
|
26
34
|
end
|
@@ -4,8 +4,9 @@ CHARSETS = {
|
|
4
4
|
alphanumcase: ('A'..'Z').to_a + ('a'..'z').to_a + (0..9).to_a
|
5
5
|
}
|
6
6
|
|
7
|
-
RailsUrlShortener.
|
8
|
-
RailsUrlShortener.
|
9
|
-
RailsUrlShortener.
|
10
|
-
RailsUrlShortener.
|
11
|
-
RailsUrlShortener.
|
7
|
+
RailsUrlShortener.host = "localhost:3000" # the host used for the helper
|
8
|
+
RailsUrlShortener.default_redirect = "/" # where the users are redirect if the link doesn't exists or is expired.
|
9
|
+
RailsUrlShortener.charset = CHARSETS[:alphanumcase] # used for generate the keys, better long.
|
10
|
+
RailsUrlShortener.key_length = 6 # Key length for random generator
|
11
|
+
RailsUrlShortener.minimum_key_length = 3 # minimun permited for a key
|
12
|
+
RailsUrlShortener.save_bots_visits = false # if save bots visits
|
data/lib/rails_url_shortener.rb
CHANGED
@@ -11,6 +11,10 @@ module RailsUrlShortener
|
|
11
11
|
alphanumcase: ('A'..'Z').to_a + ('a'..'z').to_a + (0..9).to_a
|
12
12
|
}
|
13
13
|
|
14
|
+
##
|
15
|
+
# host for build final url on helper
|
16
|
+
mattr_accessor :host, default: 'test.host'
|
17
|
+
|
14
18
|
##
|
15
19
|
# default redirection url when the key isn't found
|
16
20
|
mattr_accessor :default_redirect, default: '/'
|
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
|
+
version: 0.1.3
|
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-
|
11
|
+
date: 2022-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -38,18 +38,32 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 5.3.0
|
41
|
-
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.15.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.15.0
|
55
|
+
description: RailsUrlShortener is a simple engine that provide to your rail's app
|
56
|
+
the functionalities for short URLs. Like bitly.com, but working on your project
|
57
|
+
only.
|
42
58
|
email:
|
43
59
|
- andres.ch@protonmail.com
|
44
60
|
executables: []
|
45
61
|
extensions: []
|
46
62
|
extra_rdoc_files: []
|
47
63
|
files:
|
48
|
-
- MIT-LICENSE
|
49
64
|
- README.md
|
50
65
|
- Rakefile
|
51
66
|
- app/controllers/rails_url_shortener/urls_controller.rb
|
52
|
-
- app/helpers/rails_url_shortener/application_helper.rb
|
53
67
|
- app/helpers/rails_url_shortener/urls_helper.rb
|
54
68
|
- app/models/rails_url_shortener/application_record.rb
|
55
69
|
- app/models/rails_url_shortener/url.rb
|
@@ -63,13 +77,15 @@ files:
|
|
63
77
|
- lib/rails_url_shortener/engine.rb
|
64
78
|
- lib/rails_url_shortener/version.rb
|
65
79
|
- lib/tasks/rails_url_shortener_tasks.rake
|
66
|
-
homepage: https://www.github.com/a-chacon/
|
80
|
+
homepage: https://www.github.com/a-chacon/rails-url-shortener
|
67
81
|
licenses:
|
68
82
|
- MIT
|
69
83
|
metadata:
|
70
|
-
|
71
|
-
|
72
|
-
|
84
|
+
bug_tracker_uri: https://www.github.com/a-chacon/rails-url-shortener/issues
|
85
|
+
changelog_uri: https://www.github.com/a-chacon/rails-url-shortener/releases/tag/v0.1.3
|
86
|
+
documentation_uri: https://github.com/a-chacon/rails-url-shortener/blob/main/README.md
|
87
|
+
source_code_uri: https://github.com/a-chacon/rails-url-shortener/tree/v0.1.3
|
88
|
+
rubygems_mfa_required: 'true'
|
73
89
|
post_install_message:
|
74
90
|
rdoc_options: []
|
75
91
|
require_paths:
|
@@ -78,12 +94,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
78
94
|
requirements:
|
79
95
|
- - ">="
|
80
96
|
- !ruby/object:Gem::Version
|
81
|
-
version:
|
97
|
+
version: 2.7.0
|
82
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
99
|
requirements:
|
84
100
|
- - ">="
|
85
101
|
- !ruby/object:Gem::Version
|
86
|
-
version:
|
102
|
+
version: 1.8.11
|
87
103
|
requirements: []
|
88
104
|
rubygems_version: 3.2.22
|
89
105
|
signing_key:
|
data/MIT-LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright 2022 a-chacon
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|