Class: GitHub::Command
Defined Under Namespace
Classes: Shell
Instance Method Summary collapse
- #cache_data(user) ⇒ Object
- #cache_expired? ⇒ Boolean
- #call(*args) ⇒ Object
- #die(message) ⇒ Object
- #get_cache ⇒ Object
- #get_network_data(user, options) ⇒ Object
- #git(*command) ⇒ Object
- #git_exec(*command) ⇒ Object
- #has_cache? ⇒ Boolean
- #helper ⇒ Object
-
#initialize(block) ⇒ Command
constructor
A new instance of Command.
- #network_cache_path ⇒ Object
- #options ⇒ Object
- #pgit(*command) ⇒ Object
- #sh(*command) ⇒ Object
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.(user)).read File.open( network_cache_path, 'w' ) do |out| out.write(raw_data) end data = JSON.parse(raw_data) end |
#cache_expired? ⇒ 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() puts "=> #{message}" exit! end |
#get_cache ⇒ Object
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, ) if [:cache] && has_cache? return get_cache end if cache_expired? || [: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
81 82 83 |
# File 'lib/github/command.rb', line 81 def has_cache? File.file?(network_cache_path) end |
#helper ⇒ Object
24 25 26 |
# File 'lib/github/command.rb', line 24 def helper @helper ||= Helper.new end |
#network_cache_path ⇒ Object
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 |
#options ⇒ Object
28 29 30 |
# File 'lib/github/command.rb', line 28 def GitHub. 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 |