Class: Requeus::Request
- Inherits:
-
Object
- Object
- Requeus::Request
- Defined in:
- lib/requeus/request.rb
Class Method Summary collapse
Instance Method Summary collapse
- #delete_files ⇒ Object
- #do_request(endpoint) ⇒ Object
-
#initialize(method, path, params, headers, force_endpoint, file_uids = nil) ⇒ Request
constructor
A new instance of Request.
Constructor Details
#initialize(method, path, params, headers, force_endpoint, file_uids = nil) ⇒ Request
Returns a new instance of Request.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/requeus/request.rb', line 10 def initialize method, path, params, headers, force_endpoint, file_uids = nil @method = method @path = path @params = params.reject{|_, v| as_file(v)} @headers = headers @force_endpoint = force_endpoint if file_uids @files = file_uids else @files = params.reject{|_, v| !as_file(v)} @files.each {|k, v| @files[k] = upload_file(as_file(v)) if as_file(v)} end end |
Class Method Details
.from_json(json) ⇒ Object
25 26 27 28 |
# File 'lib/requeus/request.rb', line 25 def self.from_json json r = JSON.parse(json) new r['method'], r['path'], r['params'], r['headers'], r['force_endpoint'], r['files'] end |
Instance Method Details
#delete_files ⇒ Object
70 71 72 73 74 |
# File 'lib/requeus/request.rb', line 70 def delete_files @files.each do |_, uid| blob_sequence.any?{|bs| bs.delete uid} end end |
#do_request(endpoint) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/requeus/request.rb', line 30 def do_request endpoint uri = URI.parse(("http://#{@force_endpoint}" || endpoint) + @path) req_klass = case @method.upcase when 'GET' Net::HTTP::Get when 'POST' Net::HTTP::Post when 'PUT' Net::HTTP::Put when 'DELETE' Net::HTTP::Delete end res = Net::HTTP.start uri.host, uri.port do |http| if @files.present? params = @params.dup @files.each do |k, uid| params[k] = UploadIO.new(download_file(uid), "application/octet-stream") end req = req_klass::Multipart.new uri.path, params else req = req_klass.new uri.path req.form_data = to_http_data(@params) if req_klass::REQUEST_HAS_BODY end @headers.each{|k,v| req.add_field(k, v)} http.request req end if res.is_a? Net::HTTPOK true else puts "Error #{res.code} returned for request:" puts self.to_json false end end |