Class: Hackpad::Cli::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hackpad/cli/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, output = STDOUT) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
# File 'lib/hackpad/cli/client.rb', line 14

def initialize(options, output = STDOUT)
  @output = output
  @options = options
  Store.prepare @options
  @config = Config.load @options
  Api.prepare @config
  if @options[:plain]
    load File.expand_path('../plain_colors.rb', __FILE__)
  end
end

Instance Method Details

#checkObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hackpad/cli/client.rb', line 45

def check
  @output.puts 'New pads:'
  padlist = Padlist.check_list(@options['refresh']).map
  if padlist.count == 0
    @output.puts 'There is no new pad.'
  else
    @output.puts padlist.map { |pad|
      padline pad
    }
  end
end

#info(id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hackpad/cli/client.rb', line 57

def info(id)
  pad = Pad.new id
  pad.load 'txt'
  table 'Id', "#{id}".bold
  table 'Title', "#{pad.title}".yellow
  table 'URI', "#{@config['site']}/#{id}"
  table 'Chars', "#{pad.chars}"
  table 'Lines', "#{pad.lines}"
  table 'Guest Policy', "#{pad.guest_policy}"
  table 'Moderated', "#{pad.moderated}"
  table 'Cached', "#{pad.cached_at || 'unknown'}"
end

#listObject



39
40
41
42
43
# File 'lib/hackpad/cli/client.rb', line 39

def list
  @output.puts Padlist.get_list(@options['refresh']).map { |pad|
    padline pad
  }
end

#search(term, start = 0) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/hackpad/cli/client.rb', line 31

def search(term, start = 0)
  payload = Api.search(term, start)
  payload.each do |a|
    @output.puts "#{id_or_url a['id']} - #{unescape(a['title']).yellow}"
    @output.puts "   #{extract a['snippet']}"
  end
end

#show(id, format) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/hackpad/cli/client.rb', line 70

def show(id, format)
  ext = (format == 'md') ? 'html' : format
  pad = Pad.new id
  pad.load ext
  if format == 'md'
    @output.puts ReverseMarkdown.convert(pad.content, github_flavored: true)
  else
    @output.puts pad.content
  end
end

#statsObject



25
26
27
28
29
# File 'lib/hackpad/cli/client.rb', line 25

def stats
  table 'Site', @config['site'].blue
  table 'Cached Pads', Store.count_pads
  table 'Last Refresh', Store.last_refresh || 'not refreshed yet'
end