Class: Slideshift::Tool::Cli::Main

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/slideshift/tool/cli/main.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Main

Returns a new instance of Main.



16
17
18
19
20
21
22
23
24
25
# File 'lib/slideshift/tool/cli/main.rb', line 16

def initialize(*args)
  super(*args)

  Slideshift::Tool.config.read(File.expand_path('../default.conf', __FILE__))

  Slideshift::Tool.config.read(File.expand_path('~/.slideshift.conf')) if File.exists?(File.expand_path('~/.slideshift.conf'))
  Slideshift::Tool.config.read(File.expand_path('./slideshift.conf'))  if File.exists?(File.expand_path('./slideshift.conf'))

  Slideshift::Tool.config
end

Instance Method Details

#serverObject



29
30
31
32
33
34
# File 'lib/slideshift/tool/cli/main.rb', line 29

def server
  presentation = Slideshift::Tool::Presentation.new
  presentation.load
  server = Slideshift::Tool::Server::Server.new(presentation)
  server.start
end

#uploadObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/slideshift/tool/cli/main.rb', line 37

def upload

  say 'Uploading new presentation to SlideShift ...'

  `tar czf presentation.tar.gz .`

  http = Faraday.new(:url => Slideshift::Tool.config['service']['url']) do |builder|
    #builder.use Faraday::Response::Logger     # log the request to STDOUT
    builder.use Faraday::Adapter::NetHttp     # make http requests with Net::HTTP

    builder.request :multipart
    builder.request :url_encoded
  end

  if Slideshift::Tool.config['service']['id']
    say 'Presentation ID found, updating ...'
    type = :update
  else
    say 'Creating new presentation ...'
    type = :create
  end

  if type == :create
    name = ask 'Name of your presentation:'
    response = http.put do |req|
      req.url '/api/presentation'
      req.headers['X-Api-Key'] = Slideshift::Tool.config['service']['key']
      req.body = Yajl::Encoder.encode({ :name => name })
    end
    id = response.body
  else
    id = Slideshift::Tool.config['service']['id']
  end

  http.put do |req|
    req.url File.join('/api/presentation', id)
    req.headers['X-Api-Key'] = Slideshift::Tool.config['service']['key']
    req.headers['Content-Type'] = 'application/x-gzip'
    req.body = File.read('presentation.tar.gz')
  end

  FileUtils.rm('presentation.tar.gz')

  if type == :create
    Slideshift::Tool.config.service.id = id
    conf = File.exists?('./slideshift.conf') ? File.read('./slideshift.conf') : ''
    unless conf.index(id)
      conf << "\nservice.id = '#{id}'\n"
      File.open('./slideshift.conf', 'w') do |file|
        file.write(conf)
      end
    end
  end

  say "You presentation id: #{id}"
  say "Presentation can be accessed directly: #{Slideshift::Tool.config['service']['url']}/presentation/#{id}"
end