Class: ZiggeoConnect

Inherits:
Object
  • Object
show all
Defined in:
lib/classes/ZiggeoConnect.rb

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ ZiggeoConnect

Returns a new instance of ZiggeoConnect.



7
8
9
# File 'lib/classes/ZiggeoConnect.rb', line 7

def initialize(application)
  @application = application
end

Instance Method Details

#delete(path, data = nil, file = nil) ⇒ Object



74
75
76
# File 'lib/classes/ZiggeoConnect.rb', line 74

def delete(path, data = nil, file = nil)
  return self.request("DELETE", path, data, file)
end

#deleteJSON(path, data = nil, file = nil) ⇒ Object



78
79
80
# File 'lib/classes/ZiggeoConnect.rb', line 78

def deleteJSON(path, data = nil, file = nil)
  return self.requestJSON("DELETE", path, data, file)
end

#get(path, data = nil, file = nil) ⇒ Object



58
59
60
# File 'lib/classes/ZiggeoConnect.rb', line 58

def get(path, data = nil, file = nil)
  return self.request("GET", path, data, file)
end

#getJSON(path, data = nil, file = nil) ⇒ Object



62
63
64
# File 'lib/classes/ZiggeoConnect.rb', line 62

def getJSON(path, data = nil, file = nil)
  return self.requestJSON("GET", path, data, file)
end

#post(path, data = nil, file = nil) ⇒ Object



66
67
68
# File 'lib/classes/ZiggeoConnect.rb', line 66

def post(path, data = nil, file = nil)
  return self.request("POST", path, data, file)
end

#postJSON(path, data = nil, file = nil) ⇒ Object



70
71
72
# File 'lib/classes/ZiggeoConnect.rb', line 70

def postJSON(path, data = nil, file = nil)
  return self.requestJSON("POST", path, data, file)
end

#request(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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/classes/ZiggeoConnect.rb', line 11

def request(method, path, data = nil, file = nil)
  server_api_url = @application.config.server_api_url
  regions = @application.config.regions
  regions.each do |key, value|
    if (@application.token.start_with?(key))
      server_api_url = value
    end
  end

  url = URI.parse(server_api_url + '/v1' + 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_error_message 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_error_message 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_error_message timeout_in_seconds, error
    end
  end
end

#requestJSON(method, path, data = nil, file = nil) ⇒ Object



54
55
56
# File 'lib/classes/ZiggeoConnect.rb', line 54

def requestJSON(method, path, data = nil, file = nil)
  return JSON.parse(self.request(method, path, data, file))
end