Skip to content

Commit

Permalink
- Improved code layout.
Browse files Browse the repository at this point in the history
- Added YARD doc for sustainability purposes.
  • Loading branch information
thorsteneckel committed Apr 25, 2017
1 parent 68bab40 commit a6c3f55
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions lib/import/otrs/requester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,63 @@ module Requester
# rubocop:disable Style/ModuleFunction
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 ||= {}
if args.empty? && @cache[object]
if opts.empty? && @cache[object]
return @cache[object]
end

result = request_result(
Subaction: 'Export',
Object: object,
Limit: args[:limit] || '',
Offset: args[:offset] || '',
Diff: args[:diff] ? 1 : 0
Limit: opts[:limit] || '',
Offset: opts[:offset] || '',
Diff: opts[:diff] ? 1 : 0
)

return result if !args.empty?
return result if !opts.empty?
@cache[object] = result
@cache[object]
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
request_result(Subaction: 'List')
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
result = request_json({})
return true if result['Success']
raise 'API key not valid!'
raise 'API key not valid!' if !result['Success']
true
end

private
Expand All @@ -47,10 +75,8 @@ def request_result(params)
def request_json(params)
response = post(params)
result = handle_response(response)

return result if result

raise 'Invalid response'
raise 'Invalid response' if !result
result
end

def handle_response(response)
Expand Down

0 comments on commit a6c3f55

Please sign in to comment.