Module: Cheatr::Client

Extended by:
Pager
Defined in:
lib/cheatr/client.rb,
lib/cheatr/client/sheet.rb

Defined Under Namespace

Classes: Sheet

Class Method Summary collapse

Class Method Details

.browse(query = nil) ⇒ Object



57
58
59
60
61
# File 'lib/cheatr/client.rb', line 57

def self.browse(query = nil)
  open_cmd = `uname` =~ /Darwin/ ? 'open' : 'xdg-open'
  uri = "http://#{server}/#{query}"
  exec "#{open_cmd} #{uri}"
end

.cache_dirObject



100
101
102
# File 'lib/cheatr/client.rb', line 100

def self.cache_dir
  @@cache_dir ||= mkdir File.join(cheatr_dir, 'cache', server.gsub(/:\d+\z/, ''))
end

.cheatr_dirObject



96
97
98
# File 'lib/cheatr/client.rb', line 96

def self.cheatr_dir
  @@cheatr_dir ||= mkdir File.join(File.expand_path("~"), ".cheatr")
end

.clear_cache(name = :all, opts = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cheatr/client.rb', line 75

def self.clear_cache(name = :all, opts = {})
  if name == :all
    if opts[:quiet] || opts[:skip_confirmation] || confirm("Are you sure you want clear all cheat sheets cache?")
      FileUtils.rm Dir.glob("#{cache_dir}/*.md")
      puts "All cached cheat sheets have been removed." unless opts[:quiet]
    else
      puts "Cached cheat sheets were not removed." unless opts[:quiet]
    end
  else
    FileUtils.rm "#{cache_dir}/#{name}.md", force: true
    puts "Removed cached version of '#{name}'." unless opts[:quiet]
  end
end

.configObject



116
117
118
# File 'lib/cheatr/client.rb', line 116

def self.config
  @@config ||= YAML.load_file(config_file) || {} rescue {}
end

.config_fileObject



112
113
114
# File 'lib/cheatr/client.rb', line 112

def self.config_file
  @@config_file ||= File.join(cheatr_dir, 'config.yml')
end

.display_sheet(name, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/cheatr/client.rb', line 17

def self.display_sheet(name, opts = {})
  sheet = Sheet.new(name, opts)
  if sheet.contents
    page
    puts "(cached version)" unless sheet.remote?
    puts sheet.contents
  else
    puts sheet.errors.first
  end
end

.edit_sheet(name, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cheatr/client.rb', line 28

def self.edit_sheet(name, opts = {})
  sheet = Sheet.new(name, ignore_cache: true)

  file = Tempfile.new([name, '.md'])
  file.write(sheet.contents) if sheet.contents
  file.close
  system "#{editor} #{file.path}"
  file.open
  contents = file.read
  file.close

  if contents.strip.empty?
    puts "Edited sheet contents were empty so nothing was saved."
    return
  end

  if opts[:skip_confirmation] || confirm("Do you want to update the '#{name}' cheat sheet?")
    sheet.contents = contents
    if sheet.save
      puts "Sheet '#{name}' was updated successfully."
    else
      puts "Sheet '#{name}' could not be updated."
      $stderr.puts "Error: #{sheet.errors.first}" if sheet.errors
    end
  else
    puts "Sheet '#{name}' was left unchanged."
  end
end

.editorObject



108
109
110
# File 'lib/cheatr/client.rb', line 108

def self.editor
  @@editor ||= ENV['EDITOR']
end

.fetch_sheets(arr, opts = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cheatr/client.rb', line 63

def self.fetch_sheets(arr, opts = {})
  arr.each do |name|
    sheet = Sheet.new(name, ignore_cache: true)
    if sheet.contents
      puts "Sheet '#{name}' fetched successfully." unless opts[:quiet] || opts[:errors]
    else
      message = sheet.errors.first || "Sheet '#{name}' could not be fetched."
      puts message unless opts[:quiet]
    end
  end
end

.mkdir(dir) ⇒ Object

Configuration settings



91
92
93
94
# File 'lib/cheatr/client.rb', line 91

def self.mkdir(dir)
  FileUtils.mkdir_p dir
  dir
end

.search_sheets(query = nil) ⇒ Object

Actions



11
12
13
14
15
# File 'lib/cheatr/client.rb', line 11

def self.search_sheets(query = nil)
  query += '*' unless query.nil? || query.include?('*')
  page
  puts Sheet.all(query)
end

.serverObject



104
105
106
# File 'lib/cheatr/client.rb', line 104

def self.server
  @@sever ||= config['server']
end

.set_config(options) ⇒ Object



120
121
122
123
# File 'lib/cheatr/client.rb', line 120

def self.set_config(options)
  config.merge!(options)
  File.open(config_file, 'w') { |f| f.write config.to_yaml }
end