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
- #request(method, path, data = nil, file = nil) ⇒ Object
- #requestJSON(method, path, data = nil, file = nil) ⇒ Object
Constructor Details
#initialize(application, baseuri) ⇒ ZiggeoConnect
Returns a new instance of ZiggeoConnect.
7 8 9 10 |
# File 'lib/classes/ZiggeoConnect.rb', line 7 def initialize(application, baseuri) @application = application @baseuri = baseuri end |
Instance Method Details
#delete(path, data = nil, file = nil) ⇒ Object
67 68 69 |
# File 'lib/classes/ZiggeoConnect.rb', line 67 def delete(path, data = nil, file = nil) return self.request("DELETE", path, data, file) end |
#deleteJSON(path, data = nil, file = nil) ⇒ Object
71 72 73 |
# File 'lib/classes/ZiggeoConnect.rb', line 71 def deleteJSON(path, data = nil, file = nil) return self.requestJSON("DELETE", path, data, file) end |
#get(path, data = nil, file = nil) ⇒ Object
51 52 53 |
# File 'lib/classes/ZiggeoConnect.rb', line 51 def get(path, data = nil, file = nil) return self.request("GET", path, data, file) end |
#getJSON(path, data = nil, file = nil) ⇒ Object
55 56 57 |
# File 'lib/classes/ZiggeoConnect.rb', line 55 def getJSON(path, data = nil, file = nil) return self.requestJSON("GET", path, data, file) end |
#post(path, data = nil, file = nil) ⇒ Object
59 60 61 |
# File 'lib/classes/ZiggeoConnect.rb', line 59 def post(path, data = nil, file = nil) return self.request("POST", path, data, file) end |
#postJSON(path, data = nil, file = nil) ⇒ Object
63 64 65 |
# File 'lib/classes/ZiggeoConnect.rb', line 63 def postJSON(path, data = nil, file = nil) return self.requestJSON("POST", path, data, file) end |
#request(method, path, data = nil, file = nil) ⇒ Object
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 38 39 40 41 42 43 44 45 |
# File 'lib/classes/ZiggeoConnect.rb', line 12 def request(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) if (file.nil?) if (method == "get") begin HTTParty.send(method, url.to_s, query: data, basic_auth: auth, timeout: timeout_in_seconds).body 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).body rescue Net::ReadTimeout => error self. timeout_in_seconds, error end end else data = data.nil? ? {} : data; data["file"] = File.new(file) timeout_in_seconds = ( ( File.size(file).to_f / 2**20 ).round(0) * @application.config.request_timeout_per_mb.to_i ).to_i; begin HTTMultiParty.send(method, url.to_s, body: data, basic_auth: auth, timeout: timeout_in_seconds).body rescue Net::ReadTimeout => error self. timeout_in_seconds, error end end end |
#requestJSON(method, path, data = nil, file = nil) ⇒ Object
47 48 49 |
# File 'lib/classes/ZiggeoConnect.rb', line 47 def requestJSON(method, path, data = nil, file = nil) return JSON.parse(self.request(method, path, data, file)) end |