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.


131
132
133
134
135
136
137
138
139
# File 'lib/marklogic/connection.rb', line 131

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'
  @verbose = options[:verbose] == true || false
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

#verboseObject

Returns the value of attribute verbose


129
130
131
# File 'lib/marklogic/connection.rb', line 129

def verbose
  @verbose
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


209
210
211
212
213
214
# File 'lib/marklogic/connection.rb', line 209

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

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


185
186
187
# File 'lib/marklogic/connection.rb', line 185

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

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


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

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

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


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

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

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


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

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

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


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

def post_json(url, params = nil, headers = {})
  request(url, 'post', headers, ::Oj.dump(params, mode: :compat))
end

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


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

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


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

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

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


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/marklogic/connection.rb', line 141

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'
  }

  logger.debug(%Q{MarkLogic (#{type}):  #{query}})
  response = request('/eval', 'post', headers, params)

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

#wait_for_restart(body) ⇒ Object


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/marklogic/connection.rb', line 189

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