tagliani 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d64323e136019c5543ca2b8a4d6d3423016ba3ce678b842fc636ee14efb41197
4
+ data.tar.gz: d25e45b3d8987e926834898257dd279490920401693c5b84c1290974b7d5edc5
5
+ SHA512:
6
+ metadata.gz: 956c6f41b3b1e0b77be729eac4acf1f268afa8cb0e9989bf0efa9948e17feac735a3007bd4a414e1482d01c9c3cbba9d6a9cfa160217d5f6a41421b17206a613
7
+ data.tar.gz: 769ef669c4b7501515c8b40d3cf294ec3a01c352dce6c9516447481dfb696820af4b6dfc970936daf494efdcb5e380f88235936863be4c9f86aaabec5821788a
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /.byebug
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .byebug_history
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --require spec_helper
3
+ --color
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rrubyist@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tagliani.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tagliani (0.1.0)
5
+ activerecord
6
+ elasticsearch
7
+ rake
8
+ redis
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ activemodel (5.2.3)
14
+ activesupport (= 5.2.3)
15
+ activerecord (5.2.3)
16
+ activemodel (= 5.2.3)
17
+ activesupport (= 5.2.3)
18
+ arel (>= 9.0)
19
+ activesupport (5.2.3)
20
+ concurrent-ruby (~> 1.0, >= 1.0.2)
21
+ i18n (>= 0.7, < 2)
22
+ minitest (~> 5.1)
23
+ tzinfo (~> 1.1)
24
+ arel (9.0.0)
25
+ byebug (11.0.1)
26
+ coderay (1.1.2)
27
+ concurrent-ruby (1.1.5)
28
+ database_cleaner (1.7.0)
29
+ diff-lcs (1.3)
30
+ elasticsearch (7.1.0)
31
+ elasticsearch-api (= 7.1.0)
32
+ elasticsearch-transport (= 7.1.0)
33
+ elasticsearch-api (7.1.0)
34
+ multi_json
35
+ elasticsearch-transport (7.1.0)
36
+ faraday
37
+ multi_json
38
+ faraday (0.15.4)
39
+ multipart-post (>= 1.2, < 3)
40
+ i18n (1.6.0)
41
+ concurrent-ruby (~> 1.0)
42
+ method_source (0.9.2)
43
+ minitest (5.11.3)
44
+ multi_json (1.13.1)
45
+ multipart-post (2.1.1)
46
+ pry (0.12.2)
47
+ coderay (~> 1.1.0)
48
+ method_source (~> 0.9.0)
49
+ rake (12.3.2)
50
+ redis (4.1.1)
51
+ rspec (3.8.0)
52
+ rspec-core (~> 3.8.0)
53
+ rspec-expectations (~> 3.8.0)
54
+ rspec-mocks (~> 3.8.0)
55
+ rspec-core (3.8.0)
56
+ rspec-support (~> 3.8.0)
57
+ rspec-expectations (3.8.3)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.8.0)
60
+ rspec-mocks (3.8.0)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.8.0)
63
+ rspec-support (3.8.0)
64
+ sqlite3 (1.4.1)
65
+ thread_safe (0.3.6)
66
+ timecop (0.9.1)
67
+ tzinfo (1.2.5)
68
+ thread_safe (~> 0.1)
69
+
70
+ PLATFORMS
71
+ ruby
72
+
73
+ DEPENDENCIES
74
+ activerecord
75
+ bundler (~> 1.17)
76
+ byebug
77
+ database_cleaner
78
+ pry
79
+ rspec
80
+ rspec-mocks
81
+ sqlite3
82
+ tagliani!
83
+ timecop
84
+
85
+ BUNDLED WITH
86
+ 1.17.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Aidan Rudkovskyi
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Aidan Rudkovskyi
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,99 @@
1
+ # Tagliani
2
+
3
+ Blazing fast Ruby alternative to `acts-as-taggable-on` and other similar gems. Instead of million of records in the database it uses powerful ElasticSearch, which gives a very fast and scalable solution with a search capabilities of ElasticSearch.
4
+
5
+ ## Installation
6
+
7
+ All you need is to add in your `Gemfile` the follwing line.
8
+
9
+ ```ruby
10
+ gem 'tagliani'
11
+ ```
12
+
13
+ And run `bundle install` command.
14
+
15
+ ## Search
16
+
17
+ Let's say inside the Rails application you have a model with a name "Hashtag", that represents all the tags attached to the model "Tweet".
18
+
19
+ ```ruby
20
+ class Hashtag < ActiveRecord::Base
21
+ include Tagliani::Concerns::Search
22
+ end
23
+
24
+ class Tweet < ActiveRecord::Base
25
+ taggable tag_kls: "Hashtag"
26
+ end
27
+ ```
28
+
29
+ *tag_kls is optional. By default it set to `Tag` model*
30
+
31
+ To attach the tag simply execute:
32
+
33
+ ```ruby
34
+ tweet = Tweet.create(body: "Follow @rrubyist on Twitter")
35
+ tweet.tags.add(name: '#followme')
36
+ ```
37
+
38
+ To list the attached tags you can run:
39
+
40
+ ```ruby
41
+ tweet.tags
42
+ ```
43
+
44
+ It will return you an array of `Hashtag` objects attached to the `Tweet` model.
45
+
46
+ ```ruby
47
+ [#<Hashtag id: 2, name: "#followme", sticker: "default">]
48
+ ```
49
+
50
+ If you want to search for all tweets attached to the `Hashtag` model with a name `#followme` you can use public `search` method defined in class.
51
+
52
+ *You don't have to specify `tag_kls`, unless you have multiple models that act as Tag model*
53
+
54
+ ```ruby
55
+ Hashtag.search(where: { tag_name: ['#followme'], tag_kls: ['Hashtag'] }
56
+ ```
57
+
58
+ Tag name represents the `name` field of the `Hashtag` model.
59
+
60
+ ```ruby
61
+ [#<Tweet id: 3, body: "Tweet #0">, #<Tweet id: 4, body: "Tweet #1">,
62
+ #<Tweet id: 5, body: "Tweet #2">, #<Tweet id: 6, body: "Follow @rrubyist on Twitter">]
63
+ ```
64
+
65
+ ## Asyncronous bulk index
66
+
67
+ For a non-blocking processes you can enable option to index jobs in background queue.
68
+
69
+ ```ruby
70
+ class Artist < ActiveRecord::Base
71
+ has_many :albums
72
+ tagliani async: true
73
+ end
74
+ ```
75
+
76
+ To index in bulks, simply execute:
77
+ ```ruby
78
+ Tagliani::Search::Index.bulk!
79
+ ```
80
+
81
+ ### Requirements
82
+
83
+ * ActiveRecord
84
+ * redis-client
85
+ * elasticsearch
86
+
87
+ To run this gem it is required to have ActiveRecord.
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rudkovskyi/tagliani. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
96
+
97
+ ## Code of Conduct
98
+
99
+ Everyone interacting in the Tagliani project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rudkovskyi/tagliani/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,13 @@
1
+ module Tagliani
2
+ module Concerns
3
+ module Search
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def search(body: {}, where: nil)
8
+ Tagliani::Search.new(body: body, where: where).serialize(type: "object")
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,55 @@
1
+ module Tagliani
2
+ module Concerns
3
+ module Taggable
4
+ class Tags < Array
5
+ def initialize(parent = nil)
6
+ @parent = parent
7
+ @index = Tagliani::Search::Index.new
8
+ @tag_kls = @parent.class._tag_kls.constantize
9
+ @async = @parent.class._async
10
+
11
+ super(search)
12
+ end
13
+
14
+ def add(*objects)
15
+ objects.flat_map do |hash|
16
+ options = hash.slice(:name)
17
+
18
+ begin
19
+ record = @tag_kls.find_or_initialize_by(options)
20
+ record.save
21
+ rescue ActiveRecord::RecordNotUnique
22
+ record = @tag_kls.find_by(options)
23
+ end
24
+
25
+ @index.add!({
26
+ object_kls: @parent.class.to_s,
27
+ object_id: @parent.id,
28
+ created_at: @parent.try(:created_at),
29
+ tag_id: record.id,
30
+ tag_kls: record.class.to_s,
31
+ tag_type: record.sticker,
32
+ tag_name: record.name,
33
+ last_updated: Time.now
34
+ }, async: @async)
35
+ end
36
+ end
37
+
38
+ def search(body: {}, where: nil)
39
+ body.deep_merge!({
40
+ query: {
41
+ bool: {
42
+ must: [
43
+ { match: { object_kls: @parent.class.to_s } },
44
+ { term: { object_id: @parent.id } }
45
+ ]
46
+ }
47
+ }
48
+ })
49
+
50
+ Tagliani::Search.new(body: body, where: where).serialize(type: 'tag')
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,13 @@
1
+ require_relative "taggable/tags"
2
+
3
+ module Tagliani
4
+ module Concerns
5
+ module Taggable
6
+ extend ActiveSupport::Concern
7
+
8
+ def tags
9
+ Tags.new(self)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Tagliani
2
+ class Configuration
3
+ class Elasticsearch < Struct.new(:url, :index, :refresh, :log)
4
+ def initialize
5
+ self.url = "http://localhost:9200"
6
+ self.index = "tagliani"
7
+ self.refresh = false
8
+ self.log = false
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Tagliani
2
+ class Configuration
3
+ class Redis < Struct.new(:url, :queue, :length)
4
+ def initialize
5
+ self.url = "redis://localhost:6379/tagliani"
6
+ self.queue = "tagliani"
7
+ self.length = 200
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,64 @@
1
+ module Tagliani
2
+ class Configuration
3
+ class Schema < Struct.new(:default)
4
+ def initialize
5
+ self.default = whiteseparated_tags
6
+ end
7
+
8
+ private
9
+
10
+ def analysis_settings
11
+ {
12
+ analysis: {
13
+ analyzer: {
14
+ gossipAnalyzer: {
15
+ type: 'custom',
16
+ tokenizer: 'whitespace',
17
+ char_filter: %w[dot_remover coma_separator],
18
+ filter: ['lowercase']
19
+ }
20
+ },
21
+ char_filter: {
22
+ dot_remover: {
23
+ type: 'pattern_replace',
24
+ pattern: '\.',
25
+ replacement: ''
26
+ },
27
+ coma_separator: {
28
+ type: 'pattern_replace',
29
+ pattern: '\,',
30
+ replacement: ' '
31
+ }
32
+ }
33
+ }
34
+ }
35
+ end
36
+
37
+ def whiteseparated_tags
38
+ {
39
+ settings: analysis_settings,
40
+ mappings: {
41
+ doc: {
42
+ properties: {
43
+ query: { type: 'percolator' },
44
+ object_kls: {
45
+ type: 'keyword',
46
+ index: true
47
+ },
48
+ object_id: { type: 'integer' },
49
+ created_at: { type: 'date', format: 'yyyy-MM-dd HH:mm:ss' },
50
+ updated_at: { type: 'date', format: 'yyyy-MM-dd HH:mm:ss' },
51
+ tag_id: { type: 'integer' },
52
+ tag_kls: { type: 'keyword', index: true },
53
+ tag_name: {
54
+ type: 'text',
55
+ analyzer: 'gossipAnalyzer'
56
+ }
57
+ }
58
+ }
59
+ }
60
+ }
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,9 @@
1
+ module Tagliani
2
+ class Configuration
3
+ class Tag < Struct.new(:types)
4
+ def initialize
5
+ self.types = %i[default]
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ require_relative "configuration/elasticsearch"
2
+ require_relative "configuration/redis"
3
+ require_relative "configuration/schema"
4
+ require_relative "configuration/tag"
5
+
6
+ module Tagliani
7
+ class Configuration
8
+ attr_reader :elasticsearch, :redis, :schema, :tag
9
+
10
+ def initialize
11
+ @elasticsearch = Elasticsearch.new
12
+ @redis = Redis.new
13
+ @schema = Schema.new
14
+ @tag = Tag.new
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ require "active_record"
2
+ require "active_support/concern"
3
+
4
+ module Tagliani
5
+ module Models
6
+ class << self
7
+ attr_writer :tagged
8
+
9
+ def tagged
10
+ @tagged ||= []
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ module Tagliani
2
+ class Search
3
+ class Index
4
+ class Object
5
+ def initialize(object = {})
6
+ @object = object.is_a?(Hash) ? object.stringify_keys : JSON.parse(object)
7
+ end
8
+
9
+ def id
10
+ "#{@object['object_kls'].to_s.downcase}_#{@object['object_id']}_#{@object['tag_id']}"
11
+ end
12
+
13
+ def to_h
14
+ {
15
+ object_kls: @object['object_kls'],
16
+ object_id: @object['object_id'],
17
+ object_created_at: @object['created_at'].try(:strftime, '%Y-%m-%d %H:%M:%S'),
18
+ tag_id: @object['tag_id'],
19
+ tag_kls: @object['tag_kls'],
20
+ tag_type: @object['tag_type'],
21
+ tag_name: @object['tag_name'],
22
+ last_updated: @object['last_updated'].try(:strftime, '%Y-%m-%d %H:%M:%S')
23
+ }
24
+ end
25
+
26
+ def to_json
27
+ to_h.to_json
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,88 @@
1
+ require_relative "index/object"
2
+
3
+ module Tagliani
4
+ class Search
5
+ class Index
6
+ def initialize(name: nil, schema: nil, queue: nil, length: nil)
7
+ @name = name || Tagliani.config.elasticsearch.index
8
+ @schema = schema || Tagliani.config.schema.default
9
+ @queue = queue || Tagliani.config.redis.queue
10
+ @length = length || Tagliani.config.redis.length
11
+ @search = Tagliani::Search.new(body: {})
12
+ end
13
+
14
+ def create!
15
+ @search.client.indices.create index: @name, body: @schema
16
+ end
17
+
18
+ def delete!
19
+ @search.client.indices.delete index: @name, ignore: 404
20
+ end
21
+
22
+ def refresh
23
+ @search.client.indices.refresh
24
+ end
25
+
26
+ def parse_data(indices, index_name:)
27
+ [indices].flatten.flat_map do |text|
28
+ object = Object.new(text)
29
+
30
+ {
31
+ index: {
32
+ _index: index_name,
33
+ _id: object.id,
34
+ _type: "doc",
35
+ data: object.to_h
36
+ }
37
+ }
38
+ end
39
+ end
40
+
41
+ def bulk!
42
+ indices = Tagliani.redis.spop(@queue, @length)
43
+ data = parse_data(indices, index_name: @name)
44
+
45
+ return false if data.empty?
46
+
47
+ @search.client.bulk(body: data)
48
+ end
49
+
50
+ def add!(schema, async: false)
51
+ object = Object.new(schema)
52
+
53
+ if async
54
+ Tagliani.redis.sadd(@queue, object.to_json)
55
+ else
56
+ @search.client.index({
57
+ index: @name,
58
+ type: "doc",
59
+ id: object.id,
60
+ body: object.to_h,
61
+ refresh: true
62
+ })
63
+ end
64
+ end
65
+
66
+ class << self
67
+ def create!(name: nil, schema: nil)
68
+ new(name: name, schema: schema).create!
69
+ end
70
+
71
+ def delete!(name: nil)
72
+ new(name: name).delete!
73
+ end
74
+
75
+ def move!(from:, to:)
76
+ create!(name: to)
77
+ Tagliani::Search.client.reindex({
78
+ body: { source: { index: from }, dest: { index: to } }
79
+ })
80
+ end
81
+
82
+ def bulk!(name: nil, queue: nil, length: nil)
83
+ new(name: name, queue: queue, length: length).bulk!
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,68 @@
1
+ require "elasticsearch"
2
+ require "json"
3
+
4
+ require_relative "search/index"
5
+
6
+ module Tagliani
7
+ class Search
8
+ class << self
9
+ def client
10
+ Elasticsearch::Client.new host: Tagliani.config.elasticsearch.url,
11
+ log: Tagliani.config.elasticsearch.log
12
+ end
13
+ end
14
+
15
+ attr_reader :client
16
+
17
+ def initialize(body:, where: nil)
18
+ @index = Tagliani.config.elasticsearch.index
19
+ @client = self.class.client
20
+ @where_clause = where
21
+ @body = body
22
+
23
+ build_where(@where_clause) if @where_clause
24
+ end
25
+
26
+ def response
27
+ @client.search(index: @index, body: @body)
28
+ end
29
+
30
+ def serialize(type:)
31
+ model_kls = "#{type}_kls"
32
+ model_id = "#{type}_id"
33
+
34
+ models = {}
35
+
36
+ response['hits']['hits'].each do |entry|
37
+ entry_source = entry['_source']
38
+ class_name = entry_source[model_kls]
39
+ models[class_name] ||= []
40
+ models[class_name] << entry_source[model_id]
41
+ end
42
+
43
+ models.flat_map do |model, ids|
44
+ model.constantize.where(id: ids)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def build_where(args)
51
+ @body[:query] ||= {}
52
+ @body[:query][:bool] ||= {}
53
+ @body[:query][:bool][:must] ||= []
54
+
55
+ @body[:query][:bool][:must] << [
56
+ query_string: {
57
+ query: build_query_string(args)
58
+ }
59
+ ]
60
+ end
61
+
62
+ def build_query_string(args)
63
+ query = args.to_h.map do |key, val|
64
+ "(#{val.map { |object| "#{key}:#{object}" }.join(' OR ')})"
65
+ end.join(' AND ')
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module Tagliani
2
+ VERSION = "0.1.0"
3
+ end
data/lib/tagliani.rb ADDED
@@ -0,0 +1,46 @@
1
+ require_relative "tagliani/configuration"
2
+ require_relative "tagliani/search"
3
+ require_relative "tagliani/version"
4
+ require_relative "tagliani/models"
5
+ require_relative "tagliani/concerns/taggable"
6
+ require_relative "tagliani/concerns/search"
7
+
8
+ require "byebug"
9
+ require "securerandom"
10
+ require "redis"
11
+
12
+ module Tagliani
13
+ class Error < StandardError; end
14
+ class << self
15
+ attr_reader :config
16
+
17
+ def configure
18
+ @config ||= Configuration.new
19
+ yield(@config)
20
+ end
21
+
22
+ def redis
23
+ @redis ||= Redis.new(url: config.redis.url)
24
+ end
25
+ end
26
+
27
+ def taggable(options = {})
28
+ Models.tagged << to_s
29
+
30
+ class_attribute :_tag_kls, :_async
31
+
32
+ self._tag_kls = options[:tag_kls] || "Tag"
33
+ self._async = options[:async]
34
+
35
+ class_eval do
36
+ include Concerns::Taggable
37
+ end
38
+ end
39
+ end
40
+
41
+ require "active_model/callbacks"
42
+ ActiveModel::Callbacks.include(Tagliani)
43
+
44
+ ActiveSupport.on_load(:active_record) do
45
+ extend Tagliani
46
+ end
data/tagliani.gemspec ADDED
@@ -0,0 +1,44 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tagliani/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tagliani"
8
+ spec.version = Tagliani::VERSION
9
+ spec.authors = ["Aidan Rudkovskyi"]
10
+ spec.email = ["rrubyist@gmail.com"]
11
+
12
+ spec.summary = "Blazing fast Ruby library to create tags for Rails models"
13
+ spec.description = "Blazing fast Ruby alternative to acts-as-taggable-on and other similar gems. Instead of million of records in the database it uses powerful ElasticSearch, which gives a very fast and scalable solution with a search capabilities of ElasticSearch."
14
+ spec.homepage = "https://theoryofe.co/tagliani"
15
+ spec.license = "MIT"
16
+
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/rudkovskyi/tagliani/"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "activerecord"
31
+ spec.add_runtime_dependency "elasticsearch"
32
+ spec.add_runtime_dependency "rake"
33
+ spec.add_runtime_dependency "redis"
34
+
35
+ spec.add_development_dependency "activerecord"
36
+ spec.add_development_dependency "bundler", "~> 1.17"
37
+ spec.add_development_dependency "database_cleaner"
38
+ spec.add_development_dependency "rspec"
39
+ spec.add_development_dependency "rspec-mocks"
40
+ spec.add_development_dependency "byebug"
41
+ spec.add_development_dependency "timecop"
42
+ spec.add_development_dependency "sqlite3"
43
+ spec.add_development_dependency "pry"
44
+ end
metadata ADDED
@@ -0,0 +1,253 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tagliani
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aidan Rudkovskyi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: elasticsearch
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.17'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.17'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec-mocks
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: byebug
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: timecop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: sqlite3
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: pry
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description: Blazing fast Ruby alternative to acts-as-taggable-on and other similar
196
+ gems. Instead of million of records in the database it uses powerful ElasticSearch,
197
+ which gives a very fast and scalable solution with a search capabilities of ElasticSearch.
198
+ email:
199
+ - rrubyist@gmail.com
200
+ executables: []
201
+ extensions: []
202
+ extra_rdoc_files: []
203
+ files:
204
+ - ".gitignore"
205
+ - ".rspec"
206
+ - CODE_OF_CONDUCT.md
207
+ - Gemfile
208
+ - Gemfile.lock
209
+ - LICENSE
210
+ - LICENSE.txt
211
+ - README.md
212
+ - Rakefile
213
+ - lib/tagliani.rb
214
+ - lib/tagliani/concerns/search.rb
215
+ - lib/tagliani/concerns/taggable.rb
216
+ - lib/tagliani/concerns/taggable/tags.rb
217
+ - lib/tagliani/configuration.rb
218
+ - lib/tagliani/configuration/elasticsearch.rb
219
+ - lib/tagliani/configuration/redis.rb
220
+ - lib/tagliani/configuration/schema.rb
221
+ - lib/tagliani/configuration/tag.rb
222
+ - lib/tagliani/models.rb
223
+ - lib/tagliani/search.rb
224
+ - lib/tagliani/search/index.rb
225
+ - lib/tagliani/search/index/object.rb
226
+ - lib/tagliani/version.rb
227
+ - tagliani.gemspec
228
+ homepage: https://theoryofe.co/tagliani
229
+ licenses:
230
+ - MIT
231
+ metadata:
232
+ homepage_uri: https://theoryofe.co/tagliani
233
+ source_code_uri: https://github.com/rudkovskyi/tagliani/
234
+ post_install_message:
235
+ rdoc_options: []
236
+ require_paths:
237
+ - lib
238
+ required_ruby_version: !ruby/object:Gem::Requirement
239
+ requirements:
240
+ - - ">="
241
+ - !ruby/object:Gem::Version
242
+ version: '0'
243
+ required_rubygems_version: !ruby/object:Gem::Requirement
244
+ requirements:
245
+ - - ">="
246
+ - !ruby/object:Gem::Version
247
+ version: '0'
248
+ requirements: []
249
+ rubygems_version: 3.0.3
250
+ signing_key:
251
+ specification_version: 4
252
+ summary: Blazing fast Ruby library to create tags for Rails models
253
+ test_files: []