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.



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



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

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



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

def eval!(&block)
  file.eval! &block
end

#fileObject



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

def file
  return @file unless @file.nil?

  if gist.files.count > 1 && !filename.nil?
    @file = Gisture::File.new(gist.files[filename], basename: gist_id, strategy: strategy)
    raise ArgumentError, "The filename '#{filename}' was not found in the list of files for the gist '#{gist_id}'" if @file.nil?
  else
    @file = Gisture::File.new(gist.files.first[1], basename: gist_id, strategy: strategy)
  end

  @file
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



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

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



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

def load!(&block)
  file.load! &block
end

#require!(&block) ⇒ Object



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

def require!(&block)
  file.require! &block
end

#run!(&block) ⇒ Object



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

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

#to_hObject



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

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