Class: GitHub::Command

Inherits:
Object show all
Defined in:
lib/github/command.rb

Defined Under Namespace

Classes: Shell

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Command

Returns a new instance of Command.



14
15
16
# File 'lib/github/command.rb', line 14

def initialize(block)
  (class << self;self end).send :define_method, :command, &block
end

Instance Method Details

#cache_data(user) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/github/command.rb', line 66

def cache_data(user)
  raw_data = open(helper.network_meta_for(user)).read
  File.open( network_cache_path, 'w' ) do |out|
    out.write(raw_data)
  end
  data = JSON.parse(raw_data)
end

#cache_expired?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/github/command.rb', line 74

def cache_expired?
  return true if !has_cache?
  age = Time.now - File.stat(network_cache_path).mtime
  return true if age > (60 * 60) # 1 hour
  false
end

#call(*args) ⇒ Object



18
19
20
21
22
# File 'lib/github/command.rb', line 18

def call(*args)
  arity = method(:command).arity
  args << nil while args.size < arity
  send :command, *args
end

#die(message) ⇒ Object



89
90
91
92
# File 'lib/github/command.rb', line 89

def die(message)
  puts "=> #{message}"
  exit!
end

#get_cacheObject



85
86
87
# File 'lib/github/command.rb', line 85

def get_cache
  JSON.parse(File.read(network_cache_path))
end

#get_network_data(user, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/github/command.rb', line 50

def get_network_data(user, options)
  if options[:cache] && has_cache?
    return get_cache
  end
  if cache_expired? || options[:nocache] || !has_cache?
    return cache_data(user)
  else
    return get_cache
  end
end

#git(*command) ⇒ Object



36
37
38
# File 'lib/github/command.rb', line 36

def git(*command)
  sh ['git', command].flatten.join(' ')
end

#git_exec(*command) ⇒ Object



40
41
42
43
44
# File 'lib/github/command.rb', line 40

def git_exec(*command)
  cmdstr = ['git', command].flatten.join(' ')
  GitHub.debug "exec: #{cmdstr}"
  exec cmdstr
end

#has_cache?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/github/command.rb', line 81

def has_cache?
  File.file?(network_cache_path)
end

#helperObject



24
25
26
# File 'lib/github/command.rb', line 24

def helper
  @helper ||= Helper.new
end

#network_cache_pathObject



61
62
63
64
# File 'lib/github/command.rb', line 61

def network_cache_path
  dir = `git rev-parse --git-dir`.chomp
  File.join(dir, 'network-cache')
end

#optionsObject



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

def options
  GitHub.options
end

#pgit(*command) ⇒ Object



32
33
34
# File 'lib/github/command.rb', line 32

def pgit(*command)
  puts git(*command)
end

#sh(*command) ⇒ Object



46
47
48
# File 'lib/github/command.rb', line 46

def sh(*command)
  Shell.new(*command).run
end