Русский | English

Пример обращения к RESTful API:

require 'net/http'
require 'json'
USER_TOKEN = '?????'
HOST = 'http://ws-dss.com'
# Job creation preparing
uri = URI(HOST + "/ws_jobs.json")
req = Net::HTTP::Post.new(uri)
req['user-token'] = USER_TOKEN
req.set_form_data('ws_job[ws_method_id]' => 8, 'ws_job[input]' => '{"team": 20150310700}')
res = Net::HTTP.start(uri.hostname) {|http|http.request(req)}
if !res.is_a?(Net::HTTPSuccess) # error check
  puts "Error #{res}\n#{res.body}"
  exit(1)
end
h = JSON.parse(res.body)
ws_job_id = h["id"]
puts "Job was created! id = #{ws_job_id}"
i = 0
# if job was not completed we must wait
while h["output"].nil? and i < 100 do
  i += 1
  puts i
  sleep(i*5)
  uri = URI(HOST + "/ws_jobs/#{ws_job_id}.json")
  req = Net::HTTP::Get.new(uri)
  req['user-token'] = USER_TOKEN
  res = Net::HTTP.start(uri.hostname) {|http|http.request(req)}
  if !res.is_a?(Net::HTTPSuccess)
    puts "Error #{res}\n#{res.body}"
    exit(1)
  end
  h = JSON.parse(res.body)
end
puts "i = #{i} output =\n#{h["output"]}"