rails_version 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development do
4
+ gem "rake"
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jamon Holmgren
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # RailsVersion
2
+
3
+ This gem works with http://railsversion.herokuapp.com to track all of your Rails apps
4
+ and the versions they are running. This gives you one place to see all of your rails
5
+ application versions.
6
+
7
+ ## Installation
8
+
9
+ As usual, add this line to your application's Gemfile and bundle afterward:
10
+
11
+ gem 'rails_version', ">= 0.2.0", :group => :production, :git => "git://github.com/clearsightstudio/rails_version.git"
12
+
13
+ Sign up and get your API key from http://railsversion.herokuapp.com, then go to your config/environments/production.rb file and add this:
14
+
15
+ RailsVersion::Config.api_key = "API_KEY_HERE"
16
+
17
+ ## Manual Ping
18
+
19
+ If you want to manually trigger a ping, go to:
20
+
21
+ http://example.com/?rails_version_ping=API_KEY_HERE
22
+
23
+ Just replace example.com with your domain name and API_KEY_HERE with your API key.
24
+
25
+ ## Configuration Options
26
+
27
+ We have good defaults, but we do expose some config options:
28
+
29
+ * `RailsVersion::Config.server_url = "http://yourserver/ping"` - allows you to ping your own server instead of ours.
30
+ * `RailsVersion::Config.ping_type = :server | :image | :script` - :server is default and the safest, but you can also inject an image or javascript tag into your HTML if the :server option isn't working for you.
31
+ * `RailsVersion::Config.frequency = 100` - 100 is a 1% chance on page load of pinging our server (default). Turn this down if you have a lot of traffic or up if you have very little traffic. Your server should ping ours about once every couple days on average.
32
+ * `RailsVersion::Config.api_key = "API_KEY_HERE"` - *required* API key from http://railsversion.herokuapp.com if you're using our server.
33
+
34
+ ## Code Climate
35
+
36
+ Version 0.1.0 Grade: A
37
+
38
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/clearsightstudio/rails_version)
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b feature/my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'rake/testtask'
15
+ Rake::TestTask.new(:test) do |test|
16
+ test.libs << 'lib' << 'test'
17
+ test.pattern = 'test/**/*_test.rb'
18
+ test.verbose = true
19
+ end
20
+
21
+ task :default => :test
@@ -0,0 +1,53 @@
1
+ require "rails_version/version"
2
+ require "rails_version/pinger"
3
+ require "rails_version/rails_version_mixin"
4
+ require 'rails_version/railtie' if defined? Rails
5
+
6
+ module RailsVersion
7
+ class Config
8
+ ##
9
+ # :singleton-method:
10
+ # Specify what URL this gem will ping with its information.
11
+ def self.server_url
12
+ @@server_url ||= "https://railsversion.herokuapp.com/ping"
13
+ end
14
+ def self.server_url=(v)
15
+ @@server_url = v
16
+ end
17
+
18
+ ##
19
+ # :singleton-method:
20
+ # Specify what type of ping to use.
21
+ # Options:
22
+ # :server (default: safest, server-side)
23
+ # :image (embed img tag before </body>)
24
+ # :script (embeds external js tag)
25
+ def self.ping_type
26
+ @@ping_type ||= :server
27
+ end
28
+ def self.ping_type=(v)
29
+ @@ping_type = v
30
+ end
31
+
32
+ ##
33
+ # :singleton-method:
34
+ # Tells the client how frequent (randomly out of 10000) to ping
35
+ # the server. Default is 100 (1% of page loads).
36
+ def self.frequency
37
+ @@frequency ||= 100
38
+ end
39
+ def self.frequency=(v)
40
+ @@frequency = v
41
+ end
42
+
43
+ ##
44
+ # :singleton-method:
45
+ # API Key obtained from railsversion.herokuapp.com
46
+ def self.api_key
47
+ @@api_key ||= nil
48
+ end
49
+ def self.api_key=(v)
50
+ @@api_key = v
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ module RailsVersion
2
+ class Pinger
3
+ attr_accessor :body, :version, :host
4
+
5
+ def initialize(request, response)
6
+ @body = response.body.to_s
7
+ @host = request.host.to_s
8
+ @version = "Non-Rails"
9
+ @version = Rails.version.to_s if Object.const_defined?('Rails')
10
+ end
11
+
12
+ def ping!
13
+ case RailsVersion::Config.ping_type.to_sym
14
+ when :server
15
+ require 'open-uri'
16
+ open(ping_url) # Ping the server.
17
+ return false
18
+ when :script
19
+ inject_script_before_end_body_tag(ping_script)
20
+ else
21
+ inject_script_before_end_body_tag(ping_image)
22
+ end
23
+ rescue Timeout::Error
24
+ false
25
+ end
26
+
27
+ def inject_script_before_end_body_tag(ping_html)
28
+ @body.sub! /<\/[bB][oO][dD][yY]>/, "#{ping_html}</body>" if @body && @body.respond_to?(:sub!)
29
+ end
30
+
31
+ def ping_script
32
+ "<script language='text/javascript' src='#{ping_url}'></script>"
33
+ end
34
+
35
+ def ping_image
36
+ "<img src='#{ping_url}' style='display:none;visibility:hidden' width='0' height='0' />"
37
+ end
38
+
39
+ def ping_url
40
+ "#{RailsVersion::Config.server_url}/#{RailsVersion::Config.api_key}/?site=#{@host}&rails_version=#{@version}"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,13 @@
1
+ module RailsVersion
2
+ module RailsVersionMixin
3
+ def ping_rails_version_server
4
+ # Rolls the dice. If random number is < than your frequency, trigger the script
5
+ raise StandardError.new("RailsVersion requires RailsVersion::Config.api_key to be set in this environment.") unless RailsVersion::Config.api_key
6
+ manual_ping = params[:rails_version_ping] == RailsVersion::Config.api_key
7
+ if manual_ping || rand(10000) < RailsVersion::Config.frequency
8
+ pinger = RailsVersion::Pinger.new(request, response)
9
+ response.body = pinger.body if pinger.ping!
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ module RailsVersion
2
+ class Railtie < Rails::Railtie
3
+ initializer "rails_version" do |app|
4
+ ActionController::Base.send :include, RailsVersion::RailsVersionMixin
5
+ ActionController::Base.send :after_filter, :ping_rails_version_server
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module RailsVersion
2
+ VERSION = "0.2.2"
3
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rails_version/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jamon Holmgren"]
6
+ gem.email = ["jamon@clearsightstudio.com"]
7
+ gem.description = %q{ Pings a server and reports the current app's Rails version. }
8
+ gem.summary = %q{ Pings a server and reports the current app's Rails version. }
9
+ gem.homepage = "https://github.com/clearsightstudio/rails_version"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test)/})
14
+ gem.name = "rails_version"
15
+ gem.require_paths = ["lib", "rails"]
16
+ gem.version = RailsVersion::VERSION
17
+ gem.add_development_dependency 'actionpack'
18
+ end
@@ -0,0 +1,46 @@
1
+ require 'test/unit'
2
+ require 'rails_version'
3
+ require 'rails_version/pinger'
4
+
5
+ class FakeRequest
6
+ def host
7
+ "www.example.com"
8
+ end
9
+ end
10
+
11
+ class FakeResponse
12
+ def body
13
+ "<html><body><h1>TEST</h1><p>TEST2</p></body></html>"
14
+ end
15
+ end
16
+
17
+
18
+ class RailsVersionTest < Test::Unit::TestCase
19
+ def test_pinger_injects_script
20
+ RailsVersion::Config.ping_type = :script
21
+ RailsVersion::Config.api_key = "TESTSCRIPT"
22
+ pinger = RailsVersion::Pinger.new(FakeRequest.new, FakeResponse.new)
23
+ pinger.ping!
24
+ body = pinger.body
25
+ assert body.include?("</script></body>")
26
+ assert body.include?("/TESTSCRIPT/")
27
+ end
28
+ def test_pinger_injects_image
29
+ RailsVersion::Config.ping_type = :image
30
+ RailsVersion::Config.api_key = "TESTIMAGE"
31
+ pinger = RailsVersion::Pinger.new(FakeRequest.new, FakeResponse.new)
32
+ pinger.ping!
33
+ body = pinger.body
34
+ assert body.include?("<img src='")
35
+ assert body.include?("/TESTIMAGE/")
36
+ end
37
+ def test_pinger_server
38
+ RailsVersion::Config.ping_type = :server
39
+ RailsVersion::Config.api_key = "TESTSERVER"
40
+ pinger = RailsVersion::Pinger.new(FakeRequest.new, FakeResponse.new)
41
+ pinger.ping!
42
+ # Pretty weak, I know, but at least we know the library was loaded
43
+ # and there aren't major syntax errors.
44
+ assert Object.const_defined?('Net')
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jamon Holmgren
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ! ' Pings a server and reports the current app''s Rails version. '
31
+ email:
32
+ - jamon@clearsightstudio.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - lib/rails_version.rb
43
+ - lib/rails_version/pinger.rb
44
+ - lib/rails_version/rails_version_mixin.rb
45
+ - lib/rails_version/railtie.rb
46
+ - lib/rails_version/version.rb
47
+ - rails_version.gemspec
48
+ - test/rails_version_test.rb
49
+ homepage: https://github.com/clearsightstudio/rails_version
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ - rails
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.22
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Pings a server and reports the current app's Rails version.
74
+ test_files:
75
+ - test/rails_version_test.rb
76
+ has_rdoc: