Class: HaveAPI::Client::Action
- Inherits:
-
Object
- Object
- HaveAPI::Client::Action
- Defined in:
- lib/haveapi/client/action.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resource_path ⇒ Object
Returns the value of attribute resource_path.
Class Method Summary collapse
- .cancel(client, id) ⇒ Object
-
.wait_for_completion(client, id, interval: 15, update_in: 3, timeout: nil) {|state| ... } ⇒ Boolean, ...
Block until the action is completed or timeout occurs.
Instance Method Summary collapse
- #aliases(include_name = false) ⇒ Object
- #auth? ⇒ Boolean
- #blocking? ⇒ Boolean
- #description ⇒ Object
- #examples ⇒ Object
- #execute(data, raw: false) ⇒ Object
- #help ⇒ Object
- #http_method ⇒ Object
-
#initialize(client, api, name, spec, args) ⇒ Action
constructor
A new instance of Action.
- #input ⇒ Object
- #input_layout ⇒ Object
- #input_params ⇒ Object
- #inspect ⇒ Object
- #meta(scope) ⇒ Object
- #namespace(src) ⇒ Object
- #output ⇒ Object
- #output_layout ⇒ Object
- #param_description(dir, name) ⇒ Object
- #params ⇒ Object
- #path ⇒ Object
- #prepared_help ⇒ Object
-
#prepared_path ⇒ Object
Url with resolved parameters.
- #provide_args(*args) ⇒ Object
- #provide_path(path, help) ⇒ Object
- #reset ⇒ Object
- #structure ⇒ Object
- #unresolved_args? ⇒ Boolean
- #update_description(spec) ⇒ Object
Constructor Details
#initialize(client, api, name, spec, args) ⇒ Action
Returns a new instance of Action.
6 7 8 9 10 11 12 13 |
# File 'lib/haveapi/client/action.rb', line 6 def initialize(client, api, name, spec, args) @client = client @api = api @name = name @spec = spec apply_args(args) end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
3 4 5 |
# File 'lib/haveapi/client/action.rb', line 3 def api @api end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/haveapi/client/action.rb', line 3 def client @client end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/haveapi/client/action.rb', line 3 def name @name end |
#resource_path ⇒ Object
Returns the value of attribute resource_path.
4 5 6 |
# File 'lib/haveapi/client/action.rb', line 4 def resource_path @resource_path end |
Class Method Details
.cancel(client, id) ⇒ Object
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/haveapi/client/action.rb', line 222 def self.cancel(client, id) res = client.action_state.cancel(id, meta: { block: false }) if res.ok? && res.action.blocking? && res.[:action_state_id] res.[: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.
154 155 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 |
# File 'lib/haveapi/client/action.rb', line 154 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
45 46 47 48 49 50 51 |
# File 'lib/haveapi/client/action.rb', line 45 def aliases(include_name = false) if include_name [@name] + @spec[:aliases] else @spec[:aliases] end end |
#auth? ⇒ Boolean
37 38 39 |
# File 'lib/haveapi/client/action.rb', line 37 def auth? @spec[:auth] end |
#blocking? ⇒ Boolean
41 42 43 |
# File 'lib/haveapi/client/action.rb', line 41 def blocking? @spec[:blocking] end |
#description ⇒ Object
53 54 55 |
# File 'lib/haveapi/client/action.rb', line 53 def description @spec[:description] end |
#examples ⇒ Object
81 82 83 |
# File 'lib/haveapi/client/action.rb', line 81 def examples @spec[:examples] end |
#execute(data, raw: false) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/haveapi/client/action.rb', line 19 def execute(data, raw: false) 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 ret = @api.call(self, params_arg, raw: raw) reset ret end |
#help ⇒ Object
105 106 107 |
# File 'lib/haveapi/client/action.rb', line 105 def help @spec[:help] end |
#http_method ⇒ Object
118 119 120 |
# File 'lib/haveapi/client/action.rb', line 118 def http_method @spec[:method] end |
#input ⇒ Object
57 58 59 |
# File 'lib/haveapi/client/action.rb', line 57 def input @spec[:input] end |
#input_layout ⇒ Object
65 66 67 |
# File 'lib/haveapi/client/action.rb', line 65 def input_layout @spec[:input][:layout].to_sym end |
#input_params ⇒ Object
85 86 87 |
# File 'lib/haveapi/client/action.rb', line 85 def input_params @spec[:input][:parameters] end |
#inspect ⇒ Object
15 16 17 |
# File 'lib/haveapi/client/action.rb', line 15 def inspect "#<#{self.class.name} @name=#{@name}>" end |
#meta(scope) ⇒ Object
97 98 99 |
# File 'lib/haveapi/client/action.rb', line 97 def (scope) @spec[:meta][scope] end |
#namespace(src) ⇒ Object
77 78 79 |
# File 'lib/haveapi/client/action.rb', line 77 def namespace(src) @spec[src][:namespace] end |
#output ⇒ Object
61 62 63 |
# File 'lib/haveapi/client/action.rb', line 61 def output @spec[:output] end |
#output_layout ⇒ Object
69 70 71 |
# File 'lib/haveapi/client/action.rb', line 69 def output_layout @spec[:output][:layout].to_sym end |
#param_description(dir, name) ⇒ Object
93 94 95 |
# File 'lib/haveapi/client/action.rb', line 93 def param_description(dir, name) @spec[dir][:parameters][name] end |
#params ⇒ Object
89 90 91 |
# File 'lib/haveapi/client/action.rb', line 89 def params @spec[:output][:parameters] end |
#path ⇒ Object
101 102 103 |
# File 'lib/haveapi/client/action.rb', line 101 def path @spec[:path] end |
#prepared_help ⇒ Object
114 115 116 |
# File 'lib/haveapi/client/action.rb', line 114 def prepared_help @prepared_help || @spec[:help] end |
#prepared_path ⇒ Object
Url with resolved parameters.
110 111 112 |
# File 'lib/haveapi/client/action.rb', line 110 def prepared_path @prepared_path || @spec[:path] end |
#provide_args(*args) ⇒ Object
126 127 128 |
# File 'lib/haveapi/client/action.rb', line 126 def provide_args(*args) apply_args(args) end |
#provide_path(path, help) ⇒ Object
130 131 132 133 |
# File 'lib/haveapi/client/action.rb', line 130 def provide_path(path, help) @prepared_path = path @prepared_help = help end |
#reset ⇒ Object
135 136 137 138 |
# File 'lib/haveapi/client/action.rb', line 135 def reset @prepared_path = nil @prepared_help = nil end |
#structure ⇒ Object
73 74 75 |
# File 'lib/haveapi/client/action.rb', line 73 def structure @spec[:output][:format] end |
#unresolved_args? ⇒ Boolean
122 123 124 |
# File 'lib/haveapi/client/action.rb', line 122 def unresolved_args? prepared_path =~ /\{[a-zA-Z0-9\-_]+\}/ end |
#update_description(spec) ⇒ Object
140 141 142 |
# File 'lib/haveapi/client/action.rb', line 140 def update_description(spec) @spec = spec end |