module RedmineAPIHelper::ArgsHelper

Public Instance Methods

deserialize(object) click to toggle source

deserializes object from OpenStruct

# File lib/redmine_api_helper/args_helper.rb, line 69
def deserialize(object)
  if object.is_a?(OpenStruct)
    return deserialize( object.to_h )
  elsif object.is_a?(Hash)
    return object.map{|key, obj| [key, deserialize(obj)]}.to_h
  elsif object.is_a?(Array)
    return object.map{|obj| deserialize(obj)}
  else # assumed to be a primitive value
    return object
  end
end
iterate() { |object| ... } click to toggle source

iterates over current object, set index for functions accessing current object

# File lib/redmine_api_helper/args_helper.rb, line 27
def iterate(&block)
  args.objects.map do |object|
    obj = yield object
    @index += 1 unless @index + 1 >= args.objects.length
    obj
  end
end
jparse(object) click to toggle source

serializes JSON string to OpenStruct

# File lib/redmine_api_helper/args_helper.rb, line 62
def jparse(object)
  serialize(object, :parse => true)
end
pretty(a=args) click to toggle source

print pretty arguments passed to ruby script by plugin

# File lib/redmine_api_helper/args_helper.rb, line 84
def pretty(a=args)
  JSON.pretty_generate(deserialize(a))
end
pretty_response(hr=@http_response) click to toggle source

print pretty response returned from http request

# File lib/redmine_api_helper/args_helper.rb, line 91
def pretty_response(hr=@http_response)
  JSON.pretty_generate({
    :code => hr.try(:code),
    :body => JSON.parse(hr.try(:body).to_s)
  })
end
serialize(object, **options) click to toggle source

serializes object to OpenStruct

# File lib/redmine_api_helper/args_helper.rb, line 45
def serialize(object, **options)
  if object.is_a?(Hash)
     OpenStruct.new(object.map{ |key, val| [ key, serialize(val, options) ] }.to_h)
  elsif object.is_a?(Array)
     object.map{ |obj| serialize(obj, options) }
  else # assumed to be a primitive value
    if options[:parse]
      JSON.parse(object, object_class:OpenStruct) rescue object 
    else
      object
    end
  end
end
value( *fields ) click to toggle source

gets value of field in current object

# File lib/redmine_api_helper/args_helper.rb, line 38
def value( *fields )
  args.objects[index].deep_try(*fields)
end