detectify 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dde8d3ba42cf907fd5dc86c5c74cfbabb14c1e9d
4
+ data.tar.gz: bf5dc2515f68b28089473723b5ddef7210da5d97
5
+ SHA512:
6
+ metadata.gz: b3b09a9900217455213513cc93829d014b55f8285e535d497116624efd751b67c16f322c1be016def079b4bd99ae2f350c9fa02e9972aedd89eff5d26a1c9128
7
+ data.tar.gz: 60283ffd9598e6226fc5a351d3148baa2408cdbde968af776c4d7872ec860977e3f3f1c4d27edc7eb904db3b22f6e7e889b511e864eaf378f17dd6efb1845abf
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .DS_Store
12
+
13
+ .idea
14
+ .rvmrc
15
+ .ruby-gemset
16
+ .ruby-version
17
+
18
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format Fuubar
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at lukyanov.f.ua@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Lukyanov Fedor
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.
@@ -0,0 +1,35 @@
1
+ # Detectify
2
+
3
+ [![Code Climate](https://codeclimate.com/github/rubygarage/detectify/badges/gpa.svg)](https://codeclimate.com/github/rubygarage/detectify)
4
+ [![Coverage Status](https://coveralls.io/repos/github/rubygarage/detectify/badge.svg?branch=master)](https://coveralls.io/github/rubygarage/detectify?branch=master)
5
+ [![Build Status](https://travis-ci.org/rubygarage/detectify.svg?branch=master)](https://travis-ci.org/rubygarage/detectify)
6
+
7
+ Detectify provides a simple way to retrieve an ActiveRecord entity based on the domain/subdomain request information.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ `gem 'detectify', github: 'rubygarage/detectify'`
14
+
15
+ and then execute: `$ bundle`
16
+
17
+ Finally, restart the server to apply the changes.
18
+
19
+ ## Usage
20
+
21
+ Start off by generating an initializer:
22
+
23
+ `$ bundle exec rails g detectify:install`
24
+
25
+ this will create file `config/initializers/detectify.rb` in your application directory.
26
+
27
+ You can configure Detectify for your application needs via initializer. After this you can access detected entity via: `env['Detectify-Entity']`.
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rubygarage/detectify. 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.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'detectify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'detectify'
8
+ spec.version = Detectify::VERSION
9
+ spec.authors = ['Lukyanov Fedor']
10
+ spec.email = ['fedor@rubygarage.org']
11
+
12
+ spec.license = 'MIT'
13
+ spec.homepage = 'https://github.com/rubygarage/detectify'
14
+ spec.summary = 'Detect ActiveRecord entity via domain or subdomain'
15
+ spec.description = 'Detectify provides a simple way to retrieve an ActiveRecord entity ' \
16
+ 'based on the domain/subdomain request information.'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.require_paths = ['lib']
20
+ spec.required_ruby_version = '>= 2.2.2'
21
+
22
+ spec.add_dependency 'rack', '>= 1.6'
23
+ spec.add_dependency 'activerecord', '>= 4.2'
24
+
25
+ spec.add_development_dependency 'byebug', '~> 9.0'
26
+ spec.add_development_dependency 'bundler', '~> 1.12'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'fuubar', '~> 2.0'
30
+ spec.add_development_dependency 'coveralls', '~> 0.8'
31
+ end
@@ -0,0 +1,30 @@
1
+ require 'detectify/version'
2
+ require 'detectify/config'
3
+ require 'detectify/query_builder/base'
4
+ require 'detectify/query_builder/sql'
5
+ require 'detectify/detector'
6
+ require 'detectify/middleware'
7
+
8
+ module Detectify
9
+ def self.configure
10
+ yield(config)
11
+ end
12
+
13
+ def self.config
14
+ @config || reset_config
15
+ end
16
+
17
+ def self.reset_config(config = Config.new)
18
+ @config = config
19
+ end
20
+
21
+ def self.entity_class
22
+ if config.entity_class.is_a?(String)
23
+ Object.const_get(config.entity_class)
24
+ else
25
+ config.entity_class
26
+ end
27
+ end
28
+ end
29
+
30
+ require 'detectify/railtie' if defined?(::Rails)
@@ -0,0 +1,14 @@
1
+ module Detectify
2
+ class Config
3
+ attr_accessor :entity_class, :tld_length, :ignore_urls,
4
+ :domain_column, :subdomain_column
5
+
6
+ def initialize
7
+ @entity_class = 'Account'
8
+ @tld_length = 1
9
+ @ignore_urls = []
10
+ @domain_column = 'domain'
11
+ @subdomain_column = 'subdomain'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ require 'uri'
2
+
3
+ module Detectify
4
+ class Detector
5
+ attr_reader :request_uri
6
+
7
+ def initialize(request_url, query_builder = QueryBuilder::SQL)
8
+ @request_uri = URI(request_url)
9
+ @query_builder = query_builder
10
+ end
11
+
12
+ def detect
13
+ @entity ||= Detectify.entity_class.where(*query).first unless ignore?
14
+ end
15
+
16
+ private
17
+
18
+ def ignore?
19
+ @request_uri.to_s[Regexp.union(*Detectify.config.ignore_urls)]
20
+ end
21
+
22
+ def query
23
+ @query_builder.new(domain, subdomain).build
24
+ end
25
+
26
+ def domain
27
+ request_uri.host
28
+ end
29
+
30
+ def subdomain
31
+ chunks = request_uri.host.split('.')
32
+ chunks[0...(1 - Detectify.config.tld_length - 2)].join
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,16 @@
1
+ module Detectify
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ request = Rack::Request.new(env)
9
+ detector = Detector.new(request.url)
10
+
11
+ env['Detectify-Entity'] = detector.detect
12
+
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ module Detectify
2
+ module QueryBuilder
3
+ class Base
4
+ attr_reader :domain, :subdomain
5
+
6
+ def initialize(domain, subdomain)
7
+ @domain = domain
8
+ @subdomain = subdomain
9
+ end
10
+
11
+ def build
12
+ raise(NotImplementedError)
13
+ end
14
+
15
+ private
16
+
17
+ def need_domain_clause?
18
+ Detectify.config.domain_column && domain
19
+ end
20
+
21
+ def need_subdomain_clause?
22
+ Detectify.config.subdomain_column && subdomain
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ module Detectify
2
+ module QueryBuilder
3
+ class SQL < Base
4
+ def build
5
+ @query ||= begin
6
+ query = ["#{domain_clause}#{or_operator}#{subdomain_clause}"]
7
+ query << domain if need_domain_clause?
8
+ query << subdomain if need_subdomain_clause?
9
+ query.compact
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def domain_clause
16
+ "#{Detectify.config.domain_column} = ?" if need_domain_clause?
17
+ end
18
+
19
+ def subdomain_clause
20
+ "#{Detectify.config.subdomain_column} = ?" if need_subdomain_clause?
21
+ end
22
+
23
+ def or_operator
24
+ ' OR ' if need_domain_clause? && need_subdomain_clause?
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ module Detectify
2
+ class Railtie < Rails::Railtie
3
+ initializer 'detectify' do |app|
4
+ app.config.middleware.use 'Detectify::Middleware'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Detectify
2
+ VERSION = '1.0.3'.freeze
3
+ end
@@ -0,0 +1,11 @@
1
+ module Detectify
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def copy_initializer_file
7
+ copy_file 'detectify.rb', 'config/initializers/detectify.rb'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ Detectify.configure do |config|
2
+ # Set the model that Detectify will use to detect entity.
3
+ # Default value is 'Account'.
4
+ # config.entity_class = 'Account'
5
+
6
+ # Set the column that will be used to find entity by domain.
7
+ # If you don't need such lookup, you can simply set
8
+ # domain_column value as nil. Default value is 'domain'
9
+ # config.domain_column = 'domain'
10
+
11
+ # Set the column that will be used to find entity by subdomain.
12
+ # If you don't need such lookup, you can simply set
13
+ # subdomain_column value as nil. Default value is 'subdomain'.
14
+ # config.subdomain_column = 'subdomain'
15
+
16
+ # Set your top level domain length.
17
+ # For more information about TLD visit http://bit.ly/1jIeoL9
18
+ # By length, means count of dots in your TLD.
19
+ # For example, tld_length for 'example.co.uk' domain will be 2,
20
+ # for 'example.com' will be 1. Default value is 1.
21
+ # config.tld_length = 1
22
+
23
+ # Array of regular expressions, that will be used to determine
24
+ # whether it is necessary to detect entity. You can add as much as you want,
25
+ # for example, config.ignore_urls += [%r{/assets/}]. Default values is [].
26
+ # config.ignore_urls += [%r{/assets/}]
27
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: detectify
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Lukyanov Fedor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '9.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '9.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: fuubar
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.8'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.8'
125
+ description: Detectify provides a simple way to retrieve an ActiveRecord entity based
126
+ on the domain/subdomain request information.
127
+ email:
128
+ - fedor@rubygarage.org
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".coveralls.yml"
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - ".travis.yml"
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - detectify.gemspec
143
+ - lib/detectify.rb
144
+ - lib/detectify/config.rb
145
+ - lib/detectify/detector.rb
146
+ - lib/detectify/middleware.rb
147
+ - lib/detectify/query_builder/base.rb
148
+ - lib/detectify/query_builder/sql.rb
149
+ - lib/detectify/railtie.rb
150
+ - lib/detectify/version.rb
151
+ - lib/generators/detectify/install_generator.rb
152
+ - lib/generators/detectify/templates/detectify.rb
153
+ homepage: https://github.com/rubygarage/detectify
154
+ licenses:
155
+ - MIT
156
+ metadata: {}
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: 2.2.2
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 2.5.1
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: Detect ActiveRecord entity via domain or subdomain
177
+ test_files: []