Class: Libgss::AsyncActionRequest

Inherits:
ActionRequest show all
Defined in:
lib/libgss/async_action_request.rb

Constant Summary

Constants inherited from ActionRequest

Libgss::ActionRequest::STATUS_ERROR, Libgss::ActionRequest::STATUS_PREPARING, Libgss::ActionRequest::STATUS_RECEIVED, Libgss::ActionRequest::STATUS_SENDING, Libgss::ActionRequest::STATUS_SUCCESS, Libgss::ActionRequest::STATUS_TIMEOUT, Libgss::ActionRequest::STATUS_WAITING

Instance Attribute Summary collapse

Attributes inherited from ActionRequest

#action_url, #outputs, #req_headers, #status

Instance Method Summary collapse

Methods inherited from ActionRequest

#add_action, #apply, #approve, #block, #breakoff, #count, #create, #dice, #execute, #find_all, #find_first, #get_by_dictionary, #get_by_game_data, #get_by_int_range, #get_by_player, #get_by_probability, #get_by_schedule, #inspect, #master_diffs, #next_action_id, #paginate, #server_date, #server_time, #unblock, #update

Constructor Details

#initialize(httpclient, action_url, result_url, req_headers) ⇒ AsyncActionRequest

コンストラクタ



17
18
19
20
# File 'lib/libgss/async_action_request.rb', line 17

def initialize(httpclient, action_url, result_url, req_headers)
  super(httpclient, action_url, req_headers)
  @result_url = result_url
end

Instance Attribute Details

#action_idObject

Returns the value of attribute action_id.



12
13
14
# File 'lib/libgss/async_action_request.rb', line 12

def action_id
  @action_id
end

#idsObject (readonly)

Returns the value of attribute ids.



11
12
13
# File 'lib/libgss/async_action_request.rb', line 11

def ids
  @ids
end

#result_urlObject

Returns the value of attribute result_url.



13
14
15
# File 'lib/libgss/async_action_request.rb', line 13

def result_url
  @result_url
end

Instance Method Details

#async_statusObject

Raises:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/libgss/async_action_request.rb', line 42

def async_status()
  raise Error, "failed to get response. please exec send_request before call." unless @ids

  res = @httpclient.get(result_url, {input_ids: @ids.join(',')}, req_headers)
  case res.code.to_i
  when 200..299 then # OK
  else
    raise Error, "failed to send action request: [#{res.code}] #{res.body}"
  end
  r = JSON.parse(res.body)
end

#send_request(&callback) ⇒ Object

アクション群を実行するために実際にHTTPリクエストを送信します。



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/libgss/async_action_request.rb', line 23

def send_request(&callback)
  res = @httpclient.post(action_url, {"inputs" => @actions.map(&:to_hash)}.to_json, req_headers)
  case res.code.to_i
  when 200..299 then # OK
  else
    raise Error, "failed to send action request: [#{res.code}] #{res.body}"
  end
  r = JSON.parse(res.body)
  # puts res.body
  @outputs = Outputs.new(r["outputs"])
  callback.call(@outputs) if callback

  @ids = @outputs.map do |output|
    output['id']
  end

  @outputs
end