Class: MarkLogic::Connection

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/marklogic/connection.rb

Constant Summary collapse

NEWLINE_SPLITTER =
Regexp.new("\r\n", Regexp::MULTILINE)
DOUBLE_NEWLINE_SPLITTER =
Regexp.new("\r\n\r\n", Regexp::MULTILINE)
START_BOUNDARY_REGEX =
Regexp.new("^[\r\n]+--[^-].+?[\r\n]+", Regexp::MULTILINE)
END_BOUNDARY_REGEX =
Regexp.new("[\r\n]+--[^-]+--[\r\n]+$", Regexp::MULTILINE)
BOUNDARY_SPLITTER_REGEX =
Regexp.new(%Q{[\r\n]+--[^-]+[\r\n]+}, Regexp::MULTILINE)
CONTENT_TYPE_REGEX =
/Content-Type:\s+(.*)$/
PRIMITIVE_REGEX =
/X-Primitive:\s+(.*)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

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

Constructor Details

#initialize(host, port, username = nil, password = nil, options = {}) ⇒ Connection

Returns a new instance of Connection.



129
130
131
132
133
134
135
136
# File 'lib/marklogic/connection.rb', line 129

def initialize(host, port, username = nil, password = nil, options = {})
  @host = host
  @port = port
  @username = username || self.class.default_user
  @password = password || self.class.default_password
  @request_retries = options[:request_retries] || 3
  @http = Net::HTTP::Persistent.new 'marklogic'
end

Instance Attribute Details

#adminObject

Returns the value of attribute admin.



78
79
80
# File 'lib/marklogic/connection.rb', line 78

def admin
  @admin
end

#app_servicesObject

Returns the value of attribute app_services.



78
79
80
# File 'lib/marklogic/connection.rb', line 78

def app_services
  @app_services
end

#hostObject

Returns the value of attribute host.



78
79
80
# File 'lib/marklogic/connection.rb', line 78

def host
  @host
end

#manageObject

Returns the value of attribute manage.



78
79
80
# File 'lib/marklogic/connection.rb', line 78

def manage
  @manage
end

#passwordObject

Returns the value of attribute password.



78
79
80
# File 'lib/marklogic/connection.rb', line 78

def password
  @password
end

#portObject

Returns the value of attribute port.



78
79
80
# File 'lib/marklogic/connection.rb', line 78

def port
  @port
end

#request_retriesObject

Returns the value of attribute request_retries.



78
79
80
# File 'lib/marklogic/connection.rb', line 78

def request_retries
  @request_retries
end

#usernameObject

Returns the value of attribute username.



78
79
80
# File 'lib/marklogic/connection.rb', line 78

def username
  @username
end

Class Method Details

.admin_connection(username = self.default_user, password = self.default_password) ⇒ Object



113
114
115
# File 'lib/marklogic/connection.rb', line 113

def self.admin_connection(username = self.default_user, password = self.default_password)
  @@__admin_connection ||= Connection.new(self.host_name, self.admin_port, username, password)
end

.admin_portObject



105
106
107
# File 'lib/marklogic/connection.rb', line 105

def self.admin_port
  @@__admin_port ||= 8001
end

.app_services_connection(username = self.default_user, password = self.default_password) ⇒ Object



121
122
123
# File 'lib/marklogic/connection.rb', line 121

def self.app_services_connection(username = self.default_user, password = self.default_password)
  @@__app_services_connection ||= Connection.new(self.host_name, self.app_services_port, username, password)
end

.app_services_portObject



101
102
103
# File 'lib/marklogic/connection.rb', line 101

def self.app_services_port
  @@__app_services_port ||= 8000
end

.configure(options = {}) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/marklogic/connection.rb', line 80

def self.configure(options = {})
  @@__host_name = options[:host] if options[:host]
  @@__app_services_port = options[:app_services_port] if options[:app_services_port]
  @@__admin_port = options[:admin_port] if options[:admin_port]
  @@__manage_port = options[:manage_port] if options[:manage_port]
  @@__default_user = options[:default_user] if options[:default_user]
  @@__default_password = options[:default_password] if options[:default_password]
end

.default_passwordObject



93
94
95
# File 'lib/marklogic/connection.rb', line 93

def self.default_password
  @@__default_password ||= "admin"
end

.default_userObject



89
90
91
# File 'lib/marklogic/connection.rb', line 89

def self.default_user
  @@__default_user ||= "admin"
end

.host_nameObject



97
98
99
# File 'lib/marklogic/connection.rb', line 97

def self.host_name
  @@__host_name ||= "localhost"
end

.manage_connection(username = self.default_user, password = self.default_password) ⇒ Object



117
118
119
# File 'lib/marklogic/connection.rb', line 117

def self.manage_connection(username = self.default_user, password = self.default_password)
  @@__manage_connection ||= Connection.new(self.host_name, self.manage_port, username, password)
end

.manage_portObject



109
110
111
# File 'lib/marklogic/connection.rb', line 109

def self.manage_port
  @@__manage_port ||= 8002
end

Instance Method Details

#==(other) ⇒ Object



204
205
206
207
208
209
# File 'lib/marklogic/connection.rb', line 204

def ==(other)
  @host == other.host &&
  @port == other.port &&
  @username == other.username &&
  @password == other.password
end

#delete(url, headers = {}, body = nil) ⇒ Object



180
181
182
# File 'lib/marklogic/connection.rb', line 180

def delete(url, headers = {}, body = nil)
  request(url, 'delete', headers, body)
end

#get(url, headers = {}) ⇒ Object



158
159
160
# File 'lib/marklogic/connection.rb', line 158

def get(url, headers = {})
  request(url, 'get', headers)
end

#head(url, headers = {}) ⇒ Object



154
155
156
# File 'lib/marklogic/connection.rb', line 154

def head(url, headers = {})
  request(url, 'head', headers)
end

#post(url, params = nil, headers = {}) ⇒ Object



166
167
168
# File 'lib/marklogic/connection.rb', line 166

def post(url, params = nil, headers = {})
  request(url, 'post', headers, nil, params)
end

#post_json(url, params = nil, headers = {}) ⇒ Object



170
171
172
# File 'lib/marklogic/connection.rb', line 170

def post_json(url, params = nil, headers = {})
  request(url, 'post', headers, ::JSON.generate(params))
end

#post_multipart(url, body = nil, headers = {}, boundary = "BOUNDARY") ⇒ Object



174
175
176
177
178
# File 'lib/marklogic/connection.rb', line 174

def post_multipart(url, body = nil, headers = {}, boundary = "BOUNDARY")
  headers['Content-Type'] = %Q{multipart/mixed; boundary=#{boundary}}
  headers['Accept'] = %Q{application/json}
  request(url, 'post', headers, body)
end

#put(url, body = nil, headers = {}) ⇒ Object



162
163
164
# File 'lib/marklogic/connection.rb', line 162

def put(url, body = nil, headers = {})
  request(url, 'put', headers, body)
end

#run_query(query, type = "javascript", options = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/marklogic/connection.rb', line 138

def run_query(query, type = "javascript", options = {})
  # manually building the params yielded a performance improvement
  params = %Q{#{type}=#{URI.encode_www_form_component(query)}}
  params += %Q{&dbname=#{options[:db]}} if options[:db]

  headers = {
    'content-type' => 'application/x-www-form-urlencoded'
  }
  response = request('/eval', 'post', headers, params)

    # :xquery => options[:query],
    # :locale => LOCALE,
    # :tzoffset => "-18000",
    # :dbname => options[:db]
end

#wait_for_restart(body) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/marklogic/connection.rb', line 184

def wait_for_restart(body)
  json = Oj.load(body)
  ts_value = json["restart"]["last-startup"][0]["value"]
  timestamp = DateTime.iso8601(ts_value).to_time
  new_timestamp = timestamp

  code = nil
  logger.debug "Waiting for restart"
  until code == 200 && new_timestamp > timestamp
    begin
      rr = get(%Q{/admin/v1/timestamp})
      code = rr.code.to_i
      bb = rr.body
      new_timestamp = DateTime.iso8601(bb).to_time if code == 200
    rescue
    end
  end
  logger.debug "Restart Complete"
end