Class: Ghundle::Source::Github

Inherits:
Common
  • Object
show all
Defined in:
lib/ghundle/source/github.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Common

#fetched?

Constructor Details

#initialize(description) ⇒ Github

Returns a new instance of Github.



21
22
23
24
25
26
# 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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/ghundle/source/github.rb', line 18

def path
  @path
end

#repoObject (readonly)

Returns the value of attribute repo.



18
19
20
# File 'lib/ghundle/source/github.rb', line 18

def repo
  @repo
end

#script_nameObject (readonly)

Returns the value of attribute script_name.



19
20
21
# File 'lib/ghundle/source/github.rb', line 19

def script_name
  @script_name
end

#usernameObject (readonly)

Returns the value of attribute username.



18
19
20
# File 'lib/ghundle/source/github.rb', line 18

def username
  @username
end

Instance Method Details

#fetch(destination_path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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(.to_h))
  end

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

  local_source
end

#hook_nameObject



28
29
30
# File 'lib/ghundle/source/github.rb', line 28

def hook_name
  path.basename
end

#metadataObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ghundle/source/github.rb', line 32

def 
   ||=
    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

      .new(YAML.load(yaml))
    end
end

#to_sObject



72
73
74
# File 'lib/ghundle/source/github.rb', line 72

def to_s
  @path
end