Class: Gollum::Gitcode

Inherits:
Object
  • Object
show all
Defined in:
lib/gollum-lib/gitcode.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Gitcode

Returns a new instance of Gitcode.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
# File 'lib/gollum-lib/gitcode.rb', line 9

def initialize(path)
  raise(ArgumentError, 'path is nil or empty') if path.nil? or path.empty?

  @uri = URI::HTTP.build({
                             :path   => self.unchomp(path),
                             :host   => 'raw.github.com',
                             :scheme => 'https',
                             :port   => 443 })
end

Instance Method Details

#contentsObject



19
20
21
# File 'lib/gollum-lib/gitcode.rb', line 19

def contents
  @contents ||= self.req @uri
end

#req(uri, cut = 1) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gollum-lib/gitcode.rb', line 28

def req(uri, cut = 1)
  return "Too many redirects or retries" if cut >= 10
  http         = Net::HTTP.new uri.host, uri.port
  http.use_ssl = true
  resp         = http.get uri.path, {
      'Accept'        => 'text/plain',
      'Cache-Control' => 'no-cache',
      'Connection'    => 'keep-alive',
      'Host'          => uri.host,
      'User-Agent'    => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0'
  }
  code         = resp.code.to_i
  return resp.body if code == 200
  return "Not Found" if code == 404
  return "Unhandled Response Code #{code}" unless code == 304 or not resp.header['location'].nil?
  loc  = URI.parse resp.header['location']
  uri2 = loc.relative?() ? (uri + loc) : loc # overloads (+)
  return req uri2, (cut + 1)
end

#unchomp(p) ⇒ Object



23
24
25
26
# File 'lib/gollum-lib/gitcode.rb', line 23

def unchomp(p)
  return p if p.nil?
  p[0] == '/' ? p : ('/' + p)
end