Class: Gisture::Gist

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

Constant Summary collapse

STRATEGIES =
[:eval, :load, :require]
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.



5
6
7
# File 'lib/gisture/gist.rb', line 5

def filename
  @filename
end

#gist_idObject (readonly)

Returns the value of attribute gist_id.



5
6
7
# File 'lib/gisture/gist.rb', line 5

def gist_id
  @gist_id
end

#strategyObject

Returns the value of attribute strategy.



5
6
7
# File 'lib/gisture/gist.rb', line 5

def strategy
  @strategy
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/gisture/gist.rb', line 5

def version
  @version
end

Class Method Details

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



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

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

#eval!(&block) ⇒ Object



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

def eval!(&block)
  clean_room = Evaluator.new(raw)
  clean_room.instance_eval &block if block_given?
  clean_room
end

#gistObject



44
45
46
47
48
49
50
51
52
# File 'lib/gisture/gist.rb', line 44

def gist
  @gist ||= begin
    if @version.nil?
      github.gists.get(gist_id)
    else
      github.gists.version(gist_id, @version)
    end
  end
end

#gist_fileObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gisture/gist.rb', line 54

def gist_file
  return @gist_file unless @gist_file.nil?

  if gist.files.count > 1
    raise ArgumentError, "You must specify a filename if your gist contains more than one file" if filename.nil?
    gist.files.each do |file|
      @gist_file = file if file[0] == filename
    end
    raise ArgumentError, "The filename '#{filename}' was not found in the list of files for the gist '#{gist_id}'" if @gist_file.nil?
  else
    @gist_file = gist.files.first
  end

  @gist_file
end

#githubObject



37
38
39
40
41
42
# File 'lib/gisture/gist.rb', line 37

def github
  @github ||= begin
    github_config = Hash[Gisture::GITHUB_CONFIG_OPTS.map { |key| [key, Gisture.configuration.send(key)] }]
    Github.new(github_config)
  end
end

#load!(&block) ⇒ Object



25
26
27
28
29
# File 'lib/gisture/gist.rb', line 25

def load!(&block)
  loaded = load tempfile.path
  unlink_tempfile
  block_given? ? yield : loaded
end

#rawObject



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

def raw
  gist_file[1].content
end

#require!(&block) ⇒ Object



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

def require!(&block)
  required = require tempfile.path
  unlink_tempfile
  block_given? ? yield : required
end

#run!(&block) ⇒ Object



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

def run!(&block)
  send "#{strategy}!".to_sym, &block
end

#tempfileObject



79
80
81
82
83
84
85
86
# File 'lib/gisture/gist.rb', line 79

def tempfile
  @tempfile ||= begin
    file = Tempfile.new([gist_id, File.extname(gist_file[0])], Gisture.configuration.tmpdir)
    file.write(raw)
    file.close
    file
  end
end

#to_hObject



88
89
90
91
92
93
# File 'lib/gisture/gist.rb', line 88

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