class Ghundle::Source::Github

Represents a remote hook on github.com. The description is of the format:

github.com/<username>/<repo>/<path/to/hook>

Example:

github.com/AndrewRadev/hooks/ctags

It needs to be fetched to the local hook root in order to use the hook.

Attributes

path[R]
repo[R]
script_name[R]
username[R]

Public Class Methods

new(description) click to toggle source
# File lib/ghundle/source/github.rb, line 21
def initialize(description)
  @description            = description
  @username, @repo, @path = parse_description(@description)
  @path                   = Pathname.new(@path)
  @script_name            = path.basename
end

Public Instance Methods

fetch(destination_path) click to toggle source
# File lib/ghundle/source/github.rb, line 46
def fetch(destination_path)
  destination_path = Pathname.new(destination_path)

  local_source = Local.new(destination_path)
  return local_source if local_source.exists?

  FileUtils.mkdir_p(destination_path)

  status, script = http_get(raw_github_url(@path.join('run')))
  if status != 200
    raise AppError.new("Couldn't fetch script file from #{url}, got response status: #{status}")
  end

  destination_path.join('run').open('w') do |f|
    f.write(script)
  end

  destination_path.join('meta.yml').open('w') do |f|
    f.write(YAML.dump(metadata.to_h))
  end

  File.chmod(0755, destination_path.join('run'))

  local_source
end
hook_name() click to toggle source
# File lib/ghundle/source/github.rb, line 28
def hook_name
  path.basename
end
metadata() click to toggle source
# File lib/ghundle/source/github.rb, line 32
def metadata
  @metadata ||=
    begin
      url          = raw_github_url(@path.join('meta.yml'))
      status, yaml = http_get(url)

      if status != 200
        raise AppError.new("Couldn't fetch metadata file from #{url}, got response status: #{status}")
      end

      Metadata.new(YAML.load(yaml))
    end
end
to_s() click to toggle source
# File lib/ghundle/source/github.rb, line 72
def to_s
  @path
end

Private Instance Methods

http_get(url_string) click to toggle source
# File lib/ghundle/source/github.rb, line 78
def http_get(url_string)
  uri      = URI(url_string)
  response = Net::HTTP.get_response(uri)

  [response.code.to_i, response.body]
end
parse_description(description) click to toggle source
# File lib/ghundle/source/github.rb, line 85
def parse_description(description)
  components = description.split('/')
  username   = components[1]
  repo       = components[2]
  path       = components[3..-1].join('/')

  [username, repo, path]
end
raw_github_url(path) click to toggle source
# File lib/ghundle/source/github.rb, line 94
def raw_github_url(path)
  "https://raw.github.com/#{@username}/#{@repo}/master/#{path}"
end