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

Class Method Details

.get(slug_url, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/content_o_matic/content_o_matic.rb', line 9

def get(slug_url, options = {})
  if configatron.content_o_matic.retrieve(:cache_results, false)
    url = url_from_slug(slug_url, options)
    ContentCache.get(url) do |url|
      content = get_without_cache(slug_url, options)
      if content.success?
        ContentCache.set(url, content)
      end
      return content
    end
  else
    return get_without_cache(slug_url, options)
  end
end

.get_without_cache(slug_url, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/content_o_matic/content_o_matic.rb', line 24

def get_without_cache(slug_url, options = {})
  url = url_from_slug(slug_url, options)
  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.message)
  end
end

.loggerObject



5
6
7
# File 'lib/content_o_matic/content_o_matic.rb', line 5

def logger
  configatron.content_o_matic.logger
end

.url_from_slug(slug_url, options = {}) ⇒ Object

get_without_cache



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/content_o_matic/content_o_matic.rb', line 37

def url_from_slug(slug_url, options = {})
  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 options.is_a? Hash
    opts = options.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