Class: Hackpad::Cli::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, input = STDIN, output = STDOUT) ⇒ Client

Returns a new instance of Client.



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

def initialize(options, input = STDIN, output = STDOUT)
  @output = output
  @input = input
  @options = options
  @config = Config.new @options, input, output
  Store.prepare @config
  Api.prepare @config
  if @options[:plain] == true || @config.use_colors == false
    Paint.mode = 0
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/hackpad/cli/client.rb', line 14

def config
  @config
end

Instance Method Details

#defaultObject



34
35
36
# File 'lib/hackpad/cli/client.rb', line 34

def default
  @config.change_default
end

#getnewObject



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

def getnew
  @output.puts 'New pads:'
  padlist = Padlist.get_new
  if padlist.count == 0
    @output.puts 'There is no new pad.'
  else
    @output.puts padlist.map { |pad|
      padline pad
    }
  end
end

#info(id) ⇒ Object



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

def info(id)
  pad = Pad.new id
  pad.load 'txt'
  table 'Id', Paint[id, :bold]
  table 'Title', Paint[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



52
53
54
55
56
# File 'lib/hackpad/cli/client.rb', line 52

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

#search(term, start = 0) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/hackpad/cli/client.rb', line 44

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

#show(id, format) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/hackpad/cli/client.rb', line 83

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



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

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

#workspacesObject



28
29
30
31
32
# File 'lib/hackpad/cli/client.rb', line 28

def workspaces
  @config.workspaces.each do |s|
    table s.name, s.site
  end
end