aboutsummaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
authorJeff Shumate <jeff@catapulsion.net>2012-01-25 22:27:46 -0700
committerJeff Shumate <jeff@catapulsion.net>2012-01-25 22:27:46 -0700
commite274bb1757aa3c27645c181bae6164131a7a35dd (patch)
tree2c896df9bd0a66aa288bfdf8f30fe767516ec0bc /config
parentd1ad82d42eceb53b203716e1c37f4fbb326a2ee8 (diff)
downloadrefinerycms-blog-e274bb1757aa3c27645c181bae6164131a7a35dd.tar.gz
refinerycms-blog-e274bb1757aa3c27645c181bae6164131a7a35dd.tar.bz2
refinerycms-blog-e274bb1757aa3c27645c181bae6164131a7a35dd.zip
add source url for post
Diffstat (limited to 'config')
-rw-r--r--config/initializers/url_validator.rb85
-rw-r--r--config/locales/en.yml7
2 files changed, 91 insertions, 1 deletions
diff --git a/config/initializers/url_validator.rb b/config/initializers/url_validator.rb
new file mode 100644
index 0000000..15635e0
--- /dev/null
+++ b/config/initializers/url_validator.rb
@@ -0,0 +1,85 @@
+require 'net/http'
+
+class UrlValidator < ActiveModel::EachValidator
+
+ def validate_each(record, attribute, value)
+ url = value
+
+ # Regex code by 'Arsenic' from http://snippets.dzone.com/posts/show/3654
+ if url =~ /^
+( (https?):\/\/ )?
+( [a-z\d]+([\-\.][a-z\d]+)*\.[a-z]{2,6} )
+(
+(:
+( \d{1,5} )
+)?
+( \/.* )?
+)?
+$/ix
+ url = "http#{'s' if $7 == '81'}://#{url}" unless $1
+ else
+ record.errors[attribute] << 'Not a valid URL'
+ end
+
+ if options[:verify]
+ begin
+ url_response = RedirectFollower.new(url).resolve
+ url = url_response.url if options[:verify] == [:resolve_redirects]
+ rescue RedirectFollower::TooManyRedirects
+ record.errors[attribute] << 'URL is redirecting too many times'
+ rescue
+ record.errors[attribute] << 'could not be resolved'
+ end
+ end
+
+ if options[:update]
+ value.replace url
+ end
+ end
+end
+
+# Code below written by John Nunemaker
+# See blog post at http://railstips.org/blog/archives/2009/03/04/following-redirects-with-nethttp/
+class RedirectFollower
+ class TooManyRedirects < StandardError; end
+
+ attr_accessor :url, :body, :redirect_limit, :response
+
+ def initialize(url, limit=5)
+ @url, @redirect_limit = url, limit
+ logger.level = Logger::INFO
+ end
+
+ def logger
+ @logger ||= Logger.new(STDOUT)
+ end
+
+ def resolve
+ raise TooManyRedirects if redirect_limit < 0
+
+ self.response = Net::HTTP.get_response(URI.parse(url))
+
+ logger.info "redirect limit: #{redirect_limit}"
+ logger.info "response code: #{response.code}"
+ logger.debug "response body: #{response.body}"
+
+ if response.kind_of?(Net::HTTPRedirection)
+ self.url = redirect_url
+ self.redirect_limit -= 1
+
+ logger.info "redirect found, headed to #{url}"
+ resolve
+ end
+
+ self.body = response.body
+ self
+ end
+
+ def redirect_url
+ if response['location'].nil?
+ response.body.match(/<a href=\"([^>]+)\">/i)[1]
+ else
+ response['location']
+ end
+ end
+end \ No newline at end of file
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 64d4064..a3268c1 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -41,6 +41,10 @@ en:
published_at: Publish Date
custom_url: Custom Url
custom_url_help: Generate the url for the blog post from this text instead of the title.
+ source_url: Source Url
+ source_url_help: Stores the url for the source of the post material.
+ source_url_title: Source Url Title
+ source_url_title_help: Title for the source url for the post.
author: Author
author_help: Set which user this post will show as the author.
copy_body: Copy Post Body to Teaser
@@ -136,7 +140,8 @@ en:
name: Name
email: Email
message: Message
- by: by
+ by: By
+ source: Source
tagged:
no_blog_articles_yet: There are no blog articles posted yet. Stay tuned.
posts_tagged: Posts tagged