Skip to content

Commit

Permalink
added local mode, and list cached options
Browse files Browse the repository at this point in the history
  • Loading branch information
auron61 committed Feb 12, 2012
1 parent 2c7eda1 commit 3130a4b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/cheat.rb
Expand Up @@ -17,14 +17,19 @@ def sheets(args)

uri = "http://#{cheat_uri}/y/"

if %w[sheets all recent].include? @sheet
uri = uri.sub('/y/', @sheet == 'recent' ? '/yr/' : '/ya/')
return open(uri, headers) { |body| process(body.read) }
end

return process(File.read(cache_file)) if File.exists?(cache_file) rescue clear_cache if cache_file
if @offline
return process(File.read(cache_file)) if File.exists?(cache_file) rescue clear_cache if cache_file

else
if %w[sheets all recent].include? @sheet
uri = uri.sub('/y/', @sheet == 'recent' ? '/yr/' : '/ya/')
return open(uri, headers) { |body| process(body.read) }
end

return process(File.read(cache_file)) if File.exists?(cache_file) rescue clear_cache if cache_file

fetch_sheet(uri + @sheet) if @sheet
fetch_sheet(uri + @sheet) if @sheet
end
end

def fetch_sheet(uri, try_to_cache = true)
Expand Down Expand Up @@ -53,10 +58,14 @@ def parse_args(args)

show_versions(args.first) if args.delete('--versions')

list if args.delete('--list')

add(args.shift) and return if args.delete('--add')
incoming_file = true if @edit = args.delete('--edit')

@execute = true if args.delete("--execute") || args.delete("-x")
# use offline (use cached versions only) if no active connection to internet
@offline = true if args.delete("--local") || args.delete("-l")
@sheet = args.shift

clear_cache_file if incoming_file
Expand Down Expand Up @@ -122,6 +131,13 @@ def process(sheet_yaml)
show(sheet_yaml)
end
end

def list
if cache_dir
d = Dir.glob "#{cache_dir}/#{@sheet}*.yml"
d.each {|f| puts File.basename(f, ".yml")}
end
end

def show(sheet_yaml)
sheet = YAML.load(sheet_yaml).to_a.first
Expand Down

0 comments on commit 3130a4b

Please sign in to comment.