Class: Octopress::Printable::GistCache

Inherits:
Object
  • Object
show all
Defined in:
lib/octopress-printable/gist_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ GistCache

Returns a new instance of GistCache.



17
18
19
20
21
22
# File 'lib/octopress-printable/gist_cache.rb', line 17

def initialize(text)
  @text           = text
  @cache_disabled = false
  @cache_folder   = '.gist-cache'
  FileUtils.mkdir_p @cache_folder
end

Instance Method Details

#cache(gist, file, data) ⇒ Object



58
59
60
61
62
63
# File 'lib/octopress-printable/gist_cache.rb', line 58

def cache(gist, file, data)
  cache_file = get_cache_file_for gist, file
  File.open(cache_file, "w") do |io|
    io.write data
  end
end

#get_cache_file_for(gist, file) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/octopress-printable/gist_cache.rb', line 71

def get_cache_file_for(gist, file)
  bad_chars = /[^a-zA-Z0-9\-_.]/
  gist      = gist.gsub bad_chars, ''
  file      = file.gsub bad_chars, ''
  md5       = Digest::MD5.hexdigest "#{gist}-#{file}"
  File.join @cache_folder, "cache.#{md5}-#{gist}-#{file}"
end

#get_cached_gist(gist, file) ⇒ Object



65
66
67
68
69
# File 'lib/octopress-printable/gist_cache.rb', line 65

def get_cached_gist(gist, file)
  return nil if @cache_disabled
  cache_file = get_cache_file_for gist, file
  File.read cache_file if File.exist? cache_file
end

#get_gist_from_web(gist, file) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/octopress-printable/gist_cache.rb', line 79

def get_gist_from_web(gist, file)
  gist_url = get_gist_url_for(gist, file)
  data     = get_web_content(gist_url)
    
  locations = Array.new
  while (data.code.to_i == 301 || data.code.to_i == 302)
    data = handle_gist_redirecting(data)
    break if locations.include? data.header['Location']
    locations << data.header['Location']
  end
    
  if data.code.to_i != 200
    raise RuntimeError, "Gist replied with #{data.code} for #{gist_url}"
  end
    
  cache(gist, file, data.body) unless @cache_disabled
  data.body
end

#get_gist_url_for(gist, file) ⇒ Object



54
55
56
# File 'lib/octopress-printable/gist_cache.rb', line 54

def get_gist_url_for(gist, file)
  "https://gist.githubusercontent.com/raw/#{gist}/#{file}"
end

#get_web_content(url) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/octopress-printable/gist_cache.rb', line 107

def get_web_content(url)
  raw_uri           = URI.parse url
  proxy             = ENV['http_proxy']
  if proxy
    proxy_uri       = URI.parse(proxy)
    https           = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new raw_uri.host, raw_uri.port
  else
    https           = Net::HTTP.new raw_uri.host, raw_uri.port
  end
  https.use_ssl     = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request           = Net::HTTP::Get.new raw_uri.request_uri
  data              = https.request request
end

#handle_gist_redirecting(data) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/octopress-printable/gist_cache.rb', line 98

def handle_gist_redirecting(data)
  redirected_url = data.header['Location']
  if redirected_url.nil? || redirected_url.empty?
    raise ArgumentError, "GitHub replied with a 302 but didn't provide a location in the response headers."
  end
    
  get_web_content(redirected_url)
end

#html_output_for(script_url, code) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/octopress-printable/gist_cache.rb', line 40

def html_output_for(script_url, code)
  code = CGI.escapeHTML code
  "    iv><script src='\#{script_url}'></script>\n    oscript><pre><code>\#{code}</code></pre></noscript></div>\n  HTML\nend\n"

#renderObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/octopress-printable/gist_cache.rb', line 24

def render()
  if parts = @text.match(/([a-zA-Z\d]*) (.*)/)
    gist, file = parts[1].strip, parts[2].strip
  else
    gist, file = @text.strip, ""
  end
  if gist.empty?
    ""
  else
    script_url = script_url_for gist, file
    code       = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
    #html_output_for script_url, code
    cache_file = get_cache_file_for gist, file
  end
end

#script_url_for(gist_id, filename) ⇒ Object



48
49
50
51
52
# File 'lib/octopress-printable/gist_cache.rb', line 48

def script_url_for(gist_id, filename)
  url = "https://gist.github.com/#{gist_id}.js"
  url = "#{url}?file=#{filename}" unless filename.nil? or filename.empty?
  url
end