hackpad-cli 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
- require_relative "store"
2
- require_relative "api"
1
+ require_relative 'store'
2
+ require_relative 'api'
3
3
 
4
4
  module Hackpad
5
5
  module Cli
@@ -28,40 +28,39 @@ module Hackpad
28
28
  @content.lines.count if @content
29
29
  end
30
30
 
31
- def load(ext, refresh=false)
32
- raise UnknownFormat unless FORMATS.include? ext
33
- raise UndefinedPad unless @id
34
- if refresh or !Store.exists? ext, @id
35
- load_from_api ext
31
+ def load(ext, refresh = false, save = true)
32
+ fail UnknownFormat unless FORMATS.include? ext
33
+ fail UndefinedPad unless @id
34
+ if refresh || !Store.exists?(ext, @id)
35
+ load_from_api ext, save
36
36
  else
37
37
  load_from_cache ext
38
38
  end
39
39
  end
40
40
 
41
- def load_from_api(ext)
41
+ def load_from_api(ext, dosave = true)
42
42
  @content = Api.read @id, ext
43
- Store.save self, ext
43
+ dosave && Store.save(self, ext)
44
44
  options = Api.read_options @id
45
45
  @guest_policy = options['options']['guestPolicy']
46
- @moderated = !!options['options']['isModerated']
46
+ @moderated = options['options']['isModerated']
47
47
  options['cached_at'] = Time.now
48
48
  @cached_at = options['cached_at']
49
- Store.save_meta @id, options
49
+ dosave && Store.save_options(@id, options)
50
50
  end
51
51
 
52
52
  def load_from_cache(ext)
53
53
  @content = Store.read @id, ext
54
54
  options = Store.read_options @id
55
55
  @guest_policy = options['options']['guestPolicy']
56
- @moderated = !!options['options']['isModerated']
56
+ @moderated = options['options']['isModerated']
57
57
  @cached_at = options['cached_at']
58
58
  end
59
59
 
60
- def is_cached?
60
+ def cached?
61
61
  Store.exists? 'meta', @id
62
62
  end
63
63
 
64
64
  end
65
65
  end
66
66
  end
67
-
@@ -1,25 +1,23 @@
1
1
  require 'ostruct'
2
- require_relative "store"
3
- require_relative "api"
4
- require_relative "pad"
2
+ require_relative 'store'
3
+ require_relative 'api'
4
+ require_relative 'pad'
5
5
 
6
6
  module Hackpad
7
7
  module Cli
8
8
  module Padlist
9
- extend self
9
+ module_function
10
10
 
11
- def get_list(refresh=false)
11
+ def get_list(refresh = false, output = STDOUT)
12
12
  all = []
13
- if refresh or !Store.exists? "padlist"
14
- print "Refreshing "
13
+ if refresh || !Store.exists?('padlist')
14
+ output.print 'Refreshing '
15
15
  list = Api.list
16
16
  list.each do |a|
17
- print "."
18
- pad = Pad.new a
19
- pad.load 'txt', refresh
20
- all << OpenStruct.new( id: a, title: pad.title )
17
+ output.print '.'
18
+ all << get_pad(a, refresh)
21
19
  end
22
- puts " all done."
20
+ output.puts ' all done.'
23
21
  Store.save_list all
24
22
  else
25
23
  all = Store.read_list
@@ -27,13 +25,20 @@ module Hackpad
27
25
  all
28
26
  end
29
27
 
28
+ def get_pad(id, refresh = false)
29
+ pad = Pad.new id
30
+ pad.load 'txt', refresh
31
+ OpenStruct.new(id: id, title: pad.title)
32
+ end
33
+
30
34
  def check_list
31
35
  all = []
32
36
  list = Api.list
33
37
  list.each do |a|
34
38
  pad = Pad.new a
35
- if !pad.is_cached?
36
- all << OpenStruct.new( id: a, title: pad.title )
39
+ unless pad.cached?
40
+ pad.load 'txt', false, false
41
+ all << OpenStruct.new(id: a, title: pad.title)
37
42
  end
38
43
  end
39
44
  all
@@ -42,4 +47,3 @@ module Hackpad
42
47
  end
43
48
  end
44
49
  end
45
-
@@ -1,7 +1,7 @@
1
1
  require 'colorize'
2
2
 
3
3
  class String
4
- def colorize(params)
4
+ def colorize(_)
5
5
  self
6
6
  end
7
7
  end
@@ -1,74 +1,89 @@
1
- require "thor"
2
- require "yaml"
3
- require_relative "client"
4
- require_relative "version"
1
+ require 'thor'
2
+ require 'yaml'
3
+ require_relative 'client'
4
+ require_relative 'version'
5
5
 
6
6
  module Hackpad
7
7
  module Cli
8
8
  class Runner < Thor
9
9
 
10
+ refresh_option = [
11
+ :refresh, {
12
+ aliases: '-r',
13
+ type: 'boolean',
14
+ default: false,
15
+ desc: 'Add this if you want refresh the cache.'
16
+ }
17
+ ]
18
+
19
+ url_option = [
20
+ :urls, {
21
+ aliases: '-u',
22
+ type: 'boolean',
23
+ default: false,
24
+ desc: 'Displays urls rather than pad ids.'
25
+ }
26
+ ]
27
+
10
28
  class_option :configdir,
11
- aliases: "-c",
12
- default: File.join(ENV["HOME"], ".hackpad-cli/"),
13
- desc: "Path to the hackpad-cli directory to use."
29
+ aliases: '-c',
30
+ default: File.join(ENV['HOME'], '.hackpad-cli/'),
31
+ desc: 'Path to the hackpad-cli directory to use.'
14
32
 
15
33
  class_option :workspace,
16
- aliases: "-w",
17
- default: "default",
18
- desc: "Name of the workspace to use."
19
-
20
- class_option :refresh,
21
- aliases: "-r",
22
- type: 'boolean',
23
- default: false,
24
- desc: "Add this if you want refresh the cache."
25
-
26
- class_option :urls,
27
- aliases: "-u",
28
- type: 'boolean',
29
- default: false,
30
- desc: "Displays urls rather than pad ids."
34
+ aliases: '-w',
35
+ default: 'default',
36
+ desc: 'Name of the workspace to use.'
31
37
 
32
38
  class_option :plain,
33
- aliases: "-p",
34
- type: 'boolean',
39
+ aliases: '-p',
40
+ type: 'boolean',
35
41
  default: false,
36
- desc: "Add this if you don't want colors."
42
+ desc: "Add this if you don't want colors."
37
43
 
38
44
  default_task :help
39
45
 
40
- desc "search [term]", "Lists available pads matching [term]."
46
+ desc 'stats', 'Lists configuration values.'
47
+ def stats
48
+ Hackpad::Cli::Client.new(options).stats
49
+ end
50
+
51
+ desc 'search [term]', 'Lists available pads matching [term] (options: -u to show urls)'
52
+ method_option(*url_option)
41
53
  def search(term)
42
54
  Hackpad::Cli::Client.new(options).search term
43
55
  end
44
56
 
45
- desc "list", "Lists available pads."
57
+ desc 'list', 'Lists available pads (options: -u to show urls, -r to refresh).'
58
+ method_option(*refresh_option)
59
+ method_option(*url_option)
46
60
  def list
47
61
  Hackpad::Cli::Client.new(options).list
48
62
  end
49
63
 
50
- desc "check", "Checks differences between local cache and remote list."
64
+ desc 'check', 'Checks differences between local cache and remote list (options: -u to show urls).'
65
+ method_option(*url_option)
51
66
  def check
52
67
  Hackpad::Cli::Client.new(options).check
53
68
  end
54
69
 
55
- desc "info [pad_id]", "gets info for the pad <pad_id>."
70
+ desc 'info [pad_id]', 'gets info for the pad <pad_id>.'
56
71
  def info(pad)
57
72
  Hackpad::Cli::Client.new(options).info pad
58
73
  end
59
74
 
60
- desc "show [pad_id] [format]", "shows pad <pad_id> in format [html,txt,md] (default txt)."
61
- def show(pad,format='txt')
75
+ desc 'show [pad_id] [format]', 'shows pad <pad_id> in format [html,txt,md], default txt (options: -r to refresh).'
76
+ method_option(*refresh_option)
77
+ def show(pad, format = 'txt')
62
78
  Hackpad::Cli::Client.new(options).show pad, format
63
79
  end
64
80
 
65
- desc "version", "Displays the hackpad-cli version."
81
+ desc 'version', 'Displays the hackpad-cli version.'
66
82
  def version
67
83
  puts Hackpad::Cli::VERSION
68
84
  end
69
85
 
70
-
71
- desc "colors", "displays colorize color matrix.", hide: true
86
+ desc 'colors', 'displays colorize color matrix.', hide: true
72
87
  def colors
73
88
  require 'colorize'
74
89
  String.color_matrix ' xoxo '
@@ -5,13 +5,13 @@ require_relative '../cli'
5
5
  module Hackpad
6
6
  module Cli
7
7
  module Store
8
- extend self
8
+ module_function
9
9
 
10
10
  def prepare(config)
11
11
  @refresh = config[:refresh]
12
12
  dir = File.join(config[:configdir], config[:workspace])
13
13
  @pads_dir = File.join(dir, 'pads')
14
- @list_cache = File.join(dir, 'pads.list')
14
+ @list_cache = File.join(@pads_dir, 'padlist')
15
15
  prepare_dirs @pads_dir
16
16
  end
17
17
 
@@ -20,7 +20,7 @@ module Hackpad
20
20
  end
21
21
 
22
22
  def exists?(*path)
23
- !@refresh && File.exists?(File.join(@pads_dir, *path))
23
+ !@refresh && File.exist?(File.join(@pads_dir, *path))
24
24
  end
25
25
 
26
26
  def save(pad, ext)
@@ -29,16 +29,16 @@ module Hackpad
29
29
  end
30
30
  end
31
31
 
32
- def save_meta(id, options)
32
+ def save_options(id, options)
33
33
  File.open(File.join(@pads_dir, 'meta', id), 'w') do |f|
34
34
  f.puts JSON.pretty_generate(options)
35
35
  end
36
36
  end
37
37
 
38
38
  def save_list(pads)
39
- File.open(File.join(@pads_dir, 'padlist'), 'w') do |f|
39
+ File.open(@list_cache, 'w') do |f|
40
40
  pads.each do |p|
41
- f.puts "#{p.id} #{p.title}"
41
+ f.puts "#{p.id} [#{p.cached_at}] #{p.title}"
42
42
  end
43
43
  end
44
44
  end
@@ -54,11 +54,19 @@ module Hackpad
54
54
  end
55
55
 
56
56
  def read_list
57
- File.read(File.join(@pads_dir, 'padlist')).lines.reduce([]) { |a,line|
57
+ File.read(@list_cache).lines.reduce([]) do |a, line|
58
58
  /(?<id>[a-zA-Z0-9]*) (\[(?<cached_at>[-a-zA-Z0-9: ]*)\] )?(?<title>.*)/ =~ line
59
- a << OpenStruct.new( id: id, title: title, cached_at: cached_at )
59
+ a << OpenStruct.new(id: id, title: title, cached_at: cached_at)
60
60
  a
61
- }
61
+ end
62
+ end
63
+
64
+ def count_pads
65
+ Dir.glob(File.join(@pads_dir, 'meta', '*')).count
66
+ end
67
+
68
+ def last_refresh
69
+ File.mtime(@list_cache) if File.exist?(@list_cache)
62
70
  end
63
71
 
64
72
  end
@@ -1,5 +1,5 @@
1
1
  module Hackpad
2
2
  module Cli
3
- VERSION = "0.0.7"
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'webmock/rspec'
5
+ require 'hackpad/cli/api'
6
+
7
+ WebMock.disable_net_connect!(allow: 'codeclimate.com')
8
+
9
+ describe Hackpad::Cli::Api do
10
+
11
+ describe '.search' do
12
+ let(:config) { { 'site' => 'http://x.hackpad.com', 'client_id' => '123', 'secret' => 'aaa' } }
13
+ before { Hackpad::Cli::Api.prepare config }
14
+ context 'when just a simple term is provided,' do
15
+ it 'returns expected json' do
16
+ stub_request(:get, 'http://x.hackpad.com/api/1.0/search?limit=100&q=term&start=0')
17
+ .to_return(body: '[{"title":"API 1.0 Docs","id":"C0E68BD495E9","snippet":"example"}]', status: 200)
18
+ expect(subject.search('term')).to eq([{
19
+ 'title' => 'API 1.0 Docs',
20
+ 'id' => 'C0E68BD495E9',
21
+ 'snippet' => 'example'
22
+ }])
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '.list' do
28
+ let(:config) { { 'site' => 'http://x.hackpad.com', 'client_id' => '123', 'secret' => 'aaa' } }
29
+ before { Hackpad::Cli::Api.prepare config }
30
+ it 'returns expected json' do
31
+ stub_request(:get, 'http://x.hackpad.com/api/1.0/pads/all')
32
+ .to_return(body: '["aaa","bbb"]', status: 200)
33
+ expect(subject.list).to eq(%w(aaa bbb))
34
+ end
35
+ end
36
+
37
+ describe '.read_options' do
38
+ let(:config) { { 'site' => 'http://x.hackpad.com', 'client_id' => '123', 'secret' => 'aaa' } }
39
+ before { Hackpad::Cli::Api.prepare config }
40
+ it 'returns expected json' do
41
+ stub_request(:get, 'http://x.hackpad.com/api/1.0/pad/aaa/options')
42
+ .to_return(body: '{ "options": "xxx"}', status: 200)
43
+ expect(subject.read_options('aaa')).to eq('options' => 'xxx')
44
+ end
45
+ end
46
+
47
+ describe '.read' do
48
+ let(:config) { { 'site' => 'http://x.hackpad.com', 'client_id' => '123', 'secret' => 'aaa' } }
49
+ before { Hackpad::Cli::Api.prepare config }
50
+ it 'returns expected json' do
51
+ stub_request(:get, 'http://x.hackpad.com/api/1.0/pad/aaa/content.html')
52
+ .to_return(body: '<b>blah</b>', status: 200)
53
+ expect(subject.read('aaa', 'html')).to eq('<b>blah</b>')
54
+ end
55
+ end
56
+
57
+ describe '.get' do
58
+ let(:config) { { 'site' => 'http://x.hackpad.com', 'client_id' => '123', 'secret' => 'aaa' } }
59
+ before { Hackpad::Cli::Api.prepare config }
60
+ context 'when proper crendential are provided,' do
61
+ it 'all goes well' do
62
+ stub_request(:get, 'http://x.hackpad.com/api/1.0/xxx')
63
+ .with(headers: {
64
+ 'Accept' => '*/*',
65
+ 'Authorization' => /OAuth oauth_consumer_key="123"/,
66
+ 'User-Agent' => /hackpad-cli v#{Hackpad::Cli::VERSION}/
67
+ })
68
+ .to_return(status: 200, body: '{"some": "result"}')
69
+ expect(Hackpad::Cli::Api.get('/api/1.0/xxx')).to eq('some' => 'result')
70
+ end
71
+ end
72
+ context 'when api endpoint is not found' do
73
+ it 'throws an exception' do
74
+ stub_request(:get, 'http://x.hackpad.com/api/1.0/xxx')
75
+ .with(headers: {
76
+ 'Accept' => '*/*',
77
+ 'Authorization' => /OAuth oauth_consumer_key="123"/,
78
+ 'User-Agent' => /hackpad-cli v#{Hackpad::Cli::VERSION}/
79
+ })
80
+ .to_return(status: 404, body: '{"some": "result"}')
81
+ expect { Hackpad::Cli::Api.get('/api/1.0/xxx') }
82
+ .to raise_error(Hackpad::Cli::ApiException, 'HTTP error, code 404')
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -1,122 +1,219 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require "hackpad/cli/client"
4
+ require 'hackpad/cli/client'
5
5
 
6
6
  describe Hackpad::Cli::Client do
7
7
  let(:configdir) { File.expand_path('../../../../files', __FILE__) }
8
8
  let(:options) { { configdir: configdir, workspace: 'default' } }
9
+ let(:format) { "%-20s %s\n" }
9
10
 
10
- describe ".new" do
11
+ describe '.new' do
11
12
  before { Hackpad::Cli::Api.stub(:prepare) }
12
13
  before { Hackpad::Cli::Store.stub(:prepare) }
13
14
  before { Hackpad::Cli::Config.stub(:load) }
14
15
 
15
- context "when default options are passed," do
16
+ context 'when default options are passed,' do
16
17
  let(:client) { Hackpad::Cli::Client.new options }
17
18
  it { expect(client).to be_a Hackpad::Cli::Client }
18
- context "when colorization is expected," do
19
- it { expect("x".blue).to eq "\e[0;34;49mx\e[0m" }
19
+ context 'when colorization is expected,' do
20
+ it { expect('x'.blue).to eq "\e[0;34;49mx\e[0m" }
20
21
  end
21
22
  end
22
23
 
23
- context "when plain text is required," do
24
- context "when colorization is not expected," do
25
- before { Hackpad::Cli::Client.new options.merge({plain: true}) }
26
- after { load "colorize.rb" }
27
- it { expect("x".blue).to eq "x" }
24
+ context 'when plain text is required,' do
25
+ context 'when colorization is not expected,' do
26
+ before { Hackpad::Cli::Client.new options.merge(plain: true) }
27
+ after { load 'colorize.rb' }
28
+ it { expect('x'.blue).to eq 'x' }
28
29
  end
29
30
  end
30
31
  end
31
32
 
32
- describe ".search" do
33
+ describe '.stats' do
34
+ let(:timestamp) { Time.new(2013, 10, 2) }
35
+ before { Hackpad::Cli::Store.stub(:prepare) }
36
+ before { Hackpad::Cli::Config.stub(:load).and_return('site' => 'http://test.dev') }
37
+ before { Hackpad::Cli::Store.stub(:count_pads).and_return(12) }
38
+ before { Hackpad::Cli::Store.stub(:last_refresh).and_return(timestamp) }
39
+ let(:client) { Hackpad::Cli::Client.new options }
40
+ it do
41
+ expect(STDOUT).to receive(:printf).with(format, 'Site', "\e[0;34;49mhttp://test.dev\e[0m")
42
+ expect(STDOUT).to receive(:printf).with(format, 'Cached Pads', 12)
43
+ expect(STDOUT).to receive(:printf).with(format, 'Last Refresh', timestamp)
44
+ client.stats
45
+ end
46
+ end
47
+
48
+ describe '.search' do
33
49
  before { Hackpad::Cli::Api.stub(:prepare) }
34
50
  before { Hackpad::Cli::Store.stub(:prepare) }
35
- before { Hackpad::Cli::Config.stub(:load).and_return({'site' => 'http://test.dev'}) }
36
- before {
37
- Hackpad::Cli::Api.stub(:search).with("xxx",0).and_return(
38
- [ {
39
- "title" => "xtitle",
40
- "id" => "xxxxxx",
41
- "snippet" => "context <b class=\"hit\">x</b> context"
42
- } ]
43
- )
44
- }
45
- context "when default options are used," do
51
+ before { Hackpad::Cli::Config.stub(:load).and_return('site' => 'http://test.dev') }
52
+ before do
53
+ Hackpad::Cli::Api.stub(:search)
54
+ .with('xxx', 0)
55
+ .and_return(
56
+ [{
57
+ 'title' => 'xtitle',
58
+ 'id' => 'xxxxxx',
59
+ 'snippet' => 'context <b class="hit">x</b> context'
60
+ }]
61
+ )
62
+ end
63
+ context 'when default options are used,' do
46
64
  let(:client) { Hackpad::Cli::Client.new options }
47
- it {
65
+ it do
48
66
  expect(STDOUT).to receive(:puts).with("\e[1;39;49mxxxxxx\e[0m - \e[0;33;49mxtitle\e[0m")
49
67
  expect(STDOUT).to receive(:puts).with(" context \e[1;36;49mx\e[0m context")
50
- client.search "xxx"
51
- }
68
+ client.search 'xxx'
69
+ end
52
70
  end
53
- context "when options sets urls to true," do
54
- let(:client) { Hackpad::Cli::Client.new options.merge({urls: true}) }
55
- it {
71
+ context 'when options sets urls to true,' do
72
+ let(:client) { Hackpad::Cli::Client.new options.merge(urls: true) }
73
+ it do
56
74
  expect(STDOUT).to receive(:puts).with("http://test.dev/\e[1;39;49mxxxxxx\e[0m - \e[0;33;49mxtitle\e[0m")
57
75
  expect(STDOUT).to receive(:puts).with(" context \e[1;36;49mx\e[0m context")
58
- client.search "xxx"
59
- }
76
+ client.search 'xxx'
77
+ end
60
78
  end
61
79
  end
62
80
 
63
- describe ".list" do
81
+ describe '.list' do
64
82
  before { Hackpad::Cli::Api.stub(:prepare) }
65
83
  before { Hackpad::Cli::Store.stub(:prepare) }
66
- before { Hackpad::Cli::Config.stub(:load).and_return({'site' => 'http://test.dev'}) }
67
- before { Hackpad::Cli::Padlist.stub(:get_list).and_return( [ OpenStruct.new( id: 'xxxxxx', title: 'xtitle' ) ] ) }
68
- context "when default options are used," do
84
+ before { Hackpad::Cli::Config.stub(:load).and_return('site' => 'http://test.dev') }
85
+ before do
86
+ Hackpad::Cli::Padlist.stub(:get_list)
87
+ .and_return([OpenStruct.new(id: 'xxxxxx', title: 'xtitle')])
88
+ end
89
+ context 'when default options are used,' do
69
90
  let(:client) { Hackpad::Cli::Client.new options }
70
- it {
71
- expect(STDOUT).to receive(:puts).with(["xxxxxx - xtitle"])
91
+ it do
92
+ expect(STDOUT).to receive(:puts).with(['xxxxxx - xtitle'])
72
93
  client.list
73
- }
94
+ end
74
95
  end
75
- context "when options sets urls to true," do
76
- let(:client) { Hackpad::Cli::Client.new options.merge({urls: true}) }
77
- it {
78
- expect(STDOUT).to receive(:puts).with(["http://test.dev/xxxxxx - xtitle"])
96
+ context 'when options sets urls to true,' do
97
+ let(:client) { Hackpad::Cli::Client.new options.merge(urls: true) }
98
+ it do
99
+ expect(STDOUT).to receive(:puts).with(['http://test.dev/xxxxxx - xtitle'])
79
100
  client.list
80
- }
101
+ end
81
102
  end
82
103
  end
83
104
 
84
- describe ".check" do
105
+ describe '.check' do
85
106
  before { Hackpad::Cli::Api.stub(:prepare) }
86
107
  before { Hackpad::Cli::Store.stub(:prepare) }
87
- before { Hackpad::Cli::Config.stub(:load).and_return({'site' => 'http://test.dev'}) }
88
- before { Hackpad::Cli::Padlist.stub(:check_list).and_return( [ OpenStruct.new( id: 'xxxxxx', title: 'xtitle' ) ] ) }
89
- context "when default options are used," do
90
- let(:client) { Hackpad::Cli::Client.new options }
91
- it {
92
- expect(STDOUT).to receive(:puts).with("New pads:")
93
- expect(STDOUT).to receive(:puts).with(["xxxxxx - xtitle"])
94
- client.check
95
- }
108
+ before { Hackpad::Cli::Config.stub(:load).and_return('site' => 'http://test.dev') }
109
+
110
+ context 'when there is a new pad,' do
111
+ before do
112
+ Hackpad::Cli::Padlist.stub(:check_list)
113
+ .and_return([OpenStruct.new(id: 'xxxxxx', title: 'xtitle')])
114
+ end
115
+ context 'when default options are used,' do
116
+ let(:client) { Hackpad::Cli::Client.new options }
117
+ it do
118
+ expect(STDOUT).to receive(:puts).with('New pads:')
119
+ expect(STDOUT).to receive(:puts).with(['xxxxxx - xtitle'])
120
+ client.check
121
+ end
122
+ end
123
+ context 'when options sets urls to true,' do
124
+ let(:client) { Hackpad::Cli::Client.new options.merge(urls: true) }
125
+ it do
126
+ expect(STDOUT).to receive(:puts).with('New pads:')
127
+ expect(STDOUT).to receive(:puts).with(['http://test.dev/xxxxxx - xtitle'])
128
+ client.check
129
+ end
130
+ end
96
131
  end
97
- context "when options sets urls to true," do
98
- let(:client) { Hackpad::Cli::Client.new options.merge({urls: true}) }
99
- it {
100
- expect(STDOUT).to receive(:puts).with("New pads:")
101
- expect(STDOUT).to receive(:puts).with(["http://test.dev/xxxxxx - xtitle"])
132
+ context 'when there is no new pad,' do
133
+ before { Hackpad::Cli::Padlist.stub(:check_list).and_return([]) }
134
+ let(:client) { Hackpad::Cli::Client.new options }
135
+ it do
136
+ expect(STDOUT).to receive(:puts).with('New pads:')
137
+ expect(STDOUT).to receive(:puts).with('There is no new pad.')
102
138
  client.check
103
- }
139
+ end
104
140
  end
105
141
  end
106
142
 
107
- describe ".info" do
143
+ describe '.info' do
108
144
  before { Hackpad::Cli::Api.stub(:prepare) }
109
145
  before { Hackpad::Cli::Store.stub(:prepare) }
110
- before { Hackpad::Cli::Config.stub(:load).and_return({'site' => 'http://test.dev'}) }
111
- context "when unknown id is given" do
112
- pending "todo"
146
+ before { Hackpad::Cli::Config.stub(:load).and_return('site' => 'http://test.dev') }
147
+ let(:client) { Hackpad::Cli::Client.new options }
148
+ let(:pad) { double Hackpad::Cli::Pad }
149
+ before { Hackpad::Cli::Pad.stub(:new).with('123').and_return pad }
150
+
151
+ context 'when unknown id is given,' do
152
+ before { pad.stub(:load).and_raise Hackpad::Cli::UndefinedPad }
153
+ it { expect { client.info('123') }.to raise_error(Hackpad::Cli::UndefinedPad) }
113
154
  end
114
- context "when id is an existing pad" do
115
155
 
156
+ context 'when id is an existing pad,' do
157
+ before { pad.stub(:load) }
158
+ before { pad.stub(:title).and_return('title1') }
159
+ before { pad.stub(:chars).and_return(20) }
160
+ before { pad.stub(:lines).and_return(2) }
161
+ before { pad.stub(:guest_policy).and_return('open') }
162
+ before { pad.stub(:moderated).and_return('false') }
163
+ before { pad.stub(:cached_at).and_return }
164
+ it do
165
+ expect(STDOUT).to receive(:printf).with(format, 'Id', "\e[1;39;49m123\e[0m")
166
+ expect(STDOUT).to receive(:printf).with(format, 'Title', "\e[0;33;49mtitle1\e[0m")
167
+ expect(STDOUT).to receive(:printf).with(format, 'URI', 'http://test.dev/123')
168
+ expect(STDOUT).to receive(:printf).with(format, 'Chars', '20')
169
+ expect(STDOUT).to receive(:printf).with(format, 'Lines', '2')
170
+ expect(STDOUT).to receive(:printf).with(format, 'Guest Policy', 'open')
171
+ expect(STDOUT).to receive(:printf).with(format, 'Moderated', 'false')
172
+ expect(STDOUT).to receive(:printf).with(format, 'Cached', 'unknown')
173
+ client.info '123'
174
+ end
116
175
  end
176
+
117
177
  end
118
178
 
119
- pending "Hackpad::Cli::Client.info"
120
- pending "Hackpad::Cli::Client.show"
179
+ describe '.show' do
180
+ before { Hackpad::Cli::Api.stub(:prepare) }
181
+ before { Hackpad::Cli::Store.stub(:prepare) }
182
+ before { Hackpad::Cli::Config.stub(:load).and_return('site' => 'http://test.dev') }
183
+ let(:client) { Hackpad::Cli::Client.new options }
184
+ let(:pad) { double Hackpad::Cli::Pad }
185
+ before { Hackpad::Cli::Pad.stub(:new).with('123').and_return pad }
186
+ before { pad.stub(:load) }
187
+
188
+ context 'when a txt version is asked,' do
189
+ before { pad.stub(:content).and_return('this is content') }
190
+ it do
191
+ expect(STDOUT).to receive(:puts).with('this is content')
192
+ client.show '123', 'txt'
193
+ end
194
+ end
195
+
196
+ context 'when a html version is asked,' do
197
+ before { pad.stub(:content).and_return('<ul><li>this is content</li></ul>') }
198
+ it do
199
+ expect(STDOUT).to receive(:puts).with('<ul><li>this is content</li></ul>')
200
+ client.show '123', 'html'
201
+ end
202
+ end
203
+
204
+ context 'when a markdown version is asked,' do
205
+ before { pad.stub(:content).and_return('<ul><li>this is content</li></ul>') }
206
+ before do
207
+ ReverseMarkdown.stub(:convert)
208
+ .with('<ul><li>this is content</li></ul>', github_flavored: true)
209
+ .and_return('- this is content')
210
+ end
211
+ it do
212
+ expect(STDOUT).to receive(:puts).with('- this is content')
213
+ client.show '123', 'md'
214
+ end
215
+ end
216
+
217
+ end
121
218
 
122
219
  end