Module: RedmineAPIHelper::ArgsHelper

Included in:
Helpers
Defined in:
lib/redmine_api_helper/args_helper.rb

Instance Method Summary collapse

Instance Method Details

#deserialize(object) ⇒ Object

deserializes object from OpenStruct



69
70
71
72
73
74
75
76
77
78
79
# 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(&block) ⇒ Object

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



27
28
29
30
31
32
33
# 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) ⇒ Object

serializes JSON string to OpenStruct



62
63
64
# File 'lib/redmine_api_helper/args_helper.rb', line 62

def jparse(object)
  serialize(object, :parse => true)
end

create html link



101
102
103
# File 'lib/redmine_api_helper/args_helper.rb', line 101

def link_to(body, url)
  "<a href='#{url}'>#{body}</a>"
end

#pretty(a = args) ⇒ Object

print pretty arguments passed to ruby script by plugin



84
85
86
# 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) ⇒ Object

print pretty response returned from http request



91
92
93
94
95
96
# 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) ⇒ Object

serializes object to OpenStruct



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object

gets value of field in current object



38
39
40
# File 'lib/redmine_api_helper/args_helper.rb', line 38

def value( *fields )
  args.objects[index].deep_try(*fields)
end