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.



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

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.



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

def config
  @config
end

Instance Method Details

#defaultObject



36
37
38
# File 'lib/hackpad/cli/client.rb', line 36

def default
  @config.change_default
end

#getnewObject



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

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



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

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



54
55
56
57
58
# File 'lib/hackpad/cli/client.rb', line 54

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

#search(term, start = 0) ⇒ Object



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

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



85
86
87
88
89
# File 'lib/hackpad/cli/client.rb', line 85

def show(id, format)
  pad = Pad.new id
  pad.load format
  @output.puts pad.content
end

#statsObject



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

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



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

def workspaces
  @config.workspaces.each do |s|
    if s.name == @config.workspace
      s.name = "> #{s.name}"
    end
    table s.name, s.site
  end
end