Module: ContentOMatic
- Defined in:
- lib/content_o_matic/errors.rb,
lib/content_o_matic/response.rb,
lib/content_o_matic/content_cache.rb,
lib/content_o_matic/content_o_matic.rb
Defined Under Namespace
Classes: ContentCache, InvalidResponseError, Response
Class Method Summary collapse
-
.get(slug_url, options = {}) ⇒ Object
This method does pretty much all of the heavy work.
-
.get_without_cache(slug_url, options = {}) ⇒ Object
:nodoc:.
-
.logger ⇒ Object
Returns the logger being used by ContentOMatic.
-
.url_from_slug(slug_url, options = {}) ⇒ Object
get_without_cache.
Class Method Details
.get(slug_url, options = {}) ⇒ Object
This method does pretty much all of the heavy work. You pass it a url and it will fetch the contents of that page and return a ContentOMatic::Response object to you.
If you pass in a ‘relative’ url it will be joined with a default url that can be set with the following configatron setting:
configatron.content_o_matic.url = 'http://www.example.com'
url examples:
'foo.html' => 'http://www.example.com/foo.html'
'/foo.html' => 'http://www.example.com/foo.html'
'/foo/bar.html' => 'http://www.example.com/foo/bar.html'
'http://www.example.com/index.html' => 'http://www.example.com/index.html'
Any options passed into will become query string parameters passed to the requested url.
If you have caching enabled it will look first in the cache, if it doesn’t find it there, it will do a fetch of the page and store it in the cache, if it was successful. See ContentOMatic::ContentCache for more details on caching.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/content_o_matic/content_o_matic.rb', line 36 def get(slug_url, = {}) if configatron.content_o_matic.retrieve(:cache_results, false) url = url_from_slug(slug_url, ) ContentCache.get(url) do |url| content = get_without_cache(slug_url, ) if content.success? ContentCache.set(url, content) end return content end else return get_without_cache(slug_url, ) end end |
.get_without_cache(slug_url, options = {}) ⇒ Object
:nodoc:
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/content_o_matic/content_o_matic.rb', line 51 def get_without_cache(slug_url, = {}) # :nodoc: url = url_from_slug(slug_url, ) begin Timeout::timeout(configatron.content_o_matic.response.time_out) do response = Net::HTTP.get_response(URI.parse(url)) return ContentOMatic::Response.new(url, response.code, response.body) end rescue Exception => e ContentOMatic.logger.error(e) return ContentOMatic::Response.new(url, '500', e.) end end |
.logger ⇒ Object
Returns the logger being used by ContentOMatic. The logger can be set with the following configatron setting:
configatron.content_o_matic.logger = ::Logger.new(STDOUT)
In a Rails environment this defaults to the RAILS_DEFAULT_LOGGER
. The non-Rails default is:
<pwd>/log/content_o_matic.log
12 13 14 |
# File 'lib/content_o_matic/content_o_matic.rb', line 12 def logger configatron.content_o_matic.logger end |
.url_from_slug(slug_url, options = {}) ⇒ Object
get_without_cache
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/content_o_matic/content_o_matic.rb', line 64 def url_from_slug(slug_url, = {}) # :nodoc: return slug_url if slug_url.nil? url = slug_url if !slug_url.match(/^([a-zA-Z]+):\/\//) # it's not a 'remote' url - it has no protocol url = File.join(configatron.content_o_matic.url, slug_url) end if .is_a? Hash opts = .dup unless opts.nil? || opts.empty? opts.delete(:controller) opts.delete(:action) if url.match(/\?/) url << '&' else url << '?' end url << opts.collect {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"}.join("&") end end return url end |