Class: Webdoc::Cli::ExpressionRestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/webdoc-cli/expression_rest_client.rb

Instance Method Summary collapse

Constructor Details

#initializeExpressionRestClient

Returns a new instance of ExpressionRestClient.



17
18
19
# File 'lib/webdoc-cli/expression_rest_client.rb', line 17

def initialize
  @resource = RestClient::Resource.new 'http://' + Webdoc::Cli.host
end

Instance Method Details

#create(system_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/webdoc-cli/expression_rest_client.rb', line 68

def create(system_name)
  if File.exists?(system_name)
    Webdoc::Cli.cmd.say "Error: already a folder named #{system_name}."
    exit 1
  end

  binding = template_context system_name

  Dir.mkdir(system_name)
  root_template = File.join(File.dirname(__FILE__), '..', 'expression_template')
  ['expression.json', 'editor.html', 'player.html', '__template__.js', '__template__.css', '__template___editor.js', '__template___editor.css'].each do |name|
    template = ERB.new File.read(File.join(root_template, name + '.erb'))
    File.open(File.join(system_name, name.gsub('__template__', system_name)), 'w') do |f|
      f.write template.result(binding)
    end
  end
  Webdoc::Cli.cmd.say "Expression #{system_name} has been created: #{File.join(Dir.pwd, system_name)}"
end

#deploy(path = '.') ⇒ Object

Bundle and deploy the expression in the current directory



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/webdoc-cli/expression_rest_client.rb', line 45

def deploy(path='.')
  Webdoc::Cli.authenticate_then do |headers|
    Webdoc::Cli.cmd.say "==> create bundle"
    bundle(path) do |file|
      request = RestClient::Request.new(
        :method => :post,
        :url => url('/expressions.json'),
        :cookies => headers[:cookies],
        :payload => {
          :multipart => true,
          :bundle => file
        }
      )
      Webdoc::Cli.cmd.say "==> send bundle to #{Webdoc::Cli.host}"
      response = handle_request_error { request.execute }
      json = JSON.parse(response.strip)
      system_name = json['expression']['system_name']
      Webdoc::Cli.cmd.say "Expression #{@system_name} has been deployed on #{Webdoc::Cli.host}"
      Webdoc::Cli.cmd.say "http://#{Webdoc::Cli.host}/#{username}/#{system_name}"
    end
  end
end

#listObject

Print the list of expressions manageable by the user.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/webdoc-cli/expression_rest_client.rb', line 88

def list
  Webdoc::Cli.authenticate_then do |headers|
    response = handle_request_error do
      RestClient.get(url('/expressions.json'), headers)
    end
    list = JSON.parse(response.strip)['expressions']
    if list.empty?
      Webdoc::Cli.cmd.say "You manage no expression on #{Webdoc::Cli.host}"
    else
      Webdoc::Cli.cmd.say "Managed expression on #{Webdoc::Cli.host}:"
      list.each do |exp|
        Webdoc::Cli.cmd.say "- #{exp['system_name']} (#{exp['version']})"
      end
    end
  end
end

#migrate(system_name, v0, v1) ⇒ Object

Migrate documents using expression with the given system_name from version v0 to version v1.



107
108
109
110
111
112
113
114
115
116
# File 'lib/webdoc-cli/expression_rest_client.rb', line 107

def migrate(system_name, v0, v1)
  Webdoc::Cli.authenticate_then do |headers|
    response = handle_request_error do
      Webdoc::Cli.cmd.say "==> start migration of #{system_name} from #{v0} to #{v1} on #{Webdoc::Cli.host}"
      RestClient.post(url("/#{username}/#{system_name}/migrate/#{v0}/#{v1}.json"), {}, headers)
    end
    message = JSON.parse(response.strip)['message']
    Webdoc::Cli.cmd.say(message)
  end
end

#package(path = '.') ⇒ Object

package an expression



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/webdoc-cli/expression_rest_client.rb', line 23

def package(path='.')
  bundle(path) do |file|
    json = JSON.parse(File.read(File.join(path, 'expression.json')))
    version = json['version']
    system_name = json['system_name']

    if system_name.nil?
      Webdoc::Cli.cmd.say("System Name cannot be null")
      exit 1
    end
    if version.nil?
      Webdoc::Cli.cmd.say("Version number cannot be null")
      exit 1
    end
    filename = "#{system_name}-#{version}.expression"

    Webdoc::Cli.cmd.say "==> package bundle to #{filename}"
    FileUtils.cp file.path, filename
  end
end

#show(system_name) ⇒ Object

Print the list of expressions



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/webdoc-cli/expression_rest_client.rb', line 119

def show(system_name)
  Webdoc::Cli.authenticate_then do |headers|
    response = handle_request_error do
      RestClient.get(url("/#{username}/#{system_name}/all.json"), headers)
    end
    list = JSON.parse(response.strip)['expressions']
    list.each do |exp|
      date = DateTime.parse(exp['created_at'])
      date.strftime('%Y/%m/%d %H:%M')
      Webdoc::Cli.cmd.say "- #{exp['system_name']} (#{exp['version']}): #{date.strftime('%Y/%m/%d %H:%M')}"
    end
  end
end