Module: Awestruct::Extensions::RemotePartial

Defined in:
lib/awestruct/extensions/remotePartial.rb

Instance Method Summary collapse

Instance Method Details

#get_or_cache(tmp_file, url) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/awestruct/extensions/remotePartial.rb', line 17

def get_or_cache(tmp_file, url)
  response_body = ""
  if !File.exist?tmp_file
    $LOG.info url if $LOG.info? && !config.quiet
    response_body = RestClient.get(url, :cache => false) { |response, request, result, &block|
      case response.code
      when 404
          response
      else
        response.return!(request, result, &block)
      end
    }.body;
    File.open(tmp_file, 'w') do |out|
      out.write response_body
    end
  else
    response_body = File.read(tmp_file)
  end
  return response_body
end

#remote_partial(url) ⇒ Object Also known as: remotePartial



8
9
10
11
12
13
# File 'lib/awestruct/extensions/remotePartial.rb', line 8

def remote_partial(url)
  url_tmp = url.sub('http://', '')
  r = 'remote_partial/' + url_tmp[/(.*)\/[^\/].+$/, 1]
  tmp = File.join(tmp(site.tmp_dir, r), File.basename(url_tmp))
  get_or_cache(tmp, url)
end