Class: Velocity::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/acceleration/velocity.rb

Overview

Models the instance. This is the top level object in the Velocity API. It models the actual Velocity server or instance.

Direct Known Subclasses

Chico

Defined Under Namespace

Classes: APIModel, Alert, CollectionBroker, Dictionary, Document, Query, QueryResponse, Reports, Repository, Scheduler, SearchCollection, SearchService, SourceTest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Instance

call-seq:

new(:endpoint => endpoint, :username => username, :password => password)

Create a new instance of Instance. This is the model central to the gem. It facilitates all communication with the Velocity instance.

Args passed in as a hash may include any attributes except :error.



88
89
90
91
92
93
94
95
# File 'lib/acceleration/velocity.rb', line 88

def initialize(args)
  @v_app = args[:v_app] || 'api-rest'
  @endpoint = args[:endpoint]
  @username = args[:username]
  @password = args[:password]
  @read_timeout = args[:read_timeout] || 120
  @open_timeout = args[:open_timeout] || 30
end

Instance Attribute Details

#endpointObject

The URL of the velocity CGI application on the instance.



66
67
68
# File 'lib/acceleration/velocity.rb', line 66

def endpoint
  @endpoint
end

#errorObject (readonly)

The error a ping encounters.



77
78
79
# File 'lib/acceleration/velocity.rb', line 77

def error
  @error
end

#open_timeoutObject

How long Acceleration should wait to connect to the instance. Default is 30 seconds.



75
76
77
# File 'lib/acceleration/velocity.rb', line 75

def open_timeout
  @open_timeout
end

#passwordObject

The password of the user.



70
71
72
# File 'lib/acceleration/velocity.rb', line 70

def password
  @password
end

#read_timeoutObject

How long Acceleration should wait for a response. Default is 120 seconds.



72
73
74
# File 'lib/acceleration/velocity.rb', line 72

def read_timeout
  @read_timeout
end

#usernameObject

The username for the API user. Create this in the Admin Tool.



68
69
70
# File 'lib/acceleration/velocity.rb', line 68

def username
  @username
end

#v_appObject

The v_app in use. Defaults to api-rest.



64
65
66
# File 'lib/acceleration/velocity.rb', line 64

def v_app
  @v_app
end

Instance Method Details

#axl_service_status(args = {}) ⇒ Object

Determine the AXL service status

Optionally supply a :pool option.

TODO: implement response wrapper



229
230
231
# File 'lib/acceleration/velocity.rb', line 229

def axl_service_status(args = {})
  call __method__.dasherize, args
end

#base_parametersObject

Assemble a hash with the basic parameters for the instance.



158
159
160
161
162
# File 'lib/acceleration/velocity.rb', line 158

def base_parameters
  { 'v.app' => v_app,
    'v.username' => username,
    'v.password' => password }
end

#call(function, args = {}) ⇒ Object

Prepare and eventually execute a Velocity API function call.

This function is generally meant to be called from within APIModel#method_missing, but a method can call it directly if something special must be done with the returned Nokogiri::XML object. The classes that do that generally wrap around the object to provide convenience methods.

Raises:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/acceleration/velocity.rb', line 106

def call(function, args = {})
  sanity_check
  Logger.info "calling #{function} with args: #{args}"
  if (args.class == Array) && args.empty?
    args = {}
  elsif !args.empty? && (args.first.class == Hash)
    args = args.first
  end
  params = base_parameters.merge({ 'v.function' => function }.merge(args))
  result = Nokogiri::XML(rest_call(params))
  raise VelocityException, result if VelocityException.exception? result
  @error = nil
  result
end

#collection(name) ⇒ Object

Get just one collection



195
196
197
198
199
# File 'lib/acceleration/velocity.rb', line 195

def collection(name)
  c = SearchCollection.new(name)
  c.instance = self
  c
end

#collectionsObject

List all collections available on the instance.



184
185
186
187
188
189
190
# File 'lib/acceleration/velocity.rb', line 184

def collections
  n = call 'search-collection-list-xml'
  n.xpath('/vse-collections/vse-collection').collect do |c|
    # initialize a new one, set its instance to me
    SearchCollection.new_from_xml(xml: c, instance: self)
  end
end

#dictionariesObject

List all dictionaries available on the instance.



204
205
206
207
208
209
# File 'lib/acceleration/velocity.rb', line 204

def dictionaries
  n = call 'dictionary-list-xml'
  n.xpath('/dictionaries/dictionary').collect do |d|
    Dictionary.new_from_xml(xml: d, instance: self)
  end
end

#pingObject

Perform a simple ping against the instance using the API function appropriately named “ping”.

If Instance#ping returns false, check Instance#error for the exception that was thrown. Instance#ping should always have a boolean return.



171
172
173
174
175
176
177
178
179
# File 'lib/acceleration/velocity.rb', line 171

def ping
  begin
    n = call 'ping'
    return true if n.root.name == 'pong'
  rescue StandardError => e
    @error = e
  end
  false
end

#queryObject

Create a new query



492
493
494
# File 'lib/acceleration/velocity.rb', line 492

def query
  Query.new(self)
end

#repositoryObject

Get a handle on the repository



583
584
585
# File 'lib/acceleration/velocity.rb', line 583

def repository
  Repository.new(self)
end

#rest_call(params) ⇒ Object

Perform the actual REST action



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/acceleration/velocity.rb', line 124

def rest_call(params)
  # restclient stupidly puts query params in the...headers?
  req = { method: :get, url: endpoint, headers: { params: params } }
  req[:timeout] = read_timeout if read_timeout
  req[:open_timeout] = open_timeout if open_timeout
  Logger.info "#hitting #{endpoint} with params: #{clean_password(params.clone)}"
  begin
    RestClient::Request.execute(req)
  rescue RestClient::RequestURITooLong => e
    Logger.info "Server says #{e}, retrying with POST..."
    # try a post. I don't like falling back like this, but pretty much
    # everything but repository actions will be under the standard limit
    req.delete(:headers)
    req[:payload] = params
    req[:method] = :post
    RestClient::Request.execute(req)
  end
end

#sanity_checkObject

Ensure that all instance variables necessary to communicate with the API are set.

Raises:

  • (ArgumentError)


215
216
217
218
219
220
# File 'lib/acceleration/velocity.rb', line 215

def sanity_check
  raise ArgumentError, 'You must specify a v.app.' if v_app.nil?
  raise ArgumentError, 'You must specify a username.' if username.nil?
  raise ArgumentError, 'You must specify a password.' if password.nil?
  raise ArgumentError, 'You must specify an endpoint.' if endpoint.nil?
end

#write_environment_list(args = {}) ⇒ Object

Write a list of feature environments to disk.

Expects a :environment_list option containing a list of environments and their IDs.

TODO: implement response wrapper



241
242
243
# File 'lib/acceleration/velocity.rb', line 241

def write_environment_list(args = {})
  call __method__.dasherize, args
end