Class: ZiggeoConnect
- Inherits:
-
Object
- Object
- ZiggeoConnect
- Defined in:
- lib/classes/ZiggeoConnect.rb
Instance Method Summary collapse
- #delete(path, data = nil, file = nil) ⇒ Object
- #deleteJSON(path, data = nil, file = nil) ⇒ Object
- #get(path, data = nil, file = nil) ⇒ Object
- #getJSON(path, data = nil, file = nil) ⇒ Object
-
#initialize(application, baseuri) ⇒ ZiggeoConnect
constructor
A new instance of ZiggeoConnect.
- #post(path, data = nil, file = nil) ⇒ Object
- #postJSON(path, data = nil, file = nil) ⇒ Object
- #postUploadJSON(path, scope, data = nil, file = nil, type_key = nil) ⇒ Object
- #request(method, path, data = nil, file = nil) ⇒ Object
- #requestJSON(method, path, data = nil, file = nil) ⇒ Object
- #singleRequest(method, path, data = nil, file = nil) ⇒ Object
- #uploadFile(url, file, data) ⇒ Object
Constructor Details
#initialize(application, baseuri) ⇒ ZiggeoConnect
Returns a new instance of ZiggeoConnect.
6 7 8 9 |
# File 'lib/classes/ZiggeoConnect.rb', line 6 def initialize(application, baseuri) @application = application @baseuri = baseuri end |
Instance Method Details
#delete(path, data = nil, file = nil) ⇒ Object
70 71 72 |
# File 'lib/classes/ZiggeoConnect.rb', line 70 def delete(path, data = nil, file = nil) return self.request("DELETE", path, data, file) end |
#deleteJSON(path, data = nil, file = nil) ⇒ Object
74 75 76 |
# File 'lib/classes/ZiggeoConnect.rb', line 74 def deleteJSON(path, data = nil, file = nil) return self.requestJSON("DELETE", path, data, file) end |
#get(path, data = nil, file = nil) ⇒ Object
54 55 56 |
# File 'lib/classes/ZiggeoConnect.rb', line 54 def get(path, data = nil, file = nil) return self.request("GET", path, data, file) end |
#getJSON(path, data = nil, file = nil) ⇒ Object
58 59 60 |
# File 'lib/classes/ZiggeoConnect.rb', line 58 def getJSON(path, data = nil, file = nil) return self.requestJSON("GET", path, data, file) end |
#post(path, data = nil, file = nil) ⇒ Object
62 63 64 |
# File 'lib/classes/ZiggeoConnect.rb', line 62 def post(path, data = nil, file = nil) return self.request("POST", path, data, file) end |
#postJSON(path, data = nil, file = nil) ⇒ Object
66 67 68 |
# File 'lib/classes/ZiggeoConnect.rb', line 66 def postJSON(path, data = nil, file = nil) return self.requestJSON("POST", path, data, file) end |
#postUploadJSON(path, scope, data = nil, file = nil, type_key = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/classes/ZiggeoConnect.rb', line 95 def postUploadJSON(path, scope, data = nil, file = nil, type_key = nil) data = data || {} if type_key && (file.is_a? String) data[type_key] = File.extname(file) end result = self.postJSON(path, data) self.uploadFile(result["url_data"]["url"], file, result["url_data"]["fields"]) return result[scope] end |
#request(method, path, data = nil, file = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/classes/ZiggeoConnect.rb', line 39 def request(method, path, data = nil, file = nil) res = nil @application.config.resilience_factor.times do res = self.singleRequest(method, path, data, file) if res.response.code.to_i >= 200 && res.response.code.to_i < 500 return res.body end end return @application.config.resilience_on_fail.to_json end |
#requestJSON(method, path, data = nil, file = nil) ⇒ Object
50 51 52 |
# File 'lib/classes/ZiggeoConnect.rb', line 50 def requestJSON(method, path, data = nil, file = nil) return JSON.parse(self.request(method, path, data, file)) end |
#singleRequest(method, path, data = nil, file = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/classes/ZiggeoConnect.rb', line 11 def singleRequest(method, path, data = nil, file = nil) url = URI.parse(@baseuri + path) auth = { username: @application.token, password: @application.private_key } timeout_in_seconds = @application.config.request_timeout.to_i method.downcase! allowed_methods = %w(get post delete) return unless allowed_methods.include?(method) unless (file.nil?) data = data || {} data["file"] = File.new(file) timeout_in_seconds = ( ( File.size(file).to_f / 2**20 ).ceil * @application.config.request_timeout_per_mb.to_i ).to_i; end if (method == "get") begin HTTParty.send(method, url.to_s, query: data, basic_auth: auth, timeout: timeout_in_seconds) rescue Net::ReadTimeout => error self. timeout_in_seconds, error end else begin HTTParty.send(method, url.to_s, body: data, basic_auth: auth, timeout: timeout_in_seconds) rescue Net::ReadTimeout => error self. timeout_in_seconds, error end end end |
#uploadFile(url, file, data) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/classes/ZiggeoConnect.rb', line 78 def uploadFile(url, file, data) res = nil data["file"] = File.new(file) timeout_in_seconds = ( ( File.size(file).to_f / 2**20 ).ceil * @application.config.request_timeout_per_mb.to_i ).to_i; begin @application.config.resilience_factor.times do res = HTTParty.send("post", url.to_s, body: data, timeout: timeout_in_seconds) if res.response.code.to_i >= 200 && res.response.code.to_i < 300 return end end raise Exception.new res.response rescue Net::ReadTimeout => error self. timeout_in_seconds, error end end |