- Improved code layout.

- Added YARD doc for sustainability purposes.
This commit is contained in:
Thorsten Eckel 2017-04-25 09:51:39 +02:00
parent 68bab40c13
commit a6c3f55844

View file

@ -6,35 +6,63 @@ module Import
# rubocop:disable Style/ModuleFunction # rubocop:disable Style/ModuleFunction
extend self extend self
def load(object, args = {}) # Loads entries of the given object.
#
# @param object [String] the name of OTRS object
# @param [Hash] opts the options to load entries.
# @option opts [String] :limit the maximum amount of entries that should get loaded
# @option opts [String] :offset the offset where the entry listing should start
# @option opts [Boolean] :diff request only changed/added entries since the last import
#
# @example
# Import::OTRS::Requester.load('State', offset: '0', limit: '50')
# #=> [{'Name':'pending reminder', ...}, ...]
#
# @return [Array<Hash{String => String, Number, nil, Hash, Array}>]
def load(object, opts = {})
@cache ||= {} @cache ||= {}
if args.empty? && @cache[object] if opts.empty? && @cache[object]
return @cache[object] return @cache[object]
end end
result = request_result( result = request_result(
Subaction: 'Export', Subaction: 'Export',
Object: object, Object: object,
Limit: args[:limit] || '', Limit: opts[:limit] || '',
Offset: args[:offset] || '', Offset: opts[:offset] || '',
Diff: args[:diff] ? 1 : 0 Diff: opts[:diff] ? 1 : 0
) )
return result if !args.empty? return result if !opts.empty?
@cache[object] = result @cache[object] = result
@cache[object] @cache[object]
end end
# Lists the OTRS objects and their amount of importable entries.
#
# @example
# Import::OTRS::Requester.list #=> {'DynamicFields' => 5, ...}
#
# @return [Hash{String => Number}] key = OTRS object, value = amount
def list def list
request_result(Subaction: 'List') request_result(Subaction: 'List')
end end
# TODO: refactor to something like .connected? # Checks if the connection to the OTRS export endpoint works.
#
# @todo Refactor to something like .connected?
#
# @example
# Import::OTRS::Requester.connection_test #=> true
#
# @raise [RuntimeError] if the API key is not valid
#
# @return [true] always returns true
def connection_test def connection_test
result = request_json({}) result = request_json({})
return true if result['Success'] raise 'API key not valid!' if !result['Success']
raise 'API key not valid!' true
end end
private private
@ -47,10 +75,8 @@ module Import
def request_json(params) def request_json(params)
response = post(params) response = post(params)
result = handle_response(response) result = handle_response(response)
raise 'Invalid response' if !result
return result if result result
raise 'Invalid response'
end end
def handle_response(response) def handle_response(response)