agent_helpers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9aaa59303d4e79c60ac78003a18b82ff953e729f
4
+ data.tar.gz: 70a933988b36ec015050470af1e61d9a2e701afa
5
+ SHA512:
6
+ metadata.gz: 707b7688a9aa59d78a8bb30965943af977ed6bce2c1150daa43c7432c05f49f77b7a2d841f74a10055afc76d68b3d73fa88d111dc7f51c9165207629065fa02b
7
+ data.tar.gz: 40db4ec6b15353fd44e179ea26f3daaa25c3b06c5764e0f653c02db3966a45b2ef1295121804fc315dcb363f2d914be8ef6d086fe4f54da4c7fb2fbc4c8c87b9
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = AgentHelpers
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AgentHelpers'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,3 @@
1
+ module AgentHelpers::ApplicationController < ActionController::Base
2
+
3
+ end
@@ -0,0 +1,4 @@
1
+ module AgentHelpers
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,57 @@
1
+ module AgentHelpers::DetectorHelper
2
+ def agent_detector
3
+ if !controller.instance_variable_get(:@agent_helpers_detector)
4
+ controller.instance_variable_set(:@agent_helpers_detector, ::AgentHelpers::Detector.new(:request => request))
5
+ end
6
+
7
+ return controller.instance_variable_get(:@agent_helpers_detector)
8
+ end
9
+
10
+ def agent_robot?
11
+ agent_detector.browser == :bot
12
+ end
13
+
14
+ def agent_human?
15
+ agent_detector.browser != :bot && agent_detector.browser != :unknown
16
+ end
17
+
18
+ def agent_browser
19
+ agent_detector.browser
20
+ end
21
+
22
+ def agent_title
23
+ agent_detector.title
24
+ end
25
+
26
+ def agent_version
27
+ agent_detector.version
28
+ end
29
+
30
+ def agent_device
31
+ agent_detector.device
32
+ end
33
+
34
+ def agent_chrome?
35
+ agent_detector.browser == :chrome
36
+ end
37
+
38
+ def agent_firefox?
39
+ agent_detector.browser == :firefox
40
+ end
41
+
42
+ def agent_ie?
43
+ agent_detector.browser == :ie
44
+ end
45
+
46
+ def agent_safari?
47
+ agent_detector.browser == :safari
48
+ end
49
+
50
+ def agent_opera?
51
+ agent_detector.browser == :opera
52
+ end
53
+
54
+ def agent_mobile?
55
+ agent_detector.device == :android || agent_detector.device == :ipad || agent_detector.device == :iphone
56
+ end
57
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>AgentHelpers</title>
5
+ <%= stylesheet_link_tag "agent_helpers/application", media: "all" %>
6
+ <%= javascript_include_tag "agent_helpers/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ AgentHelpers::Engine.routes.draw do
2
+ end
@@ -0,0 +1,16 @@
1
+ require "agent_helpers/engine"
2
+ require "string-cases"
3
+
4
+ module AgentHelpers
5
+ def self.const_missing(name)
6
+ filepath = "#{File.dirname(__FILE__)}/agent_helpers/#{::StringCases.camel_to_snake(name)}.rb"
7
+
8
+ if File.exists?(filepath)
9
+ require filepath
10
+ raise LoadError, "Still not defined: #{name}" unless ::AgentHelpers.const_defined?(name)
11
+ return ::AgentHelpers.const_get(name)
12
+ end
13
+
14
+ super
15
+ end
16
+ end
@@ -0,0 +1,130 @@
1
+ class AgentHelpers::Detector
2
+ attr_reader :browser, :title, :version, :device
3
+
4
+ def initialize(args)
5
+ @request = args[:request]
6
+ raise "No request was given." unless @request
7
+ @env = @request.env
8
+ @user_agent = @request.user_agent.to_s.downcase.strip
9
+ raise "No user agent was given." if @user_agent.empty?
10
+ parse
11
+ end
12
+
13
+ private
14
+
15
+ def parse
16
+ if match = @user_agent.match(/chrome\/(\d+\.\d+)/)
17
+ @browser = :chrome
18
+ @title = "Google Chrome"
19
+ @version = match[1]
20
+ elsif match = @user_agent.match(/firefox\/(\d+\.\d+)/)
21
+ @browser = :firefox
22
+ @title = "Mozilla Firefox"
23
+ @version = match[1]
24
+ elsif @env["HTTP_ACCEPT"] == "*/*" && @env["HTTP_ACCEPT_LANGUAGE"] == "zh-cn,zh-hk,zh-tw,en-us" && @env["HTTP_USER_AGENT"] == "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" && (@env["REMOTE_ADDR"][0,10] == "114.80.93." || @env["HTTP_X_FORWARDED_FOR"][0, 10] == "114.80.93.")
25
+ @browser = :bot
26
+ @title = "bot"
27
+ @version = "(unknown annoying bot from China)"
28
+ elsif match = @user_agent.match(/msie\s*(\d+\.\d+)/)
29
+ @browser = :ie
30
+ @title = "Microsoft Internet Explorer"
31
+ @version = match[1]
32
+ elsif match = @user_agent.match(/opera\/([\d+\.]+)/)
33
+ @browser = :opera
34
+ @title = "Opera"
35
+ @version = match[1]
36
+ elsif match = @user_agent.match(/wget\/([\d+\.]+)/)
37
+ @browser = :bot
38
+ @title = "Bot"
39
+ @version = "Wget #{match[1]}"
40
+ elsif @user_agent.include?("baiduspider")
41
+ @browser = :bot
42
+ @title = "Bot"
43
+ @version = "Baiduspider"
44
+ elsif match = @user_agent.match(/googlebot\/([\d\.]+)/)
45
+ @browser = :bot
46
+ @title = "Googlebot"
47
+ @version = match[1]
48
+ elsif @user_agent.include?("gidbot")
49
+ @browser = :bot
50
+ @title = "Bot"
51
+ @version = "GIDBot"
52
+ elsif match = @user_agent.match(/android\s+([\d\.]+)/)
53
+ @browser = :android
54
+ @title = "Android"
55
+ @version = match[1]
56
+ elsif match = @user_agent.match(/safari\/(\d+)/)
57
+ @browser = :safari
58
+ @title = "Safari"
59
+ @version = match[1]
60
+ elsif @user_agent.include?("bingbot")
61
+ @browser = :bot
62
+ @title = "Bot"
63
+ @version = "Bingbot"
64
+ elsif @user_agent.include?("yahoo! slurp")
65
+ @browser = :bot
66
+ @title = "Bot"
67
+ @version = "Yahoo! Slurp"
68
+ elsif @user_agent.include?("hostharvest")
69
+ @browser = :bot
70
+ @title = "Bot"
71
+ @version = "HostHarvest"
72
+ elsif @user_agent.include?("exabot")
73
+ @browser = :bot
74
+ @title = "Bot"
75
+ @version = "Exabot"
76
+ elsif @user_agent.include?("dotbot")
77
+ @browser = :bot
78
+ @title = "Bot"
79
+ @version = "DotBot"
80
+ elsif @user_agent.include?("msnbot")
81
+ @browser = :bot
82
+ @title = "Bot"
83
+ @version = "MSN bot"
84
+ elsif @user_agent.include?("yandexbot")
85
+ @browser = :bot
86
+ @title = "Bot"
87
+ @version = "Yandex Bot"
88
+ elsif @user_agent.include?("mj12bot")
89
+ @browser = :bot
90
+ @title = "Bot"
91
+ @version = "Majestic12 Bot"
92
+ elsif @user_agent.include?("facebookexternalhit")
93
+ @browser = :bot
94
+ @title = "Bot"
95
+ @version = "Facebook Externalhit"
96
+ elsif @user_agent.include?("sitebot")
97
+ @browser = :bot
98
+ @title = "Bot"
99
+ @version = "SiteBot"
100
+ elsif match = @user_agent.match(/java\/([\d\.]+)/)
101
+ @browser = :bot
102
+ @title = "Java"
103
+ @version = match[1]
104
+ elsif match = @user_agent.match(/ezooms\/([\d\.]+)/)
105
+ @browser = :bot
106
+ @title = "Ezooms"
107
+ @version = match[1]
108
+ elsif match = @user_agent.match(/ahrefsbot\/([\d\.]+)/)
109
+ @browser = :bot
110
+ @title = "AhrefsBot"
111
+ @version = match[1]
112
+ elsif @user_agent.include?("sosospider")
113
+ @browser = :bot
114
+ @title = "Bot"
115
+ @version = "Sosospider"
116
+ else
117
+ @browser = :unknown
118
+ @title = "(unknown browser)"
119
+ @version = "(unknown version)"
120
+ end
121
+
122
+ if @user_agent.include?("ipad")
123
+ @device = :ipad
124
+ elsif @user_agent.include?("iphone")
125
+ @device = :iphone
126
+ elsif @user_agent.include?("android")
127
+ @device = :android
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,11 @@
1
+ module AgentHelpers
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace AgentHelpers
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ g.integration_tool :rspec
8
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module AgentHelpers
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :agent_helpers do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: agent_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Johansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: string-cases
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: sqlite3
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: rspec-rails
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
+ - !ruby/object:Gem::Dependency
70
+ name: forgery
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: factory_girl_rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Inspects the user agent for you and allows you to take action based on
98
+ the users browser, if it is a detectable bot with one-line-helpers.
99
+ email:
100
+ - k@spernj.org
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - app/helpers/agent_helpers/application_helper.rb
106
+ - app/helpers/agent_helpers/detector_helper.rb
107
+ - app/assets/javascripts/agent_helpers/application.js
108
+ - app/assets/stylesheets/agent_helpers/application.css
109
+ - app/controllers/agent_helpers/application_controller.rb
110
+ - app/views/layouts/agent_helpers/application.html.erb
111
+ - config/routes.rb
112
+ - lib/agent_helpers.rb
113
+ - lib/agent_helpers/engine.rb
114
+ - lib/agent_helpers/version.rb
115
+ - lib/agent_helpers/detector.rb
116
+ - lib/tasks/agent_helpers_tasks.rake
117
+ - MIT-LICENSE
118
+ - Rakefile
119
+ - README.rdoc
120
+ homepage: https://www.github.com/kaspernj/agent_helpers
121
+ licenses: []
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.0.7
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Easily detect robots, browser, versions and more with convenient helpers.
143
+ test_files: []