Class: Hackpad::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(configdir) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hackpad/client.rb', line 11

def initialize(configdir)
  @configdir = configdir
  @config = Config.load configdir
  site = URI.parse @config['site']
  consumer = OAuth::Consumer.new(
    @config['client_id'],
    @config['secret'],
    site: @config['site']
  )
  @token = OAuth::AccessToken.new consumer
end

Instance Method Details

#getinfo(pad) ⇒ Object



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

def getinfo(pad)
  res = @token.get "/api/1.0/pad/#{pad}/content.txt"
  if res.is_a? Net::HTTPSuccess
    puts "#{@config['site']}/#{pad} - #{res.body.lines.first.chomp}"
  else
    puts "#{pad} failed".colorize :red
  end
end

#listObject

GET /api/1.0/pads/all



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hackpad/client.rb', line 24

def list
  res = @token.get "/api/1.0/pads/all"
  if res.is_a? Net::HTTPSuccess
    all = JSON.parse res.body
    all.each do |a|
      puts getinfo(a)
    end
  else
    puts "#{res.inspect}".colorize :red
    puts "#{res.body}".colorize :red
    return back
  end
end

#show(pad, format) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hackpad/client.rb', line 47

def show(pad,format)
  ext = (format == 'md') ? 'html' : format
  res = @token.get "/api/1.0/pad/#{pad}/content.#{ext}"
  if res.is_a? Net::HTTPSuccess
    puts "#{@config['site']}/#{pad}"
    puts
    if format == 'md'
      puts ReverseMarkdown.convert(res.body, github_flavored: true)
    else
      puts res.body
    end
  else
    puts "#{pad} failed".colorize :red
  end
end