Class: Util::Client
- Inherits:
-
Object
- Object
- Util::Client
- Defined in:
- lib/vas/util/client.rb
Overview
:nodoc: all
Constant Summary collapse
- TASK_POLLING_INTERVAL =
1
Instance Method Summary collapse
- #delete(location) ⇒ Object
- #get(location) ⇒ Object
- #get_stream(location, &block) ⇒ Object
-
#initialize(username, password) ⇒ Client
constructor
A new instance of Client.
- #post(location, payload, rel = nil) ⇒ Object
- #post_image(location, path, metadata = nil) ⇒ Object
Constructor Details
#initialize(username, password) ⇒ Client
Returns a new instance of Client.
24 25 26 27 |
# File 'lib/vas/util/client.rb', line 24 def initialize(username, password) @username = username @password = password end |
Instance Method Details
#delete(location) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/vas/util/client.rb', line 59 def delete(location) req = Net::HTTP::Delete.new(location) do_request(req) { |res| case res.code when "200" return when "202" await_task(res["Location"]) else raise_vas_exception(res) end } end |
#get(location) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vas/util/client.rb', line 29 def get(location) req = Net::HTTP::Get.new(location) do_request(req) { |res| case res.code when "200" return JSON.parse(res.body) else raise_vas_exception(res) end } end |
#get_stream(location, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/vas/util/client.rb', line 44 def get_stream(location, &block) req = Net::HTTP::Get.new(location) do_request(req) { |res| case res.code when "200" return res.read_body(&block) else raise_vas_exception(res) end } end |
#post(location, payload, rel = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/vas/util/client.rb', line 74 def post(location, payload, rel = nil) req = Net::HTTP::Post.new(location) req['Content-Type'] = "application/json" req.body = payload.to_json do_request(req) { |res| case res.code when "202" return await_task(res["Location"], rel) else raise_vas_exception(res) end } end |
#post_image(location, path, metadata = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/vas/util/client.rb', line 91 def post_image(location, path, = nil) File.open(path) { |image| if (.nil?) req = Net::HTTP::Post::Multipart.new(location, :data => UploadIO.new(image, "application/octet-stream")) elsif req = Net::HTTP::Post::Multipart.new(location, :data => UploadIO.new(image, "application/octet-stream"), :metadata => UploadIO.new(StringIO.new(.to_json), "application/json", "metadata.json")) end do_request(req) { |res| case res.code when "201" return res["Location"] else raise_vas_exception(res) end } } end |