Skip to content

Commit 7ec0733

Browse files
authored
Merge pull request #4 from rubygarage/develop
Merge before first release to add intergrations
2 parents 389519b + b05a10d commit 7ec0733

File tree

20 files changed

+460
-31
lines changed

20 files changed

+460
-31
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
.rvmrc
1515
.ruby-gemset
1616
.ruby-version
17+
18+
.byebug_history

README.md

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,25 @@
11
# Detectify
22

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/detectify`. 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
3+
Detectify provides a simple way to retrieve an ActiveRecord entity based on the domain/subdomain request information.
64

75
## Installation
86

97
Add this line to your application's Gemfile:
108

11-
```ruby
12-
gem 'detectify'
13-
```
14-
15-
And then execute:
9+
`gem 'detectify', github: 'rubygarage/detectify'`
1610

17-
$ bundle
11+
and then execute: `$ bundle`
1812

19-
Or install it yourself as:
20-
21-
$ gem install detectify
13+
Finally, restart the server to apply the changes.
2214

2315
## Usage
2416

25-
TODO: Write usage instructions here
26-
27-
## Development
28-
29-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. 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).
17+
Start off by generating an initializer: `$ bundle exec rails g detectify:install`, this will create file `config/initializers/detectify.rb` in your application directory. You can configure Detectify for your application needs via initializer. After this you can access detected entity via `env['Detectify-Entity']`.
3218

3319
## Contributing
3420

35-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/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.
36-
21+
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.
3722

3823
## License
3924

4025
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41-

detectify.gemspec

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ Gem::Specification.new do |spec|
77
spec.name = 'detectify'
88
spec.version = Detectify::VERSION
99
spec.authors = ['Lukyanov Fedor']
10-
spec.email = ['lukyanov.f.ua@gmail.com']
10+
spec.email = ['fedor@rubygarage.org']
1111

12-
spec.summary = 'Write a short summary, because Rubygems requires one.'
13-
spec.description = 'Write a longer description or delete this line.'
14-
spec.homepage = 'https://rubygarage.org'
12+
spec.summary = 'Detect ActiveRecord entity via domain or subdomain'
13+
spec.description = 'Detectify provides a simple way to retrieve an ActiveRecord entity' \
14+
'based on the domain/subdomain request information.'
15+
spec.homepage = 'https://github.com/rubygarage/detectify'
1516
spec.license = 'MIT'
1617

1718
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
1819
spec.require_paths = ['lib']
1920

21+
spec.add_dependency 'rack', '>= 1.6'
22+
spec.add_dependency 'activerecord', '>= 4.2'
23+
24+
spec.add_development_dependency 'byebug', '~> 9.0'
2025
spec.add_development_dependency 'bundler', '~> 1.12'
2126
spec.add_development_dependency 'rake', '~> 10.0'
2227
spec.add_development_dependency 'rspec', '~> 3.0'

lib/detectify.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
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'
27

38
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
428
end
29+
30+
require 'detectify/railtie' if defined?(::Rails)

lib/detectify/config.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/detectify/detector.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/detectify/middleware.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/detectify/query_builder/base.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/detectify/query_builder/sql.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/detectify/railtie.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)