Class: Tes::Request::Client
- Inherits:
-
Object
- Object
- Tes::Request::Client
- Defined in:
- lib/tes/request/client.rb
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
Instance Method Summary collapse
- #get_all_res ⇒ Object
-
#initialize(driver) ⇒ Client
constructor
Construction.
- #release_all_res(user) ⇒ Object
- #release_res(id, user) ⇒ Object
- #request_env(user, asks) ⇒ Hash
- #request_res(id, user, lock = 1) ⇒ Object
Constructor Details
#initialize(driver) ⇒ Client
Construction
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/tes/request/client.rb', line 10 def initialize(driver) @driver = driver http_methods = [:get, :post, :put, :delete, :head] http_methods.each do |m| unless @driver.respond_to?(m) fail_msg = "invalid arg [driver]: not support method: #{m}" raise(ArgumentError, fail_msg) end end end |
Instance Attribute Details
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
21 22 23 |
# File 'lib/tes/request/client.rb', line 21 def driver @driver end |
Instance Method Details
#get_all_res ⇒ Object
61 62 63 |
# File 'lib/tes/request/client.rb', line 61 def get_all_res parse_res(@driver.get('res'))[:data] end |
#release_all_res(user) ⇒ Object
55 56 57 58 59 |
# File 'lib/tes/request/client.rb', line 55 def release_all_res(user) get_all_res.select {|_k, c| c[:users] && c[:users].include?(user)}.each do |id, _c| release_res(id, user) end end |
#release_res(id, user) ⇒ Object
50 51 52 53 |
# File 'lib/tes/request/client.rb', line 50 def release_res(id, user) res = @driver.post("res/#{id}/release", body: {user: user}) parse_res res end |
#request_env(user, asks) ⇒ Hash
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tes/request/client.rb', line 26 def request_env(user, asks) res = @driver.post('env', body: {user: user, ask: asks.join("\n")}.to_json) ret = parse_res res if ret.is_a?(Hash) and ret[:success] and ret[:data].is_a?(Hash) and ret[:data][:res].is_a?(Hash) res_hash = ret[:data][:res] res_hash.keys.each {|k| res_hash[k.to_s] = res_hash.delete(k)} ret[:data][:res] = res_hash end ret end |
#request_res(id, user, lock = 1) ⇒ Object
45 46 47 48 |
# File 'lib/tes/request/client.rb', line 45 def request_res(id, user, lock = 1) res = @driver.post("res/#{id}/lock", body: {user: user, lock: lock}) parse_res res end |