Sha256: f42f1e473ab687096ae2ced64fa9da96c5e68d93e0389b9c4d03e9d61226cc0e

Contents?: true

Size: 1.52 KB

Versions: 31

Compression:

Stored size: 1.52 KB

Contents

# ~*~ encoding: utf-8 ~*~
require 'net/http'
require 'net/https' # ruby 1.8.7 fix, remove at upgrade
require 'uri'
require 'open-uri'

module Gollum
  class Gitcode
    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

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

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

    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
  end
end

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
gitlab-gollum-lib-4.2.7.10.gitlab.2 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.10.gitlab.1 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.9 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.8 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.7 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.6 lib/gollum-lib/gitcode.rb
gollum-lib-4.2.10-java lib/gollum-lib/gitcode.rb
gollum-lib-4.2.10 lib/gollum-lib/gitcode.rb
gollum-lib-4.2.9-java lib/gollum-lib/gitcode.rb
gollum-lib-4.2.9 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.5 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.4 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.3 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.2 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7.1 lib/gollum-lib/gitcode.rb
gitlab-gollum-lib-4.2.7 lib/gollum-lib/gitcode.rb
gollum-lib-4.2.7-java lib/gollum-lib/gitcode.rb
gollum-lib-4.2.7 lib/gollum-lib/gitcode.rb
gollum-lib-4.2.5-java lib/gollum-lib/gitcode.rb
gollum-lib-4.2.5 lib/gollum-lib/gitcode.rb