Module: RuoteKit::Helpers::JsonHelpers

Defined in:
lib/ruote-kit/helpers/json_helpers.rb

Overview

Helpers for JSON rendering

Instance Method Summary collapse

Instance Method Details

#json(resource, *args) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 11

def json(resource, *args)

  object = respond_to?("json_#{resource}") ?
    send("json_#{resource}", *args) : nil

  Rufus::Json.encode(
    'links' => links(resource), resource.to_s => object || args.first
  )
end

#json_error(error = @error) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 90

def json_error(error=@error)

  fei = error.fei
  wfid = fei.wfid

  error.to_h.merge('links' => [
    hlink('errors', fei.sid, :rel => 'self'),
    hlink('errors', wfid, :rel => '#process_errors'),
    hlink('processes', wfid, :rel => '#process')
  ])
end

#json_errorsObject



85
86
87
88
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 85

def json_errors

  @errors.collect { |e| json_error(e) }
end

#json_expression(expression = @expression) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 49

def json_expression(expression=@expression)

  detailed = (@expression != nil)

  links = [
    hlink('expressions', expression.fei.sid, :rel => 'self'),
    hlink('processes', expression.fei.wfid, :rel => '#process'),
    hlink('expressions', expression.fei.wfid, :rel => '#process_expressions')
  ]
  links << hlink(
    'expressions', expression.parent.fei.sid, 'parent'
  ) if expression.parent

  expression.as_h(detailed).merge('links' => links)
end

#json_expressionsObject



44
45
46
47
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 44

def json_expressions

  @process.expressions.map { |e| json_expression(e) }
end

#json_http_error(err) ⇒ Object



132
133
134
135
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 132

def json_http_error(err)

  { 'code' => err[0], 'message' => err[1], 'cause' => err[2].to_s }
end

#json_participantsObject



102
103
104
105
106
107
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 102

def json_participants

  @participants.collect { |pa|
    pa.as_h.merge('links' => [ hlink('participants', :rel => 'self') ])
  }
end

#json_process(process = @process) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 30

def json_process(process=@process)

  detailed = (@process != nil)

  process.as_h(detailed).merge('links' => [
    hlink('processes', process.wfid, :rel => 'self'),
    hlink('processes', process.wfid, :rel => '#process'),
    hlink('expressions', process.wfid, :rel => '#process_expressions'),
    hlink('workitems', process.wfid, :rel => '#process_workitems'),
    hlink('errors', process.wfid, :rel => '#process_errors'),
    hlink('schedules', process.wfid, :rel => '#process_schedules')
  ])
end

#json_processesObject

def json_exception(code, exception)

{ 'code' => code, 'exception' => { 'message' => exception.message } }

end



25
26
27
28
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 25

def json_processes

  @processes.map { |p| json_process(p) }
end

#json_schedulesObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 109

def json_schedules

  @schedules.each do |sched|

    owner_fei = sched.delete('owner')
    target_fei = sched.delete('target')

    sched['owner'] = owner_fei.to_h
    sched['target'] = target_fei ? target_fei.to_h : nil

    sched['links'] = []

    sched['links'] << hlink(
      'expressions', owner_fei.sid, :rel => '#schedule_owner'
    )
    sched['links'] << hlink(
      'expressions', target_fei.sid, :rel => '#schedule_target'
    ) if target_fei
  end

  @schedules
end

#json_workitem(workitem = @workitem) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 70

def json_workitem(workitem=@workitem)

  detailed = (@workitem != nil)

  links = [
    hlink('workitems', workitem.fei.sid, :rel => 'self'),
    hlink('processes', workitem.fei.wfid, :rel => '#process'),
    hlink('expressions', workitem.fei.sid, :rel => '#expression'),
    hlink('expressions', workitem.fei.wfid, :rel => '#process_expressions'),
    hlink('errors', workitem.fei.wfid, :rel => '#process_errors')
  ]

  workitem.as_h(detailed).merge('links' => links)
end

#json_workitemsObject



65
66
67
68
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 65

def json_workitems

  @workitems.map { |w| json_workitem(w) }
end


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ruote-kit/helpers/json_helpers.rb', line 137

def links(resource)

  result = [
    hlink(:rel => '#root'),
    hlink('processes', :rel => '#processes'),
    hlink('workitems', :rel => '#workitems'),
    hlink('errors', :rel => '#errors'),
    hlink('participants', :rel => '#participants'),
    hlink('schedules', :rel => '#schedules'),
    hlink('history', :rel => '#history'),
    hlink(request.fullpath, :rel => 'self')
  ]

  if @skip # pagination is active

    result << hlink(resource.to_s, :rel => 'all')

    lim = @limit || settings.limit
    las = (@count / lim) * lim rescue 0
    pre = [ 0, @skip - lim ].max
    nex = [ @skip + lim, las ].min

    result << hlink(
      resource.to_s,
      :skip => 0, :limit => lim, :rel => 'first')
    result << hlink(
      resource.to_s,
      :skip => las, :limit => lim, :rel => 'last')
    result << hlink(
      resource.to_s,
      :skip => pre, :limit => lim, :rel => 'previous')
    result << hlink(
      resource.to_s,
      :skip => nex, :limit => lim, :rel => 'next')
  end

  result
end