hc-permalink 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hc-permalink.gemspec +28 -0
- data/lib/hc/permalink.rb +127 -0
- data/lib/hc/permalink/version.rb +5 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 978cd5dbb1f687cc5dbe04ba53a1b30efa8991df
|
4
|
+
data.tar.gz: a6dd96b86744843e55e687e7f7ba49ad82436959
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b14723f2f4efce95713afc7aeed53a77b7c532a7020370fadef94e5b4e663a1fa4598fc2e0e02ce5cb3ab59b7256392e0f987a45507ec17048691fae6d755976
|
7
|
+
data.tar.gz: 8ff2d8629b78ced17428109cad84f010eb8a1efb647c130a5daab1f4d0f5bb9017db4d8593bff3a3ad217cdc9331b4102865f1f49ea319144fad4e8fd271001d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Jack Hayter
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# HC::Permalink
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hc/permalink`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'hc-permalink'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install hc-permalink
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hc-permalink.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hc/permalink"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "hc/permalink/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hc-permalink"
|
8
|
+
spec.version = HC::Permalink::VERSION
|
9
|
+
spec.authors = ["Jack Hayter"]
|
10
|
+
spec.email = ["jack@hockey-community.com"]
|
11
|
+
|
12
|
+
spec.summary = "Generates URL friendly (and unique) strings of latin characters for use as permalinks"
|
13
|
+
spec.description = "Transliterates characters into Latin character set and allows for generation of uniuqe variants with an incrementing ID"
|
14
|
+
spec.homepage = "https://github.com/HockeyCommunity/permalink"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "russian"
|
27
|
+
spec.add_development_dependency "activesupport"
|
28
|
+
end
|
data/lib/hc/permalink.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
require "hc/permalink/version"
|
2
|
+
require "active_support/inflector/transliterate"
|
3
|
+
require "russian"
|
4
|
+
|
5
|
+
module HC
|
6
|
+
module Permalink
|
7
|
+
|
8
|
+
@@shared_resource_classes = []
|
9
|
+
|
10
|
+
def self.shared_resource_classes=(class_list)
|
11
|
+
raise "Class list must be an array of classes" unless class_list.is_a?(Array)
|
12
|
+
@@shared_resource_classes = class_list.dup
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.shared_resource_classes
|
16
|
+
@@shared_resource_classes
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.generate_unique(value:, target_class:, target_field: 'permalink', ignore_id: nil, constraint: nil)
|
20
|
+
|
21
|
+
# Create a base name which we will later increment
|
22
|
+
#
|
23
|
+
base_value = self.from_string(value, remove_spaces: true).to_s[0..63]
|
24
|
+
current_value = base_value
|
25
|
+
counter = 0
|
26
|
+
|
27
|
+
# Loop on finding objects with these parameters until no object found
|
28
|
+
#
|
29
|
+
while self.locate_object(target_class: target_class, target_field: target_field, target_value: current_value, ignore_id: ignore_id, constraint: constraint)
|
30
|
+
counter += 1
|
31
|
+
current_value = "#{base_value[0..(63 - counter.to_s.length)]}#{counter}"
|
32
|
+
end
|
33
|
+
|
34
|
+
# Return the current generated value
|
35
|
+
#
|
36
|
+
return current_value
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.from_string(value, remove_spaces: false)
|
41
|
+
|
42
|
+
# Replace spaces and underscores with hyphens, then replace all
|
43
|
+
# other common punctuation and symbols, then downcase the result
|
44
|
+
#
|
45
|
+
value = value.to_s.gsub(/[\s_]+/,'-').gsub(/[!"#$%&'\(\)*+,\.\/:;<=>?@\[\\\]\^_`{}¦|£¬~]+/, '').gsub(/[-]{2,}/, '-')
|
46
|
+
value = value.gsub('-','') if remove_spaces
|
47
|
+
|
48
|
+
# Attempt to Transliterate Russian and latin-based characters
|
49
|
+
# with accents into their latin(ish) equivalents
|
50
|
+
#
|
51
|
+
value = I18n.transliterate(Russian::transliterate(value).gsub('ș', 's').gsub('ț', 't').gsub('ồ', 'o').gsub('ế', 'e').gsub('ộ', 'o').gsub('ạ', 'a')).downcase.gsub("?", "")
|
52
|
+
|
53
|
+
# Return the resulting slug, but remove any trailing hyphens
|
54
|
+
#
|
55
|
+
return value[/[\w-]*\w/]
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.simple(value)
|
60
|
+
value.to_s.gsub(/[\s-]+/,'_').gsub(/[^\w]+/, '').downcase
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def self.locate_object(target_class:, target_field:, target_value:, ignore_id:, constraint:)
|
66
|
+
self.find_object(
|
67
|
+
target_class: target_class,
|
68
|
+
target_field: target_field,
|
69
|
+
target_value: target_value,
|
70
|
+
ignore_id: ignore_id,
|
71
|
+
constraint: constraint
|
72
|
+
) || self.taken_by_shared_resource?(
|
73
|
+
target_class: target_class,
|
74
|
+
value: target_value,
|
75
|
+
ignore_own: true
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.taken_by_shared_resource?(target_class:, value:, ignore_own: false)
|
80
|
+
|
81
|
+
# No need to check presence of values for irelevent classes
|
82
|
+
#
|
83
|
+
return false unless @@shared_resource_classes.include?(target_class)
|
84
|
+
|
85
|
+
# Identify and class search space
|
86
|
+
#
|
87
|
+
classes_to_search = @@shared_resource_classes.dup
|
88
|
+
classes_to_search = classes_to_search.reject{|k|k==target_class} if ignore_own
|
89
|
+
|
90
|
+
# Loop on search space and try to find something
|
91
|
+
#
|
92
|
+
classes_to_search.each do |shared_target_class|
|
93
|
+
found_object = shared_target_class.find_by_shared_resource_identifier(value)
|
94
|
+
return true if !found_object.nil?
|
95
|
+
end
|
96
|
+
|
97
|
+
# Fall back to false if nothing was found
|
98
|
+
#
|
99
|
+
return false
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.find_object(target_class:, target_field:, target_value:, ignore_id: nil, constraint: nil)
|
104
|
+
|
105
|
+
# Ensure that the constraint matches the following format:
|
106
|
+
# constraint: {key: value}
|
107
|
+
#
|
108
|
+
if !constraint.nil?
|
109
|
+
raise "constraint must be a hash containing only one key" if !constraint.is_a?(Hash) || constraint.keys.count > 1
|
110
|
+
end
|
111
|
+
|
112
|
+
# Attempt to find objects matching these constraints
|
113
|
+
#
|
114
|
+
if !constraint.nil?
|
115
|
+
located_objects = target_class.where(target_field.to_sym => target_value).where.not(id: ignore_id).where(constraint)
|
116
|
+
else
|
117
|
+
located_objects = target_class.where(target_field.to_sym => target_value).where.not(id: ignore_id)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Return any results of our lookup
|
121
|
+
#
|
122
|
+
return located_objects.count > 0 ? located_objects.first : nil
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hc-permalink
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jack Hayter
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: russian
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Transliterates characters into Latin character set and allows for generation
|
70
|
+
of uniuqe variants with an incrementing ID
|
71
|
+
email:
|
72
|
+
- jack@hockey-community.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- hc-permalink.gemspec
|
85
|
+
- lib/hc/permalink.rb
|
86
|
+
- lib/hc/permalink/version.rb
|
87
|
+
homepage: https://github.com/HockeyCommunity/permalink
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.6.12
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Generates URL friendly (and unique) strings of latin characters for use as
|
111
|
+
permalinks
|
112
|
+
test_files: []
|