Class: HaveAPI::Client::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/client/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, api, name, spec, args) ⇒ Action

Returns a new instance of Action.



8
9
10
11
12
13
14
15
# File 'lib/haveapi/client/action.rb', line 8

def initialize(client, api, name, spec, args)
  @client = client
  @api = api
  @name = name
  @spec = spec

  apply_args(args)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



5
6
7
# File 'lib/haveapi/client/action.rb', line 5

def api
  @api
end

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/haveapi/client/action.rb', line 5

def client
  @client
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/haveapi/client/action.rb', line 5

def name
  @name
end

#resource_pathObject

Returns the value of attribute resource_path.



6
7
8
# File 'lib/haveapi/client/action.rb', line 6

def resource_path
  @resource_path
end

Class Method Details

.cancel(client, id) ⇒ Object



224
225
226
227
228
229
230
231
232
233
# File 'lib/haveapi/client/action.rb', line 224

def self.cancel(client, id)
  res = client.action_state.cancel(id, meta: { block: false })

  if res.ok? && res.action.blocking? && res.meta[:action_state_id]
    res.meta[:action_state_id]

  else
    res
  end
end

.wait_for_completion(client, id, interval: 15, update_in: 3, timeout: nil) {|state| ... } ⇒ Boolean, ...

Block until the action is completed or timeout occurs. If the block is given, it is regularly called with the action’s state.

Parameters:

  • interval (Float) (defaults to: 15)

    how often should the action state be checked

  • timeout (Integer) (defaults to: nil)

    timeout in seconds

Yield Parameters:

Returns:

  • (Boolean)

    when the action is finished

  • (nil)

    when timeout occurs

  • (Response)

    if the action was cancelled and the cancel itself isn’t blocking

  • (Integer)

    id of cancellation if the action was cancelled, cancel is blocking and no cancel block is provided



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/haveapi/client/action.rb', line 156

def self.wait_for_completion(client, id, interval: 15, update_in: 3, timeout: nil)
  res = client.action_state.show(id)
  state = ActionState.new(res)

  yield(state) if block_given?
  return state.status if state.finished?

  last = {}
  t = Time.now if timeout

  loop do
    res = client.action_state.poll(
      id,
      timeout: interval,
      update_in: update_in,
      status: last[:status],
      current: last[:current],
      total: last[:total]
    )

    state = ActionState.new(res)

    last[:status] = res.response[:status]
    last[:current] = res.response[:current]
    last[:total] = res.response[:total]

    yield(state) if block_given?
    break if state.finished?

    if state.cancel?
      state.stop
      cancel_block = state.cancel_block

      ret = cancel(client, id)

      if ret.is_a?(Response)
        # The cancel is not a blocking operation, return immediately
        raise ActionFailed, ret unless ret.ok?

        return ret
      end

      # Cancel is a blocking operation
      if cancel_block
        return wait_for_completion(
          client,
          ret,
          interval: interval,
          timeout: timeout,
          update_in: update_in,
          &cancel_block
        )
      end

      return ret
    end

    return nil if (timeout && (Time.now - t) >= timeout) || state.stop?
  end

  state.status
rescue Interrupt => e
  %i[show poll].each do |action|
    client.action_state.actions[action].reset
  end
  raise e
end

Instance Method Details

#aliases(include_name = false) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/haveapi/client/action.rb', line 47

def aliases(include_name = false)
  if include_name
    [@name] + @spec[:aliases]
  else
    @spec[:aliases]
  end
end

#auth?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/haveapi/client/action.rb', line 39

def auth?
  @spec[:auth]
end

#blocking?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/haveapi/client/action.rb', line 43

def blocking?
  @spec[:blocking]
end

#descriptionObject



55
56
57
# File 'lib/haveapi/client/action.rb', line 55

def description
  @spec[:description]
end

#examplesObject



83
84
85
# File 'lib/haveapi/client/action.rb', line 83

def examples
  @spec[:examples]
end

#execute(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/haveapi/client/action.rb', line 21

def execute(data)
  params_arg = {}

  if input
    params = Params.new(self, data)

    unless params.valid?
      raise ValidationError.new(self, params.errors)
    end

    params_arg = params.to_api
  end

  @api.call(self, params_arg)
ensure
  reset
end

#helpObject



107
108
109
# File 'lib/haveapi/client/action.rb', line 107

def help
  @spec[:help]
end

#http_methodObject



120
121
122
# File 'lib/haveapi/client/action.rb', line 120

def http_method
  @spec[:method]
end

#inputObject



59
60
61
# File 'lib/haveapi/client/action.rb', line 59

def input
  @spec[:input]
end

#input_layoutObject



67
68
69
# File 'lib/haveapi/client/action.rb', line 67

def input_layout
  @spec[:input][:layout].to_sym
end

#input_paramsObject



87
88
89
# File 'lib/haveapi/client/action.rb', line 87

def input_params
  @spec[:input][:parameters]
end

#inspectObject



17
18
19
# File 'lib/haveapi/client/action.rb', line 17

def inspect
  "#<#{self.class.name} @name=#{@name}>"
end

#meta(scope) ⇒ Object



99
100
101
# File 'lib/haveapi/client/action.rb', line 99

def meta(scope)
  @spec[:meta][scope]
end

#namespace(src) ⇒ Object



79
80
81
# File 'lib/haveapi/client/action.rb', line 79

def namespace(src)
  @spec[src][:namespace]
end

#outputObject



63
64
65
# File 'lib/haveapi/client/action.rb', line 63

def output
  @spec[:output]
end

#output_layoutObject



71
72
73
# File 'lib/haveapi/client/action.rb', line 71

def output_layout
  @spec[:output][:layout].to_sym
end

#param_description(dir, name) ⇒ Object



95
96
97
# File 'lib/haveapi/client/action.rb', line 95

def param_description(dir, name)
  @spec[dir][:parameters][name]
end

#paramsObject



91
92
93
# File 'lib/haveapi/client/action.rb', line 91

def params
  @spec[:output][:parameters]
end

#pathObject



103
104
105
# File 'lib/haveapi/client/action.rb', line 103

def path
  @spec[:path]
end

#prepared_helpObject



116
117
118
# File 'lib/haveapi/client/action.rb', line 116

def prepared_help
  @prepared_help || @spec[:help]
end

#prepared_pathObject

Url with resolved parameters.



112
113
114
# File 'lib/haveapi/client/action.rb', line 112

def prepared_path
  @prepared_path || @spec[:path]
end

#provide_args(*args) ⇒ Object



128
129
130
# File 'lib/haveapi/client/action.rb', line 128

def provide_args(*args)
  apply_args(args)
end

#provide_path(path, help) ⇒ Object



132
133
134
135
# File 'lib/haveapi/client/action.rb', line 132

def provide_path(path, help)
  @prepared_path = path
  @prepared_help = help
end

#resetObject



137
138
139
140
# File 'lib/haveapi/client/action.rb', line 137

def reset
  @prepared_path = nil
  @prepared_help = nil
end

#structureObject



75
76
77
# File 'lib/haveapi/client/action.rb', line 75

def structure
  @spec[:output][:format]
end

#unresolved_args?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/haveapi/client/action.rb', line 124

def unresolved_args?
  prepared_path =~ /\{[a-zA-Z0-9\-_]+\}/
end

#update_description(spec) ⇒ Object



142
143
144
# File 'lib/haveapi/client/action.rb', line 142

def update_description(spec)
  @spec = spec
end