Class: MarkLogic::AppServer

Inherits:
Object
  • Object
show all
Includes:
Persistence
Defined in:
lib/marklogic/app_server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#admin_connection, #admin_connection=, #connection, #connection=, #manage_connection, #manage_connection=

Methods included from Loggable

#default_logger, #logger, #logger=, #rails_logger

Constructor Details

#initialize(server_name, port, server_type = "http", group_name = "Default", options = {}) ⇒ AppServer

Returns a new instance of AppServer.


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/marklogic/app_server.rb', line 7

def initialize(server_name, port, server_type = "http", group_name = "Default", options = {})
  content_database = options[:content_database] || "#{server_name.gsub(/_/, "-")}-content"
  modules_database = options[:modules_database] || "#{server_name.gsub(/_/, "-")}-modules"
  self.connection = options[:connection]
  self.admin_connection = options[:admin_connection]

  @server_name = server_name
  @server_type = server_type
  @group_name = group_name

  @options = {
    "server-name" => @server_name,
    "root" => options[:root] || "/",
    "port" => port,
    "content-database" => content_database,
    "modules-database" => modules_database,
    "url-rewriter" => "/MarkLogic/rest-api/rewriter.xml",
    "error-handler" => "/MarkLogic/rest-api/error-handler.xqy",
    "rewrite-resolves-globally" => true,
    "group-name" => @group_name
  }
end

Instance Attribute Details

#group_nameObject

Returns the value of attribute group_name


5
6
7
# File 'lib/marklogic/app_server.rb', line 5

def group_name
  @group_name
end

#server_nameObject

Returns the value of attribute server_name


5
6
7
# File 'lib/marklogic/app_server.rb', line 5

def server_name
  @server_name
end

#server_typeObject

Returns the value of attribute server_type


5
6
7
# File 'lib/marklogic/app_server.rb', line 5

def server_type
  @server_type
end

Class Method Details

.load(server_name, group_name = "Default") ⇒ Object


30
31
32
33
34
# File 'lib/marklogic/app_server.rb', line 30

def self.load(server_name, group_name = "Default")
  app_server = AppServer.new(server_name, 0, 'http', group_name)
  app_server.load
  app_server
end

Instance Method Details

#[](key) ⇒ Object


59
60
61
# File 'lib/marklogic/app_server.rb', line 59

def [](key)
  @options[key]
end

#[]=(key, value) ⇒ Object


55
56
57
# File 'lib/marklogic/app_server.rb', line 55

def []=(key, value)
  @options[key] = value
end

#createObject


67
68
69
70
71
# File 'lib/marklogic/app_server.rb', line 67

def create
  r = manage_connection.post_json(
    %Q{/manage/v2/servers/?group-id=#{group_name}&server-type=#{server_type}&format=json},
    @options)
end

#dropObject


91
92
93
94
95
96
97
98
# File 'lib/marklogic/app_server.rb', line 91

def drop
  r = manage_connection.delete(%Q{/manage/v2/servers/#{server_name}?group-id=#{group_name}&format=json})

  # wait for restart
  admin_connection.wait_for_restart(r.body) if r.code.to_i == 202

  return r
end

#exists?Boolean

Returns:

  • (Boolean)

100
101
102
# File 'lib/marklogic/app_server.rb', line 100

def exists?
  manage_connection.head(%Q{/manage/v2/servers/#{server_name}?group-id=#{group_name}}).code.to_i == 200
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/marklogic/app_server.rb', line 63

def has_key?(key)
  @options.has_key?(key)
end

#inspectObject


46
47
48
49
50
51
52
53
# File 'lib/marklogic/app_server.rb', line 46

def inspect
  as_nice_string = [
    "server_name: #{server_name}",
    "server_type: #{server_type}",
    "port: #{self['port']}"
  ].join(",")
  "#<#{self.class}#{as_nice_string}>"
end

#loadObject


36
37
38
39
40
41
42
43
44
# File 'lib/marklogic/app_server.rb', line 36

def load
  resp = manage_connection.get(%Q{/manage/v2/servers/#{server_name}/properties?group-id=#{group_name}&format=json})
  if resp.code.to_i == 200
    options = Oj.load(resp.body)
    options.each do |key, value|
      self[key] = value
    end
  end
end

#to_jsonObject


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/marklogic/app_server.rb', line 78

def to_json
  json = {}
  @options.each do |k, v|
    if v.kind_of?(Array)
      value = v.map { |item| item.to_json }
    else
      value = v
    end
    json[k] = value
  end
  json
end

#updateObject


73
74
75
76
# File 'lib/marklogic/app_server.rb', line 73

def update
  url = %Q{/manage/v2/servers/#{server_name}/properties?format=json}
  manage_connection.put(url, ::Oj.dump(to_json, mode: :compat))
end