Class: ShellCast::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/shellcast/publisher.rb

Instance Method Summary collapse

Instance Method Details

#api_keyObject



21
22
23
24
25
26
27
28
# File 'lib/shellcast/publisher.rb', line 21

def api_key
  unless ShellCast.api_key
    print 'Paste your API KEY [or Enter to publish as Anonymous]: '
    key = STDIN.gets.strip
    ShellCast.api_key = key unless key.empty?
  end
  ShellCast.api_key
end

#prepare(id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/shellcast/publisher.rb', line 30

def prepare(id)
  out = {}
  ['meta', 'timing', 'typescript'].each do |file|
    out[file] = File.read(File.join(ShellCast.shellcast_dir(id), file))
  end
  meta = JSON.parse(out.delete('meta'))
  meta.each { |k,v| out[k] = v }
  print 'Description: '
  out['description'] = STDIN.gets.strip
  print 'Tags (ex: howto, linux): '
  out['tags'] = STDIN.gets.strip
  return out.to_json
end

#publish(id) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/shellcast/publisher.rb', line 7

def publish(id)
  uri = URI.parse(ShellCast::API_URL + '/records')
  params = { 'record' =>  prepare(id) }
  params.merge!({'api_key' => ShellCast.api_key}) if api_key
  res = Net::HTTP.post_form(uri, params)
  res = JSON.parse(res.body)
  if res['ok']
    puts res['message']
    puts ShellCast::API_URL + '/records/' + res['id']
  else
    puts res['message']
  end
end