Class: Gisture::Gist

Inherits:
Object
  • Object
show all
Defined in:
lib/gisture/gist.rb

Constant Summary collapse

GIST_URL_REGEX =
/\Ahttp.+([0-9a-f]{20,20})\/?\Z/
GIST_URL_WITH_VERSION_REGEX =
/\Ahttp.+([0-9a-f]{20,20})\/([0-9a-f]{40,40})\/?\Z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/gisture/gist.rb', line 3

def filename
  @filename
end

#gist_idObject (readonly)

Returns the value of attribute gist_id.



3
4
5
# File 'lib/gisture/gist.rb', line 3

def gist_id
  @gist_id
end

#strategyObject

Returns the value of attribute strategy.



3
4
5
# File 'lib/gisture/gist.rb', line 3

def strategy
  @strategy
end

#versionObject (readonly)

Returns the value of attribute version.



3
4
5
# File 'lib/gisture/gist.rb', line 3

def version
  @version
end

Class Method Details

.run!(gist, strategy: nil, filename: nil, version: nil, &block) ⇒ Object



8
9
10
# File 'lib/gisture/gist.rb', line 8

def self.run!(gist, strategy: nil, filename: nil, version: nil, &block)
  new(gist, strategy: strategy, filename: filename, version: version).run!(&block)
end

Instance Method Details

#clone(&block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gisture/gist.rb', line 73

def clone(&block)
  return self if cloned?

  Gisture.logger.info "[gisture] Cloning #{owner}/#{gist_id} into #{clone_path}"

  repo_url = "https://#{Gisture.configuration.github.auth_str}@gist.github.com/#{gist_id}.git"
  Git.clone(repo_url, gist_id, path: ::File.dirname(clone_path))

  FileUtils.rm_rf("#{clone_path}/.git")
  ::File.write("#{clone_path}/.gisture", Time.now.to_i.to_s)

  if block_given?
    instance_eval &block
    destroy_clone!
  end

  self
end

#clone!(&block) ⇒ Object



68
69
70
71
# File 'lib/gisture/gist.rb', line 68

def clone!(&block)
  destroy_clone!
  clone
end

#clone_pathObject



64
65
66
# File 'lib/gisture/gist.rb', line 64

def clone_path
  @clone_path ||= ::File.join(Gisture.configuration.tmpdir, owner, gist_id)
end

#cloned?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/gisture/gist.rb', line 96

def cloned?
  ::File.read("#{clone_path}/.gisture").strip
rescue
  false
end

#destroy_clone!Object



92
93
94
# File 'lib/gisture/gist.rb', line 92

def destroy_clone!
  FileUtils.rm_rf(clone_path)
end

#eval!(*args, &block) ⇒ Object



24
25
26
# File 'lib/gisture/gist.rb', line 24

def eval!(*args, &block)
  file.eval! *args, &block
end

#exec!(*args, &block) ⇒ Object



28
29
30
# File 'lib/gisture/gist.rb', line 28

def exec!(*args, &block)
  file.exec! *args, &block
end

#file(fname = nil) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gisture/gist.rb', line 48

def file(fname=nil)
  fname ||= filename
  fname ||= gist.files.first[1].filename
  raise ArgumentError, "The filename '#{fname}' was not found in the list of files for the gist '#{gist_id}'" if gist.files[fname].nil?

  if cloned?
    Gisture::ClonedFile.new(clone_path, fname, basename: "#{owner}/#{gist_id}", strategy: strategy)
  else
    Gisture::File.new(gist.files[fname], basename: "#{owner}/#{gist_id}", strategy: strategy)
  end
end

#gistObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gisture/gist.rb', line 36

def gist
  @gist ||= begin
    if @version.nil?
      g = github.gists.get(gist_id)
    else
      g = github.gists.version(gist_id, @version)
    end
    raise OwnerBlacklisted.new(g.owner.) unless Gisture.configuration.whitelisted?(g.owner.)
    g
  end
end

#githubObject



32
33
34
# File 'lib/gisture/gist.rb', line 32

def github
  @github ||= Github.new(Gisture.configuration.github.to_h)
end

#load!(*args, &block) ⇒ Object



20
21
22
# File 'lib/gisture/gist.rb', line 20

def load!(*args, &block)
  file.load! *args, &block
end

#ownerObject



60
61
62
# File 'lib/gisture/gist.rb', line 60

def owner
  gist.owner.
end

#require!(*args, &block) ⇒ Object



16
17
18
# File 'lib/gisture/gist.rb', line 16

def require!(*args, &block)
  file.require! *args, &block
end

#run!(*args, &block) ⇒ Object



12
13
14
# File 'lib/gisture/gist.rb', line 12

def run!(*args, &block)
  file.run! *args, &block
end

#to_hObject



109
110
111
112
113
114
# File 'lib/gisture/gist.rb', line 109

def to_h
  { gist_id: gist_id,
    version: version,
    strategy: strategy,
    filename: filename }
end