pingr 0.0.1

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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pingr.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bodacious
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,79 @@
1
+ # Pingr - Tell search engines about your sitemap changes
2
+
3
+ Pingr is a super-simple gem built for the [blogit](http://github.com/KatanaCode/blogit "A Rails Blogging Engine") project.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pingr'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pingr
18
+
19
+ ## Usage
20
+
21
+ From within your app (most likely a controller):
22
+
23
+ ``` ruby
24
+ def ping_sitemaps
25
+ Pingr::Request.new(:google, my_sitemap_url).ping
26
+ end
27
+ ```
28
+
29
+ A good way to do this would be using Rails's after filters:
30
+
31
+ ``` ruby
32
+ class PostsController < ActionController::Base
33
+
34
+ after_filter :ping_sitemaps, only: [:create, :update, :destroy]
35
+
36
+ # ...
37
+
38
+ private
39
+
40
+ def ping_sitemaps
41
+ Pingr::Request.new(:google, my_sitemap_url).ping
42
+ Pingr::Request.new(:bing, my_sitemap_url).ping
43
+ end
44
+
45
+ end
46
+ ```
47
+
48
+ You can ping all [supported search engines](https://github.com/KatanaCode/pingr/blob/master/lib/pingr.rb#L9) by doing:
49
+
50
+ ``` ruby
51
+ def ping_sitemaps
52
+ for search_engine in Pingr::SUPPORTED_SEARCH_ENGINES
53
+ Pingr::Request.new(search_engine, my_sitemap_url).ping
54
+ end
55
+ end
56
+ ```
57
+
58
+ ... not the most elegant solution but we'll improve that in future versions.
59
+
60
+ ## Modes
61
+
62
+ By default, Pingr is set to **:test** mode, meaning it won't actually perform the requests. If the Rails environment is :production then the mode is set to **:live** which *will* perform requests to the search engines.
63
+
64
+ You can change this by manually setting `Pingr.mode`
65
+
66
+ ``` ruby
67
+ # in config/initializers/pingr.rb
68
+ Pingr.mode = :live if Rails.env =~ /staging|production/
69
+ ```
70
+
71
+ **NOTE:** Search engines may penalise or black-list you if you perform too many requests - they recommend no more than one per hour.
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,63 @@
1
+ require "pingr/version"
2
+ require "pingr/request"
3
+
4
+ module Pingr
5
+
6
+ # Exceptions raised from within Pingr are of this class
7
+ class PingrError < StandardError; end
8
+
9
+ # Currently supported search engines
10
+ SUPPORTED_SEARCH_ENGINES = [:google, :bing]
11
+
12
+ # Public: Set the mode Pingr is running in
13
+ #
14
+ # mode_name - A Symbol or String value for the mode. Must be one of :test or :live
15
+ #
16
+ # Raises PingrError if an invalid value is passed
17
+ def self.mode=(mode_name)
18
+ raise PingrError, "Unknown mode: #{mode_name}" unless mode_name.to_s =~ /test|live/
19
+ @mode = mode_name.to_sym
20
+ end
21
+
22
+ # Public: The mode Pingr is running in
23
+ #
24
+ # Returns a Symbol either :test or :live
25
+ def self.mode
26
+ @mode ||= begin
27
+ if defined?(Rails) && Rails.env.production?
28
+ :live
29
+ else
30
+ :test
31
+ end
32
+ end
33
+ end
34
+
35
+ # Public: A custom logger to keep track of the Pings
36
+ #
37
+ # Returns a Logger object. By default, the log clears itself every week
38
+ def self.logger
39
+ @logger ||= Logger.new(logger_name, shift_age = 'weekly')
40
+ end
41
+
42
+ # Public: Sets the logger to be used.
43
+ # If the developer would like to use their own custom logger, let 'em.
44
+ def self.logger=(_logger)
45
+ @logger = _logger
46
+ end
47
+
48
+ private
49
+
50
+ # Private: The name of the Logger used by Pingr.
51
+ # If we've detected a Rails app, sets up a log file in the log directory.
52
+ # Otherwise, use STDOUT
53
+ #
54
+ # Returns a String with the logger name or an IO object
55
+ def self.logger_name
56
+ if defined?(Rails)
57
+ Rails.join('log', "pingr.#{Rails.env}.log")
58
+ else
59
+ STDOUT
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,73 @@
1
+ module Pingr
2
+
3
+ # Public: The object created to ping a search engine with a sitemap
4
+ class Request
5
+
6
+ require 'logger'
7
+ require 'net/http'
8
+ require 'uri'
9
+
10
+ # Public: Gets/Sets the String url of the sitemap we're submitting
11
+ attr_accessor :sitemap_url
12
+
13
+ # Public: Gets/Sets the String or Symbol name of the search engine we're pinging
14
+ attr_accessor :search_engine
15
+
16
+ # Public: Initialize a new ping request
17
+ #
18
+ # search_engine - A String or Symbol name of the search engine we're pinging
19
+ # sitemap_url - A String url of the sitemap we're submitting
20
+ #
21
+ # Returns a Pingr::Request object
22
+ def initialize(search_engine, sitemap_url)
23
+ self.search_engine = search_engine.to_sym
24
+ self.sitemap_url = sitemap_url
25
+ end
26
+
27
+ # Public: The path to ping to submit sitemaps for this search_engine
28
+ #
29
+ # Returns a String with the correct path to submit sitemaps
30
+ #
31
+ # Raises: A PingrError if the search_engine attribute's value is not supported
32
+ def ping_path
33
+ case search_engine
34
+ # http://www.google.com/webmasters/tools/ping?sitemap=
35
+ when :google then "webmasters/tools/ping?sitemap=#{URI.escape(sitemap_url)}"
36
+ # http://www.bing.com/ping?sitemap=
37
+ when :bing then "ping?sitemap=#{URI.escape(sitemap_url)}"
38
+ else
39
+ raise PingrError, "Don't know how to ping search engine: #{search_engine}"
40
+ end
41
+ end
42
+
43
+ # Public: Perform the ping request (if in :live mode)
44
+ # Logs the success/failure of the ping in logger.
45
+ #
46
+ # Returns true if ping was a success
47
+ # Returns false if ping was not successful
48
+ def ping
49
+ return true unless Pingr.mode == :test
50
+ uri = URI.parse("http://#{search_engine}.com/#{ping_path}")
51
+ http = Net::HTTP.new(uri.host, uri.port)
52
+ request = Net::HTTP::Get.new(uri.request_uri)
53
+ response = http.request(request)
54
+ if response.code.to_s =~ /200|301/
55
+ logger.info "Pinged #{search_engine} Successfully - #{Time.now}"
56
+ return true
57
+ else
58
+ logger.warn "Error pinging #{search_engine}! (response code: #{response.code})- #{Time.now}"
59
+ return false
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ # Private: A helper method to access Pingr::logger
66
+ #
67
+ # Returns A Logger instance
68
+ def logger
69
+ Pingr.logger
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module Pingr
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pingr/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "pingr"
8
+ gem.version = Pingr::VERSION
9
+ gem.authors = ["Bodacious"]
10
+ gem.email = ["bodacious@katanacode.com"]
11
+ gem.description = %q{A simple gem for pinging search engines with your XML sitemap}
12
+ gem.summary = %q{Ping search engines with your XML Sitemap}
13
+ gem.homepage = "https://github.com/KatanaCode/pingr"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rake"
21
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pingr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bodacious
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
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: A simple gem for pinging search engines with your XML sitemap
31
+ email:
32
+ - bodacious@katanacode.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/pingr.rb
43
+ - lib/pingr/request.rb
44
+ - lib/pingr/version.rb
45
+ - pingr.gemspec
46
+ homepage: https://github.com/KatanaCode/pingr
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ segments:
59
+ - 0
60
+ hash: 3122621333235688264
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ segments:
68
+ - 0
69
+ hash: 3122621333235688264
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.24
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Ping search engines with your XML Sitemap
76
+ test_files: []